apiaccountratelimitstatus.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Dummy action that emulates Twitter's rate limit status API resource
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category API
  23. * @package StatusNet
  24. * @author Brion Vibber <brion@pobox.com>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Robin Millette <robin@millette.info>
  27. * @author Siebrand Mazeland <s.mazeland@xs4all.nl>
  28. * @author Zach Copley <zach@status.net>
  29. * @copyright 2009 StatusNet, Inc.
  30. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  31. * @link http://status.net/
  32. */
  33. if (!defined('GNUSOCIAL')) {
  34. exit(1);
  35. }
  36. /**
  37. * We don't have a rate limit, but some clients check this method.
  38. * It always returns the same thing: 150 hits left.
  39. *
  40. * @category API
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Robin Millette <robin@millette.info>
  44. * @author Zach Copley <zach@status.net>
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  46. * @link http://status.net/
  47. */
  48. class ApiAccountRateLimitStatusAction extends ApiBareAuthAction
  49. {
  50. /**
  51. * Return true if read only.
  52. *
  53. * MAY override
  54. *
  55. * @param array $args other arguments
  56. *
  57. * @return boolean is read only action?
  58. */
  59. public function isReadOnly($args)
  60. {
  61. return true;
  62. }
  63. /**
  64. * Handle the request
  65. *
  66. * Return some Twitter-ish data about API limits
  67. *
  68. * @return void
  69. * @throws ClientException
  70. */
  71. protected function handle()
  72. {
  73. parent::handle();
  74. if (!in_array($this->format, ['xml', 'json'])) {
  75. $this->clientError(
  76. // TRANS: Client error displayed when coming across a non-supported API method.
  77. _('API method not found.'),
  78. 404,
  79. $this->format
  80. );
  81. }
  82. $reset = new DateTime();
  83. $reset->modify('+1 hour');
  84. $this->initDocument($this->format);
  85. if ($this->format == 'xml') {
  86. $this->elementStart('hash');
  87. $this->element('remaining-hits', ['type' => 'integer'], "150");
  88. $this->element('hourly-limit', ['type' => 'integer'], "150");
  89. $this->element(
  90. 'reset-time',
  91. ['type' => 'datetime'],
  92. common_date_iso8601($reset->format('r'))
  93. );
  94. $this->element(
  95. 'reset_time_in_seconds',
  96. ['type' => 'integer'],
  97. strtotime('+1 hour')
  98. );
  99. $this->elementEnd('hash');
  100. } elseif ($this->format == 'json') {
  101. $out = [
  102. 'reset_time_in_seconds' => strtotime('+1 hour'),
  103. 'remaining_hits' => 150,
  104. 'hourly_limit' => 150,
  105. 'reset_time' => common_date_rfc2822(
  106. $reset->format('r')
  107. )
  108. ];
  109. print json_encode($out);
  110. }
  111. $this->endDocument($this->format);
  112. }
  113. }