3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once 'libomb/datastore.php';
24 class StatusNetOAuthDataStore extends OAuthDataStore
27 // We keep a record of who's contacted us
28 function lookup_consumer($consumer_key)
30 $con = Consumer::staticGet('consumer_key', $consumer_key);
32 $con = new Consumer();
33 $con->consumer_key = $consumer_key;
34 $con->seed = common_good_rand(16);
35 $con->created = DB_DataObject_Cast::dateTime();
36 if (!$con->insert()) {
40 return new OAuthConsumer($con->consumer_key, '');
43 function lookup_token($consumer, $token_type, $token_key)
46 if (!is_null($consumer)) {
47 $t->consumer_key = $consumer->key;
50 $t->type = ($token_type == 'access') ? 1 : 0;
52 return new OAuthToken($t->tok, $t->secret);
58 // http://oauth.net/core/1.0/#nonce
59 // "The Consumer SHALL then generate a Nonce value that is unique for
60 // all requests with that timestamp."
62 // XXX: It's not clear why the token is here
64 function lookup_nonce($consumer, $token, $nonce, $timestamp)
67 $n->consumer_key = $consumer->key;
73 $n->created = DB_DataObject_Cast::dateTime();
79 function new_request_token($consumer)
82 $t->consumer_key = $consumer->key;
83 $t->tok = common_good_rand(16);
84 $t->secret = common_good_rand(16);
85 $t->type = 0; // request
86 $t->state = 0; // unauthorized
87 $t->created = DB_DataObject_Cast::dateTime();
91 return new OAuthToken($t->tok, $t->secret);
95 // defined in OAuthDataStore, but not implemented anywhere
97 function fetch_request_token($consumer)
99 return $this->new_request_token($consumer);
102 function new_access_token($token, $consumer)
104 common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
106 $rt->consumer_key = $consumer->key;
107 $rt->tok = $token->key;
108 $rt->type = 0; // request
109 if ($rt->find(true) && $rt->state == 1) { // authorized
110 common_debug('request token found.', __FILE__);
112 $at->consumer_key = $consumer->key;
113 $at->tok = common_good_rand(16);
114 $at->secret = common_good_rand(16);
115 $at->type = 1; // access
116 $at->created = DB_DataObject_Cast::dateTime();
117 if (!$at->insert()) {
118 $e = $at->_lastError;
119 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
122 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
124 $orig_rt = clone($rt);
125 $rt->state = 2; // used
126 if (!$rt->update($orig_rt)) {
129 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
130 // Update subscription
131 // XXX: mixing levels here
132 $sub = Subscription::staticGet('token', $rt->tok);
136 common_debug('subscription for request token found', __FILE__);
137 $orig_sub = clone($sub);
138 $sub->token = $at->tok;
139 $sub->secret = $at->secret;
140 if (!$sub->update($orig_sub)) {
143 common_debug('subscription updated to use access token', __FILE__);
144 return new OAuthToken($at->tok, $at->secret);
152 // defined in OAuthDataStore, but not implemented anywhere
154 function fetch_access_token($consumer)
156 return $this->new_access_token($consumer);
161 * Revoke specified OAuth token
163 * Revokes the authorization token specified by $token_key.
164 * Throws exceptions in case of error.
166 * @param string $token_key The token to be revoked
170 public function revoke_token($token_key) {
172 $rt->tok = $token_key;
175 if (!$rt->find(true)) {
176 throw new Exception('Tried to revoke unknown token');
178 if (!$rt->delete()) {
179 throw new Exception('Failed to delete revoked token');
184 * Authorize specified OAuth token
186 * Authorizes the authorization token specified by $token_key.
187 * Throws exceptions in case of error.
189 * @param string $token_key The token to be authorized
193 public function authorize_token($token_key) {
195 $rt->tok = $token_key;
198 if (!$rt->find(true)) {
199 throw new Exception('Tried to authorize unknown token');
201 $orig_rt = clone($rt);
202 $rt->state = 1; # Authorized but not used
203 if (!$rt->update($orig_rt)) {
204 throw new Exception('Failed to authorize token');
209 * Get profile by identifying URI
211 * Returns an OMB_Profile object representing the OMB profile identified by
213 * Returns null if there is no such OMB profile.
214 * Throws exceptions in case of other error.
216 * @param string $identifier_uri The OMB identifier URI specifying the
221 * @return OMB_Profile The corresponding profile
223 public function getProfile($identifier_uri) {
224 /* getProfile is only used for remote profiles by libomb.
225 TODO: Make it work with local ones anyway. */
226 $remote = Remote_profile::staticGet('uri', $identifier_uri);
227 if (!$remote) throw new Exception('No such remote profile');
228 $profile = Profile::staticGet('id', $remote->id);
229 if (!$profile) throw new Exception('No profile for remote user');
231 require_once INSTALLDIR.'/lib/omb.php';
232 return profile_to_omb_profile($identifier_uri, $profile);
236 * Save passed profile
238 * Stores the OMB profile $profile. Overwrites an existing entry.
239 * Throws exceptions in case of error.
241 * @param OMB_Profile $profile The OMB profile which should be saved
245 public function saveProfile($omb_profile) {
246 if (common_profile_url($omb_profile->getNickname()) ==
247 $omb_profile->getProfileURL()) {
248 throw new Exception('Not implemented');
250 $remote = Remote_profile::staticGet('uri', $omb_profile->getIdentifierURI());
254 $profile = Profile::staticGet($remote->id);
255 $orig_remote = clone($remote);
256 $orig_profile = clone($profile);
257 # XXX: compare current postNotice and updateProfile URLs to the ones
258 # stored in the DB to avoid (possibly...) above attack
261 $remote = new Remote_profile();
262 $remote->uri = $omb_profile->getIdentifierURI();
263 $profile = new Profile();
266 $profile->nickname = $omb_profile->getNickname();
267 $profile->profileurl = $omb_profile->getProfileURL();
269 $fullname = $omb_profile->getFullname();
270 $profile->fullname = is_null($fullname) ? '' : $fullname;
271 $homepage = $omb_profile->getHomepage();
272 $profile->homepage = is_null($homepage) ? '' : $homepage;
273 $bio = $omb_profile->getBio();
274 $profile->bio = is_null($bio) ? '' : $bio;
275 $location = $omb_profile->getLocation();
276 $profile->location = is_null($location) ? '' : $location;
279 $profile->update($orig_profile);
281 $profile->created = DB_DataObject_Cast::dateTime(); # current time
282 $id = $profile->insert();
284 throw new Exception(_('Error inserting new profile'));
289 $avatar_url = $omb_profile->getAvatarURL();
291 if (!$this->add_avatar($profile, $avatar_url)) {
292 throw new Exception(_('Error inserting avatar'));
295 $avatar = $profile->getOriginalAvatar();
296 if($avatar) $avatar->delete();
297 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
298 if($avatar) $avatar->delete();
299 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
300 if($avatar) $avatar->delete();
301 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
302 if($avatar) $avatar->delete();
306 if (!$remote->update($orig_remote)) {
307 throw new Exception(_('Error updating remote profile'));
310 $remote->created = DB_DataObject_Cast::dateTime(); # current time
311 if (!$remote->insert()) {
312 throw new Exception(_('Error inserting remote profile'));
318 function add_avatar($profile, $url)
320 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
321 copy($url, $temp_filename);
322 $imagefile = new ImageFile($profile->id, $temp_filename);
323 $filename = Avatar::filename($profile->id,
324 image_type_to_extension($imagefile->type),
327 rename($temp_filename, Avatar::path($filename));
328 return $profile->setOriginal($filename);
334 * Stores the OMB notice $notice. The datastore may change the passed notice.
335 * This might by neccessary for URIs depending on a database key. Note that
336 * it is the user’s duty to present a mechanism for his OMB_Datastore to
337 * appropriately change his OMB_Notice.
338 * Throws exceptions in case of error.
340 * @param OMB_Notice $notice The OMB notice which should be saved
344 public function saveNotice(&$omb_notice) {
345 if (Notice::staticGet('uri', $omb_notice->getIdentifierURI())) {
346 throw new Exception(_('Duplicate notice'));
348 $author_uri = $omb_notice->getAuthor()->getIdentifierURI();
349 common_log(LOG_DEBUG, $author_uri, __FILE__);
350 $author = Remote_profile::staticGet('uri', $author_uri);
352 $author = User::staticGet('uri', $author_uri);
355 throw new Exception('No such user');
358 common_log(LOG_DEBUG, print_r($author, true), __FILE__);
360 $notice = Notice::saveNew($author->id,
361 $omb_notice->getContent(),
365 $omb_notice->getIdentifierURI());
366 if (is_string($notice)) {
367 throw new Exception($notice);
369 common_broadcast_notice($notice, true);
373 * Get subscriptions of a given profile
375 * Returns an array containing subscription informations for the specified
376 * profile. Every array entry should in turn be an array with keys
377 * 'uri´: The identifier URI of the subscriber
378 * 'token´: The subscribe token
379 * 'secret´: The secret token
380 * Throws exceptions in case of error.
382 * @param string $subscribed_user_uri The OMB identifier URI specifying the
387 * @return mixed An array containing the subscriptions or 0 if no
388 * subscription has been found.
390 public function getSubscriptions($subscribed_user_uri) {
391 $sub = new Subscription();
393 $user = $this->_getAnyProfile($subscribed_user_uri);
395 $sub->subscribed = $user->id;
397 if (!$sub->find(true)) {
401 /* Since we do not use OMB_Service_Provider’s action methods, there
402 is no need to actually return the subscriptions. */
406 private function _getAnyProfile($uri)
408 $user = Remote_profile::staticGet('uri', $uri);
410 $user = User::staticGet('uri', $uri);
413 throw new Exception('No such user');
419 * Delete a subscription
421 * Deletes the subscription from $subscriber_uri to $subscribed_user_uri.
422 * Throws exceptions in case of error.
424 * @param string $subscriber_uri The OMB identifier URI specifying the
425 * subscribing profile
427 * @param string $subscribed_user_uri The OMB identifier URI specifying the
432 public function deleteSubscription($subscriber_uri, $subscribed_user_uri)
434 $sub = new Subscription();
436 $subscribed = $this->_getAnyProfile($subscribed_user_uri);
437 $subscriber = $this->_getAnyProfile($subscriber_uri);
439 $sub->subscribed = $subscribed->id;
440 $sub->subscriber = $subscriber->id;
446 * Save a subscription
448 * Saves the subscription from $subscriber_uri to $subscribed_user_uri.
449 * Throws exceptions in case of error.
451 * @param string $subscriber_uri The OMB identifier URI specifying
452 * the subscribing profile
454 * @param string $subscribed_user_uri The OMB identifier URI specifying
455 * the subscribed profile
456 * @param OAuthToken $token The access token
460 public function saveSubscription($subscriber_uri, $subscribed_user_uri,
463 $sub = new Subscription();
465 $subscribed = $this->_getAnyProfile($subscribed_user_uri);
466 $subscriber = $this->_getAnyProfile($subscriber_uri);
468 $sub->subscribed = $subscribed->id;
469 $sub->subscriber = $subscriber->id;
471 $sub_exists = $sub->find(true);
474 $orig_sub = clone($sub);
476 $sub->created = DB_DataObject_Cast::dateTime();
479 $sub->token = $token->key;
480 $sub->secret = $token->secret;
483 $result = $sub->update($orig_sub);
485 $result = $sub->insert();
489 common_log_db_error($sub, ($sub_exists) ? 'UPDATE' : 'INSERT', __FILE__);
490 throw new Exception(_('Couldn\'t insert new subscription.'));
494 /* Notify user, if necessary. */
496 if ($subscribed instanceof User) {
497 mail_subscribe_notify_profile($subscribed,
498 Profile::staticGet($subscriber->id));