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 / facebook / facebook_controller.php

facebook_controller.php in Gianism 1.1, at sdks/facebook/facebook_controller.php

489 lines 12.8 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 /**
5 * Description of facebook_controller
6 *
7 * @package gianism
8 */
9 class Facebook_Controller extends Gianism_Controller{
10
11 /**
12 * @var string
13 */
14 private $app_id = '';
15
16 /**
17 * @var string
18 */
19 private $app_secret = '';
20
21 /**
22 * Facebook API Controller
23 * @var Facebook
24 */
25 public $_api = null;
26
27 /**
28 * Page ID of Fan gate.
29 * @var int
30 */
31 private $fan_gate = 0;
32
33 /**
34 * Path to cert file.
35 * @var string
36 */
37 private $cert_path = '';
38
39 /**
40 * Meta key of postmeta for facebook id
41 * @var string
42 */
43 public $umeta_id = '_wpg_facebook_id';
44
45 /**
46 * Meta key of postmeta for facebook mail
47 * @var string
48 */
49 public $umeta_mail = '_wpg_facebook_mail';
50
51 /**
52 * @var string
53 */
54 private $message = "";
55
56 /**
57 * @var array
58 */
59 private $_signed_request = array();
60
61 /**
62 * Setup Everything
63 * @param array $option
64 */
65 protected function set_option($option) {
66 $option = shortcode_atts(array(
67 'fb_app_id' => '',
68 'fb_app_secret' => '',
69 'fb_fan_gate' => 0
70 ), $option);
71 $this->app_id = (string)$option['fb_app_id'];
72 $this->app_secret = (string)$option['fb_app_secret'];
73 $this->fan_gate = (int) $option['fb_fan_gate'];
74 $this->cert_path = dirname(__FILE__).DIRECTORY_SEPARATOR."fb_ca_chain_bundle.crt";
75 //Add Hook on Fan Gate
76 if($fan_gate){
77 add_action('template_redirect', array($this, 'fan_gate_helper'));
78 }
79 //Start Session
80 session_start();
81 }
82
83
84 /**
85 * Executed on init hook.
86 * @global wpdb $wpdb
87 * @global int $user_ID
88 * @global WP_Gianism $giasnism
89 */
90 public function init_action(){
91 global $user_ID, $wpdb, $gianism;
92 switch($this->get_action()){
93 case "facebook_connect":
94 $uid = $this->facebook()->getUser();
95 $this->message = $gianism->_("Oops, Failed to Authenticate.");
96 if($uid && is_user_logged_in()){
97 try{
98 if(!isset($_SESSION['uid'])){
99 $_SESSION['uid'] = $uid;
100 }
101 try{
102 $profile = $this->facebook()->api('/me');
103 }catch(FacebookApiException $e){
104 $profile = $this->facebook()->api('/'.$uid);
105 }
106 if(isset($profile['email'])){
107 //Check if other user has these as meta_value
108 $email = $profile['email'];
109 $sql = <<<EOS
110 SELECT user_id FROM {$wpdb->usermeta}
111 WHERE ((meta_key = %s) AND (meta_value = %s) AND (user_id != %d))
112 OR ((meta_key = %s) AND (meta_value = %s) AND (user_id != %d))
113 EOS;
114 $others = $wpdb->get_row($wpdb->prepare($sql, $this->umeta_id, $uid, $user_ID, $this->umeta_mail, $email, $user_ID));
115 $email_exitance = email_exists($email);
116 if(!$others && (!$email_exitance || $user_ID == $email_exitance)){
117 update_user_meta($user_ID, $this->umeta_id, $uid);
118 update_user_meta($user_ID, $this->umeta_mail, $email);
119 $this->message = sprintf($gianism->_('Welcome!, %s'), $profile['name']);
120 }else{
121 $this->message = sprintf($gianism->_('Mm...? This %s account seems to be connected to another account.'), "Facebook");
122 }
123 }
124 }catch(FacebookApiException $e){
125 $this->message = $gianism->_("Oops, Failed to Authenticate.")."\n".$e->getMessage();
126 }
127 }
128 break;
129 case "facebook_disconnect":
130 if(is_user_logged_in() && isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'facebook_disconnect')){
131 delete_user_meta($user_ID, $this->umeta_id);
132 delete_user_meta($user_ID, $this->umeta_mail);
133 $this->message = sprintf($gianism->_("Disconected your %s account."), "Facebook");
134 }
135 break;
136 case "facebook_login":
137 if(!is_user_logged_in()){
138 $this->message = $giasnism->_('Oops, Failed to Authenticate.');
139 $facebook_id = $this->facebook()->getUser();
140 if($facebook_id){
141 //Get Facebook ID, So try to find registered user.
142 global $wpdb;
143 $sql = <<<EOS
144 SELECT user_id FROM {$wpdb->usermeta}
145 WHERE meta_key = %s AND meta_value = %s
146 EOS;
147 $user_id = $wpdb->get_var($wpdb->prepare($sql, $this->umeta_id, $facebook_id));
148 if(!$user_id){
149 //Cant Find user, try to find by email
150 try{
151 try{
152 $profile = $this->facebook()->api('/me');
153 }catch(FacebookApiException $e){
154 $profile = $this->facebook()->api('/'.$facebook_id);
155 }
156 if(isset($profile['email'])){
157 $email = (string)$profile['email'];
158 //Try to find registered user
159 $user_id = email_exists($email);
160 if(!$user_id){
161 //Not found, thus seek usermeta
162 $sql = <<<EOS
163 SELECT user_id FROM {$wpdb->usermeta}
164 WHERE meta_key = %s AND meta_value = %s
165 EOS;
166 $user_id = $wpdb->get_var($wpdb->prepare($sql, $this->umeta_mail, $email));
167 if(!$user_id){
168 //Not found, Create New User
169 require_once(ABSPATH . WPINC . '/registration.php');
170 //Check if username exists
171 $user_name = (!username_exists($profile['username'])) ? $profile['username'] : $email;
172 $user_id = wp_create_user($user_name, wp_generate_password(), $email);
173 if(!is_wp_error($user_id)){
174 update_user_meta($user_id, $this->umeta_id, $facebook_id);
175 update_user_meta($user_id, $this->umeta_mail, $email);
176 $wpdb->update(
177 $wpdb->users,
178 array(
179 'display_name' => $profile['name'],
180 'user_url' => $profile['link']
181 ),
182 array('ID' => $user_id),
183 array('%s', '%s'),
184 array('%d')
185 );
186 }
187 }
188 }
189 }
190 }catch(FacebookApiException $e){
191 //Can't get email, so, error.
192 $this->message .= "\n".$e->getMessage();
193 }
194 }
195 }
196 if($user_id && !is_wp_error($user_id)){
197 wp_set_auth_cookie($user_id, true);
198 if(isset($_GET['redirect_to'])){
199 header('Location: '.$_GET['redirect_to']);
200 die();
201 }
202 }
203 }
204 break;
205 }
206 }
207
208
209 /**
210 * Add User form
211 * @global WP_Gianism $gianism
212 * @param WP_User $current_user
213 */
214 public function user_profile($current_user){
215 if(!defined("IS_PROFILE_PAGE")){
216 return;
217 }
218 global $gianism;
219 //Show Login button
220 if(!$this->get_user_id()){
221 $link_text = $gianism->_('Connect');
222 $desc = sprintf($gianism->_('Connecting with Facebook account, you can log in %s via Facebook account.'), get_bloginfo('name'));
223 $onclick = '';
224 $url = $this->facebook()->getLoginUrl(array(
225 'scope' => 'email',
226 'redirect_uri' => admin_url('profile.php?wpg=facebook_connect')
227 ));
228 }else{
229 $link_text = $gianism->_('Disconnect');
230 $desc = '<img src="'.$gianism->url.'/assets/icon-checked.png" alt="Connected" width="16" height="16" />'
231 .$gianism->_('Your account is already connected with Facebook account.');
232 $onclick = ' onclick="if(!confirm(\''.$gianism->_('You really disconnect this account?').'\')) return false;"';
233 $url = wp_nonce_url(admin_url('profile.php?wpg=facebook_disconnect'), 'facebook_disconnect');
234 }
235 ?>
236 <tr>
237 <th><?php $gianism->e('Facebook'); ?></th>
238 <td>
239 <div id="fb-connector">
240 <a class="fb_button fb_button_medium" id="fb-login" href="<?php echo $url; ?>"<?php echo $onclick; ?>>
241 <span class="fb_button_text"><?php echo $link_text;?></span>
242 </a>
243 <p class="description"><?php echo $desc;?></p>
244 </div>
245 <!-- #fb-connector -->
246 </td>
247 </tr>
248 <?php
249 }
250
251 /**
252 * Show Login Button on Facebook.
253 * @global WP_Gianism $gianism
254 */
255 function login_form(){
256 global $gianism;
257 $redirect = isset($_REQUEST['redirect_to']) ? $gianism->request('redirect_to') : admin_url('profile.php');
258 $login_url = wp_login_url($redirect)."&wpg=facebook_login";
259 $url = $this->facebook()->getLoginUrl(array(
260 'scope' => 'email',
261 'redirect_uri' => $login_url,
262 ));
263 //Show Login Button
264 $this->js = true;
265 ?>
266 <a class="fb_button fb_button_medium" id="fb-login" href="<?php echo $url; ?>">
267 <span class="fb_button_text"><?php $gianism->e('Log in with Facebook');?></span>
268 </a>
269 <?php
270 }
271
272 /**
273 * Returns if user like my page. Only available on Facebook Tab page or application.
274 * @return boolean
275 */
276 public function is_user_like_me_on_fangate(){
277 if($this->fan_gate > 0){
278 $page = $this->signed_request('page');
279 return (isset($page['liked']) && $page['liked']);
280 }else{
281 return false;
282 }
283 }
284
285 /**
286 * Returns if current facebook user is wordpress registered user.
287 *
288 * If current Facebook user is registerd on your WordPress, returns user ID on WordPress.
289 *
290 * @global wpdb $wpdb
291 * @return int
292 */
293 public function is_registered_user_on_fangate(){
294 global $wpdb;
295 if($this->fan_gate > 0){
296 $uid = $this->signed_request('user_id');
297 $sql = <<<EOS
298 SELECT user_id FROM {$wpdb->usermeta}
299 WHERE meta_key = %s AND meta_value = %s
300 EOS;
301 return $wpdb->get_var($wpdb->prepare($sql, $this->umeta_id, $uid));
302 }else{
303 return false;
304 }
305 }
306
307 /**
308 * Return true if current user is not logged in Facebook
309 * @return boolean
310 */
311 public function is_guest_on_fangate(){
312 if($this->fan_gate > 0){
313 return (false == (boolean)$this->signed_request('user_id'));
314 }else{
315 return false;
316 }
317 }
318
319 /**
320 * Returns if current page is fan gate.
321 * @return string
322 */
323 public function is_fangate(){
324 if($this->fan_gate > 0){
325 $page = $this->signed_request('page');
326 if($page){
327 return is_page($this->fan_gate);
328 }else{
329 return false;
330 }
331 }else{
332 return false;
333 }
334 }
335
336 /**
337 * Get signed request
338 * @return string
339 */
340 private function signed_request($key){
341 if(empty($this->_signed_request)){
342 $this->_signed_request = $this->facebook()->getSignedRequest();
343 }
344 return isset($this->_signed_request[$key]) ? $this->_signed_request[$key]: null;
345 }
346
347 /**
348 * Initialize Facebook Fangate Scripts
349 * @global WP_Gianism $gianism
350 */
351 public function fan_gate_helper(){
352 global $gianism;
353 if(is_page($this->fan_gate)){
354 $this->js = true;
355 }
356 $this->scripts .= <<<EOS
357 FB.Canvas.setAutoGrow();
358 EOS;
359 }
360
361 /**
362 * Print JS on footer of both admin panel and public page.
363 *
364 * Paramater 'js' should be true and if 'js' is 'no-fb-root',
365 * div#fb-root won't be displayed.
366 *
367 * @return void
368 */
369 public function print_script(){
370 $locale = 'ja_JP';
371 if($this->js !== false):
372 ?>
373 <?php if($this->js !== 'no-fb-root'):?>
374 <div id="fb-root"></div>
375 <?php endif;?>
376 <script type="text/javascript">
377 window.fbAsyncInit = function() {
378 <?php if(is_ssl()):?>
379 FB._https = true;
380 <?php endif;?>
381 FB.init({
382 appId: '<?php echo $this->app_id ?>',
383 cookie: true,
384 xfbml: true,
385 oauth: true
386 });
387 <?php echo $this->scripts; ?>
388 };
389 (function(){
390 var e = document.createElement('script');
391 e.async = true;
392 e.src = document.location.protocol + "//connect.facebook.net/<?php echo $locale; ?>/all.js";
393 document.getElementById('fb-root').appendChild(e);
394 })();
395 </script>
396 <?php
397 endif;
398 if(!empty($this->message)): ?>
399 <script type="text/javascript">
400 jQuery(document).ready(function($){
401 alert("<?php echo esc_attr($this->message); ?>");
402 });
403 </script>
404 <?php endif;
405 }
406
407 /**
408 * Returns User's Facebook ID
409 * @global int $user_ID
410 * @param int $user_id
411 * @return int
412 */
413 public function get_user_id($user_id = null){
414 $user_id = $this->wp_user_id($user_id);
415 if($user_id){
416 $facebook_id = get_user_meta($user_id, '_wpg_facebook_id', true);
417 return $facebook_id ? $facebook_id : 0;
418 }else{
419 return 0;
420 }
421 }
422
423 /**
424 * Returns Facebook mail
425 * @param int $user_id
426 * @return string
427 */
428 public function get_user_mail($user_id = null){
429 $user_id = $this->wp_user_id($user_id);
430 if($user_id){
431 $facebook_mail = get_user_meta($user_id, '_wpg_facebook_mail', true);
432 return $facebook_mail ? $facebook_mail : null;
433 }else{
434 return null;
435 }
436 }
437
438 /**
439 * Save Facebook Mail
440 * @param string $mail
441 * @param int $user_id
442 * @return void
443 */
444 public function set_user_mail($mail, $user_id = null){
445 $user_id = $this->wp_user_id($user_id);
446 update_user_meta($user_id, '_wpg_facebook_mail');
447 }
448
449 /**
450 * Save Facebook ID
451 * @param string $fb_user_id
452 * @param int $user_id
453 * @return void
454 */
455 public function set_user_id($fb_user_id, $user_id = null){
456 $user_id = $this->wp_user_id($user_id);
457 update_user_meta($user_id, '_wpg_facebook_id', $fb_user_id);
458 }
459
460 /**
461 * Returns WordPress's user ID
462 * @global int $user_ID
463 * @param int $user_id
464 * @return int
465 */
466 private function wp_user_id($user_id = null){
467 if(is_null($user_id)){
468 global $user_ID;
469 $user_id = $user_ID;
470 }
471 return (int)$user_id;
472 }
473
474 /**
475 * Returns facebook Controller
476 * @return Facebook
477 */
478 private function facebook(){
479 if(is_null($this->_api)){
480 require_once dirname(__FILE__).DIRECTORY_SEPARATOR."facebook.php";
481 $this->_api = new Facebook(array(
482 'appId' => $this->app_id,
483 'secret' => $this->app_secret,
484 'cookie' => true
485 ));
486 }
487 return $this->_api;
488 }
489 }