]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newmessage.php
generate an etag for shownotice
[quix0rs-gnu-social.git] / actions / newmessage.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class NewmessageAction extends Action {
23         
24         function handle($args) {
25                 parent::handle($args);
26
27                 if (!common_logged_in()) {
28                         $this->client_error(_('Not logged in.'), 403);
29                 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
30                         $this->save_new_message();
31                 } else {
32                         $this->show_form();
33                 }
34         }
35
36         function save_new_message() {
37                 $user = common_current_user();
38                 assert($user); # XXX: maybe an error instead...
39
40                 # CSRF protection
41                 
42                 $token = $this->trimmed('token');
43                 if (!$token || $token != common_session_token()) {
44                         $this->show_form(_('There was a problem with your session token. Try again, please.'));
45                         return;
46                 }
47                 
48                 $content = $this->trimmed('content');
49                 $to = $this->trimmed('to');
50                 
51                 if (!$content) {
52                         $this->show_form(_('No content!'));
53                         return;
54                 } else {
55                         $content_shortened = common_shorten_links($content);
56
57                         if (mb_strlen($content_shortened) > 140) {
58                                 common_debug("Content = '$content_shortened'", __FILE__);
59                                 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
60                                 $this->show_form(_('That\'s too long. Max message size is 140 chars.'));
61                                 return;
62                         }
63                 }
64
65                 $other = User::staticGet('id', $to);
66                 
67                 if (!$other) {
68                         $this->show_form(_('No recipient specified.'));
69                         return;
70                 } else if (!$user->mutuallySubscribed($other)) {
71                         $this->client_error(_('You can\'t send a message to this user.'), 404);
72                         return;
73                 } else if ($user->id == $other->id) {
74                         $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'), 403);
75                         return;
76                 }
77                 
78                 $message = Message::saveNew($user->id, $other->id, $content, 'web');
79                 
80                 if (is_string($message)) {
81                         $this->show_form($message);
82                         return;
83                 }
84
85                 $this->notify($user, $other, $message);
86
87                 $url = common_local_url('outbox', array('nickname' => $user->nickname));
88
89                 common_redirect($url, 303);
90         }
91
92         function show_top($params) {
93
94                 list($content, $user, $to) = $params;
95                 
96                 assert(!is_null($user));
97
98                 common_message_form($content, $user, $to);
99         }
100
101         function show_form($msg=NULL) {
102                 
103                 $content = $this->trimmed('content');
104                 $user = common_current_user();
105
106                 $to = $this->trimmed('to');
107                 
108                 $other = User::staticGet('id', $to);
109
110                 if (!$other) {
111                         $this->client_error(_('No such user'), 404);
112                         return;
113                 }
114
115                 if (!$user->mutuallySubscribed($other)) {
116                         $this->client_error(_('You can\'t send a message to this user.'), 404);
117                         return;
118                 }
119                 
120                 common_show_header(_('New message'), NULL,
121                                                    array($content, $user, $other),
122                                    array($this, 'show_top'));
123                 
124                 if ($msg) {
125                         common_element('p', 'error', $msg);
126                 }
127                 
128                 common_show_footer();
129         }
130         
131         function notify($from, $to, $message) {
132                 mail_notify_message($message, $from, $to);
133                 # XXX: Jabber, SMS notifications... probably queued
134         }
135 }