]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountratelimitstatus.php
3c6c3e714dcf55a943877c2abab970a021a26360
[quix0rs-gnu-social.git] / actions / apiaccountratelimitstatus.php
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    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/apibareauth.php';
35
36 /**
37  * We don't have a rate limit, but some clients check this method.
38  * It always returns the same thing: 100 hits left.
39  *
40  * @category API
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46
47 class ApiAccountRateLimitStatusAction extends ApiBareAuthAction
48 {
49     var $format = null;
50
51     /**
52      * Take arguments for running
53      *
54      * @param array $args $_REQUEST args
55      *
56      * @return boolean success flag
57      *
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         if ($this->requiresAuth()) {
65             if ($this->checkBasicAuthUser() == false) {
66                 return false;
67             }
68         }
69
70         $this->format = $this->arg('format');
71         return true;
72     }
73
74     /**
75      * Handle the request
76      *
77      * Return some Twitter-ish data about API limits
78      *
79      * @param array $args $_REQUEST data (unused)
80      *
81      * @return void
82      */
83
84     function handle($args)
85     {
86         parent::handle($args);
87
88         if (!in_array($this->format, array('xml', 'json'))) {
89             $this->clientError(
90                 _('API method not found!'),
91                 404,
92                 $this->format
93             );
94             return;
95         }
96
97         $reset   = new DateTime();
98         $reset->modify('+1 hour');
99
100         $this->init_document($this->format);
101
102          if ($this->format == 'xml') {
103              $this->elementStart('hash');
104              $this->element('remaining-hits', array('type' => 'integer'), 150);
105              $this->element('hourly-limit', array('type' => 'integer'), 150);
106              $this->element(
107                  'reset-time', array('type' => 'datetime'),
108                  common_date_iso8601($reset->format('r'))
109              );
110              $this->element(
111                  'reset_time_in_seconds',
112                  array('type' => 'integer'),
113                  strtotime('+1 hour')
114              );
115              $this->elementEnd('hash');
116          } elseif ($this->format == 'json') {
117              $out = array(
118                  'reset_time_in_seconds' => strtotime('+1 hour'),
119                  'remaining_hits' => 150,
120                  'hourly_limit' => 150,
121                  'reset_time' => common_date_rfc2822(
122                      $reset->format('r')
123                   )
124              );
125              print json_encode($out);
126          }
127
128          $this->end_document($this->format);
129     }
130
131 }
132