Beispiel #1
0
 /**
  * Store the user's profile data in the database, if it doesn't exist yet.
  * @param Array $facebookData The profile data received from Facebook
  * @return Void
  */
 protected function _getUserData(array $facebookData)
 {
     $uid = $facebookData['id'];
     $ini = Zend_Registry::get('config');
     $sessionColumns = Zend_Db_Select::SQL_WILDCARD;
     if (!empty($ini->auth->login->sessionColumns)) {
         $sessionColumns = $ini->auth->login->sessionColumns;
         $sessionColumns = explode(',', $sessionColumns);
     }
     $userModel = new Model_User();
     $userConditions = $userModel->select()->from($userModel->getName(), $sessionColumns);
     $model = new Model_AuthFacebook();
     $model->bindModel('Model_User', array('conditions' => $userConditions, 'rule' => 'User'));
     $userData = $model->fetchRow($model->select()->where('facebook_uid = ?', $uid));
     if (!$userData || !$userData->Model_User) {
         $userData = $model->createNew(array('facebook_uid' => $uid, 'access_token' => $facebookData['access_token']), $this->_mapProperties($facebookData));
     } else {
         $model->getObserver('Authenticatable')->updateLoginStats($userData->user_id, array('access_token' => $facebookData['access_token']));
         $userData = $userData->Model_User;
     }
     return $userData;
 }
Beispiel #2
0
 /**
  * Find friends of logged in user and map to local friends table.
  * @param Array $config
  * @return Bool Success
  */
 public function mapFriends(array $config)
 {
     $config = $config instanceof Garp_Util_Configuration ? $config : new Garp_Util_Configuration($config);
     $config->obligate('bindingModel')->obligate('user_id')->setDefault('accessToken', $this->getAccessToken());
     if (!$config['accessToken']) {
         // Find the auth record
         $authModel = new Model_AuthFacebook();
         $authRow = $authModel->fetchRow($authModel->select()->where('user_id = ?', $config['user_id']));
         if (!$authRow || !$authRow->access_token) {
             return false;
         }
         // Use the stored access token to create a user session. Me() in the FQL ahead will contain the user's Facebook ID.
         // Note that the access token is available for a very limited time. Chances are it's not valid anymore.
         $accessToken = $authRow->access_token;
     }
     try {
         $this->_client->setAccessToken($config['accessToken']);
         // Find the friends' Facebook UIDs
         $friends = $this->_client->api(array('method' => 'fql.query', 'query' => 'SELECT uid2 FROM friend WHERE uid1 = me()'));
         // Find local user records
         $userModel = new Model_User();
         $userTable = $userModel->getName();
         $authFbModel = new Model_AuthFacebook();
         $authFbTable = $authFbModel->getName();
         $fbIds = '';
         $friendCount = count($friends);
         foreach ($friends as $i => $friend) {
             $fbIds .= $userModel->getAdapter()->quote($friend['uid2']);
             if ($i < $friendCount - 1) {
                 $fbIds .= ',';
             }
         }
         $friendQuery = $userModel->select()->setIntegrityCheck(false)->from($userTable, array('id'))->join($authFbTable, $authFbTable . '.user_id = ' . $userTable . '.id', array())->where('facebook_uid IN (' . $fbIds . ')')->order($userTable . '.id');
         $localUsers = $userModel->fetchAll($friendQuery);
         $localUserCount = count($localUsers);
         // Insert new friendships into binding model
         $bindingModel = new $config['bindingModel']();
         $insertSql = 'INSERT IGNORE INTO ' . $bindingModel->getName() . ' (user1_id, user2_id) VALUES ';
         foreach ($localUsers as $i => $localUser) {
             $insertSql .= '(' . $localUser->id . ',' . $config['user_id'] . '),';
             $insertSql .= '(' . $config['user_id'] . ',' . $localUser->id . ')';
             if ($i < $localUserCount - 1) {
                 $insertSql .= ',';
             }
         }
         $result = $bindingModel->getAdapter()->query($insertSql);
         // Clear cache manually, since the table isn't updated thru conventional paths.
         Garp_Cache_Manager::purge($bindingModel);
         return !!$result;
     } catch (Exception $e) {
         return false;
     }
 }