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