]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newmessage.php
Preparing File for dynamic thumbnail generation.
[quix0rs-gnu-social.git] / actions / newmessage.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Handler for posting new messages
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  Personal
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @copyright 2013 Free Software Foundation, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('STATUSNET')) {
34     exit(1);
35 }
36
37 /**
38  * Action for posting new direct messages
39  *
40  * @category Personal
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @author   Zach Copley <zach@status.net>
44  * @author   Sarven Capadisli <csarven@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
49 class NewmessageAction extends FormAction
50 {
51     var $content = null;
52     var $to = null;
53     var $other = null;
54
55     /**
56      * Title of the page
57      *
58      * Note that this usually doesn't get called unless something went wrong
59      *
60      * @return string page title
61      */
62
63     function title()
64     {
65         // TRANS: Page title for new direct message page.
66         return _('New message');
67     }
68
69     /**
70      * Handle input, produce output
71      *
72      * @param array $args $_REQUEST contents
73      *
74      * @return void
75      */
76
77     protected function prepare(array $args=array())
78     {
79         parent::prepare($args);
80
81         $user = $this->scoped->getUser();
82
83         $this->content = $this->trimmed('content');
84         $this->to = $this->trimmed('to');
85
86         if ($this->to) {
87
88             $this->other = User::getKV('id', $this->to);
89
90             if (!$this->other) {
91                 // TRANS: Client error displayed trying to send a direct message to a non-existing user.
92                 $this->clientError(_('No such user.'), 404);
93             }
94
95             if (!$user->mutuallySubscribed($this->other)) {
96                 // TRANS: Client error displayed trying to send a direct message to a user while sender and
97                 // TRANS: receiver are not subscribed to each other.
98                 $this->clientError(_('You cannot send a message to this user.'), 404);
99             }
100         }
101
102         return true;
103     }
104
105     protected function handlePost()
106     {
107         parent::handlePost();
108
109         assert($this->scoped); // XXX: maybe an error instead...
110         $user = $this->scoped->getUser();
111
112         if (!$this->content) {
113             // TRANS: Form validator error displayed trying to send a direct message without content.
114             $this->clientError(_('No content!'));
115         } else {
116             $content_shortened = $user->shortenLinks($this->content);
117
118             if (Message::contentTooLong($content_shortened)) {
119                 // TRANS: Form validation error displayed when message content is too long.
120                 // TRANS: %d is the maximum number of characters for a message.
121                 $this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
122                                            'That\'s too long. Maximum message size is %d characters.',
123                                            Message::maxContent()),
124                                         Message::maxContent()));
125             }
126         }
127
128         if (!$this->other) {
129             // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
130             $this->clientError(_('No recipient specified.'));
131         } else if (!$user->mutuallySubscribed($this->other)) {
132             // TRANS: Client error displayed trying to send a direct message to a user while sender and
133             // TRANS: receiver are not subscribed to each other.
134             $this->clientError(_('You cannot send a message to this user.'), 404);
135         } else if ($this->scoped->id == $this->other->id) {
136             // TRANS: Client error displayed trying to send a direct message to self.
137             $this->clientError(_('Do not send a message to yourself; ' .
138                 'just say it to yourself quietly instead.'), 403);
139         }
140
141         $message = Message::saveNew($this->scoped->id, $this->other->id, $this->content, 'web');
142         $message->notify();
143
144         if ($this->boolean('ajax')) {
145             $this->startHTML('text/xml;charset=utf-8');
146             $this->elementStart('head');
147             // TRANS: Page title after sending a direct message.
148             $this->element('title', null, _('Message sent'));
149             $this->elementEnd('head');
150             $this->elementStart('body');
151             $this->element('p', array('id' => 'command_result'),
152                 // TRANS: Confirmation text after sending a direct message.
153                 // TRANS: %s is the direct message recipient.
154                 sprintf(_('Direct message to %s sent.'),
155                     $this->other->nickname));
156             $this->elementEnd('body');
157             $this->endHTML();
158         } else {
159             $url = common_local_url('outbox',
160                 array('nickname' => $this->scoped->nickname));
161             common_redirect($url, 303);
162         }
163     }
164
165     /**
166      * Show an Ajax-y error message
167      *
168      * Goes back to the browser, where it's shown in a popup.
169      *
170      * @param string $msg Message to show
171      *
172      * @return void
173      */
174
175     function ajaxErrorMsg($msg)
176     {
177         $this->startHTML('text/xml;charset=utf-8', true);
178         $this->elementStart('head');
179         // TRANS: Page title after an AJAX error occurred on the "send direct message" page.
180         $this->element('title', null, _('Ajax Error'));
181         $this->elementEnd('head');
182         $this->elementStart('body');
183         $this->element('p', array('id' => 'error'), $msg);
184         $this->elementEnd('body');
185         $this->endHTML();
186     }
187
188     function showForm($msg = null)
189     {
190         if ($msg && $this->boolean('ajax')) {
191             $this->ajaxErrorMsg($msg);
192             return;
193         }
194
195         $this->msg = $msg;
196         if ($this->trimmed('ajax')) {
197             $this->startHTML('text/xml;charset=utf-8');
198             $this->elementStart('head');
199             // TRANS: Page title on page for sending a direct message.
200             $this->element('title', null, _('New message'));
201             $this->elementEnd('head');
202             $this->elementStart('body');
203             $this->showNoticeForm();
204             $this->elementEnd('body');
205             $this->endHTML();
206         }
207         else {
208             $this->showPage();
209         }
210     }
211
212     function showPageNotice()
213     {
214         if ($this->msg) {
215             $this->element('p', 'error', $this->msg);
216         }
217     }
218
219     // Do nothing (override)
220
221     function showNoticeForm()
222     {
223         $message_form = new MessageForm($this, $this->other, $this->content);
224         $message_form->show();
225     }
226 }