]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apistatusesupdate.php
CamelCase all function names in the API code
[quix0rs-gnu-social.git] / actions / apistatusesupdate.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Post a notice (update your status) through the API
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/apiauth.php';
35
36 /**
37  * Updates the authenticating user's status (posts a notice).
38  *
39  * @category API
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 class ApiStatusesUpdateAction extends ApiAuthAction
47 {
48
49     var $user                  = null;
50     var $source                = null;
51     var $status                = null;
52     var $in_reply_to_status_id = null;
53
54     static $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
55
56     /**
57      * Take arguments for running
58      *
59      * @param array $args $_REQUEST args
60      *
61      * @return boolean success flag
62      *
63      */
64
65     function prepare($args)
66     {
67         parent::prepare($args);
68
69         $this->user = $this->auth_user;
70
71         if (empty($this->user)) {
72             $this->clientError(_('No such user!'), 404, $this->format);
73             return false;
74         }
75
76         $this->status = $this->trimmed('status');
77
78         if (empty($this->status)) {
79             $this->clientError(
80                 'Client must provide a \'status\' parameter with a value.',
81                 400,
82                 $this->format
83             );
84
85             return false;
86         }
87
88         $this->source = $this->trimmed('source');
89
90         if (empty($this->source) || in_array($source, $this->reserved_sources)) {
91             $this->source = 'api';
92         }
93
94         $this->in_reply_to_status_id
95             = intval($this->trimmed('in_reply_to_status_id'));
96
97         return true;
98     }
99
100     /**
101      * Handle the request
102      *
103      * Make a new notice for the update, save it, and show it
104      *
105      * @param array $args $_REQUEST data (unused)
106      *
107      * @return void
108      */
109
110     function handle($args)
111     {
112         parent::handle($args);
113
114         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
115             $this->clientError(
116                 _('This method requires a POST.'),
117                 400, $this->format
118             );
119             return;
120         }
121
122         $status_shortened = common_shorten_links($this->status);
123
124         if (Notice::contentTooLong($status_shortened)) {
125
126             // Note: Twitter truncates anything over 140, flags the status
127             // as "truncated."
128
129             $this->clientError(
130                 sprintf(
131                     _('That\'s too long. Max notice size is %d chars.'),
132                     Notice::maxContent()
133                 ),
134                 406,
135                 $this->format
136             );
137
138             return;
139         }
140
141         // Check for commands
142
143         $inter = new CommandInterpreter();
144         $cmd = $inter->handle_command($this->user, $status_shortened);
145
146         if ($cmd) {
147
148             if ($this->supported($cmd)) {
149                 $cmd->execute(new Channel());
150             }
151
152             // Cmd not supported?  Twitter just returns your latest status.
153             // And, it returns your last status whether the cmd was successful
154             // or not!
155
156             $this->notice = $this->user->getCurrentNotice();
157
158         } else {
159
160             $reply_to = null;
161
162             if (!empty($this->in_reply_to_status_id)) {
163
164                 // Check whether notice actually exists
165
166                 $reply = Notice::staticGet($this->in_reply_to_status_id);
167
168                 if ($reply) {
169                     $reply_to = $this->in_reply_to_status_id;
170                 } else {
171                     $this->clientError(
172                         _('Not found'),
173                         $code = 404,
174                         $this->format
175                     );
176                     return;
177                 }
178             }
179
180             $this->notice = Notice::saveNew(
181                 $this->user->id,
182                 html_entity_decode($this->status, ENT_NOQUOTES, 'UTF-8'),
183                 $this->source,
184                 1,
185                 $reply_to
186             );
187
188             common_broadcast_notice($this->notice);
189         }
190
191         $this->showNotice();
192     }
193
194     /**
195      * Show the resulting notice
196      *
197      * @return void
198      */
199
200     function showNotice()
201     {
202         if (!empty($this->notice)) {
203             if ($this->format == 'xml') {
204                 $this->showSingleXmlStatus($this->notice);
205             } elseif ($this->format == 'json') {
206                 $this->show_single_json_status($this->notice);
207             }
208         }
209     }
210
211     /**
212      * Is this command supported when doing an update from the API?
213      *
214      * @param string $cmd the command to check for
215      *
216      * @return boolean true or false
217      */
218
219     function supported($cmd)
220     {
221         static $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
222             'FavCommand', 'OnCommand', 'OffCommand');
223
224         if (in_array(get_class($cmd), $cmdlist)) {
225             return true;
226         }
227
228         return false;
229     }
230
231 }