Ejemplo n.º 1
0
 /**
  * Signs up the user
  *
  * @param string $password
  * @param string $registration_id
  * @return bool
  */
 public function signUp($registration_id = false, $password = false)
 {
     global $f3, $db;
     if ($this->email && $password) {
         $sql_params = array();
         $sql = "INSERT INTO user ";
         $sql .= "(email, registration_id, handle, password, date_created) ";
         $sql .= "VALUES (?, ?, ?, ?, NOW());";
         $query = $db->prepare($sql);
         $sql_params = array($this->email, $registration_id, $this->generateHandle(), $this->passwordHash($password));
         $query->execute($sql_params);
         return $db->lastInsertId();
     } else {
         if ($this->facebook_id) {
             $session = new FacebookSession(getFacebookAccessToken());
             try {
                 // Get information about fb user
                 $me = (new FacebookRequest($session, 'GET', '/me?fields=about,email,first_name,last_name,picture.height(200)'))->execute()->getGraphObject(GraphUser::className())->asArray();
                 // Setup the object with obtained info
                 $this->registration_id = $registration_id;
                 $this->first_name = $me['first_name'];
                 $this->last_name = $me['last_name'];
                 if (@$me['email']) {
                     $this->email = $me['email'];
                 } else {
                     $this->email = '';
                 }
                 if (@$me['picture']) {
                     $this->image = $me['picture']->data->url;
                 }
                 $sql_params = array();
                 $sql = "INSERT INTO user ";
                 $sql .= "(facebook_id, email, registration_id, first_name, last_name, image, handle, date_created) ";
                 $sql .= "VALUES (?, ?, ?, ?, ?, ?, ?, NOW());";
                 $query = $db->prepare($sql);
                 $sql_params = array($this->facebook_id, $this->email, $this->registration_id, $this->first_name, $this->last_name, $this->image, $this->generateHandle());
                 $query->execute($sql_params);
                 $this->id = $db->lastInsertId();
                 // Get fb user friends
                 $friends = (new FacebookRequest($session, 'GET', '/me/friends'))->execute()->getGraphObject(GraphUser::className())->asArray();
                 // Create records of friendship in our own database
                 if (sizeof($friends['data'])) {
                     foreach ($friends['data'] as $friend) {
                         $friend_id = User::getUserIdByFacebookId($friend->id);
                         if ($friend_request_id = $this->addFriend($friend_id)) {
                             $tmp_user = new User(null, $friend_id);
                             $tmp_user->acceptFriend($friend_request_id);
                         }
                         unset($friend, $tmp_user, $friend_id, $friend_request_id);
                     }
                 }
                 return $this->id;
             } catch (FacebookRequestException $e) {
                 echo $e->getMessage();
                 // The Graph API returned an error
             } catch (\Exception $e) {
                 echo $e->getMessage();
                 // Some other error occurred
             }
         } else {
             throw new Exception('No password/email was provided/in object.');
         }
     }
 }
Ejemplo n.º 2
0
        $response = (object) array('status' => -3, 'status_explanation' => 'Invalid token.');
    }
    header('Content-Type: application/json');
    echo json_encode($response);
}, $f3->get('route_ttl'));
/**
 * Route: Accept Friend Request
 *
 * @example /user/accept-friend
 */
$f3->route(array('POST /user/accept-friend'), function ($f3, $params) use($db) {
    // Attempt to sign in
    if ($sender_id = authenticated()) {
        $fs_id = $f3->get('POST.friendship_id');
        $user = new User(null, $sender_id);
        if ($user->acceptFriend($fs_id)) {
            $response = (object) array('status' => 1, 'status_explanation' => 'Success.', 'user_info' => $user->getInfo(), 'user_friends' => $user->getFriends());
        } else {
            $response = (object) array('status' => -1, 'status_explanation' => 'Could not accept friend request for unknown reason.');
        }
    } else {
        $response = (object) array('status' => -3, 'status_explanation' => 'Invalid token.');
    }
    header('Content-Type: application/json');
    echo json_encode($response);
}, $f3->get('route_ttl'));
/**
 * Route: Ignore Friend Request
 *
 * @example /user/ignore-friend
 */