]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newmessage.php
5aa3633234f3a74b1bde58f5e9f96e5c68da9818
[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
38                 $user = common_current_user();
39                 assert($user); # XXX: maybe an error instead...
40                 
41                 $content = $this->trimmed('content');
42                 $to = $this->trimmed('to');
43                 
44                 if (!$content) {
45                         $this->show_form(_('No content!'));
46                         return;
47                 } else if (mb_strlen($content) > 140) {
48                         common_debug("Content = '$content'", __FILE__);
49                         common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
50                         $this->show_form(_('That\'s too long. Max message size is 140 chars.'));
51                         return;
52                 }
53
54                 $other = User::staticGet('id', $to);
55                 
56                 if (!$other) {
57                         $this->show_form(_('No recipient specified.'));
58                         return;
59                 } else if (!$user->mutuallySubscribed($other)) {
60                         $this->client_error(_('You can\'t send a message to this user.'), 404);
61                         return;
62                 } else if ($user->id == $other->id) {
63                         $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'), 403);
64                         return;
65                 }
66                 
67                 $message = Message::saveNew($user->id, $other->id, $content, 'web');
68                 
69                 if (is_string($message)) {
70                         $this->show_form($message);
71                         return;
72                 }
73
74                 $this->notify($user, $to, $message);
75
76                 $url = common_local_url('showmessage',
77                                                                 array('message' => $message->id));
78
79                 common_redirect($url, 303);
80         }
81
82         function show_top($params) {
83
84                 list($content, $user, $to) = $params;
85                 
86                 assert(!is_null($user));
87                 
88                 common_element_start('form', array('id' => 'message_form',
89                                                                                    'method' => 'post',
90                                                                                    'action' => $this->self_url()));
91                 
92                 $mutual_users = $user->mutuallySubscribedUsers();
93                 
94                 $mutual = array();
95                 
96                 while ($mutual_users->fetch()) {
97                         if ($mutual_users->id != $user->id) {
98                                 $mutual[$mutual_users->id] = $mutual_users->nickname;
99                         }
100                 }
101
102                 $mutual_users->free();
103                 unset($mutual_users);
104
105                 common_dropdown('to', _('To'), $mutual, NULL, FALSE, $to->id);
106                 
107                 common_element_start('p');
108                 
109                 common_element('textarea', array('id' => 'message_content',
110                                                                                  'cols' => 60,
111                                                                                  'rows' => 3,
112                                                                                  'name' => 'content'),
113                                            ($content) ? $content : '');
114                                                 
115                 common_element('input', array('id' => 'message_send',
116                                                                           'name' => 'message_send',
117                                                                           'type' => 'submit',
118                                                                           'value' => _('Send')));
119                 
120                 common_element_end('p');
121                 common_element_end('form');
122         }
123
124         function show_form($msg=NULL) {
125                 
126                 $content = $this->trimmed('content');
127                 $user = common_current_user();
128
129                 $to = $this->trimmed('to');
130                 
131                 $other = User::staticGet('id', $to);
132
133                 if (!$other) {
134                         $this->client_error(_('No such user'), 404);
135                         return;
136                 }
137
138                 if (!$user->mutuallySubscribed($other)) {
139                         $this->client_error(_('You can\'t send a message to this user.'), 404);
140                         return;
141                 }
142                 
143                 common_show_header(_('New message'), NULL,
144                                                    array($content, $user, $other),
145                                    array($this, 'show_top'));
146                 
147                 if ($msg) {
148                         common_element('p', 'error', $msg);
149                 }
150                 
151                 common_show_footer();
152         }
153         
154         function notify($from, $to, $message) {
155                 mail_notify_message($message, $from, $to);
156                 # XXX: Jabber, SMS notifications... probably queued
157         }
158 }