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