Example #1
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;
 }
 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;
 }
Example #3
0
 function get_import_status($import_id)
 {
     $response = @dsq_urlopen(DISQUS_IMPORTER_URL . '/api/get-import-status/', array('forum_url' => $this->short_name, 'forum_api_key' => $this->forum_api_key, 'import_id' => $import_id, 'response_type' => 'php'));
     $data = unserialize($response['data']);
     if (!$data || $data['stat'] == 'fail') {
         return -1;
     }
     return $data;
 }
Example #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;
 }
Example #5
0
 function wp_check_version()
 {
     global $dsq_version;
     $response = @dsq_urlopen(DISQUS_MEDIA_URL . '/wp/LATEST_VERSION');
     $latest_version = floatval($response['data']);
     if ($dsq_version < $latest_version) {
         return $latest_version;
     }
     return false;
 }