3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009-2010, 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/>.
21 * @package FeedSubPlugin
22 * @maintainer Brion Vibber <brion@status.net>
26 PuSH subscription flow:
29 generate random verification token
31 sends a sub request to the hub...
34 hub sends confirmation back to us via GET
35 We verify the request, then echo back the challenge.
36 On our end, we save the time we subscribed and the lease expiration
39 hub sends us updates via POST
43 class FeedDBException extends FeedSubException
47 function __construct($obj)
49 parent::__construct('Database insert failure');
54 class Ostatus_profile extends Memcached_DataObject
56 public $__table = 'ostatus_profile';
65 // PuSH subscription data
69 public $sub_state; // subscribe, active, unsubscribe
78 public /*static*/ function staticGet($k, $v=null)
80 return parent::staticGet(__CLASS__, $k, $v);
84 * return table definition for DB_DataObject
86 * DB_DataObject needs to know something about the table to manipulate
87 * instances. This method provides all the DB_DataObject needs to know.
89 * @return array array of column definitions
94 return array('id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
95 'profile_id' => DB_DATAOBJECT_INT,
96 'group_id' => DB_DATAOBJECT_INT,
97 'feeduri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
98 'homeuri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
99 'huburi' => DB_DATAOBJECT_STR,
100 'secret' => DB_DATAOBJECT_STR,
101 'verify_token' => DB_DATAOBJECT_STR,
102 'sub_state' => DB_DATAOBJECT_STR,
103 'sub_start' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
104 'sub_end' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
105 'salmonuri' => DB_DATAOBJECT_STR,
106 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
107 'lastupdate' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
110 static function schemaDef()
112 return array(new ColumnDef('id', 'integer',
118 /*auto_increment*/ true),
119 new ColumnDef('profile_id', 'integer',
121 new ColumnDef('group_id', 'integer',
123 new ColumnDef('feeduri', 'varchar',
125 new ColumnDef('homeuri', 'varchar',
127 new ColumnDef('huburi', 'text',
129 new ColumnDef('verify_token', 'varchar',
131 new ColumnDef('secret', 'varchar',
133 new ColumnDef('sub_state', "enum('subscribe','active','unsubscribe')",
135 new ColumnDef('sub_start', 'datetime',
137 new ColumnDef('sub_end', 'datetime',
139 new ColumnDef('salmonuri', 'text',
141 new ColumnDef('created', 'datetime',
143 new ColumnDef('lastupdate', 'datetime',
148 * return key definitions for DB_DataObject
150 * DB_DataObject needs to know about keys that the table has; this function
153 * @return array key definitions
158 return array_keys($this->keyTypes());
162 * return key definitions for Memcached_DataObject
164 * Our caching system uses the same key definitions, but uses a different
165 * method to get them.
167 * @return array key definitions
172 return array('id' => 'K', 'profile_id' => 'U', 'group_id' => 'U', 'feeduri' => 'U');
175 function sequenceKey()
177 return array('id', true, false);
181 * Fetch the StatusNet-side profile for this feed
184 public function localProfile()
186 if ($this->profile_id) {
187 return Profile::staticGet('id', $this->profile_id);
193 * Fetch the StatusNet-side profile for this feed
196 public function localGroup()
198 if ($this->group_id) {
199 return User_group::staticGet('id', $this->group_id);
205 * @param FeedMunger $munger
206 * @param boolean $isGroup is this a group record?
207 * @return Ostatus_profile
209 public static function ensureProfile($munger)
211 $profile = $munger->ostatusProfile();
213 $current = self::staticGet('feeduri', $profile->feeduri);
215 // @fixme we should probably update info as necessary
219 $profile->query('BEGIN');
221 // Awful hack! Awful hack!
222 $profile->verify = common_good_rand(16);
223 $profile->secret = common_good_rand(32);
226 $local = $munger->profile();
228 if ($profile->isGroup()) {
229 $group = new User_group();
230 $group->nickname = $local->nickname . '@remote'; // @fixme
231 $group->fullname = $local->fullname;
232 $group->homepage = $local->homepage;
233 $group->location = $local->location;
234 $group->created = $local->created;
236 if (empty($result)) {
237 throw new FeedDBException($group);
239 $profile->group_id = $group->id;
241 $result = $local->insert();
242 if (empty($result)) {
243 throw new FeedDBException($local);
245 $profile->profile_id = $local->id;
248 $profile->created = common_sql_now();
249 $profile->lastupdate = common_sql_now();
250 $result = $profile->insert();
251 if (empty($result)) {
252 throw new FeedDBException($profile);
255 $profile->query('COMMIT');
256 } catch (FeedDBException $e) {
257 common_log_db_error($e->obj, 'INSERT', __FILE__);
258 $profile->query('ROLLBACK');
262 $avatar = $munger->getAvatar();
265 $profile->updateAvatar($avatar);
266 } catch (Exception $e) {
267 common_log(LOG_ERR, "Exception setting OStatus avatar: " .
276 * Download and update given avatar image
278 * @throws Exception in various failure cases
280 public function updateAvatar($url)
282 // @fixme this should be better encapsulated
283 // ripped from oauthstore.php (for old OMB client)
284 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
285 copy($url, $temp_filename);
287 // @fixme should we be using different ids?
288 $imagefile = new ImageFile($this->id, $temp_filename);
289 $filename = Avatar::filename($this->id,
290 image_type_to_extension($imagefile->type),
293 rename($temp_filename, Avatar::path($filename));
294 if ($this->isGroup()) {
295 $group = $this->localGroup();
296 $group->setOriginal($filename);
298 $profile = $this->localProfile();
299 $profile->setOriginal($filename);
304 * Returns an XML string fragment with profile information as an
305 * Activity Streams noun object with the given element type.
307 * Assumes that 'activity' namespace has been previously defined.
309 * @param string $element one of 'actor', 'subject', 'object', 'target'
312 function asActivityNoun($element)
314 $xs = new XMLStringer(true);
316 $avatarHref = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
317 $avatarType = 'image/png';
318 if ($this->isGroup()) {
319 $type = 'http://activitystrea.ms/schema/1.0/group';
320 $self = $this->localGroup();
322 // @fixme put a standard getAvatar() interface on groups too
323 if ($self->homepage_logo) {
324 $avatarHref = $self->homepage_logo;
325 $map = array('png' => 'image/png',
326 'jpg' => 'image/jpeg',
327 'jpeg' => 'image/jpeg',
328 'gif' => 'image/gif');
329 $extension = pathinfo(parse_url($avatarHref, PHP_URL_PATH), PATHINFO_EXTENSION);
330 if (isset($map[$extension])) {
331 $avatarType = $map[$extension];
335 $type = 'http://activitystrea.ms/schema/1.0/person';
336 $self = $this->localProfile();
337 $avatar = $self->getAvatar(AVATAR_PROFILE_SIZE);
339 $avatarHref = $avatar->
340 $avatarType = $avatar->mediatype;
343 $xs->elementStart('activity:' . $element);
345 'activity:object-type',
352 $this->homeuri); // ?
353 $xs->element('title', null, $self->getBestName());
357 'type' => $avatarType,
358 'href' => $avatarHref
363 $xs->elementEnd('activity:' . $element);
365 return $xs->getString();
373 return (strpos($this->feeduri, '/groups/') !== false);
377 * Send a subscription request to the hub for this feed.
378 * The hub will later send us a confirmation POST to /main/push/callback.
380 * @return bool true on success, false on failure
381 * @throws ServerException if feed state is not valid
383 public function subscribe($mode='subscribe')
385 if ($this->sub_state != '') {
386 throw new ServerException("Attempting to start PuSH subscription to feed in state $this->sub_state");
388 if (empty($this->huburi)) {
389 if (common_config('feedsub', 'nohub')) {
390 // Fake it! We're just testing remote feeds w/o hubs.
393 throw new ServerException("Attempting to start PuSH subscription for feed with no hub");
397 return $this->doSubscribe('subscribe');
401 * Send a PuSH unsubscription request to the hub for this feed.
402 * The hub will later send us a confirmation POST to /main/push/callback.
404 * @return bool true on success, false on failure
405 * @throws ServerException if feed state is not valid
407 public function unsubscribe() {
408 if ($this->sub_state != 'active') {
409 throw new ServerException("Attempting to end PuSH subscription to feed in state $this->sub_state");
411 if (empty($this->huburi)) {
412 if (common_config('feedsub', 'nohub')) {
413 // Fake it! We're just testing remote feeds w/o hubs.
416 throw new ServerException("Attempting to end PuSH subscription for feed with no hub");
420 return $this->doSubscribe('unsubscribe');
423 protected function doSubscribe($mode)
425 $orig = clone($this);
426 $this->verify_token = md5(mt_rand() . ':' . $this->feeduri);
427 $this->sub_state = $mode;
428 $this->update($orig);
432 $callback = common_local_url('pushcallback', array('feed' => $this->id));
433 $headers = array('Content-Type: application/x-www-form-urlencoded');
434 $post = array('hub.mode' => $mode,
435 'hub.callback' => $callback,
436 'hub.verify' => 'async',
437 'hub.verify_token' => $this->verify_token,
438 'hub.secret' => $this->secret,
439 //'hub.lease_seconds' => 0,
440 'hub.topic' => $this->feeduri);
441 $client = new HTTPClient();
442 $response = $client->post($this->huburi, $headers, $post);
443 $status = $response->getStatus();
444 if ($status == 202) {
445 common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
447 } else if ($status == 204) {
448 common_log(LOG_INFO, __METHOD__ . ': sub req ok and verified');
450 } else if ($status >= 200 && $status < 300) {
451 common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
454 common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
457 } catch (Exception $e) {
459 common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to $this->feeduri");
461 $orig = clone($this);
462 $this->verify_token = null;
463 $this->sub_state = null;
464 $this->update($orig);
472 * Save PuSH subscription confirmation.
473 * Sets approximate lease start and end times and finalizes state.
475 * @param int $lease_seconds provided hub.lease_seconds parameter, if given
477 public function confirmSubscribe($lease_seconds=0)
479 $original = clone($this);
481 $this->sub_state = 'active';
482 $this->sub_start = common_sql_date(time());
483 if ($lease_seconds > 0) {
484 $this->sub_end = common_sql_date(time() + $lease_seconds);
486 $this->sub_end = null;
488 $this->lastupdate = common_sql_date();
490 return $this->update($original);
494 * Save PuSH unsubscription confirmation.
495 * Wipes active PuSH sub info and resets state.
497 public function confirmUnsubscribe()
499 $original = clone($this);
501 $this->verify_token = null;
502 $this->secret = null;
503 $this->sub_state = null;
504 $this->sub_start = null;
505 $this->sub_end = null;
506 $this->lastupdate = common_sql_date();
508 return $this->update($original);
512 * Send an Activity Streams notification to the remote Salmon endpoint,
515 * @param Profile $actor
516 * @param $verb eg Activity::SUBSCRIBE or Activity::JOIN
517 * @param $object object of the action; if null, the remote entity itself is assumed
519 public function notify(Profile $actor, $verb, $object=null)
521 if ($object == null) {
524 if ($this->salmonuri) {
525 $text = 'update'; // @fixme
526 $id = 'tag:' . common_config('site', 'server') .
529 ':' . time(); // @fixme
531 $entry = new Atom10Entry();
532 $entry->elementStart('entry');
533 $entry->element('id', null, $id);
534 $entry->element('title', null, $text);
535 $entry->element('summary', null, $text);
536 $entry->element('published', null, common_date_w3dtf());
538 $entry->element('activity:verb', null, $verb);
539 $entry->raw($profile->asAtomAuthor());
540 $entry->raw($profile->asActivityActor());
541 $entry->raw($object->asActivityNoun('object'));
542 $entry->elmentEnd('entry');
544 $feed = $this->atomFeed($actor);
546 $feed->addEntry($entry);
547 $feed->renderEntries();
550 $xml = $feed->getString();
551 common_log(LOG_INFO, "Posting to Salmon endpoint $salmon: $xml");
553 $salmon = new Salmon(); // ?
554 $salmon->post($this->salmonuri, $xml);
558 function getBestName()
560 if ($this->isGroup()) {
561 return $this->localGroup()->getBestName();
563 return $this->localProfile()->getBestName();
567 function atomFeed($actor)
569 $feed = new Atom10Feed();
570 // @fixme should these be set up somewhere else?
571 $feed->addNamespace('activity', 'http://activitystrea.ms/spec/1.0/');
572 $feed->addNamesapce('thr', 'http://purl.org/syndication/thread/1.0');
573 $feed->addNamespace('georss', 'http://www.georss.org/georss');
574 $feed->addNamespace('ostatus', 'http://ostatus.org/schema/1.0');
576 $taguribase = common_config('integration', 'taguri');
577 $feed->setId("tag:{$taguribase}:UserTimeline:{$actor->id}"); // ???
579 $feed->setTitle($actor->getBestName() . ' timeline'); // @fixme
580 $feed->setUpdated(time());
581 $feed->setPublished(time());
583 $feed->addLink(common_url('ApiTimelineUser',
584 array('id' => $actor->id,
586 array('rel' => 'self',
587 'type' => 'application/atom+xml'));
589 $feed->addLink(common_url('userbyid',
590 array('id' => $actor->id)),
591 array('rel' => 'alternate',
592 'type' => 'text/html'));
598 * Read and post notices for updates from the feed.
599 * Currently assumes that all items in the feed are new,
600 * coming from a PuSH hub.
602 * @param string $xml source of Atom or RSS feed
603 * @param string $hmac X-Hub-Signature header, if present
605 public function postUpdates($xml, $hmac)
607 common_log(LOG_INFO, __METHOD__ . ": packet for \"$this->feeduri\"! $hmac $xml");
609 if ($this->sub_state != 'active') {
610 common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH for inactive feed $this->feeduri (in state '$this->sub_state')");
615 if (preg_match('/^sha1=([0-9a-fA-F]{40})$/', $hmac, $matches)) {
616 $their_hmac = strtolower($matches[1]);
617 $our_hmac = hash_hmac('sha1', $xml, $this->secret);
618 if ($their_hmac !== $our_hmac) {
619 common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bad SHA-1 HMAC: got $their_hmac, expected $our_hmac");
623 common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bogus HMAC '$hmac'");
627 common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with unexpected HMAC '$hmac'");
631 require_once "XML/Feed/Parser.php";
632 $feed = new XML_Feed_Parser($xml, false, false, true);
633 $munger = new FeedMunger($feed);
636 foreach ($feed as $index => $entry) {
637 // @fixme this might sort in wrong order if we get multiple updates
639 $notice = $munger->notice($index);
641 // Double-check for oldies
642 // @fixme this could explode horribly for multiple feeds on a blog. sigh
644 $dupe = Notice::staticGet('uri', $notice->uri);
647 common_log(LOG_WARNING, __METHOD__ . ": tried to save dupe notice for entry {$notice->uri} of feed {$this->feeduri}");
651 // @fixme need to ensure that groups get handled correctly
652 $saved = Notice::saveNew($notice->profile_id,
655 array('is_local' => Notice::REMOTE_OMB,
656 'uri' => $notice->uri,
657 'lat' => $notice->lat,
658 'lon' => $notice->lon,
659 'location_ns' => $notice->location_ns,
660 'location_id' => $notice->location_id));
663 common_log(LOG_DEBUG, "going to check group delivery...");
664 if ($this->group_id) {
665 $group = User_group::staticGet($this->group_id);
667 common_log(LOG_INFO, __METHOD__ . ": saving to local shadow group $group->id $group->nickname");
668 $groups = array($group);
670 common_log(LOG_INFO, __METHOD__ . ": lost the local shadow group?");
673 common_log(LOG_INFO, __METHOD__ . ": no local shadow groups");
676 common_log(LOG_DEBUG, "going to add to inboxes...");
677 $notice->addToInboxes($groups, array());
678 common_log(LOG_DEBUG, "added to inboxes.");
684 common_log(LOG_INFO, __METHOD__ . ": no updates in packet for \"$this->feeduri\"! $xml");