| 1 |
<?php |
| 2 |
require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR."gianism_controller.php"; |
| 3 |
|
| 4 |
/** |
| 5 |
* Description of google_controller |
| 6 |
* |
| 7 |
* @author guy |
| 8 |
*/ |
| 9 |
class Google_Controller extends Gianism_Controller{ |
| 10 |
|
| 11 |
/** |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
private $consumer_key = ''; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
private $consumer_secret = ''; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $redirect_uri = ''; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private $umeta_account = '_wpg_google_account'; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $umeta_plus = "_wpg_google_plus_id"; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var apiClient |
| 38 |
*/ |
| 39 |
private $_oauth = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var apiPlusService |
| 43 |
*/ |
| 44 |
private $plus = null; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Set up option |
| 49 |
* @param array $option |
| 50 |
*/ |
| 51 |
protected function set_option($option) { |
| 52 |
$option = shortcode_atts(array( |
| 53 |
"ggl_consumer_key" => "", |
| 54 |
"ggl_consumer_secret" => "", |
| 55 |
"ggl_redirect_uri" => "" |
| 56 |
), $option); |
| 57 |
$this->consumer_key = $option['ggl_consumer_key']; |
| 58 |
$this->consumer_secret = $option['ggl_consumer_secret']; |
| 59 |
$this->redirect_uri = $option['ggl_redirect_uri']; |
| 60 |
session_start(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Executed on init hoook |
| 65 |
* @global int $user_ID |
| 66 |
* @global wpdb $wpdb |
| 67 |
* @global WP_Gianism $gianism |
| 68 |
*/ |
| 69 |
public function init_action(){ |
| 70 |
if(!$this->is_endpoint()){ |
| 71 |
return; |
| 72 |
} |
| 73 |
global $user_ID, $wpdb, $gianism; |
| 74 |
switch($this->get_action()){ |
| 75 |
case "google_connect": |
| 76 |
if(isset($_GET['code']) && is_user_logged_in()){ |
| 77 |
$this->add_message($gianism->_('Oops, Failed to Authenticate.')); |
| 78 |
$this->api()->authenticate(); |
| 79 |
if($this->api()->getAccessToken()){ |
| 80 |
//Can authenticate |
| 81 |
$profile = $this->get_profile(); |
| 82 |
if(!empty($profile)){ |
| 83 |
$mail = $profile['email']; |
| 84 |
$plus_id = isset($profile['id']) ? $profile['id'] : 0; |
| 85 |
//Check if other user has mail address |
| 86 |
$existance = email_exists($mail); |
| 87 |
$sql = <<<EOS |
| 88 |
SELECT user_id FROM {$wpdb->usermeta} |
| 89 |
WHERE meta_key = %s AND user_id != %d AND meta_value = %s |
| 90 |
EOS; |
| 91 |
$mail_owner = $wpdb->get_var($wpdb->prepare($sql, $this->umeta_account, $user_ID, $mail)); |
| 92 |
$valid = (!$existance || $existance == $user_ID) && !$mail_owner; |
| 93 |
//Check if other user has plus ID |
| 94 |
if($plus_id && $wpdb->get_var($wpdb->prepare($sql, $this->umeta_plus, $user_ID, $plus_id))){ |
| 95 |
$valid = false; |
| 96 |
} |
| 97 |
if($valid){ |
| 98 |
update_user_meta($user_ID, $this->umeta_account, $mail); |
| 99 |
if($plus_id){ |
| 100 |
update_user_meta($user_ID, $this->umeta_plus, $plus_id); |
| 101 |
} |
| 102 |
$this->add_message(sprintf($gianism->_('Welcom, %s!'), $profile['name'])); |
| 103 |
}else{ |
| 104 |
$this->add_message(sprintf($gianism->_('Mm...? This %s account seems to be connected to another account.'), "Google")); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
$this->do_redirect(); |
| 109 |
} |
| 110 |
break; |
| 111 |
case "google_disconnect": |
| 112 |
if(isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'google_disconnect')){ |
| 113 |
delete_user_meta($user_ID, $this->umeta_account); |
| 114 |
delete_user_meta($user_ID, $this->umeta_plus); |
| 115 |
$this->add_message(sprintf($gianism->_("Disconected your %s account."), "Google")); |
| 116 |
$this->do_redirect(); |
| 117 |
} |
| 118 |
break; |
| 119 |
case "google_login": |
| 120 |
if(isset($_GET['code']) && !is_user_logged_in()){ |
| 121 |
$this->api()->authenticate(); |
| 122 |
if($this->api()->getAccessToken()){ |
| 123 |
$profile = $this->get_profile(); |
| 124 |
if(!empty($profile)){ |
| 125 |
$email = $profile['email']; |
| 126 |
$plus_id = isset($profile['id']) ? $profile['id'] : 0; |
| 127 |
$user_id = email_exists($email); |
| 128 |
if(!$user_id){ |
| 129 |
$query = <<<EOS |
| 130 |
SELECT user_id FROM {$wpdb->usermeta} |
| 131 |
WHERE meta_key = %s AND meta_value = %s |
| 132 |
EOS; |
| 133 |
$user_id = $wpdb->get_var($wpdb->prepare($query, $this->umeta_account, $email)); |
| 134 |
if(!$user_id){ |
| 135 |
//Not found, Create New User |
| 136 |
require_once(ABSPATH . WPINC . '/registration.php'); |
| 137 |
//Check if username exists |
| 138 |
$user_id = wp_create_user($email, wp_generate_password(), $email); |
| 139 |
if(!is_wp_error($user_id)){ |
| 140 |
update_user_meta($user_id, $this->umeta_account, $email); |
| 141 |
if($plus_id){ |
| 142 |
update_user_meta($user_id, $this->umeta_plus, $plus_id); |
| 143 |
} |
| 144 |
$wpdb->update( |
| 145 |
$wpdb->users, |
| 146 |
array( |
| 147 |
'display_name' => $profile['name'] |
| 148 |
), |
| 149 |
array('ID' => $user_id), |
| 150 |
array('%s'), |
| 151 |
array('%d') |
| 152 |
); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
if(!$user_id || is_wp_error($user_id)){ |
| 159 |
$this->add_message('Oops, Failed to Authenticate.'); |
| 160 |
$this->set_redirect(wp_login_url($this->get_redirect())); |
| 161 |
}else{ |
| 162 |
wp_set_auth_cookie($user_id, true); |
| 163 |
} |
| 164 |
$this->do_redirect(); |
| 165 |
} |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Output google connect |
| 172 |
* @global WP_Gianism $gianism |
| 173 |
* @global int $user_ID |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
public function user_profile(){ |
| 177 |
if(!defined("IS_PROFILE_PAGE")){ |
| 178 |
return; |
| 179 |
} |
| 180 |
global $gianism, $user_ID; |
| 181 |
$google_account = get_user_meta($user_ID, $this->umeta_account, true); |
| 182 |
if($google_account){ |
| 183 |
$url = wp_nonce_url($this->redirect_uri, 'google_disconnect'); |
| 184 |
$this->set_redirect(admin_url('profile.php')); |
| 185 |
$this->set_action('google_disconnect'); |
| 186 |
$link_text = $gianism->_('Disconnect'); |
| 187 |
$desc = '<img src="'.$gianism->url.'/assets/icon-checked.png" alt="Connected" width="16" height="16" />' |
| 188 |
.sprintf($gianism->_('Your account is already connected with Google <%s>.'), esc_html($google_account)); |
| 189 |
$onclick = ' onclick="if(!confirm(\''.$gianism->_('You really disconnect this account?').'\')) return false;"'; |
| 190 |
}else{ |
| 191 |
$this->set_redirect(admin_url('profile.php')); |
| 192 |
$this->set_action("google_connect"); |
| 193 |
$url = $this->api()->createAuthUrl(); |
| 194 |
$link_text = $gianism->_('Connect'); |
| 195 |
$desc = sprintf($gianism->_('Connecting %1$s account, you can log in %2$s via %1$s account.'),"Google", get_bloginfo('name')); |
| 196 |
$onclick = ''; |
| 197 |
} |
| 198 |
?> |
| 199 |
<tr> |
| 200 |
<th><?php $gianism->e('Google'); ?></th> |
| 201 |
<td> |
| 202 |
<a class="wpg_ggl_btn" href="<?php echo $url; ?>"<?php echo $onclick; ?>> |
| 203 |
<i></i> |
| 204 |
<?php echo $link_text;?> |
| 205 |
</a> |
| 206 |
<p class="description"><?php echo $desc;?></p> |
| 207 |
</td> |
| 208 |
</tr> |
| 209 |
<?php |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Echo login form |
| 214 |
* @global WP_Gianism $gianism |
| 215 |
*/ |
| 216 |
public function login_form(){ |
| 217 |
global $gianism; |
| 218 |
$this->set_redirect($this->get_redirect_to(admin_url('profile.php'))); |
| 219 |
$this->set_action('google_login'); |
| 220 |
$url = $this->api()->createAuthUrl(); |
| 221 |
$link_text = $gianism->_('Login with Google'); |
| 222 |
$onclick = ''; |
| 223 |
?> |
| 224 |
<a class="wpg_ggl_btn" href="<?php echo $url; ?>"<?php echo $onclick; ?>> |
| 225 |
<i></i> |
| 226 |
<?php echo $link_text;?> |
| 227 |
</a> |
| 228 |
<?php |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* |
| 233 |
* @param string $redirect |
| 234 |
* @return apiClient |
| 235 |
*/ |
| 236 |
private function api(){ |
| 237 |
if(!$this->_oauth){ |
| 238 |
require_once dirname(__FILE__).DIRECTORY_SEPARATOR."apiClient.php"; |
| 239 |
$this->_oauth = new apiClient(); |
| 240 |
$this->_oauth->setClientId($this->consumer_key); |
| 241 |
$this->_oauth->setClientSecret($this->consumer_secret); |
| 242 |
$this->_oauth->setRedirectUri($this->redirect_uri); |
| 243 |
$this->_oauth->setApprovalPrompt('auto'); |
| 244 |
$this->_oauth->setAccessType('online'); |
| 245 |
$this->_oauth->setScopes(array( |
| 246 |
'https://www.googleapis.com/auth/userinfo.profile', |
| 247 |
'https://www.googleapis.com/auth/userinfo.email', |
| 248 |
'https://www.googleapis.com/auth/plus.me' |
| 249 |
)); |
| 250 |
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'contrib/apiPlusService.php'; |
| 251 |
$this->plus = new apiPlusService($this->_oauth); |
| 252 |
} |
| 253 |
return $this->_oauth; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Returns Profile |
| 258 |
* @return array |
| 259 |
*/ |
| 260 |
private function get_profile(){ |
| 261 |
$req = new apiHttpRequest("https://www.googleapis.com/oauth2/v1/userinfo"); |
| 262 |
$req = apiClient::$auth->sign($req); |
| 263 |
$resp = apiClient::$io->makeRequest($req)->getResponseBody(); |
| 264 |
return json_decode($resp, 1); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Save redirect url to session |
| 269 |
* @param string $redirect |
| 270 |
*/ |
| 271 |
private function set_redirect($redirect){ |
| 272 |
if($_SESSION){ |
| 273 |
$_SESSION['_wpg_ggl_redirect'] = $redirect; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Returns currentlly saved url. |
| 279 |
* @return string |
| 280 |
*/ |
| 281 |
private function get_redirect(){ |
| 282 |
if(isset($_SESSION['_wpg_ggl_redirect']) && !empty($_SESSION['_wpg_ggl_redirect'])){ |
| 283 |
$url = (string)$_SESSION['_wpg_ggl_redirect']; |
| 284 |
unset($_SESSION['_wpg_ggl_redirect']); |
| 285 |
return $url; |
| 286 |
}else{ |
| 287 |
return null; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Do redirect on session information |
| 293 |
*/ |
| 294 |
private function do_redirect(){ |
| 295 |
$url = $this->get_redirect(); |
| 296 |
if($url){ |
| 297 |
header("Location: ".$url); |
| 298 |
die(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Save action name on session. |
| 304 |
* @param string $action_name |
| 305 |
*/ |
| 306 |
private function set_action($action_name){ |
| 307 |
if(isset($_SESSION)){ |
| 308 |
$_SESSION['_wpg_ggl_action'] = (string)$action_name; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Returns action name from string |
| 314 |
* @return string |
| 315 |
*/ |
| 316 |
protected function get_action(){ |
| 317 |
if(isset($_SESSION['_wpg_ggl_action']) && !empty($_SESSION['_wpg_ggl_action'])){ |
| 318 |
$action = (string)$_SESSION['_wpg_ggl_action']; |
| 319 |
unset($_SESSION['_wpg_ggl_action']); |
| 320 |
return $action; |
| 321 |
}else{ |
| 322 |
return ""; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Save message |
| 328 |
* @param string $string |
| 329 |
*/ |
| 330 |
private function add_message($string){ |
| 331 |
if(isset($_SESSION)){ |
| 332 |
$_SESSION['_wpg_ggl_message'] = $string; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Return if current url is endpoint. |
| 338 |
* @return boolean |
| 339 |
*/ |
| 340 |
private function is_endpoint(){ |
| 341 |
$protocol = is_ssl() ? "https://" : "http://"; |
| 342 |
$current_url = $protocol.$_SERVER["SERVER_NAME"]; |
| 343 |
$path = explode('?', $_SERVER['REQUEST_URI']); |
| 344 |
$current_url .= $path[0]; |
| 345 |
return (untrailingslashit($current_url) == untrailingslashit($this->redirect_uri)); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Echo message |
| 350 |
*/ |
| 351 |
public function print_script(){ |
| 352 |
if(isset($_SESSION) && !empty($_SESSION['_wpg_ggl_message'])){ |
| 353 |
?> |
| 354 |
<script type="text/javascript"> |
| 355 |
jQuery(document).ready(function($){ |
| 356 |
alert("<?php echo esc_attr($_SESSION['_wpg_ggl_message'])?>"); |
| 357 |
}); |
| 358 |
</script> |
| 359 |
<?php |
| 360 |
unset($_SESSION['_wpg_ggl_message']); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|