コード例 #1
0
ファイル: Facebook.php プロジェクト: tmaiaroto/li3_facebook
 /**
  * Called by the `Auth` class to run an authentication check against the Facebook API
  * and returns an array of user information on success, or `false` on failure.
  * 
  * @todo move the FacebookConfig::checkConfiguration part into the __init?
  * 
  * @throws lithium\core\ConfigException if the facebook App credentials arent set
  *
  * @param object $credentials A data container which wraps the authentication credentials used
  *               to query the model (usually a `Request` object). See the documentation for this
  *               class for further details.
  * @param array $options Options which include the options for session key names and also FB API method options.
  * @return array Returns an array containing user information on success, or `false` on failure.
  */
 public function check($credentials, array $options = array())
 {
     FacebookProxy::checkConfiguration();
     //get Url
     $base = $credentials->env('HTTPS') ? 'https://' : 'http://';
     $base .= $credentials->env('HTTP_HOST');
     $base .= $credentials->env('base');
     $facebook_config = Libraries::get('li3_facebook');
     // get the options from the li3_facebook library configuration if set there
     $options += $facebook_config;
     // otherwise, set some defaults
     $defaults = array('logout_url_options' => array('next' => $base), 'login_url_options' => array(), 'logout_url_session_key' => 'fb_logout_url', 'login_url_session_key' => 'fb_login_url', 'local_fb_session_name' => 'fb_session');
     /**
      * If the adapter config() has those keys set, then use those as the default values.
      * This allows various adapters to be created all which can change the options for logging in and out
      * for Facebook, so when Auth::check() is called, each check can be used for different reasons.
      * If the options are set with the Facebook library ($facebook_config) then there can only be one
      * "configuration" for these login and logout parameters.
      *
      * So for example, Auth::check('popup', $this->request); or Auth::check('page', $this->request);
      * The difference maybe between the two Auth configurations is the "login_url_options" array values
      * of "display" being "page" or "popup" which tells the FB API how to display the login.
      *
      * We could also pass these options in the configuration under Libraries::add('li3_facebook'), but then
      * it wouldn't be quite as easy to switch behaviors while using Auth::check();
      */
     $defaults['logout_url_options'] = isset($this->_config['logout_url_options']) ? $this->_config['logout_url_options'] : $defaults['logout_url_options'];
     $defaults['login_url_options'] = isset($this->_config['login_url_options']) ? $this->_config['login_url_options'] : $defaults['login_url_options'];
     $defaults['logout_url_session_key'] = isset($this->_config['logout_url_session_key']) ? $this->_config['logout_url_session_key'] : $defaults['logout_url_session_key'];
     $defaults['login_url_session_key'] = isset($this->_config['login_url_session_key']) ? $this->_config['login_url_session_key'] : $defaults['login_url_session_key'];
     $defaults['local_fb_session_name'] = isset($this->_config['local_fb_session_name']) ? $this->_config['local_fb_session_name'] : $defaults['local_fb_session_name'];
     // combine the defults with the options passed, giving those passed options the priority
     $options += $defaults;
     $user_data = false;
     $session = FacebookProxy::getSession();
     $uid = null;
     // Session based API call.
     if ($session) {
         // Set the session locally
         Session::write($options['local_fb_session_name'], $session);
         try {
             $uid = FacebookProxy::getUser();
         } catch (Exception $e) {
             //error_log($e);
         }
     }
     // If $uid is set, then write the fb_logout_url session key
     if (!empty($uid)) {
         if ($options['logout_url_session_key']) {
             Session::write($options['logout_url_session_key'], FacebookProxy::getLogoutUrl($options['logout_url_options']));
         }
         // Get the user data to return
         $user_data = array();
         try {
             $user_data = FacebookProxy::api('/me');
         } catch (Exception $e) {
             //error_log($e);
         }
     } else {
         // Else, the user hasn't logged in yet, write the fb_login_url session key
         if ($options['login_url_session_key']) {
             Session::write($options['login_url_session_key'], FacebookProxy::getLoginUrl($options['login_url_options']));
         }
     }
     return $user_data;
 }