]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountratelimitstatus.php
$format is used by every API action. Set it in the base class.
[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     /**
50      * Take arguments for running
51      *
52      * @param array $args $_REQUEST args
53      *
54      * @return boolean success flag
55      *
56      */
57
58     function prepare($args)
59     {
60         parent::prepare($args);
61
62         if ($this->requiresAuth()) {
63             if ($this->checkBasicAuthUser() == false) {
64                 return false;
65             }
66         }
67
68         return true;
69     }
70
71     /**
72      * Handle the request
73      *
74      * Return some Twitter-ish data about API limits
75      *
76      * @param array $args $_REQUEST data (unused)
77      *
78      * @return void
79      */
80
81     function handle($args)
82     {
83         parent::handle($args);
84
85         if (!in_array($this->format, array('xml', 'json'))) {
86             $this->clientError(
87                 _('API method not found!'),
88                 404,
89                 $this->format
90             );
91             return;
92         }
93
94         $reset   = new DateTime();
95         $reset->modify('+1 hour');
96
97         $this->init_document($this->format);
98
99          if ($this->format == 'xml') {
100              $this->elementStart('hash');
101              $this->element('remaining-hits', array('type' => 'integer'), 150);
102              $this->element('hourly-limit', array('type' => 'integer'), 150);
103              $this->element(
104                  'reset-time', array('type' => 'datetime'),
105                  common_date_iso8601($reset->format('r'))
106              );
107              $this->element(
108                  'reset_time_in_seconds',
109                  array('type' => 'integer'),
110                  strtotime('+1 hour')
111              );
112              $this->elementEnd('hash');
113          } elseif ($this->format == 'json') {
114              $out = array(
115                  'reset_time_in_seconds' => strtotime('+1 hour'),
116                  'remaining_hits' => 150,
117                  'hourly_limit' => 150,
118                  'reset_time' => common_date_rfc2822(
119                      $reset->format('r')
120                   )
121              );
122              print json_encode($out);
123          }
124
125          $this->end_document($this->format);
126     }
127
128 }
129