| 1 |
<?php |
| 2 |
require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR."gianism_controller.php"; |
| 3 |
/** |
| 4 |
* Description of twitter_controller |
| 5 |
* |
| 6 |
* @package gianism |
| 7 |
*/ |
| 8 |
class Twitter_Controller extends Gianism_Controller{ |
| 9 |
|
| 10 |
/** |
| 11 |
* @var string |
| 12 |
*/ |
| 13 |
private $consumer_key = ''; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $consumer_secret = ''; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $my_access_token = ''; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $my_access_token_secret = ''; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $umeta_id = '_wpg_twitter_id'; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public $umeta_screen_name = '_wpg_twitter_screen_name'; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $pseudo_domain = 'pseudo.twitter.com'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor |
| 47 |
* @param array $option |
| 48 |
*/ |
| 49 |
protected function set_option($option) { |
| 50 |
$option = shortcode_atts(array( |
| 51 |
"tw_consumer_key" => "", |
| 52 |
"tw_consumer_secret" => "", |
| 53 |
"tw_access_token" => "", |
| 54 |
"tw_access_token_secret" => "" |
| 55 |
), $option); |
| 56 |
$this->consumer_key = (string)$option['tw_consumer_key']; |
| 57 |
$this->consumer_secret = (string)$option['tw_consumer_secret']; |
| 58 |
$this->my_access_token = (string)$option['tw_access_token']; |
| 59 |
$this->my_access_token_secret = (string)$option['tw_access_token_secret']; |
| 60 |
session_start(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Executed on init hook |
| 65 |
* @global wpdb $wpdb |
| 66 |
* @global int $user_ID |
| 67 |
* @global WP_Gianism $gianism |
| 68 |
*/ |
| 69 |
public function init_action(){ |
| 70 |
global $user_ID, $wpdb, $gianism; |
| 71 |
switch($this->get_action()){ |
| 72 |
case 'twitter_connect': |
| 73 |
if(isset($_GET['oauth_verifier']) && is_user_logged_in()){ |
| 74 |
$token = $this->get_token(); |
| 75 |
$token_secret = $this->get_token(true); |
| 76 |
if(!empty($token) && !empty($token_secret)){ |
| 77 |
$oauth = $this->get_oauth($token, $token_secret); |
| 78 |
$access_token = $oauth->getAccessToken($_GET['oauth_verifier']); |
| 79 |
if(isset($access_token['user_id'], $access_token['screen_name'])){ |
| 80 |
//Check if other user has registered. |
| 81 |
$sql = <<<EOS |
| 82 |
SELECT umeta_id FROM {$wpdb->usermeta} |
| 83 |
WHERE ((meta_key = %s) AND (meta_value = %s) AND (user_id != %d)) |
| 84 |
OR ((meta_key = %s) AND (meta_value = %s) AND (user_id != %d)) |
| 85 |
EOS; |
| 86 |
if(!$wpdb->get_row($sql, $this->umeta_id, $access_token['user_id'], $user_ID, $this->umeta_screen_name, $access_token['screen_name'], $user_ID)){ |
| 87 |
update_user_meta($user_ID, $this->umeta_id, $access_token['user_id']); |
| 88 |
update_user_meta($user_ID, $this->umeta_screen_name, $access_token['screen_name']); |
| 89 |
$this->add_alert(sprintf($gianism->_('Welcome, @%s!'), $access_token['screen_name'])); |
| 90 |
}else{ |
| 91 |
$this->add_alert(sprintf($gianism->_('Mm...? This %s account seems to be connected to another account.'), "Twitter")); |
| 92 |
} |
| 93 |
}else{ |
| 94 |
$this->add_alert($gianism->_('Oops, Failed to Authenticate.')); |
| 95 |
} |
| 96 |
}else{ |
| 97 |
$this->add_alert($gianism->_('Oops, Failed to Authenticate.')); |
| 98 |
} |
| 99 |
}else{ |
| 100 |
$this->add_alert($gianism->_('Oops, Failed to Authenticate.')); |
| 101 |
} |
| 102 |
break; |
| 103 |
case "twitter_disconnect": |
| 104 |
if(wp_verify_nonce($gianism->request('_wpnonce'), 'twitter_disconnect') && is_user_logged_in()){ |
| 105 |
delete_user_meta($user_ID, $this->umeta_id); |
| 106 |
delete_user_meta($user_ID, $this->umeta_screen_name); |
| 107 |
$this->add_alert($gianism->_('Disconnect now :(')); |
| 108 |
} |
| 109 |
break; |
| 110 |
case "twitter_login": |
| 111 |
$user_id = false; |
| 112 |
if(isset($_GET['oauth_verifier'])){ |
| 113 |
$token = $this->get_token(); |
| 114 |
$token_secret = $this->get_token(true); |
| 115 |
if(!empty($token) && !empty($token_secret)){ |
| 116 |
$oauth = $this->get_oauth($token, $token_secret); |
| 117 |
$access_token = $oauth->getAccessToken($_GET['oauth_verifier']); |
| 118 |
if(isset($access_token['user_id'], $access_token['screen_name'])){ |
| 119 |
$twitter_id = $access_token['user_id']; |
| 120 |
$screen_name = $access_token['screen_name']; |
| 121 |
//Get User ID. |
| 122 |
$sql = <<<EOS |
| 123 |
SELECT user_id FROM {$wpdb->usermeta} |
| 124 |
WHERE meta_key = %s AND meta_value = %s |
| 125 |
EOS; |
| 126 |
$user_id = $wpdb->get_var($wpdb->prepare($sql, $this->umeta_id, $twitter_id)); |
| 127 |
if(!$user_id){ |
| 128 |
//Not found, Create New User |
| 129 |
require_once(ABSPATH . WPINC . '/registration.php'); |
| 130 |
//Check if username exists |
| 131 |
$email = $screen_name."@".$this->pseudo_domain; |
| 132 |
$user_name = (!username_exists('@'.$screen_name)) ? '@'.$screen_name : $email; |
| 133 |
$user_id = wp_create_user($user_name, wp_generate_password(), $email); |
| 134 |
|
| 135 |
if(!is_wp_error($user_id)){ |
| 136 |
update_user_meta($user_id, $this->umeta_id, $twitter_id); |
| 137 |
update_user_meta($user_id, $this->umeta_screen_name, $screen_name); |
| 138 |
$wpdb->update( |
| 139 |
$wpdb->users, |
| 140 |
array( |
| 141 |
'display_name' => "@{$screen_name}", |
| 142 |
'user_url' => 'https://twitter.com/#!/'.$screen_name |
| 143 |
), |
| 144 |
array('ID' => $user_id), |
| 145 |
array('%s', '%s'), |
| 146 |
array('%d') |
| 147 |
); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
if($user_id && !is_wp_error($user_id)){ |
| 154 |
wp_set_auth_cookie($user_id, true); |
| 155 |
$redirect = $this->get_redirect_to(admin_url('profile.php')); |
| 156 |
header('Location: '.$redirect); |
| 157 |
die(); |
| 158 |
}else{ |
| 159 |
$this->add_alert($gianism->_('Oops, Failed to Authenticate.')); |
| 160 |
} |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Show connect button on login form |
| 167 |
* @global WP_Gianism $gianism |
| 168 |
* @return void |
| 169 |
*/ |
| 170 |
public function user_profile(){ |
| 171 |
if(!defined("IS_PROFILE_PAGE")){ |
| 172 |
return; |
| 173 |
} |
| 174 |
global $gianism, $user_ID; |
| 175 |
if($this->is_connected()){ |
| 176 |
$url = wp_nonce_url(admin_url('profile.php?wpg=twitter_disconnect'), 'twitter_disconnect'); |
| 177 |
$link_text = $gianism->_('Disconnect'); |
| 178 |
$account = get_user_meta($user_ID, $this->umeta_screen_name, true); |
| 179 |
$desc = '<img src="'.$gianism->url.'/assets/icon-checked.png" alt="Connected" width="16" height="16" />' |
| 180 |
.sprintf($gianism->_('Your account is already connected with Twitter <a target="_blank" href="%1$s">%2$s</a> .'), 'https://twitter.com/#!/'.$account, "@".$account); |
| 181 |
//If user has pseudo mail, add caution. |
| 182 |
global $user_email; |
| 183 |
if($this->is_pseudo_mail($user_email)){ |
| 184 |
$desc .= '<br /><strong>Note:</strong> '.sprintf($gianism->_('Your e-mail address is pseudo "%1$s" and cannot be sent a mail for. If you disconnect twitter account, you may not be able to log in %2$s. Please change it to available e-mail address.'), $user_email, get_bloginfo('name')); |
| 185 |
} |
| 186 |
$onclick = ' onclick="if(!confirm(\''.$gianism->_('You really disconnect this account?').'\')) return false;"'; |
| 187 |
}else{ |
| 188 |
//Create Link |
| 189 |
$callback_url = admin_url('profile.php?wpg=twitter_connect'); |
| 190 |
$oauth = $this->get_oauth(); |
| 191 |
$token = $oauth->getRequestToken($callback_url); |
| 192 |
$this->save_token($token); |
| 193 |
$url = $oauth->getAuthorizeURL($token); |
| 194 |
$link_text = $gianism->_('Connect'); |
| 195 |
$desc = sprintf($gianism->_('Connecting %1$s account, you can log in %2$s via %1$s account.'),"Twitter", get_bloginfo('name')); |
| 196 |
$onclick = ''; |
| 197 |
} |
| 198 |
?> |
| 199 |
<tr> |
| 200 |
<th><?php $gianism->e('Twitter'); ?></th> |
| 201 |
<td> |
| 202 |
<a class="wpg_tw_btn" href="<?php echo $url; ?>"<?php echo $onclick; ?>> |
| 203 |
<i></i> |
| 204 |
<span class="label"><?php echo $link_text;?></span> |
| 205 |
</a> |
| 206 |
<p class="description"><?php echo $desc;?></p> |
| 207 |
</td> |
| 208 |
</tr> |
| 209 |
<?php |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Show login button on login form |
| 214 |
* @global WP_Giasnism $gianism |
| 215 |
*/ |
| 216 |
public function login_form(){ |
| 217 |
global $gianism; |
| 218 |
$redirect_to = $this->get_redirect_to(admin_url('profile.php')); |
| 219 |
$login_url = wp_login_url($redirect_to); |
| 220 |
$login_url .= (false !== strpos($login_url, '?')) ? "&" : '?'; |
| 221 |
$login_url .= 'wpg=twitter_login'; |
| 222 |
$oauth = $this->get_oauth(); |
| 223 |
$token = $oauth->getRequestToken($login_url); |
| 224 |
$this->save_token($token); |
| 225 |
$url = $oauth->getAuthorizeURL($token); |
| 226 |
$link_text = $gianism->_('Login with Twitter'); |
| 227 |
$onclick = ''; |
| 228 |
?> |
| 229 |
<a class="wpg_tw_btn" href="<?php echo $url; ?>"<?php echo $onclick; ?>> |
| 230 |
<i></i> |
| 231 |
<span class="label"><?php echo $link_text;?></span> |
| 232 |
</a> |
| 233 |
<?php |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns whether user has twitter account |
| 238 |
* @global int $user_ID |
| 239 |
* @param int $user_ID |
| 240 |
* @return boolean |
| 241 |
*/ |
| 242 |
private function is_connected($user_ID = null){ |
| 243 |
if(is_null($user_ID)){ |
| 244 |
global $user_ID; |
| 245 |
} |
| 246 |
return (boolean)get_user_meta($user_ID, $this->umeta_id, true); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Get API wrapper |
| 251 |
* @param string $oauth_token |
| 252 |
* @param string $oauth_token_secret |
| 253 |
* @return TwitterOAuth |
| 254 |
*/ |
| 255 |
private function get_oauth($oauth_token = NULL, $oauth_token_secret = NULL){ |
| 256 |
require_once dirname(__FILE__).DIRECTORY_SEPARATOR."twitteroauth.php"; |
| 257 |
return new TwitterOAuth($this->consumer_key, $this->consumer_secret, $oauth_token, $oauth_token_secret); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Save token. if failed, return false. |
| 262 |
* @param array $token |
| 263 |
* @return booelan |
| 264 |
*/ |
| 265 |
private function save_token($token){ |
| 266 |
if(isset($token['oauth_token'], $token['oauth_token_secret'])){ |
| 267 |
$_SESSION['_wpg_twitter_token'] = $token; |
| 268 |
return true; |
| 269 |
}else{ |
| 270 |
return false; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Returns session stored token. |
| 276 |
* @param boolean $secret |
| 277 |
* @return string |
| 278 |
*/ |
| 279 |
private function get_token($secret = false){ |
| 280 |
$key = $secret ? 'oauth_token_secret' : 'oauth_token'; |
| 281 |
if(isset($_SESSION['_wpg_twitter_token'][$key])){ |
| 282 |
return $_SESSION['_wpg_twitter_token'][$key]; |
| 283 |
}else{ |
| 284 |
return false; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Add alert message throw Javascript |
| 290 |
* @param string $text |
| 291 |
*/ |
| 292 |
public function add_alert($text){ |
| 293 |
$this->js = true; |
| 294 |
if(!empty($this->scripts)){ |
| 295 |
$this->scripts .= "\n"; |
| 296 |
} |
| 297 |
$this->scripts .= $text; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Print Javascript on footer |
| 302 |
*/ |
| 303 |
public function print_script(){ |
| 304 |
if($this->js && !empty($this->scripts)){ |
| 305 |
?> |
| 306 |
<script type="text/javascript"> |
| 307 |
jQuery(document).ready(function($){ |
| 308 |
alert("<?php echo esc_attr($this->scripts); ?>"); |
| 309 |
}); |
| 310 |
</script> |
| 311 |
<?php |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Mail Handler for pseudo mail |
| 317 |
* @global WP_Gianism $gianism |
| 318 |
* @param int $user_id |
| 319 |
* @param string $subject |
| 320 |
* @param string $message |
| 321 |
* @param array $headers |
| 322 |
* @param array $attchment |
| 323 |
*/ |
| 324 |
public function wp_mail($user_id, $subject, $message, $headers, $attchment){ |
| 325 |
global $gianism; |
| 326 |
//Save Message |
| 327 |
wp_insert_post(array( |
| 328 |
'post_type' => $gianism->message_post_type, |
| 329 |
'post_title' => $subject, |
| 330 |
'post_content' => $message, |
| 331 |
'post_author' => $user_id, |
| 332 |
'post_status' => 'publish' |
| 333 |
)); |
| 334 |
//Send DM |
| 335 |
$this->send_dm($user_id, $subject); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Send direct message on twitter. |
| 340 |
* @global WP_Gianism $gianism |
| 341 |
* @param int $user_id |
| 342 |
* @param string $text |
| 343 |
*/ |
| 344 |
private function send_dm($user_id, $subject){ |
| 345 |
global $gianism; |
| 346 |
$oauth = $this->get_oauth($this->my_access_token, $this->my_access_token_secret); |
| 347 |
$twitter_id = get_user_meta($user_id, $this->umeta_id, true); |
| 348 |
if($twitter_id){ |
| 349 |
$endpoint = "https://api.twitter.com/1/direct_messages/new.json"; |
| 350 |
$body = sprintf($gianism->_('You have message "%1$s" on %2$s. %3$s'), $subject, get_bloginfo('name'), admin_url('profile.php')); |
| 351 |
$result = $oauth->oAuthRequest($endpoint, 'POST', array( |
| 352 |
'user_id' => $twitter_id, |
| 353 |
'text' => $body |
| 354 |
)); |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|