Exemplo n.º 1
0
function oauth_process($url, $post_data = false)
{
    if ($post_data === true) {
        $post_data = array();
    }
    user_oauth_sign($url, $post_data);
    $ch = curl_init($url);
    if ($post_data !== false) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_USERAGENT, 'zlbnc');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    $response_info = curl_getinfo($ch);
    curl_close($ch);
    if (intval($response_info['http_code']) == 200) {
        $json = json_decode($response);
        if ($json) {
            return $json;
        }
        return $response;
    } else {
        delCookie('bOauth');
        header('location: index.php?ln=error');
    }
}
Exemplo n.º 2
0
function twitter_process($url, $post_data = false)
{
    if ($post_data === true) {
        $post_data = array();
    }
    if (user_type() == 'oauth' && (strpos($url, '/twitter.com') !== false || strpos($url, 'api.twitter.com') !== false)) {
        user_oauth_sign($url, $post_data);
    } elseif (strpos($url, 'api.twitter.com') !== false && is_array($post_data)) {
        // Passing $post_data as an array to twitter.com (non-oauth) causes an error :(
        $s = array();
        foreach ($post_data as $name => $value) {
            $s[] = $name . '=' . urlencode($value);
        }
        $post_data = implode('&', $s);
    }
    $api_start = microtime(1);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($post_data !== false && !$_GET['page']) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    if (user_type() != 'oauth' && user_is_authenticated()) {
        curl_setopt($ch, CURLOPT_USERPWD, user_current_username() . ':' . $GLOBALS['user']['password']);
    }
    //from  http://github.com/abraham/twitteroauth/blob/master/twitteroauth/twitteroauth.php
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    $response = curl_exec($ch);
    $response_info = curl_getinfo($ch);
    $erno = curl_errno($ch);
    $er = curl_error($ch);
    curl_close($ch);
    global $api_time;
    $api_time += microtime(1) - $api_start;
    switch (intval($response_info['http_code'])) {
        case 200:
        case 201:
            $json = json_decode($response);
            if ($json) {
                return $json;
            }
            return $response;
        case 401:
            user_logout();
            theme('error', "<p>Error: Login credentials incorrect.</p><p>{$response_info['http_code']}: {$result}</p><hr><p>{$url}</p>");
        case 0:
            $result = $erno . ":" . $er . "<br />";
            /*
            			 foreach ($response_info as $key => $value)
            			 {
            $result .= "Key: $key; Value: $value<br />";
            }
            */
            theme('error', '<h2>Twitter timed out</h2><p>Dabr gave up on waiting for Twitter to respond. They\'re probably overloaded right now, try again in a minute. <br />' . $result . ' </p>');
        default:
            $result = json_decode($response);
            $result = $result->error ? $result->error : $response;
            if (strlen($result) > 500) {
                $result = 'Something broke on Twitter\'s end.';
                /*
                				 $result .= $erno . ":" . $er . "<br />" ;
                				 foreach ($response_info as $key => $value)
                				 {
                $result .= "Key: $key; Value: $value<br />";
                }
                */
            }
            theme('error', "<h2>An error occured while calling the Twitter API</h2><p>{$response_info['http_code']}: {$result}</p><hr><p>{$url}</p>");
    }
}
Exemplo n.º 3
0
function twitter_process($url, $post_data = false, $original = false)
{
    if ($post_data === true) {
        $post_data = array();
    }
    if (user_type() == 'oauth') {
        user_oauth_sign($url, $post_data);
    } elseif (is_array($post_data)) {
        $s = array();
        foreach ($post_data as $name => $value) {
            $s[] = $name . '=' . urlencode($value);
        }
        $post_data = implode('&', $s);
    }
    $ch = curl_init($url);
    if ($post_data !== false && !$_GET['page']) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    if (user_type() != 'oauth' && user_is_authenticated()) {
        curl_setopt($ch, CURLOPT_USERPWD, user_current_username() . ':' . $GLOBALS['user']['password']);
    }
    curl_setopt($ch, CURLOPT_USERAGENT, 'dabr');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    //15
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    //30
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    $response = curl_exec($ch);
    $response_info = curl_getinfo($ch);
    $erno = curl_errno($ch);
    $er = curl_error($ch);
    curl_close($ch);
    if ($original) {
        return json_decode($response);
    }
    switch (intval($response_info['http_code'])) {
        case 200:
        case 201:
            $json = json_decode($response);
            if ($json) {
                return $json;
            }
            return $response;
        case 401:
            user_logout();
            theme('error', "<p>" . __("Error: Login credentials incorrect.") . "</p><p>{$response_info['http_code']}: {$result}</p><hr><p>{$url}</p>");
        case 0:
            $result = $erno . ":" . $er . "<br />";
            theme("error", "<h3>" . __("Twitter timed out") . "</h3><p>" . __("Dabr gave up on waiting for Twitter to respond. They're probably overloaded right now, try again in a minute.") . "<br />{$result}</p>");
        default:
            $origin_result = json_decode($response);
            $result = $origin_result->error ? $origin_result->error : $response;
            if (strlen($result) > 500) {
                $result = __("Something broke on Twitter's end.");
            }
            theme('error', "<h3>" . __("An error occured while calling the Twitter API") . "</h3><p>{$response_info['http_code']}: {$result}</p><hr /><p>{$url}</p>");
    }
}
Exemplo n.º 4
0
function twitter_process($url, $post_data = false)
{
    if ($post_data === true) {
        $post_data = array();
    }
    $status = $post_data['status'];
    user_oauth_sign($url, $post_data);
    $api_start = microtime(1);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($post_data !== false && !$_GET['page']) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    //from  http://github.com/abraham/twitteroauth/blob/master/twitteroauth/twitteroauth.php
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $response = curl_exec($ch);
    $response_info = curl_getinfo($ch);
    $erno = curl_errno($ch);
    $er = curl_error($ch);
    curl_close($ch);
    global $api_time;
    global $rate_limit;
    //	Split that headers and the body
    list($headers, $body) = explode("\r\n\r\n", $response, 2);
    //	Place the headers into an array
    $headers = explode("\n", $headers);
    foreach ($headers as $header) {
        list($key, $value) = explode(':', $header, 2);
        $headers_array[$key] = $value;
    }
    //	Not every request is rate limited
    if ($headers_array['x-rate-limit-limit']) {
        $current_time = time();
        $ratelimit_time = $headers_array['x-rate-limit-reset'];
        $time_until_reset = $ratelimit_time - $current_time;
        $minutes_until_reset = round($time_until_reset / 60);
        $rate_limit .= " Rate Limit: " . $headers_array['x-rate-limit-remaining'] . " out of " . $headers_array['x-rate-limit-limit'] . " calls remaining for the next {$minutes_until_reset} minutes";
    }
    $api_time += microtime(1) - $api_start;
    switch (intval($response_info['http_code'])) {
        case 200:
        case 201:
            $json = json_decode($body);
            if ($json) {
                return $json;
            }
            return $body;
        case 401:
            user_logout();
            theme('error', "<p>Error: Login credentials incorrect.</p><p>{$response_info['http_code']}: {$result}</p><hr><p>{$url}</p>");
        case 429:
            theme('error', "<h2>Rate limit exceeded!</h2><p>All {$headers_array['x-rate-limit-limit']} calls used, next reset in {$minutes_until_reset} minutes.</p>");
        case 0:
            $result = $erno . ":" . $er . "<br />";
            /*
            foreach ($response_info as $key => $value) {
            	$result .= "Key: $key; Value: $value<br />";
            }
            */
            theme('error', "<h2>Twitter timed out</h2><p>Dabr gave up on waiting for Twitter to respond. They're probably overloaded right now, try again in a minute. <br />{$result}</p>");
        default:
            $result = json_decode($body);
            $result = $result->error ? $result->error : $body;
            if (strlen($result) > 500) {
                $result = "Something broke on Twitter's end.";
                /*
                foreach ($response_info as $key => $value) {
                	$result .= "Key: $key; Value: $value<br />";
                }
                */
            } else {
                if ($result == "Status is over 140 characters.") {
                    theme('error', "<h2>Status was tooooooo loooooong!</h2><p>{$status}</p><hr>");
                    //theme('status_form',$status);
                }
            }
            if (DEBUG_MODE == 'ON') {
                theme('error', "<h2>An error occured while calling the Twitter API</h2><p>{$response_info['http_code']}: {$result}<br />{$url}</p><hr>");
            } else {
                theme('error', "<h2>An error occured while calling the Twitter API</h2><p>{$response_info['http_code']}: {$result}</p><hr>");
            }
    }
}
Exemplo n.º 5
0
 function process($url, $postargs = false, $method = false)
 {
     if ($this->debug) {
         echo $url . '<br/>';
         print_r($postargs);
     }
     $ch = curl_init($url);
     if ($this->isOAuth) {
         user_oauth_sign($url, $postargs);
     }
     if ($postargs !== false) {
         curl_setopt($ch, CURLOPT_POST, true);
         if (is_array($postargs)) {
             $postargs = $this->_glue($postargs);
             $postargs = substr($postargs, 1);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $postargs);
         } else {
             curl_setopt($ch, CURLOPT_POSTFIELDS, '');
         }
         if ($method === "DELETE") {
             curl_setopt($ch, CURLOPT_POSTFIELDS, $postargs . "&_method=DELETE");
         }
     }
     if (!$this->isOAuth && $this->username !== false && $this->password !== false) {
         curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
     }
     curl_setopt($ch, CURLOPT_VERBOSE, 0);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_TIMEOUT, 10);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
     $response = curl_exec($ch);
     $responseInfo = curl_getinfo($ch);
     curl_close($ch);
     $http_code = intval($responseInfo['http_code']);
     if ($this->debug) {
         echo 'respones:<br/>';
         print_r($response);
         echo '<br/>httpcode:';
         print $http_code;
         echo '<br/>';
     }
     if ($http_code != 0) {
         //0无法连接 403超过limit
         return $response;
     } else {
         return false;
     }
 }
