PageRenderTime 25ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/disqus-comment-system/lib/api/disqus/disqus.php

https://github.com/di0fref/Wordpress-stuff
PHP | 459 lines | 174 code | 46 blank | 239 comment | 24 complexity | d7f635f23359e5b68a15a81496eb28f7 MD5 | raw file
  1. <?php
  2. /**
  3. * Implementation of the Disqus v1.1 API.
  4. *
  5. * http://groups.google.com/group/disqus-dev/web/api-1-1
  6. *
  7. * @author Disqus <team@disqus.com>
  8. * @copyright 2007-2010 Big Head Labs
  9. * @link http://disqus.com/
  10. * @package Disqus
  11. * @version 1.1
  12. */
  13. require_once(dirname(__FILE__) . '/url.php');
  14. /** @#+
  15. * Constants
  16. */
  17. /**
  18. * Base URL for Disqus.
  19. */
  20. define('DISQUS_TYPE_SPAM', 'spam');
  21. define('DISQUS_TYPE_DELETED', 'killed');
  22. define('DISQUS_TYPE_KILLED', DISQUS_TYPE_DELETED);
  23. define('DISQUS_TYPE_NEW', 'new');
  24. define('DISQUS_STATE_APPROVED', 'approved');
  25. define('DISQUS_STATE_UNAPPROVED', 'unapproved');
  26. define('DISQUS_STATE_SPAM', 'spam');
  27. define('DISQUS_STATE_DELETED', 'killed');
  28. define('DISQUS_STATE_KILLED', DISQUS_STATE_DELETED);
  29. define('DISQUS_ACTION_SPAM', 'spam');
  30. define('DISQUS_ACTION_APPROVE', 'approve');
  31. define('DISQUS_ACTION_DELETE', 'delete');
  32. define('DISQUS_ACTION_KILL', 'kill');
  33. if (!extension_loaded('json')) {
  34. require_once(dirname(__FILE__) . '/json.php');
  35. function dsq_json_decode($data) {
  36. $json = new JSON;
  37. return $json->unserialize($data);
  38. }
  39. } else {
  40. function dsq_json_decode($data) {
  41. return json_decode($data);
  42. }
  43. }
  44. /**
  45. * Helper methods for all of the Disqus 1.1 API methods.
  46. *
  47. * @package Disqus
  48. * @author DISQUS.com <team@disqus.com>
  49. * @copyright 2007-2010 Big Head Labs
  50. * @version 1.1
  51. */
  52. class DisqusAPI {
  53. var $user_api_key;
  54. var $forum_api_key;
  55. var $api_url = 'http://www.disqus.com/api/';
  56. var $api_version = '1.1';
  57. /**
  58. * Creates a new interface to the Disqus API.
  59. *
  60. * @param $user_api_key
  61. * (optional) The User API key to use.
  62. * @param $forum_api_key
  63. * (optional) The Forum API key to use.
  64. * @param $api_url
  65. * (optional) The prefix URL to use when calling the Disqus API.
  66. */
  67. function DisqusAPI($user_api_key, $forum_api_key, $api_url='http://www.disqus.com/api/') {
  68. $this->user_api_key = $user_api_key;
  69. $this->forum_api_key = $forum_api_key;
  70. $this->api_url = $api_url;
  71. $this->last_error = null;
  72. }
  73. /**
  74. * Makes a call to a Disqus API method.
  75. *
  76. * @return
  77. * The Disqus object.
  78. * @param $method
  79. * The Disqus API method to call.
  80. * @param $args
  81. * An associative array of arguments to be passed.
  82. * @param $post
  83. * TRUE or FALSE, depending on whether we're making a POST call.
  84. */
  85. function call($method, $args=array(), $post=false) {
  86. $url = $this->api_url . $method . '/';
  87. if (!isset($args['user_api_key'])) {
  88. $args['user_api_key'] = $this->user_api_key;
  89. }
  90. if (!isset($args['forum_api_key'])) {
  91. $args['forum_api_key'] = $this->forum_api_key;
  92. }
  93. if (!isset($args['api_version'])) {
  94. $args['api_version'] = $this->api_version;
  95. }
  96. foreach ($args as $key=>$value) {
  97. // XXX: Disqus is lacking some exception handling and we sometimes
  98. // end up with 500s when passing invalid values
  99. if (empty($value)) unset($args[$key]);
  100. }
  101. if (!$post) {
  102. $url .= '?' . dsq_get_query_string($args);
  103. $args = null;
  104. }
  105. if (!($response = dsq_urlopen($url, $args)) || !$response['code']) {
  106. $this->last_error = 'Unable to connect to the Disqus API servers';
  107. return false;
  108. }
  109. if ($response['code'] != 200) {
  110. if ($response['code'] == 500) {
  111. // Try to grab the exception ID for better reporting
  112. if (!empty($response['headers']['X-Sentry-ID'])) {
  113. $this->last_error = 'DISQUS returned a bad response (HTTP '.$response['code'].', ReferenceID: '.$response['headers']['X-Sentry-ID'].')';
  114. return false;
  115. }
  116. } elseif ($response['code'] == 400) {
  117. $data = dsq_json_decode($response['data']);
  118. if ($data && $data->message) {
  119. $this->last_error = $data->message;
  120. } else {
  121. $this->last_error = "DISQUS returned a bad response (HTTP ".$response['code'].")";
  122. }
  123. return false;
  124. }
  125. $this->last_error = "DISQUS returned a bad response (HTTP ".$response['code'].")";
  126. return false;
  127. }
  128. $data = dsq_json_decode($response['data']);
  129. if (!$data) {
  130. $this->last_error = 'No valid JSON content returned from Disqus';
  131. return false;
  132. }
  133. if (!$data->succeeded) {
  134. if (!$data->message) {
  135. $this->last_error = '(No error message was received)';
  136. } else {
  137. $this->last_error = $data->message;
  138. }
  139. return false;
  140. }
  141. return $data->message;
  142. }
  143. /**
  144. * Retrieve the last error message recorded.
  145. *
  146. * @return
  147. * The last recorded error from the API
  148. */
  149. function get_last_error() {
  150. if (empty($this->last_error)) return;
  151. if (!is_string($this->last_error)) {
  152. return var_export($this->last_error);
  153. }
  154. return $this->last_error;
  155. }
  156. /**
  157. * Validate API key and get username.
  158. *
  159. * @return
  160. * Username matching the API key
  161. */
  162. function get_user_name() {
  163. return $this->call('get_user_name', array(), true);
  164. }
  165. /**
  166. * Returns an array of hashes representing all forums the user owns.
  167. *
  168. * @return
  169. * An array of hashes representing all forums the user owns.
  170. */
  171. function get_forum_list() {
  172. return $this->call('get_forum_list');
  173. }
  174. /**
  175. * Get a forum API key for a specific forum.
  176. *
  177. * @param $forum_id
  178. * the unique id of the forum
  179. * @return
  180. * A string which is the Forum Key for the given forum.
  181. */
  182. function get_forum_api_key($forum_id) {
  183. $params = array(
  184. 'forum_id' => $forum_id,
  185. );
  186. return $this->call('get_forum_api_key', $params);
  187. }
  188. /**
  189. * Get a list of comments on a website.
  190. *
  191. * Both filter and exclude are multivalue arguments with comma as a divider.
  192. * That makes is possible to use combined requests. For example, if you want
  193. * to get all deleted spam messages, your filter argument should contain
  194. * 'spam,killed' string.
  195. *
  196. * @param $forum_id
  197. * The forum ID.
  198. * @param $params
  199. * - limit: Number of entries that should be included in the response. Default is 25.
  200. * - start: Starting point for the query. Default is 0.
  201. * - filter: Type of entries that should be returned.
  202. * - exclude: Type of entries that should be excluded from the response.
  203. * @return
  204. * Returns posts from a forum specified by id.
  205. */
  206. function get_forum_posts($forum_id, $params=array()) {
  207. $params['forum_id'] = $forum_id;
  208. return $this->call('get_forum_posts', $params);
  209. }
  210. /**
  211. * Count a number of comments in articles.
  212. *
  213. * @param $thread_ids
  214. * an array of thread IDs belonging to the given forum.
  215. * @return
  216. * A hash having thread_ids as keys and 2-element arrays as values.
  217. */
  218. function get_num_posts($thread_ids) {
  219. $params = array(
  220. 'thread_ids' => is_array($thread_ids) ? implode(',', $thread_ids) : $thread_ids,
  221. );
  222. return $this->call('get_num_posts', $params);
  223. }
  224. /**
  225. * Returns a list of categories that were created for a website (forum) provided.
  226. *
  227. * @param $forum_id
  228. * the unique of the forum
  229. * @return
  230. * A hash containing category_id, title, forum_id, and is_default.
  231. */
  232. function get_categories_list($forum_id) {
  233. $params = array(
  234. 'forum_id' => $forum_id,
  235. );
  236. return $this->call('get_categories_list', $params);
  237. }
  238. /**
  239. * Get a list of threads on a website.
  240. *
  241. * @param $forum_id
  242. * the unique id of the forum.
  243. * @param $params
  244. * - limit: Number of entries that should be included in the response. Default is 25.
  245. * - start: Starting point for the query. Default is 0.
  246. * - category_id: Filter entries by category
  247. * @return
  248. * An array of hashes representing all threads belonging to the given forum.
  249. */
  250. function get_thread_list($forum_id, $params=array()) {
  251. $params['forum_id'] = $forum_id;
  252. return $this->call('get_thread_list', $params);
  253. }
  254. /**
  255. * Get a list of threads with new comments.
  256. *
  257. * @param $forum_id
  258. * The Forum ID.
  259. * @param $since
  260. * Start date for new posts. Format: 2009-03-30T15:41, Timezone: UTC.
  261. * @return
  262. * An array of hashes representing all threads with new comments since offset.
  263. */
  264. function get_updated_threads($forum_id, $since) {
  265. $params = array(
  266. 'forum_id' => $forum_id,
  267. 'since' => is_string($since) ? $string : strftime('%Y-%m-%dT%H:%M', $since),
  268. );
  269. return $this->call('get_updated_threads', $params);
  270. }
  271. /**
  272. * Get a list of comments in a thread.
  273. *
  274. * Both filter and exclude are multivalue arguments with comma as a divider.
  275. * That makes is possible to use combined requests. For example, if you want
  276. * to get all deleted spam messages, your filter argument should contain
  277. * 'spam,killed' string. Note that values are joined by AND statement so
  278. * 'spam,new' will return all messages that are new and marked as spam. It
  279. * will not return messages that are new and not spam or that are spam but
  280. * not new (i.e. has already been moderated).
  281. *
  282. * @param $thread_id
  283. * The ID of a thread belonging to the given forum
  284. * @param $params
  285. * - limit: Number of entries that should be included in the response. Default is 25.
  286. * - start: Starting point for the query. Default is 0.
  287. * - filter: Type of entries that should be returned (new, spam or killed).
  288. * - exclude: Type of entries that should be excluded from the response (new, spam or killed).
  289. * @return
  290. * An array of hashes representing representing all posts belonging to the
  291. * given forum.
  292. */
  293. function get_thread_posts($thread_id, $params=array()) {
  294. $params['thread_id'] = $thread_id;
  295. return $this->call('get_thread_posts', $params);
  296. }
  297. /**
  298. * Get or create thread by identifier.
  299. *
  300. * This method tries to find a thread by its identifier and title. If there is
  301. * no such thread, the method creates it. In either case, the output value is
  302. * a thread object.
  303. *
  304. * @param $identifier
  305. * Unique value (per forum) for a thread that is used to keep be able to get
  306. * data even if permalink is changed.
  307. * @param $title
  308. * The title of the thread to possibly be created.
  309. * @param $params
  310. * - category_id: Filter entries by category
  311. * - create_on_fail: if thread does not exist, the method will create it
  312. * @return
  313. * Returns a hash with two keys:
  314. * - thread: a hash representing the thread corresponding to the identifier.
  315. * - created: indicates whether the thread was created as a result of this
  316. * method call. If created, it will have the specified title.
  317. */
  318. function thread_by_identifier($identifier, $title, $params=array()) {
  319. $params['identifier'] = $identifier;
  320. $params['title'] = $title;
  321. return $this->call('thread_by_identifier', $params, true);
  322. }
  323. /**
  324. * Get thread by URL.
  325. *
  326. * Finds a thread by its URL. Output value is a thread object.
  327. *
  328. * @param $url
  329. * the URL to check for an associated thread
  330. * @param $partner_api_key
  331. * (optional) The Partner API key.
  332. * @return
  333. * A thread object, otherwise NULL.
  334. */
  335. function get_thread_by_url($url, $partner_api_key=null) {
  336. $params = array(
  337. 'url' => $url,
  338. 'partner_api_key' => $partner_api_key,
  339. );
  340. return $this->call('get_thread_by_url', $params);
  341. }
  342. /**
  343. * Updates thread.
  344. *
  345. * Updates thread, specified by id and forum API key, with values described in
  346. * the optional arguments.
  347. *
  348. * @param $thread_id
  349. * the ID of a thread belonging to the given forum
  350. * @param $params
  351. * - title: the title of the thread
  352. * - slug: the per-forum-unique string used for identifying this thread in
  353. * disqus.com URL’s relating to this thread. Composed of
  354. * underscore-separated alphanumeric strings.
  355. * - url: the URL this thread is on, if known.
  356. * - allow_comments: whether this thread is open to new comments
  357. * @return
  358. * Returns an empty success message.
  359. */
  360. function update_thread($thread_id, $params=array()) {
  361. $params['thread_id'] = $thread_id;
  362. return $this->call('update_thread', $params, true);
  363. }
  364. /**
  365. * Creates a new post.
  366. *
  367. * Creates a comment to the thread specified by id.
  368. *
  369. * @param $thread_id
  370. * the thread to post to
  371. * @param $message
  372. * the content of the post
  373. * @param $author_name
  374. * the post creator’s name
  375. * @param $author_email
  376. * the post creator’s email address
  377. * @param $params
  378. * - partner_api_key
  379. * - created_at: Format: 2009-03-30T15:41, Timezone: UTC
  380. * - ip_address: the author’s IP address
  381. * - author_url: the author's homepage
  382. * - parent_post: the id of the parent post
  383. * - state: Comment's state, must be one of the following: approved,
  384. * unapproved, spam, killed
  385. * @return
  386. * Returns modified version.
  387. */
  388. function create_post($thread_id, $message, $author_name, $author_email, $params=array()) {
  389. $params['thread_id'] = $thread_id;
  390. $params['message'] = $message;
  391. $params['author_name'] = $author_name;
  392. $params['author_email'] = $author_email;
  393. return $this->call('create_post', $params, true);
  394. }
  395. /**
  396. * Delete a comment or mark it as spam (or not spam).
  397. *
  398. * @param $post_id
  399. * The Post ID.
  400. * @param $action
  401. * Name of action to be performed. Value can be 'spam', 'approve' or 'kill'.
  402. * @return
  403. * Returns modified version.
  404. */
  405. function moderate_post($post_id, $action) {
  406. $params = array(
  407. 'post_id' => $post_id,
  408. 'action' => $action,
  409. );
  410. return $this->call('moderate_post', $params, true);
  411. }
  412. }
  413. ?>