public function __construct() { if (!self::$Facebook) { FacebookInfo::getConfig(); self::$Facebook = new Facebook(FacebookInfo::$configs); } }
/** * Get the info on this plugin * @param string name to retrieve (default 'version') * - 'name' => Plugin Name * - 'author' => Author Name * - 'email' => Support Email * - 'link' => Support Link * - 'license' => License Info * @return string plugin version */ function info($name = 'version') { if (FacebookInfo::_isAvailable($name)) { return FacebookInfo::$name(); } else { return "{$name} is not an available option"; } }
/** * Testing getting a configuration option. * @param key to search for * @return mixed result of configuration key. * @access public */ static function getConfig($key = null) { if (isset(self::$configs[$key])) { return self::$configs[$key]; } //try configure setting if (self::$configs[$key] = Configure::read("Facebook.{$key}")) { return self::$configs[$key]; } //try load configuration file and try again. Configure::load('facebook'); self::$configs = Configure::read('Facebook'); if (self::$configs[$key] = Configure::read("Facebook.{$key}")) { return self::$configs[$key]; } return null; }
/** * HTML XMLNS tag (required) * Facebook Auth 2.0 support * @param array $options * @example $this->Facebook->init(); * @return string of scriptBlock for FB.init() or error */ public function init($options = null, $reload = true) { $options = array_merge(array('perms' => 'email'), (array) $this->loginOptions); if ($appId = FacebookInfo::getConfig('appId')) { $init = '<div id="fb-root"></div>'; $init .= $this->Html->scriptBlock("\n\twindow.fbAsyncInit = function() {\n\t\tFB.init({\n\t\t\tappId : '{$appId}', // App ID\n\t\t\tchannelURL : '../../Vendor/channel.php', // Channel File\n\t\t\tstatus : true, // check login status\n\t\t\tcookie : true, // enable cookies to allow the server to access the session\n\t\t\toauth : true, // enable OAuth 2.0\n\t\t\txfbml : true // parse XFBML\n\t\t});\n\n\n\t\t// Checks whether the user is logged in\n\t\tFB.getLoginStatus(function(response) {\n\t\t\tif (response.authResponse) {\n\t\t\t\t// logged in and connected user, someone you know\n\t\t\t\t// alert('You are connected');\n\t\t\t} else {\n\t\t\t\t// no user session available, someone you dont know\n\t\t\t\t// alert('You are disconnected');\n\t\t\t}\n\t\t});\n\n\t\tFB.Event.subscribe('auth.authResponseChange', function(response) {\n\t\t\tif (response.authResponse) {\n\t\t\t\t// the user has just logged in\n\t\t\t\t// alert('You just logged in facebook from somewhere');\n\t\t\t} else {\n\t\t\t\t// the user has just logged out\n\t\t\t\t// alert('You just logged out from faceboook');\n\t\t\t}\n\t\t});\n\n\t\t// Other javascript code goes here!\n\n\t};\n\n\t// logs the user in the application and facebook\n\tfunction login(redirection){\n\t\tFB.login(function (response) {\n\t\t\tif(response.authResponse) {\n\t\t\t\t// user is logged in\n\t\t\t\t// console.log('Welcome!');\n\t\t\t\tif(redirection != null && redirection != ''){\n\t\t\t\t\ttop.location.href = redirection;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// user could not log in\n\t\t\t\tconsole.log('User cancelled login or did not fully authorize.');\n\t\t\t}\n\t\t}, {scope: '" . $options['perms'] . "'});\n\t}\n\n\t// logs the user out of the application and facebook\n\tfunction logout(redirection){\n\t\tif (FB.getAuthResponse()) {\n\t\t\tFB.logout(function(response) {\n\t // user is logged out\n\t // redirection if any\n\t if(redirection != null && redirection != ''){\n\t top.location.href = redirection;\n\t }\n\t });} else {\n\t\t if(redirection != null && redirection != ''){\n \ttop.location.href = redirection;\n \t }\n\t };\n\t}\n\n\t// Load the SDK Asynchronously\n\t(function() {\n\tvar e = document.createElement('script'); e.async = true;\n\te.src = document.location.protocol\n\t+ '//connect.facebook.net/" . $this->locale . "/all.js';\n\tdocument.getElementById('fb-root').appendChild(e);\n\t}());"); return $init; } else { return "<span class='error'>No Facebook configuration detected. Please add the facebook configuration file to your config folder.</span>"; } }
function endTest() { unset($this->Connect); FacebookInfo::$configs = null; Configure::delete('Facebook'); }
/** * HTML XMLNS tag (required) * @param array $options * @example $this->Facebook->init(); * @return string of scriptBlock for FB.init() or error */ function init($options = null, $reload = true) { if (empty($options)) { $options = array(); } if ($appId = FacebookInfo::getConfig('appId')) { $session = json_encode($this->Session->read('FB.Session')); if ($reload) { $callback = "FB.Event.subscribe('auth.login',function(){window.location.reload()});"; } else { $callback = "if(typeof(facebookReady)=='function'){facebookReady()}"; } $init = '<div id="fb-root"></div>'; $init .= $this->Html->scriptBlock(<<<JS window.fbAsyncInit = function() { \tFB.init({ \t\tappId : '{$appId}', \t\tstatus : true, // check login status \t\tcookie : true, // enable cookies to allow the server to access the session \t\txfbml : true, // parse XFBML \t\toauth : true // use Oauth \t}); \t{$callback} }; (function() { \tvar e = document.createElement('script'); \te.src = document.location.protocol + '//connect.facebook.net/{$this->locale}/all.js'; \te.async = true; \tdocument.getElementById('fb-root').appendChild(e); }()); JS , $options); return $init; } else { return "<span class='error'>No Facebook configuration detected. Please add the facebook configuration file to your config folder.</span>"; } }
/** * HTML XMLNS tag (required) * @param array of options * @example $this->Facebook->init(); * @return string of scriptBlock for FB.init() or error * @access public */ function init($options = array()) { if (FacebookInfo::getConfig('appId')) { $appId = FacebookInfo::getConfig('appId'); $session = json_encode($this->Session->read('FB.Session')); $init = '<div id="fb-root"></div>'; $init .= $this->Html->scriptBlock("\n\t\t window.fbAsyncInit = function() {\n\t\t FB.init({\n\t\t appId : '{$appId}',\n\t\t session : {$session}, // don't refetch the session when PHP already has it\n\t\t status : true, // check login status\n\t\t cookie : true, // enable cookies to allow the server to access the session\n\t\t xfbml : true // parse XFBML\n\t\t });\n\t\t // whenever the user logs in, we refresh the page\n\t\t FB.Event.subscribe('auth.login', function() {\n\t\t window.location.reload();\n\t\t });\n\t\t };\n\t\t (function() {\n\t\t var e = document.createElement('script');\n\t\t e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';\n\t\t e.async = true;\n\t\t document.getElementById('fb-root').appendChild(e);\n\t\t }());\n \t", $options); return $init; } else { return "<span class='error'>No Facebook configuration detected. Please add the facebook configuration file to your config folder.</span>"; } }
/** * Initialize a new instance if none exists */ private function __initInstance() { if (empty(self::$Facebook)) { self::$Facebook = new Facebook(FacebookInfo::getConfig()); } }
public function __construct() { if (empty(self::$Facebook)) { self::$Facebook = new Facebook(FacebookInfo::getConfig()); } }
/** * Sync the connected Facebook user. * * If User is logged in: * a. but doesn't have a facebook account associated, try to associate it. * * If User is not logged in: * b. but have a facebook account associated, try to log the user in. * c. and doesn't have a facebook account associated, * 1. try to automatically create an account and associate it (if $this->createUser). * 2. try to log the user in, afterwards. * * @return boolean True if successful, false otherwise. */ private function __syncFacebookUser() { if (!isset($this->Controller->Auth)) { return false; } // set Auth to a convenience publiciable $Auth = $this->Controller->Auth; if (!$this->__initUserModel()) { return false; } // if you don't have a facebook_id field in your user table, throw an error if (!$this->User->hasField('facebook_id')) { $this->__error("Facebook.Connect handleFacebookUser Error. facebook_id not found in {$Auth->userModel} table."); return false; } // check if the user already has an account // User is logged in but doesn't have a if ($Auth->user('id')) { $this->hasAccount = true; $this->User->id = $Auth->user($this->User->primaryKey); if (!$this->User->field('facebook_id')) { $this->User->saveField('facebook_id', $this->uid); } return true; } else { // attempt to find the user by their facebook id $this->authUser = $this->User->findByFacebookId($this->uid); //if we have a user, set hasAccount if (!empty($this->authUser)) { $this->hasAccount = true; } elseif (empty($this->authUser) && $this->createUser) { $this->authUser[$this->User->alias]['facebook_id'] = $this->uid; $this->authUser[$this->User->alias][$this->modelFields['password']] = $Auth->password(FacebookInfo::randPass()); if ($this->__runCallback('beforeFacebookSave')) { $this->hasAccount = $this->User->save($this->authUser, array('validate' => false)); } else { $this->authUser = null; } } //Login user if we have one if ($this->authUser) { $this->__runCallback('beforeFacebookLogin', $this->authUser); $Auth->authenticate = array('Form' => array('fields' => array('username' => 'facebook_id', 'password' => $this->modelFields['password']))); if ($Auth->login($this->authUser[$this->model])) { $this->__runCallback('afterFacebookLogin'); } } return true; } }
/** * Get registration Data * @return associative array of registration data (if there is any) */ function registrationData() { if (isset($this->Controller->request->data['signed_request'])) { return FacebookInfo::parseSignedRequest($this->Controller->request->data['signed_request']); } return array(); }