Exemplo n.º 6
0
function twitter_process($url, $post_data = false)
{
    if ($post_data === true) {
        $post_data = array();
    }
    $status = $post_data['status'];
    //if (user_type() == 'oauth' && ( strpos($url, '/twitter.com') !== false || strpos($url, 'api.twitter.com') !== false || strpos($url, 'upload.twitter.com') !== false))
    //{
    user_oauth_sign($url, $post_data);
    //}
    /*
        if (strpos($url, 'api.twitter.com') !== false && is_array($post_data)) 
        {
            // Passing $post_data as an array to twitter.com (non-oauth) causes an error :(
            $s = array();
            foreach ($post_data as $name => $value)
                $s[] = $name.'='.urlencode($value);
            $post_data = implode('&', $s);
        }
    */
    $api_start = microtime(1);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($post_data !== false && !$_GET['page']) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    //from  http://github.com/abraham/twitteroauth/blob/master/twitteroauth/twitteroauth.php
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    $response = curl_exec($ch);
    $response_info = curl_getinfo($ch);
    $erno = curl_errno($ch);
    $er = curl_error($ch);
    curl_close($ch);
    global $api_time;
    global $rate_limit;
    /*
        // Split that headers and the body
        list($headers, $body) = explode("\n\n", $response, 2);
    
        // Place the headers into an array
        $headers = explode("\n", $headers);
        $headers_array;
        foreach ($headers as $header) {
            list($key, $value) = explode(':', $header, 2);
            $headers_array[$key] = $value;
        }
            
        // Not ever request is rate limited
        if ($headers_array['X-RateLimit-Limit']) {
            $current_time = time();
            $ratelimit_time = $headers_array['X-RateLimit-Reset'];
    
            $time_until_reset = $ratelimit_time - $current_time;
    
            $minutes_until_reset = round($time_until_reset / 60);
    
            $currentdate = strtotime("now");
    
            $rate_limit = "Rate Limit: " . $headers_array['X-RateLimit-Remaining'] . " / " . $headers_array['X-RateLimit-Limit'] . " for the next $minutes_until_reset minutes";
        }
    
        // The body of the request is at the end of the headers
        $body = end($headers);
    */
    $body = $response;
    $api_time += microtime(1) - $api_start;
    switch (intval($response_info['http_code'])) {
        case 200:
        case 201:
            $json = json_decode($body);
            if ($json) {
                return $json;
            }
            return $body;
        case 401:
            user_logout();
            if (DEBUG_MODE == 'ON') {
                theme('error', "<p>Error: Login credentials incorrect.</p><p>{$response_info['http_code']}: {$response}</p><hr /><p>{$url}</p>");
            } else {
                theme('error', "<p>Error: Login credentials incorrect.</p><p>{$response_info['http_code']}: {$response}</p>");
            }
        case 0:
            $result = $erno . ":" . $er . "<br />";
            /*
            foreach ($response_info as $key => $value) 
            {
                $result .= "Key: $key; Value: $value<br />";
            }
            */
            theme('error', '<h2>Twitter timed out</h2><p>Dabr gave up on waiting for Twitter to respond. They\'re probably overloaded right now, try again in a minute. <br />' . $result . ' </p>');
        default:
            $result = json_decode($body);
            $result = $result->error ? $result->error : $body;
            if (strlen($result) > 500) {
                $result = 'Something broke on Twitter\'s end.';
                /*    
                $result .= $erno . ":" . $er . "<br />" ;
                foreach ($response_info as $key => $value) 
                {
                $result .= "Key: $key; Value: $value<br />";
                }
                */
            } else {
                if ($result == "Status is over 140 characters.") {
                    theme('error', "<h2>Status was tooooooo loooooong!</h2><p>{$status}</p><hr />");
                    //theme('status_form',$status);
                }
            }
            if (DEBUG_MODE == 'ON') {
                theme('error', "<h2>An error occured while calling the Twitter API</h2><p>{$response_info['http_code']}: {$result}</p><hr /><p>{$url}</p>");
            } else {
                theme('error', "<h2>An error occured while calling the Twitter API</h2><p>{$response_info['http_code']}: {$result}</p>");
            }
    }
}
Exemplo n.º 7
0
        $url = TWITTER_URL . $request_api;
        if (isset($post_data['oauth_token']) && isset($post_data['oauth_token_secret'])) {
            $oauthStr = $post_str;
        } else {
            if (preg_match('/\\?(.*)/', $request_api, $get_key) != 0) {
                parse_str($get_key[1], $get_data);
                if (isset($get_data['oauth_token']) && isset($get_data['oauth_token_secret'])) {
                    $oauthStr = $get_key[1];
                }
            }
            if (!isset($oauthStr)) {
                $oauthStr = oauth_str($url, $post_data);
            }
        }
    }
    user_oauth_sign($url, $oauthStr, $post_data);
    $result = process_curl($url, $post_data);
    if ($request_api === '/takeoAuth.json') {
        //Special For get oauth_token&&oauth_token_secret
        $checkResult = json_decode($result);
        if (!isset($checkResult->error)) {
            parse_str($oauthStr, $tokenF);
            $result = '{"oauth_token":"' . $tokenF['oauth_token'] . '","oauth_token_secret":"' . $tokenF['oauth_token_secret'] . '"}';
        }
    }
}
echo $result;
/**
 * Check the request type
 * @return string
 */