]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/postnotice.php
883cd53fbc8c83f0867ee8d97779e7979330f350
[quix0rs-gnu-social.git] / actions / postnotice.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/omb.php');
23
24 class PostnoticeAction extends Action
25 {
26     function handle($args)
27     {
28         parent::handle($args);
29         try {
30             common_remove_magic_from_request();
31             $req = OAuthRequest::from_request('POST', common_local_url('postnotice'));
32             # Note: server-to-server function!
33             $server = omb_oauth_server();
34             list($consumer, $token) = $server->verify_request($req);
35             if ($this->save_notice($req, $consumer, $token)) {
36                 print "omb_version=".OMB_VERSION_01;
37             }
38         } catch (OAuthException $e) {
39             $this->serverError($e->getMessage());
40             return;
41         }
42     }
43
44     function save_notice(&$req, &$consumer, &$token)
45     {
46         $version = $req->get_parameter('omb_version');
47         if ($version != OMB_VERSION_01) {
48             $this->clientError(_('Unsupported OMB version'), 400);
49             return false;
50         }
51         # First, check to see
52         $listenee =  $req->get_parameter('omb_listenee');
53         $remote_profile = Remote_profile::staticGet('uri', $listenee);
54         if (!$remote_profile) {
55             $this->clientError(_('Profile unknown'), 403);
56             return false;
57         }
58         $sub = Subscription::staticGet('token', $token->key);
59         if (!$sub) {
60             $this->clientError(_('No such subscription'), 403);
61             return false;
62         }
63         $content = $req->get_parameter('omb_notice_content');
64         $content_shortened = common_shorten_links($content);
65         if (mb_strlen($content_shortened) > 140) {
66             $this->clientError(_('Invalid notice content'), 400);
67             return false;
68         }
69         $notice_uri = $req->get_parameter('omb_notice');
70         if (!Validate::uri($notice_uri) &&
71             !common_valid_tag($notice_uri)) {
72             $this->clientError(_('Invalid notice uri'), 400);
73             return false;
74         }
75         $notice_url = $req->get_parameter('omb_notice_url');
76         if ($notice_url && !common_valid_http_url($notice_url)) {
77             $this->clientError(_('Invalid notice url'), 400);
78             return false;
79         }
80         $notice = Notice::staticGet('uri', $notice_uri);
81         if (!$notice) {
82             $notice = Notice::saveNew($remote_profile->id, $content, 'omb', false, null, $notice_uri);
83             if (is_string($notice)) {
84                 common_server_serror($notice, 500);
85                 return false;
86             }
87             common_broadcast_notice($notice, true);
88         }
89         return true;
90     }
91 }