PluginProbe
Gianism / 1.1
Gianism v1.1
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / sdks / twitter / twitter_controller.php

twitter_controller.php in Gianism 1.1, at sdks/twitter/twitter_controller.php

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