2be7e464 by Chris Boden

Updates on Facebook auth

1 parent b58e14b4
...@@ -12,7 +12,9 @@ const REG_METH_VALID_EMAIL = 2; ...@@ -12,7 +12,9 @@ const REG_METH_VALID_EMAIL = 2;
12 const FORGOT_METH_VALID_EMAIL = 1; 12 const FORGOT_METH_VALID_EMAIL = 1;
13 const FORGOT_METH_RAND_PASS = 2; 13 const FORGOT_METH_RAND_PASS = 2;
14 14
15 const ACTION_ACTIVATE = 'activate_account'; 15 const ACTION_LOGIN = 'auth_login'; // probably don't need
16 const ACTION_LOGOUT = 'auth_logout'; // probably need, tell FB/etc to remove their cookies
17 const ACTION_ACTIVATE = 'auth_activate';
16 18
17 const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`) 19 const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`)
18 20
......
1 <?php 1 <?php
2 /** 2 /**
3 * Note: If there is an inconsistent error
4 * it's due to how I changed the FB load
5 * process, may need to change how JS is loaded
6 * Proabably move FB.init and FB.Event.subscribe
7 * to my init method
8 *
3 * This needs to go in the <html tag 9 * This needs to go in the <html tag
4 * xmlns:fb="http://www.facebook.com/2008/fbml" 10 * xmlns:fb="http://www.facebook.com/2008/fbml"
5 * 11 *
...@@ -12,60 +18,94 @@ ...@@ -12,60 +18,94 @@
12 * API Key: 3bcccfd8c28c52197141266d9e417649 18 * API Key: 3bcccfd8c28c52197141266d9e417649
13 * App Secret: 9bfcd828bc6ccef12336dea57df93ecb 19 * App Secret: 9bfcd828bc6ccef12336dea57df93ecb
14 * App ID: 138943536118944 20 * App ID: 138943536118944
21 *
22 * Graph API Reference:
23 * http://developers.facebook.com/docs/reference/api/user
15 */ 24 */
16 25
17 namespace Tz\WordPress\Tools\Auth\Facebook; 26 namespace Tz\WordPress\Tools\Auth\Facebook;
18 27
19 use Tz\WordPress\Tools; 28 use Tz\WordPress\Tools;
20 use Tz\WordPress\Tools\ShortCodes as SC; 29 use Tz\WordPress\Tools\Auth;
21 30
22 use InvalidArgumentException; 31 use InvalidArgumentException;
23 32
24 const OPTION_NAME = 'tz_auth_fb'; 33 const OPTION_NAME = 'tz_auth_fb';
25 34
26 call_user_func(function() { 35 call_user_func(function() {
27 SC\add_shortcodes(__NAMESPACE__ . '\ShortCodes'); 36 Vars::$options = new Tools\WP_Option(OPTION_NAME, Array('button_title' => 'Login'));
28 Vars::$options = new Tools\WP_Option(OPTION_NAME); 37
38 Tools\add_actions(__NAMESPACE__ . '\Actions');
39 Tools\add_shortcodes(__NAMESPACE__ . '\ShortCodes');
29 40
30 if (is_admin()) { 41 if (is_admin()) {
31 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php'); 42 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
32 } 43 }
33 }); 44 });
34 45
35 function loadJSSDK() { 46 function drawLoginButton($echo = true) {
36 return ' 47 $btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . Vars::$options['button_title'] . '</span></a>';
37 <div id="fb-root"></div> 48
38 <script> 49 if (!$echo) {
39 window.fbAsyncInit = function() { 50 return $btn;
40 FB.init({appId: \'' . Vars::$options['application_id'] . '\', status: true, cookie: true, xfbml: true}); 51 }
41 FB.Event.subscribe(\'auth.login\', function(response) { window.location.reload(); });
42 };
43 (function() {
44 var e = document.createElement(\'script\'); e.async = true;
45 e.src = document.location.protocol + \'//connect.facebook.net/en_US/all.js\';
46 document.getElementById(\'fb-root\').appendChild(e);
47 }());
48 </script>
49 ';
50 }
51 52
52 function drawLoginButton($value = 'Login') { 53 echo $btn;
53 echo '<fb:login-button></fb:login-button>';
54 // echo '<a class="fb_button fb_button_medium"><span class="fb_button_text">' . $value . '</span></a>';
55 } 54 }
56 55
57 class Vars { 56 class Actions {
58 public static $options = false; 57 public static function wp() {
58 global $post; // I want a better way to do this
59
60 if ($post->ID == Auth\Vars::$options['login_page'] && !is_user_logged_in()) {
61 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'facebook-sdk.php');
62 Vars::$sdk = new \FB\Facebook(Array(
63 'appId' => Vars::$options['application_id']
64 , 'secret' => Vars::$options['application_secret']
65 , 'cookie' => true
66 ));
67
68 if (Vars::$sdk->getSession()) {
69 $info = Vars::$sdk->api('/me');
70 // get email, verify vs database
71 // register and/or login
72 }
73 }
74 }
75
76 public static function wp_enqueue_scripts() {
77 if (is_admin() || is_user_logged_in()) {
78 return;
79 }
80
81 _enqueue_script('facebook-all', 'http://connect.facebook.net/en_US/all.js');
82 _enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent'));
83
84 _localize_script('tz-facebook', 'TzFBData', Array('AppID' => Vars::$options['application_id'], 'ext_perms' => implode(',', array_keys(Vars::$options['ext_perms']))));
85 }
86
87 public static function get_footer() {
88 echo '<div id="fb-root"></div>';
89 }
59 } 90 }
60 91
61 class ShortCodes { 92 class ShortCodes {
62 public static function fb_login_button() { 93 public static function fb_login_button() {
94 if (Vars::$sdk->getSession()) {
63 ob_start(); 95 ob_start();
64 drawLoginButton(); 96 print_r(Vars::$sdk->api('/me'));
65 $btn = ob_get_contents(); 97 $data = '<pre>' . ob_get_contents() . '</pre>';
66 ob_end_clean(); 98 ob_end_clean();
67 99
68 return $btn; 100 return $data;
101 } else {
102 return drawLoginButton(false);
103 }
69 } 104 }
70 } 105 }
106
107 class Vars {
108 public static $options;
109 public static $sdk;
110 }
71 ?> 111 ?>
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -10,7 +10,6 @@ const OPTION_SECTION_CRED = 'fb_creds'; ...@@ -10,7 +10,6 @@ const OPTION_SECTION_CRED = 'fb_creds';
10 const OPTION_SECTION_SEL = 'fb_opts'; 10 const OPTION_SECTION_SEL = 'fb_opts';
11 11
12 call_user_func(function() { 12 call_user_func(function() {
13 Vars::$options = new Tools\WP_Option(Facebook\OPTION_NAME);
14 Tools\add_actions(__NAMESPACE__ . '\Actions'); 13 Tools\add_actions(__NAMESPACE__ . '\Actions');
15 }); 14 });
16 15
...@@ -19,7 +18,6 @@ function validate($data) { ...@@ -19,7 +18,6 @@ function validate($data) {
19 } 18 }
20 19
21 class Vars { 20 class Vars {
22 public static $options = false;
23 public static $data_permissions = Array('email', 'read_insights', 'read_stream', 'read_mailbox', 'ads_management', 'xmpp_login', 'user_about_me', 'user_activities', 'user_birthday', 'user_education_history', 'user_events', 'user_groups', 'user_hometown', 'user_interests', 'user_likes', 'user_location', 'user_notes', 'user_online_presence', 'user_photo_video_tags', 'user_photos', 'user_relationships', 'user_religion_politics', 'user_status', 'user_videos', 'user_website', 'user_work_history', 'read_friendlists', 'read_requests'); 21 public static $data_permissions = Array('email', 'read_insights', 'read_stream', 'read_mailbox', 'ads_management', 'xmpp_login', 'user_about_me', 'user_activities', 'user_birthday', 'user_education_history', 'user_events', 'user_groups', 'user_hometown', 'user_interests', 'user_likes', 'user_location', 'user_notes', 'user_online_presence', 'user_photo_video_tags', 'user_photos', 'user_relationships', 'user_religion_politics', 'user_status', 'user_videos', 'user_website', 'user_work_history', 'read_friendlists', 'read_requests');
24 } 22 }
25 23
...@@ -27,7 +25,7 @@ class Actions { ...@@ -27,7 +25,7 @@ class Actions {
27 public static function admin_init() { 25 public static function admin_init() {
28 register_setting(Auth\Settings\OPTION_GROUP, Facebook\OPTION_NAME, __NAMESPACE__ . '\validate'); 26 register_setting(Auth\Settings\OPTION_GROUP, Facebook\OPTION_NAME, __NAMESPACE__ . '\validate');
29 27
30 add_settings_section(OPTION_SECTION_CRED, 'Facebook Credentials', function(){}, Auth\Settings\ADMIN_PAGE); 28 add_settings_section(OPTION_SECTION_CRED, 'Facebook Credentials', function(){ echo '<p>You can retreive this information from your <a href="http://www.facebook.com/developers/apps.php?app_id=' . Facebook\Vars::$options['application_id'] . '">Facebook | Developers page</a></p>'; }, Auth\Settings\ADMIN_PAGE);
31 Tools\add_settings_fields(__NAMESPACE__ . '\Cred_Fields', Auth\Settings\ADMIN_PAGE, OPTION_SECTION_CRED); 29 Tools\add_settings_fields(__NAMESPACE__ . '\Cred_Fields', Auth\Settings\ADMIN_PAGE, OPTION_SECTION_CRED);
32 30
33 add_settings_section(OPTION_SECTION_SEL, 'Facebook Extended Data Permissions', function() { echo '<p>Select which additional data you wish to collect from the user</p>'; }, Auth\Settings\ADMIN_PAGE); 31 add_settings_section(OPTION_SECTION_SEL, 'Facebook Extended Data Permissions', function() { echo '<p>Select which additional data you wish to collect from the user</p>'; }, Auth\Settings\ADMIN_PAGE);
...@@ -39,21 +37,25 @@ class Actions { ...@@ -39,21 +37,25 @@ class Actions {
39 37
40 class Cred_Fields { 38 class Cred_Fields {
41 public static function api_key() { 39 public static function api_key() {
42 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" size="45" />'; 40 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" size="45" />';
43 } 41 }
44 42
45 public static function application_secret() { 43 public static function application_secret() {
46 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" size="45" />'; 44 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" size="45" />';
47 } 45 }
48 46
49 public static function application_id() { 47 public static function application_id() {
50 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Vars::$options[__FUNCTION__] . '" />'; 48 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" />';
49 }
50
51 public static function button_title() {
52 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" />';
51 } 53 }
52 } 54 }
53 55
54 class Opt_Fields { 56 class Opt_Fields {
55 public function __call($fn, $args) { 57 public function __call($fn, $args) {
56 echo '<input type="checkbox" id="' . $fn . '" name="' . Facebook\OPTION_NAME . '[ext_perms][' . $fn . ']" value="1" ' . checked('1', Vars::$options['ext_perms'][$fn], false) . ' />'; 58 echo '<input type="checkbox" id="' . $fn . '" name="' . Facebook\OPTION_NAME . '[ext_perms][' . $fn . ']" value="1" ' . checked('1', Facebook\Vars::$options['ext_perms'][$fn], false) . ' />';
57 } 59 }
58 } 60 }
59 ?> 61 ?>
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -284,13 +284,7 @@ class Facebook ...@@ -284,13 +284,7 @@ class Facebook
284 $cookieName = $this->getSessionCookieName(); 284 $cookieName = $this->getSessionCookieName();
285 if (isset($_COOKIE[$cookieName])) { 285 if (isset($_COOKIE[$cookieName])) {
286 $session = array(); 286 $session = array();
287 parse_str(trim( 287 parse_str(trim(stripslashes($_COOKIE[$cookieName]), '"'), $session);
288 get_magic_quotes_gpc()
289 ? stripslashes($_COOKIE[$cookieName])
290 : $_COOKIE[$cookieName],
291 '"'
292 ), $session);
293 $session = $this->validateSessionObject($session);
294 // write only if we need to delete a invalid session cookie 288 // write only if we need to delete a invalid session cookie
295 $write_cookie = empty($session); 289 $write_cookie = empty($session);
296 } 290 }
......
1 window.fbAsyncInit = function() {
2 FB.init({appId: TzFBData.AppID, status: true, cookie: true, xfbml: true});
3 FB.Event.subscribe('auth.login', function(response) { window.location.reload(); });
4 };
5
6 var TzFB = function() {
7 var init = function() {
8 var oBtn = document.getElementById('TzFB');
9 if (oBtn) {
10 addEvent(oBtn, 'click', api.login);
11 }
12 }
13
14 var api = {
15 login: function() {
16 FB.login(function() {}, {perms: TzFBData.ext_perms});
17 }
18 };
19
20
21 addEvent(window, 'load', init);
22 return api;
23 }();
...\ No newline at end of file ...\ No newline at end of file
...@@ -8,6 +8,8 @@ Author: Tenzing ...@@ -8,6 +8,8 @@ Author: Tenzing
8 8
9 namespace Tz\WordPress\Tools; 9 namespace Tz\WordPress\Tools;
10 10
11 use Tz\WordPress\Tools\ShortCodes;
12
11 use \ReflectionClass, \ReflectionMethod; 13 use \ReflectionClass, \ReflectionMethod;
12 use \Exception; 14 use \Exception;
13 15
...@@ -80,6 +82,10 @@ function add_filters($class) { ...@@ -80,6 +82,10 @@ function add_filters($class) {
80 } 82 }
81 } 83 }
82 84
85 function add_shortcodes($class) {
86 ShortCodes\add_shortcodes($class);
87 }
88
83 function add_settings_fields($class, $page = 'general', $section = 'default') { 89 function add_settings_fields($class, $page = 'general', $section = 'default') {
84 if (!class_exists($class)) { 90 if (!class_exists($class)) {
85 throw new Exception("{$class} does not exist"); 91 throw new Exception("{$class} does not exist");
......