/**
  * Perform a query, return the first column of the first row
  * 
  * @param string sql
  * @param vararg arguments (for sprintf)
  * @return mixed
  */
 protected function query_one($sql)
 {
     list($sql, $args) = $this->sql_args(func_get_args());
     return any_db_query_one($sql, $args);
 }
Exemple #2
0
    /**
     * Exchange an authorized request token for new access token.
     * 
     * @param string token
     * @param array options		options for the token, token_ttl
     * @exception OAuthException when token could not be exchanged
     * @return array (token, token_secret)
     */
    public function exchangeConsumerRequestForAccessToken($token, $options = array())
    {
        $new_token = $this->generateKey(true);
        $new_secret = $this->generateKey();
        // Maximum time to live for this token
        if (isset($options['token_ttl']) && is_numeric($options['token_ttl'])) {
            $ttl_sql = 'DATE_ADD(NOW(), INTERVAL ' . intval($options['token_ttl']) . ' SECOND)';
        } else {
            $ttl_sql = "'9999-12-31'";
        }
        $this->query('
					UPDATE oauth_server_token
					SET ost_token			= \'%s\',
						ost_token_secret	= \'%s\',
						ost_token_type		= \'access\',
						ost_timestamp		= NOW(),
						ost_token_ttl       = ' . $ttl_sql . '
					WHERE ost_token      = \'%s\'
					  AND ost_token_type = \'request\'
					  AND ost_authorized = 1
					  AND ost_token_ttl  >= NOW()
					', $new_token, $new_secret, $token);
        if ($this->query_affected_rows() != 1) {
            throw new OAuthException('Can\'t exchange request token "' . $token . '" for access token. No such token or not authorized');
        }
        $ret = array('token' => $new_token, 'token_secret' => $new_secret);
        $ttl = any_db_query_one('
					SELECT	IF(ost_token_ttl >= \'9999-12-31\', NULL, UNIX_TIMESTAMP(ost_token_ttl) - UNIX_TIMESTAMP(NOW())) as token_ttl
					FROM oauth_server_token
					WHERE ost_token = \'%s\'', $new_token);
        if (is_numeric($ttl)) {
            $ret['token_ttl'] = intval($ttl);
        }
        return $ret;
    }