]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/postnotice.php
3b5a4ee1536264ad24da69956fd0128838d27187
[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 function is_readonly() {
25         return false;
26 }
27
28 class PostnoticeAction extends Action {
29         function handle($args) {
30                 parent::handle($args);
31                 try {
32                         $req = OAuthRequest::from_request();
33                         # Note: server-to-server function!
34                         $server = omb_oauth_server();
35                         list($consumer, $token) = $server->verify_request($req);
36                         if ($this->save_notice($req, $consumer, $token)) {
37                                 print "omb_version=".OMB_VERSION_01;
38                         }
39                 } catch (OAuthException $e) {
40                         common_server_error($e->getMessage());
41                         return;
42                 }
43         }
44
45         function save_notice(&$req, &$consumer, &$token) {
46                 $version = $req->get_parameter('omb_version');
47                 if ($version != OMB_VERSION_01) {
48                         common_user_error(_('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                         common_user_error(_('Profile unknown'), 403);
56                         return false;
57                 }
58                 $sub = Subscription::staticGet('token', $token->key);
59                 if (!$sub) {
60                         common_user_error(_('No such subscription'), 403);
61                         return false;
62                 }
63                 $content = $req->get_parameter('omb_notice_content');
64                 if (!$content || strlen($content) > 140) {
65                         common_user_error(_('Invalid notice content'), 400);
66                         return false;
67                 }
68                 $notice_uri = $req->get_parameter('omb_notice');
69                 if (!Validate::uri($notice_uri) &&
70                         !common_valid_tag($notice_uri)) {
71                         common_user_error(_('Invalid notice uri'), 400);
72                         return false;
73                 }
74                 $notice_url = $req->get_parameter('omb_notice_url');
75                 if ($notice_url && !common_valid_http_url($notice_url)) {
76                         common_user_error(_('Invalid notice url'), 400);
77                         return false;
78                 }
79                 $notice = Notice::staticGet('uri', $notice_uri);
80                 if (!$notice) {
81                         $notice = new Notice();
82                         $notice->is_local = 0;
83                         $notice->profile_id = $remote_profile->id;
84                         $notice->uri = $notice_uri;
85                         $notice->content = $content;
86                         $notice->rendered = common_render_content($notice->content, $notice);
87                         if ($notice_url) {
88                                 $notice->url = $notice_url;
89                         }
90                         $notice->created = DB_DataObject_Cast::dateTime(); # current time
91                         $id = $notice->insert();
92                         if (!$id) {
93                                 common_server_error(_('Error inserting notice'), 500);
94                                 return false;
95                         }
96                         common_save_replies($notice);   
97                         common_broadcast_notice($notice, true);
98                 }
99                 return true;
100         }
101 }