]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apidirectmessagenew.php
d836409ae01686d363e6577df319e2aab0211b60
[quix0rs-gnu-social.git] / actions / apidirectmessagenew.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Send a direct message via 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  * Creates a new direct message from the authenticating user to
38  * the user specified by id.
39  *
40  * @category API
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46
47 class ApiDirectMessageNewAction extends ApiAuthAction
48 {
49     var $format  = null;
50     var $source  = null;
51     var $user    = null;
52     var $other   = null;
53     var $content = null;
54
55     /**
56      * Take arguments for running
57      *
58      * @param array $args $_REQUEST args
59      *
60      * @return boolean success flag
61      *
62      */
63
64     function prepare($args)
65     {
66         parent::prepare($args);
67
68         if ($this->requiresAuth()) {
69             if ($this->checkBasicAuthUser() == false) {
70                 return;
71             }
72         }
73
74         $this->user = $this->auth_user;
75
76         if (empty($this->user)) {
77             $this->clientError(_('No such user!'), 404, $this->format);
78             return;
79         }
80
81         $this->source = $this->trimmed('source'); // Not supported by Twitter.
82
83         $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
84         if (empty($thtis->source) || in_array($this->source, $reserved_sources)) {
85             $source = 'api';
86         }
87
88         $this->content = $this->trimmed('text');
89
90         $this->user  = $this->auth_user;
91
92         $user_param  = $this->trimmed('user');
93         $user_id     = $this->arg('user_id');
94         $screen_name = $this->trimmed('screen_name');
95
96         if (isset($user_param) || isset($user_id) || isset($screen_name)) {
97             $this->other = $this->getTargetUser($user_param);
98         }
99
100         $this->format = $this->arg('format');
101
102         return true;
103     }
104
105     /**
106      * Handle the request
107      *
108      * Save the new message
109      *
110      * @param array $args $_REQUEST data (unused)
111      *
112      * @return void
113      */
114
115     function handle($args)
116     {
117         parent::handle($args);
118
119         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
120             $this->clientError(
121                 _('This method requires a POST.'),
122                 400,
123                 $this->format
124             );
125             return;
126         }
127
128         if (empty($this->content)) {
129             $this->clientError(
130                 _('No message text!'),
131                 406,
132                 $this->format
133             );
134         } else {
135             $content_shortened = common_shorten_links($this->content);
136             if (Message::contentTooLong($content_shortened)) {
137                 $this->clientError(
138                     sprintf(
139                         _('That\'s too long. Max message size is %d chars.'),
140                         Message::maxContent()
141                     ),
142                     406,
143                     $this->format
144                 );
145                 return;
146             }
147         }
148
149         if (empty($this->other)) {
150             $this->clientError(_('Recipient user not found.'), 403, $this->format);
151             return;
152         } else if (!$this->user->mutuallySubscribed($this->other)) {
153             $this->clientError(
154                 _('Can\'t send direct messages to users who aren\'t your friend.'),
155                 403,
156                 $this->format
157             );
158             return;
159         } else if ($this->user->id == $this->other->id) {
160
161             // Note: sending msgs to yourself is allowed by Twitter
162
163             $errmsg = 'Don\'t send a message to yourself; ' . 
164                    'just say it to yourself quietly instead.'
165
166             $this->clientError(_($errmsg), 403, $this->format);
167             return;
168         }
169
170         $message = Message::saveNew(
171             $this->user->id,
172             $this->other->id,
173             html_entity_decode($this->content, ENT_NOQUOTES, 'UTF-8'),
174             $this->source
175         );
176
177         if (is_string($message)) {
178             $this->serverError($message);
179             return;
180         }
181
182         mail_notify_message($message, $this->user, $this->other);
183
184         if ($this->format == 'xml') {
185             $this->showSingleXmlDirectMessage($message);
186         } elseif ($this->format == 'json') {
187             $this->showSingleJsondirectMessage($message);
188         }
189     }
190
191 }
192