Пример #1
0
function _dsq_fopen_urlopen($url, $postdata, &$response, $file_name, $file_field)
{
    $params = array();
    if ($file_name && $file_field) {
        $boundary = '----------' . md5(time());
        $content = dsq_get_post_content($boundary, $postdata, $file_name, $file_field);
        $header = dsq_get_http_headers_for_request($boundary, $content, $file_name, $file_field);
        $params = array('http' => array('method' => 'POST', 'header' => $header, 'content' => $content, 'timeout' => SOCKET_TIMEOUT));
    } else {
        if ($postdata) {
            $params = array('http' => array('method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => dsq_get_query_string($postdata), 'timeout' => SOCKET_TIMEOUT));
        }
    }
    ini_set('user_agent', USER_AGENT);
    $ctx = stream_context_create($params);
    $fp = fopen($url, 'rb', false, $ctx);
    if (!$fp) {
        return false;
    }
    // Get status code from headers.
    list($unused, $response['code'], $unused) = explode(' ', $http_response_header[0], 3);
    $headers = array_slice($http_response_header, 1);
    // Convert headers into associative array.
    foreach ($headers as $unused => $header) {
        $header = explode(':', $header);
        $header[0] = trim($header[0]);
        $header[1] = trim($header[1]);
        $headers[strtolower($header[0])] = strtolower($header[1]);
    }
    $response['data'] = stream_get_contents($fp);
}
 public function __call($name, $args)
 {
     $resource = $this->interface->{$name};
     if (!$resource) {
         throw new DisqusInterfaceNotDefined();
     }
     $kwargs = (array) $args[0];
     foreach ((array) $resource->required as $k) {
         if (empty($kwargs[$k])) {
             throw new Exception('Missing required argument: ' . $k);
         }
     }
     $api = $this->api;
     if (empty($kwargs['api_secret'])) {
         $kwargs['api_secret'] = $api->key;
     }
     // emulate a named pop
     $version = !empty($kwargs['version']) ? $kwargs['version'] : $api->version;
     $format = !empty($kwargs['format']) ? $kwargs['format'] : $api->format;
     unset($kwargs['version'], $kwargs['format']);
     $url = $api->is_secure ? 'https://' . DISQUS_API_SSL_HOST : 'http://' . DISQUS_API_HOST;
     $path = '/api/' . $version . '/' . implode('/', $this->tree) . '/' . $name . '.' . $format;
     if (!empty($kwargs)) {
         if ($resource->method == 'POST') {
             $post_data = $kwargs;
         } else {
             $post_data = false;
             $path .= '?' . dsq_get_query_string($kwargs);
         }
     }
     $response = dsq_urlopen($url . $path, $post_data);
     $data = call_user_func($api->formats[$format], $response['data']);
     if ($response['code'] != 200) {
         throw new DisqusAPIError($data->code, $data->response);
     }
     return $data->response;
 }
Пример #3
0
 /**
  * Makes a call to a Disqus API method.
  *
  * @return
  *   The Disqus object.
  * @param $method
  *   The Disqus API method to call.
  * @param $args
  *   An associative array of arguments to be passed.
  * @param $post
  *   TRUE or FALSE, depending on whether we're making a POST call.
  */
 function call($method, $args = array(), $post = false)
 {
     $url = $this->api_url . $method . '/';
     if (!isset($args['user_api_key'])) {
         $args['user_api_key'] = $this->user_api_key;
     }
     if (!isset($args['forum_api_key'])) {
         $args['forum_api_key'] = $this->forum_api_key;
     }
     if (!isset($args['api_version'])) {
         $args['api_version'] = $this->api_version;
     }
     foreach ($args as $key => $value) {
         // XXX: Disqus is lacking some exception handling and we sometimes
         // end up with 500s when passing invalid values
         if (empty($value)) {
             unset($args[$key]);
         }
     }
     if (!$post) {
         $url .= '?' . dsq_get_query_string($args);
         $args = null;
     }
     if (!($response = dsq_urlopen($url, $args)) || !$response['code']) {
         $this->last_error = 'Unable to connect to the Disqus API servers';
         return false;
     }
     if ($response['code'] != 200) {
         if ($response['code'] == 500) {
             // Try to grab the exception ID for better reporting
             if (!empty($response['headers']['X-Sentry-ID'])) {
                 $this->last_error = 'DISQUS returned a bad response (HTTP ' . $response['code'] . ', ReferenceID: ' . $response['headers']['X-Sentry-ID'] . ')';
                 return false;
             }
         } elseif ($response['code'] == 400) {
             $data = dsq_json_decode($response['data']);
             if ($data && $data->message) {
                 $this->last_error = $data->message;
             } else {
                 $this->last_error = "DISQUS returned a bad response (HTTP " . $response['code'] . ")";
             }
             return false;
         }
         $this->last_error = "DISQUS returned a bad response (HTTP " . $response['code'] . ")";
         return false;
     }
     $data = dsq_json_decode($response['data']);
     if (!$data) {
         $this->last_error = 'No valid JSON content returned from Disqus';
         return false;
     }
     if (!$data->succeeded) {
         if (!$data->message) {
             $this->last_error = '(No error message was received)';
         } else {
             $this->last_error = $data->message;
         }
         return false;
     }
     $this->last_error = null;
     return $data->message;
 }
Пример #4
0
 /**
  * Makes a call to a Disqus API method.
  *
  * @return
  *   The Disqus object.
  * @param $method
  *   The Disqus API method to call.
  * @param $args
  *   An associative array of arguments to be passed.
  * @param $post
  *   TRUE or FALSE, depending on whether we're making a POST call.
  */
 function call($method, $args = array(), $post = false)
 {
     $url = $this->api_url . $method . '/';
     if (!isset($args['user_api_key'])) {
         $args['user_api_key'] = $this->user_api_key;
     }
     if (!isset($args['forum_api_key'])) {
         $args['forum_api_key'] = $this->forum_api_key;
     }
     if (!isset($args['api_version'])) {
         $args['api_version'] = $this->api_version;
     }
     foreach ($args as $key => $value) {
         // XXX: Disqus is lacking some exception handling and we sometimes
         // end up with 500s when passing invalid values
         if (empty($value)) {
             unset($args[$key]);
         }
     }
     if (!$post) {
         $url .= '?' . dsq_get_query_string($args);
         $args = null;
     }
     if (!($response = dsq_urlopen($url, $args)) || !$response['code']) {
         $this->last_error = 'Unable to connect to the Disqus API servers';
         return false;
     }
     $data = dsq_json_decode($response['data']);
     if (!$data || !$data->succeeded) {
         $this->last_error = $data->message;
         return false;
     }
     if ($response['code'] != 200) {
         $this->last_error = 'Unknown error';
         return false;
     }
     return $data->message;
 }