/plugins/doDiqusPlugin/lib/vendor/disqusapi/disqusapi.php

https://github.com/makerlabs/Symfohub · PHP · 154 lines · 110 code · 28 blank · 16 comment · 16 complexity · 9b660b8a28c2711afaca5daeeb5c9277 MD5 · raw file

  1. <?php
  2. /**
  3. * Implementation of the Disqus API.
  4. *
  5. * http://disqus.com/api/
  6. *
  7. * @author DISQUS <team@disqus.com>
  8. * @copyright 2007-2010 Big Head Labs
  9. * @link http://disqus.com/
  10. * @package disqusapi
  11. * @version 0.0.1
  12. *
  13. * $disqus = new DisqusAPI($secret_key)
  14. * $disqus->trends->listThreads()
  15. *
  16. */
  17. if (!defined('DISQUS_API_HOST')) {
  18. define('DISQUS_API_HOST', 'disqus.com');
  19. }
  20. if (!defined('DISQUS_API_SSL_HOST')) {
  21. define('DISQUS_API_SSL_HOST', 'secure.disqus.com');
  22. }
  23. define('DISQUS_API_VERSION', '0.0.1');
  24. require_once(dirname(__FILE__) . '/url.php');
  25. if (!extension_loaded('json')) {
  26. require_once(dirname(__FILE__) . '/json.php');
  27. function dsq_json_decode($data) {
  28. $json = new JSON;
  29. return $json->unserialize($data);
  30. }
  31. } else {
  32. function dsq_json_decode($data) {
  33. return json_decode($data);
  34. }
  35. }
  36. global $DISQUS_API_INTERFACES;
  37. $DISQUS_API_INTERFACES = dsq_json_decode(file_get_contents(dirname(__FILE__) . '/interfaces.json'));
  38. class DisqusInterfaceNotDefined extends Exception {}
  39. class DisqusAPIError extends Exception {
  40. public function __construct($code, $message) {
  41. $this->code = $code;
  42. $this->message = $message;
  43. }
  44. }
  45. class DisqusResource {
  46. public function __construct($api, $interface=null, $node=null, $tree=array()) {
  47. global $DISQUS_API_INTERFACES;
  48. if (!$interface) {
  49. $interface = $DISQUS_API_INTERFACES;
  50. }
  51. $this->api = $api;
  52. $this->interface = $interface;
  53. $this->node = $node;
  54. if ($node) {
  55. array_push($tree, $node);
  56. }
  57. $this->tree = $tree;
  58. }
  59. public function __get($attr) {
  60. $interface = $this->interface->$attr;
  61. if (!$interface) {
  62. throw new DisqusInterfaceNotDefined();
  63. }
  64. return new DisqusResource($this->api, $interface, $attr, $this->tree);
  65. }
  66. public function __call($name, $args) {
  67. $resource = $this->interface->$name;
  68. if (!$resource) {
  69. throw new DisqusInterfaceNotDefined();
  70. }
  71. $kwargs = (array)$args[0];
  72. foreach ((array)$resource->required as $k) {
  73. if (empty($kwargs[$k])) {
  74. throw new Exception('Missing required argument: '.$k);
  75. }
  76. }
  77. $api = $this->api;
  78. if (empty($kwargs['api_secret'])) {
  79. $kwargs['api_secret'] = $api->key;
  80. }
  81. // emulate a named pop
  82. $version = (!empty($kwargs['version']) ? $kwargs['version'] : $api->version);
  83. $format = (!empty($kwargs['format']) ? $kwargs['format'] : $api->format);
  84. unset($kwargs['version'], $kwargs['format']);
  85. $url = ($api->is_secure ? 'https://'.DISQUS_API_SSL_HOST : 'http://'.DISQUS_API_HOST);
  86. $path = '/api/'.$version.'/'.implode('/', $this->tree).'/'.$name.'.'.$format;
  87. if (!empty($kwargs)) {
  88. if ($resource->method == 'POST') {
  89. $post_data = $kwargs;
  90. } else {
  91. $post_data = false;
  92. $path .= '?'.dsq_get_query_string($kwargs);
  93. }
  94. }
  95. $response = dsq_urlopen($url.$path, $post_data);
  96. $data = call_user_func($api->formats[$format], $response['data']);
  97. if ($response['code'] != 200) {
  98. throw new DisqusAPIError($data->code, $data->response);
  99. }
  100. return $data->response;
  101. }
  102. }
  103. class DisqusAPI extends DisqusResource {
  104. public $formats = array(
  105. 'json' => 'dsq_json_decode'
  106. );
  107. public function __construct($key=null, $format='json', $version='3.0', $is_secure=false) {
  108. $this->key = $key;
  109. $this->format = $format;
  110. $this->version = $version;
  111. $this->is_secure = $is_secure;
  112. parent::__construct($this);
  113. }
  114. public function __invoke() {
  115. throw new Exception('You cannot call the API without a resource.');
  116. }
  117. public function setKey($key) {
  118. $this->key = $key;
  119. }
  120. public function setFormat($format) {
  121. $this->format = $format;
  122. }
  123. public function setVersion($version) {
  124. $this->version = $version;
  125. }
  126. }