Example #1
0
function Streams_invite_response_data()
{
    if (isset(Streams::$cache['invited'])) {
        return Streams::$cache['invited'];
    }
    $user = Users::loggedInUser(true);
    $publisherId = Streams::requestedPublisherId();
    $streamType = Streams::requestedType();
    $invitingUserId = Streams::requestedField('invitingUserId');
    $limit = Q::ifset($_REQUEST, 'limit', Q_Config::get('Streams', 'invites', 'limit', 100));
    $invited = Streams_Invited::select('*')->where(array('userId' => $user->id, 'state' => 'pending', 'expireTime <' => new Db_Expression('CURRENT_TIMESTAMP')))->limit($limit)->fetchDbRows(null, null, 'token');
    $query = Streams_Invite::select('*')->where(array('token' => array_keys($invited)));
    if (isset($publisherId)) {
        $query = $query->where(array('publisherId' => $publisherId));
    }
    if (isset($streamType)) {
        $query = $query->where(array('streamName' => new Db_Range($streamType . '/', true, false, true)));
    }
    if (isset($invitingUserId)) {
        $query = $query->where(array('invitingUserId' => $invitingUserId));
    }
    $invites = $query->fetchDbRows();
    $streams = array();
    foreach ($invites as $invite) {
        $stream = new Streams_Stream();
        $stream->publisherId = $invite->publisherId;
        $stream->name = $invite->streamName;
        if ($stream->retrieve()) {
            $streams[$invite->token] = $stream->exportArray();
            $streams[$invite->token]['displayName'] = $invite->displayName;
        }
    }
    return compact('streams', 'invites');
}
Example #2
0
function Streams_invite_response_accountStatus()
{
    if (empty($_REQUEST['token'])) {
        throw new Q_Exception("Missing token!");
    }
    $invite = new Streams_Invite();
    $invite->token = $_REQUEST['token'];
    if (!$invite->retrieve()) {
        throw new Q_Exception("Wrong token '" . $invite->token . "'!");
    }
    $user = new Users_User();
    $user->id = $invite->userId;
    if (!$user->retrieve()) {
        throw new Users_Exception_NoSuchUser();
    }
    if (!empty($user->passphraseHash) || !empty($user->fb_uid)) {
        return "complete";
    }
}
Example #3
0
function Streams_invited_response()
{
    if (!($token = Q_Dispatcher::uri()->token)) {
        throw new Q_Exception_RequiredField(array('field' => 'token'), 'token');
    }
    if (!($invite = Streams_Invite::fromToken($token))) {
        throw new Q_Exception_MissingRow(array('table' => 'invite', 'criteria' => "token: {$token}"), 'token');
    }
    Users_User::fetch($invite->userId, true)->setVerified();
    Q_Response::redirect($invite->appUrl . "?" . http_build_query(array('Q.Streams.token' => $token), null, '&'));
}
Example #4
0
function Streams_before_Q_objects()
{
    $token = Q_Request::special('Streams.token', null);
    if ($token === null) {
        return;
    }
    $invite = Streams_Invite::fromToken($token);
    if (!$invite) {
        throw new Q_Exception_MissingRow(array('table' => 'invite', 'criteria' => "token = '{$token}"), 'token');
    }
    // did invite expire?
    $ts = Streams_Invite::db()->select("CURRENT_TIMESTAMP")->fetchAll(PDO::FETCH_NUM);
    if (isset($invite->expireTime) and $invite->expireTime < $ts[0][0]) {
        $invite->state = 'expired';
        $invite->save();
    }
    // is invite still pending?
    if ($invite->state !== 'pending') {
        switch ($invite->state) {
            case 'expired':
                $exception = new Streams_Exception_AlreadyExpired(null, 'token');
                break;
            case 'accepted':
                $exception = new Streams_Exception_AlreadyAccepted(null, 'token');
                break;
            case 'declined':
                $exception = new Streams_Exception_AlreadyDeclined(null, 'token');
                break;
            case 'forwarded':
                $exception = new Streams_Exception_AlreadyForwarded(null, 'token');
                break;
            default:
                $exception = new Q_Exception("This invite has already been " . $invite->state, 'token');
                break;
        }
        $shouldThrow = Q::event('Streams/objects/inviteException', compact('invite', 'exception'), 'before');
        if ($shouldThrow === null) {
            Q_Response::setNotice('Streams/objects', $exception->getMessage(), true);
        } else {
            if ($shouldThrow === true) {
                throw $exception;
            }
        }
    }
    // now process the invite
    $invitedUser = Users_User::fetch($invite->userId, true);
    $stream = Streams::fetchOne($invitedUser->id, $invite->publisherId, $invite->streamName);
    if (!$stream) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => "publisherId = '{$invite->publisherId}', name = '{$invite->streamName}'"));
    }
    $byUser = Users_User::fetch($invite->invitingUserId, true);
    $byStream = Streams::fetchOne($byUser->id, $invite->publisherId, $invite->streamName);
    if (!$byStream) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => "publisherId = '{$invite->publisherId}', name = '{$invite->streamName}'"));
    }
    $access = new Streams_Access();
    $access->publisherId = $byStream->publisherId;
    $access->streamName = $byStream->name;
    $access->ofUserId = $invite->userId;
    $specified_access = false;
    foreach (array('readLevel', 'writeLevel', 'adminLevel') as $level_type) {
        $access->{$level_type} = -1;
        if (empty($invite->{$level_type})) {
            continue;
        }
        // Give access level from the invite.
        // However, if inviting user has a lower access level now,
        // then give that level instead, unless it is lower than
        // what the invited user would have had otherwise.
        $min = min($invite->{$level_type}, $byStream->get($level_type, 0));
        if ($min > $stream->get($level_type, 0)) {
            $access->{$level_type} = $min;
            $specified_access = true;
        }
    }
    if ($specified_access) {
        $access->save(true);
    }
    // now log invited user in
    $user = Users::loggedInUser();
    if (empty($user) or $user->id !== $invite->userId) {
        $user = new Users_User();
        $user->id = $invite->userId;
        if (!$user->retrieve()) {
            // The user who was invited doesn't exist
            // This shouldn't happen. We just silently log it and return.
            Q::log("Sanity check failed: invite with {$invite->token} pointed to nonexistent user");
            return;
        }
        Users::setLoggedInUser($user);
    }
    // accept invite and autosubscribe if first time
    if ($invite->accept() and !$stream->subscription($user->id)) {
        $stream->subscribe();
    }
    // retain the invite object for further processing
    Streams::$followedInvite = $invite;
}
Example #5
0
 /**
  * Retrieves invite
  * @method getInvite
  * @static
  * @param {string} $token
  * @return {Streams_Invite|null}
  */
 static function fromToken($token)
 {
     if (empty($token)) {
         return null;
     }
     if (!empty(self::$cache['getInvite'][$token])) {
         return self::$cache['getInvite'][$token];
     }
     $invite = new Streams_Invite();
     $invite->token = $token;
     if (!$invite->retrieve()) {
         return null;
     }
     self::$cache['getInvite'][$token] = $invite;
     return $invite;
 }