33# Copyright (c) 2012 John Reese
44# Licensed under the MIT license
55
6+ use GuzzleHttp \RequestOptions ;
7+
68if ( false === include_once ( config_get ( 'plugin_path ' ) . 'Source/MantisSourceGitBasePlugin.class.php ' ) ) {
79 return ;
810}
@@ -16,7 +18,8 @@ class SourceGithubPlugin extends MantisSourceGitBasePlugin {
1618 const MANTIS_VERSION = '2.3.0 ' ;
1719
1820 const URL_API = 'https://api.github.com/ ' ;
19-
21+ const URL_OAUTH = 'https://github.com/login/oauth/ ' ;
22+
2023 public $ linkPullRequest = '/pull/%s ' ;
2124
2225 /**
@@ -717,6 +720,12 @@ private function json_commit_changeset( $p_repo, $p_json, $p_branch='' ) {
717720 }
718721 }
719722
723+ /**
724+ * Return the GitHub OAuth URL for the given repository
725+ *
726+ * @param SourceRepo $p_repo
727+ * @return string
728+ */
720729 private function oauth_authorize_uri ( $ p_repo ) {
721730 $ t_hub_app_client_id = null ;
722731 $ t_hub_app_secret = null ;
@@ -740,19 +749,20 @@ private function oauth_authorize_uri( $p_repo ) {
740749 'scope ' => 'repo ' ,
741750 'allow_signup ' => false ,
742751 );
743- return ' https://github.com/login/oauth/ authorize? ' . http_build_query ( $ t_param );
752+ return self :: URL_OAUTH . ' authorize? ' . http_build_query ( $ t_param );
744753 } else {
745754 return '' ;
746755 }
747756 }
748757
749758 public static function oauth_get_access_token ( $ p_repo , $ p_code ) {
750759 # build the GitHub URL & POST data
751- $ t_url = ' https://github.com/login/oauth/access_token ' ;
752- $ t_post_data = array ( 'client_id ' => $ p_repo ->info ['hub_app_client_id ' ],
760+ $ t_post_data = array (
761+ 'client_id ' => $ p_repo ->info ['hub_app_client_id ' ],
753762 'client_secret ' => $ p_repo ->info ['hub_app_secret ' ],
754- 'code ' => $ p_code );
755- $ t_data = self ::url_post ( $ t_url , $ t_post_data );
763+ 'code ' => $ p_code
764+ );
765+ $ t_data = self ::url_post ( self ::URL_OAUTH . 'access_token ' , $ t_post_data );
756766
757767 $ t_access_token = '' ;
758768 if ( !empty ( $ t_data ) ) {
@@ -776,26 +786,23 @@ public static function oauth_get_access_token( $p_repo, $p_code ) {
776786 }
777787 }
778788
789+ /**
790+ * Sends a POST request.
791+ *
792+ * @param string $p_url Target URL
793+ * @param array $p_post_data Post data
794+ *
795+ * @return string Response
796+ */
779797 public static function url_post ( $ p_url , $ p_post_data ) {
780- $ t_post_data = http_build_query ( $ p_post_data );
781-
782- # Use the PHP cURL extension
783- if ( function_exists ( 'curl_init ' ) ) {
784- $ t_curl = curl_init ( $ p_url );
785- curl_setopt ( $ t_curl , CURLOPT_RETURNTRANSFER , true );
786- curl_setopt ( $ t_curl , CURLOPT_POST , true );
787- curl_setopt ( $ t_curl , CURLOPT_POSTFIELDS , $ t_post_data );
788-
789- $ t_data = curl_exec ( $ t_curl );
790- curl_close ( $ t_curl );
791-
792- return $ t_data ;
793- } else {
794- # Last resort system call
795- $ t_url = escapeshellarg ( $ p_url );
796- $ t_post_data = escapeshellarg ( $ t_post_data );
797- return shell_exec ( 'curl ' . $ t_url . ' -d ' . $ t_post_data );
798- }
798+ $ t_request = new GuzzleHttp \Client ();
799+ $ t_response = $ t_request ->post (
800+ $ p_url ,
801+ array (
802+ 'form_params ' => $ p_post_data ,
803+ )
804+ );
805+ return (string )$ t_response ->getBody ();
799806 }
800807
801808}
0 commit comments