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