diff --git a/SourceGithub/SourceGithub.php b/SourceGithub/SourceGithub.php index bbed21a..98974a0 100644 --- a/SourceGithub/SourceGithub.php +++ b/SourceGithub/SourceGithub.php @@ -83,8 +83,9 @@ public function update_repo_form( $p_repo ) { $t_hub_username = null; $t_hub_reponame = null; - $t_hub_api_login = null; - $t_hub_api_token = null; + $t_hub_app_client_id = null; + $t_hub_app_secret = null; + $t_hub_app_access_token = null; if ( isset( $p_repo->info['hub_username'] ) ) { $t_hub_username = $p_repo->info['hub_username']; @@ -94,12 +95,16 @@ $t_hub_reponame = $p_repo->info['hub_reponame']; } - if ( isset( $p_repo->info['hub_api_login'] ) ) { - $t_hub_api_login = $p_repo->info['hub_api_login']; + if ( isset( $p_repo->info['hub_app_client_id'] ) ) { + $t_hub_app_client_id = $p_repo->info['hub_app_client_id']; } - if ( isset( $p_repo->info['hub_api_token'] ) ) { - $t_hub_api_token = $p_repo->info['hub_api_token']; + if ( isset( $p_repo->info['hub_app_secret'] ) ) { + $t_hub_app_secret = $p_repo->info['hub_app_secret']; + } + + if ( isset( $p_repo->info['hub_app_access_token'] ) ) { + $t_hub_app_access_token = $p_repo->info['hub_app_access_token']; } if ( isset( $p_repo->info['master_branch'] ) ) { @@ -117,12 +122,22 @@ > - - + + > - - + + + +> + +oauth_authorize_uri( $p_repo ), plugin_lang_get( 'hub_app_authorize' ) ); +else: +echo plugin_lang_get( 'hub_app_authorized' ); +endif; ?> > @@ -134,8 +149,8 @@ public function update_repo( $p_repo ) { $f_hub_username = gpc_get_string( 'hub_username' ); $f_hub_reponame = gpc_get_string( 'hub_reponame' ); - $f_hub_api_login = gpc_get_string( 'hub_api_login' ); - $f_hub_api_token = gpc_get_string( 'hub_api_token' ); + $f_hub_app_client_id = gpc_get_string( 'hub_app_client_id' ); + $f_hub_app_secret = gpc_get_string( 'hub_app_secret' ); $f_master_branch = gpc_get_string( 'master_branch' ); if ( !preg_match( '/\*|^[a-zA-Z0-9_\., -]*$/', $f_master_branch ) ) { @@ -145,8 +160,8 @@ $p_repo->info['hub_username'] = $f_hub_username; $p_repo->info['hub_reponame'] = $f_hub_reponame; - $p_repo->info['hub_api_login'] = $f_hub_api_login; - $p_repo->info['hub_api_token'] = $f_hub_api_token; + $p_repo->info['hub_app_client_id'] = $f_hub_app_client_id; + $p_repo->info['hub_app_secret'] = $f_hub_app_secret; $p_repo->info['master_branch'] = $f_master_branch; return $p_repo; @@ -154,16 +169,10 @@ private function api_uri( $p_repo, $p_path ) { $t_uri = 'https://api.github.com/' . $p_path; - - if ( !is_blank( $p_repo->info['hub_api_token'] ) ) { - $t_token = $p_repo->info['hub_api_token']; - $t_login = $p_repo->info['hub_username']; - - if ( !is_blank( $p_repo->info['hub_api_login'] ) ) { - $t_login = $p_repo->info['hub_api_login']; - } - - $t_uri .= '?login=' . $t_login . '&token=' . $t_token; + + $t_access_token = $p_repo->info['hub_app_access_token']; + if ( !is_blank( $t_access_token ) ) { + $t_uri .= '?access_token=' . $t_access_token; } return $t_uri; @@ -361,4 +370,61 @@ return array( null, array() ); } } + + private function oauth_authorize_uri( $p_repo ) { + $t_hub_app_client_id = null; + $t_hub_app_secret = null; + $t_hub_app_access_token = null; + + if ( isset( $p_repo->info['hub_app_client_id'] ) ) { + $t_hub_app_client_id = $p_repo->info['hub_app_client_id']; + } + + if ( isset( $p_repo->info['hub_app_secret'] ) ) { + $t_hub_app_secret = $p_repo->info['hub_app_secret']; + } + + if ( !empty( $t_hub_app_client_id ) && !empty( $t_hub_app_secret ) ) { + return 'https://github.com/login/oauth/authorize?client_id=' . $t_hub_app_client_id . '&redirect_uri=' . urlencode(config_get('path') . 'plugin.php?page=SourceGithub/oauth_authorize&id=' . $p_repo->id ) . '&scope=repo'; + } else { + return ''; + } + } + + public static function oauth_get_access_token( $p_repo, $p_code ) { + # build the GitHub URL & POST data + $t_url = 'https://github.com/login/oauth/access_token'; + $t_post_data = array( 'client_id' => $p_repo->info['hub_app_client_id'], + 'client_secret' => $p_repo->info['hub_app_secret'], + 'code' => $p_code ); + $t_data = url_post( $t_url, $t_post_data ); + + $t_access_token = ''; + if ( !empty( $t_data ) ) { + /* + $t_reader = new XMLReader; + if ( $t_reader->xml( $t_data ) === true ) { + if ( $t_reader->moveToAttribute( 'access_token' ) === true ) { + $t_access_token = $t_reader->readString(); + } + } + */ + $t_response = array(); + parse_str( $t_data, $t_response ); + if ( isset( $t_response['access_token'] ) === true ) { + $t_access_token = $t_response['access_token']; + } + } + + if ( !empty( $t_access_token ) ) { + if ( $t_access_token != $p_repo->info['hub_app_access_token'] ) { + $p_repo->info['hub_app_access_token'] = $t_access_token; + $p_repo->save(); + } + return true; + } else { + return false; + } + } + } diff --git a/SourceGithub/lang/strings_english.txt b/SourceGithub/lang/strings_english.txt index ac7f42f..dbaba36 100644 --- a/SourceGithub/lang/strings_english.txt +++ b/SourceGithub/lang/strings_english.txt @@ -10,6 +10,11 @@ $s_plugin_SourceGithub_hub_username = 'GitHub Username'; $s_plugin_SourceGithub_hub_reponame = 'GitHub Repository
(lowercase, dashed name)'; -$s_plugin_SourceGithub_hub_api_login = 'GitHub API Login
For private repositories'; -$s_plugin_SourceGithub_hub_api_token = 'GitHub API Token
For private repositories'; +$s_plugin_SourceGithub_hub_app_client_id = 'GitHub Application Client ID
For private repositories. Create a new GitHub Application if needed'; +$s_plugin_SourceGithub_hub_app_secret = 'GitHub Application Secret
For private repositories'; +$s_plugin_SourceGithub_hub_app_access_token = 'GitHub Application Access Token
For private repositories'; $s_plugin_SourceGithub_master_branch = 'Primary Branches
(comma-separated list)'; + +$s_plugin_SourceGithub_hub_app_client_id_secret_missing = 'N/A
You must enter the GitHub Application Client ID & Secret and update before you can authorize'; +$s_plugin_SourceGithub_hub_app_authorize = 'Click to Authorize'; +$s_plugin_SourceGithub_hub_app_authorized = 'Authorized'; \ No newline at end of file diff --git a/SourceGithub/pages/oauth_authorize.php b/SourceGithub/pages/oauth_authorize.php new file mode 100644 index 0000000..906e37c --- /dev/null +++ b/SourceGithub/pages/oauth_authorize.php @@ -0,0 +1,24 @@ +Your app is now authorized with GitHub.

'; +} else { + echo '

Sorry, your app could not be authorized with GitHub.

'; +} + +html_page_bottom1( __FILE__ ); + +?> \ No newline at end of file