]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/postnotice.php
cf9a3f3a0f697a15201b8b40e76f4b790af259e2
[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                         $req = OAuthRequest::from_request();
29                         # Note: server-to-server function!
30                         $server = omb_oauth_server();
31                         list($consumer, $token) = $server->verify_request($req);
32                         if ($this->save_notice($req, $consumer, $token)) {
33                                 print "omb_version=".OMB_VERSION_01;
34                         }
35                 } catch (OAuthException $e) {
36                         common_server_error($e->getMessage());
37                         return;
38                 }
39         }
40         
41         function save_notice(&$req, &$consumer, &$token) {
42                 $version = $req->get_parameter('omb_version');
43                 if ($version != OMB_VERSION_01) {
44                         common_user_error(_t('Unsupported OMB version'), 400);
45                         return false;
46                 }
47                 # First, check to see
48                 $listenee =  $req->get_parameter('omb_listenee');
49                 $remote_profile = Remote_profile::staticGet('uri', $listenee);
50                 if (!$remote_profile) {
51                         common_user_error(_t('Profile unknown'), 403);
52                         return false;
53                 }
54                 $sub = Subscription::staticGet('token', $token->key);
55                 if (!$sub) {
56                         common_user_error(_t('No such subscription'), 403);
57                         return false;
58                 }
59                 $content = $req->get_parameter('omb_notice_content');
60                 if (!$content || strlen($content) > 140) {
61                         common_user_error(_t('Invalid notice content'), 400);
62                         return false;
63                 }
64                 $notice_uri = $req->get_parameter('omb_notice');
65                 if (!Validate::uri($notice_uri) &&
66                         !common_valid_tag($notice_uri)) {
67                         common_user_error(_t('Invalid notice uri'), 400);
68                         return false;
69                 }
70                 $notice_url = $req->get_parameter('omb_notice_url');
71                 if ($notice_url && !common_valid_http_url($notice_url)) {
72                         common_user_error(_t('Invalid notice url'), 400);
73                         return false;
74                 }
75                 $notice = Notice::staticGet('uri', $notice_uri);
76                 if (!$notice) {
77                         $notice = new Notice();
78                         $notice->profile_id = $remote_profile->id;
79                         $notice->uri = $notice->uri;
80                         $notice->content = $content;
81                         if ($notice_url) {
82                                 $notice->url = $notice_url;
83                         }
84                         $notice->created = DB_DataObject_Cast::dateTime(); # current time
85                         $id = $notice->insert();
86                         if (!$id) {
87                                 common_server_error(_t('Error inserting notice'), 500);
88                                 return false;
89                         }
90                         common_broadcast_notice($notice, true);
91                 }
92                 return true;
93         }
94 }