]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/omb.php
move notice-posting stuff from util.php to omb.php
[quix0rs-gnu-social.git] / lib / omb.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('OAuth.php');
23 require_once(INSTALLDIR.'/lib/oauthstore.php');
24
25 require_once(INSTALLDIR.'/classes/Consumer.php');
26 require_once(INSTALLDIR.'/classes/Nonce.php');
27 require_once(INSTALLDIR.'/classes/Token.php');
28
29 define('OAUTH_NAMESPACE', 'http://oauth.net/core/1.0/');
30 define('OMB_NAMESPACE', 'http://openmicroblogging.org/protocol/0.1');
31 define('OMB_VERSION_01', 'http://openmicroblogging.org/protocol/0.1');
32 define('OAUTH_DISCOVERY', 'http://oauth.net/discovery/1.0');
33
34 define('OMB_ENDPOINT_UPDATEPROFILE', OMB_NAMESPACE.'/updateProfile');
35 define('OMB_ENDPOINT_POSTNOTICE', OMB_NAMESPACE.'/postNotice');
36 define('OAUTH_ENDPOINT_REQUEST', OAUTH_NAMESPACE.'endpoint/request');
37 define('OAUTH_ENDPOINT_AUTHORIZE', OAUTH_NAMESPACE.'endpoint/authorize');
38 define('OAUTH_ENDPOINT_ACCESS', OAUTH_NAMESPACE.'endpoint/access');
39 define('OAUTH_ENDPOINT_RESOURCE', OAUTH_NAMESPACE.'endpoint/resource');
40 define('OAUTH_AUTH_HEADER', OAUTH_NAMESPACE.'parameters/auth-header');
41 define('OAUTH_POST_BODY', OAUTH_NAMESPACE.'parameters/post-body');
42 define('OAUTH_HMAC_SHA1', OAUTH_NAMESPACE.'signature/HMAC-SHA1');
43            
44 function omb_oauth_consumer() {
45         static $con = NULL;
46         if (!$con) {
47                 $con = new OAuthConsumer(common_root_url(), '');
48         }
49         return $con;
50 }
51
52 function omb_oauth_server() {
53         static $server = null;
54         if (!$server) {
55                 $server = new OAuthServer(omb_oauth_datastore());
56                 $server->add_signature_method(omb_hmac_sha1());
57         }
58         return $server;
59 }
60
61 function omb_oauth_datastore() {
62         static $store = NULL;
63         if (!$store) {
64                 $store = new LaconicaOAuthDataStore();
65         }
66         return $store;
67 }
68
69 function omb_hmac_sha1() {
70         static $hmac_method = NULL;
71         if (!$hmac_method) {
72                 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
73         }
74         return $hmac_method;
75 }
76
77 function omb_get_services($xrd, $type) {
78         return $xrd->services(array(omb_service_filter($type)));
79 }
80
81 function omb_service_filter($type) {
82         return create_function('$s', 
83                                                    'return omb_match_service($s, \''.$type.'\');');
84 }
85         
86 function omb_match_service($service, $type) {
87         return in_array($type, $service->getTypes());
88 }
89
90 function omb_service_uri($service) {
91         if (!$service) {
92                 return NULL;
93         }
94         $uris = $service->getURIs();
95         if (!$uris) {
96                 return NULL;
97         }
98         return $uris[0];
99 }
100
101 function omb_local_id($service) {
102         if (!$service) {
103                 return NULL;
104         }
105         $els = $service->getElements('xrd:LocalID');
106         if (!$els) {
107                 return NULL;
108         }
109         $el = $els[0];
110         return $service->parser->content($el);
111 }
112         
113 function omb_broadcast_remote_subscribers($notice) {
114         # First, get remote users subscribed to this profile
115         $sub = new Subscription();
116         $sub->subscribed = $notice->profile_id;
117         $rp = new Remote_profile();
118         $sub->addJoin($rp, 'INNER', NULL, 'subscriber');
119         if ($sub->find()) {
120                 $posted = array();
121                 while ($sub->fetch()) {
122                         if (!$posted[$rp->postnoticeurl]) {
123                                 if (omb_post_notice($notice, $rp, $sub)) {
124                                         $posted[$rp->postnoticeurl] = TRUE;
125                                 }
126                         }
127                 }
128         }
129 }
130
131 function omb_post_notice($notice, $remote_profile, $subscription) {
132         global $config; # for license URL
133         $user = User::staticGet('id', $notice->profile_id);
134         $con = omb_oauth_consumer();
135         $token = new OAuthToken($subscription->token, $subscription->secret);
136         $url = $remote_profile->postnoticeurl;
137         $parsed = parse_url($url);
138         $params = array();
139         parse_str($parsed['query'], $params);
140         $req = OAuthRequest::from_consumer_and_token($con, $token,
141                                                                                                  "POST", $url, $params);
142         $req->set_parameter('omb_version', OMB_VERSION_01);
143         $req->set_parameter('omb_listenee', $user->uri);
144         $req->set_parameter('omb_notice', $notice->uri);
145         $req->set_parameter('omb_notice_content', $notice->content);
146         $req->set_parameter('omb_notice_url', common_local_url('shownotice',
147                                                                                                                    array('notice' =>
148                                                                                                                                  $notice->id)));
149         $req->set_parameter('omb_notice_license', $config['license']['url']);
150         $req->sign_request(omb_hmac_sha1(), $con, $tok);
151
152         # We re-use this tool's fetcher, since it's pretty good
153
154         $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
155
156         $result = $fetcher->post($req->get_normalized_http_url(),
157                                                          $req->to_postdata());
158
159         if ($result->status == 403) { # not authorized, don't send again
160                 $subscription->delete();
161                 return false;
162         } else if ($result->status != 200) {
163                 return false;
164         } else { # success!
165                 parse_str($result->body, $return);
166                 if ($return['omb_version'] == OMB_VERSION_01) {
167                         return true;
168                 } else {
169                         return false;
170                 }
171         }
172 }