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 ($entity->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 = sql_common_date();
249 $profile->lastupdate = sql_common_date();
250 $result = $profile->insert();
251 if (empty($result)) {
252 throw new FeedDBException($profile);
255 $entity->query('COMMIT');
256 } catch (FeedDBException $e) {
257 common_log_db_error($e->obj, 'INSERT', __FILE__);
258 $entity->query('ROLLBACK');
262 $avatar = $munger->getAvatar();
265 $this->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);
286 $imagefile = new ImageFile($profile->id, $temp_filename);
287 $filename = Avatar::filename($profile->id,
288 image_type_to_extension($imagefile->type),
291 rename($temp_filename, Avatar::path($filename));
292 if ($this->isGroup()) {
293 $group = $this->localGroup();
294 $group->setOriginal($filename);
296 $profile = $this->localProfile();
297 $profile->setOriginal($filename);
302 * Returns an XML string fragment with profile information as an
303 * Activity Streams noun object with the given element type.
305 * Assumes that 'activity' namespace has been previously defined.
307 * @param string $element one of 'actor', 'subject', 'object', 'target'
310 function asActivityNoun($element)
312 $xs = new XMLStringer(true);
314 $avatarHref = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
315 $avatarType = 'image/png';
316 if ($this->isGroup()) {
317 $type = 'http://activitystrea.ms/schema/1.0/group';
318 $self = $this->localGroup();
320 // @fixme put a standard getAvatar() interface on groups too
321 if ($self->homepage_logo) {
322 $avatarHref = $self->homepage_logo;
323 $map = array('png' => 'image/png',
324 'jpg' => 'image/jpeg',
325 'jpeg' => 'image/jpeg',
326 'gif' => 'image/gif');
327 $extension = pathinfo(parse_url($avatarHref, PHP_URL_PATH), PATHINFO_EXTENSION);
328 if (isset($map[$extension])) {
329 $avatarType = $map[$extension];
333 $type = 'http://activitystrea.ms/schema/1.0/person';
334 $self = $this->localProfile();
335 $avatar = $self->getAvatar(AVATAR_PROFILE_SIZE);
337 $avatarHref = $avatar->
338 $avatarType = $avatar->mediatype;
341 $xs->elementStart('activity:' . $element);
343 'activity:object-type',
350 $this->homeuri); // ?
351 $xs->element('title', null, $self->getBestName());
355 'type' => $avatarType,
356 'href' => $avatarHref
361 $xs->elementEnd('activity:' . $element);
363 return $xs->getString();
371 return (strpos($this->feeduri, '/groups/') !== false);
375 * Send a subscription request to the hub for this feed.
376 * The hub will later send us a confirmation POST to /main/push/callback.
378 * @return bool true on success, false on failure
380 public function subscribe($mode='subscribe')
382 if (common_config('feedsub', 'nohub')) {
383 // Fake it! We're just testing remote feeds w/o hubs.
386 // @fixme use the verification token
387 #$token = md5(mt_rand() . ':' . $this->feeduri);
388 #$this->verify_token = $token;
389 #$this->update(); // @fixme
391 $callback = common_local_url('pushcallback', array('feed' => $this->id));
392 $headers = array('Content-Type: application/x-www-form-urlencoded');
393 $post = array('hub.mode' => $mode,
394 'hub.callback' => $callback,
395 'hub.verify' => 'async',
396 'hub.verify_token' => $this->verify_token,
397 'hub.secret' => $this->secret,
398 //'hub.lease_seconds' => 0,
399 'hub.topic' => $this->feeduri);
400 $client = new HTTPClient();
401 $response = $client->post($this->huburi, $headers, $post);
402 $status = $response->getStatus();
403 if ($status == 202) {
404 common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
406 } else if ($status == 204) {
407 common_log(LOG_INFO, __METHOD__ . ': sub req ok and verified');
409 } else if ($status >= 200 && $status < 300) {
410 common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
413 common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
416 } catch (Exception $e) {
418 common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to $this->feeduri");
424 * Save PuSH subscription confirmation.
425 * Sets approximate lease start and end times and finalizes state.
427 * @param int $lease_seconds provided hub.lease_seconds parameter, if given
429 public function confirmSubscribe($lease_seconds=0)
431 $original = clone($this);
433 $this->sub_state = 'active';
434 $this->sub_start = common_sql_date(time());
435 if ($lease_seconds > 0) {
436 $this->sub_end = common_sql_date(time() + $lease_seconds);
438 $this->sub_end = null;
440 $this->lastupdate = common_sql_date();
442 return $this->update($original);
446 * Save PuSH unsubscription confirmation.
447 * Wipes active PuSH sub info and resets state.
449 public function confirmUnsubscribe()
451 $original = clone($this);
453 $this->verify_token = null;
454 $this->secret = null;
455 $this->sub_state = null;
456 $this->sub_start = null;
457 $this->sub_end = null;
458 $this->lastupdate = common_sql_date();
460 return $this->update($original);
464 * Send a PuSH unsubscription request to the hub for this feed.
465 * The hub will later send us a confirmation POST to /main/push/callback.
467 * @return bool true on success, false on failure
469 public function unsubscribe() {
470 return $this->subscribe('unsubscribe');
474 * Send an Activity Streams notification to the remote Salmon endpoint,
477 * @param Profile $actor
478 * @param $verb eg Activity::SUBSCRIBE or Activity::JOIN
479 * @param $object object of the action; if null, the remote entity itself is assumed
481 public function notify(Profile $actor, $verb, $object=null)
483 if ($object == null) {
486 if ($this->salmonuri) {
487 $text = 'update'; // @fixme
488 $id = 'tag:' . common_config('site', 'server') .
491 ':' . time(); // @fixme
493 $entry = new Atom10Entry();
494 $entry->elementStart('entry');
495 $entry->element('id', null, $id);
496 $entry->element('title', null, $text);
497 $entry->element('summary', null, $text);
498 $entry->element('published', null, common_date_w3dtf());
500 $entry->element('activity:verb', null, $verb);
501 $entry->raw($profile->asAtomAuthor());
502 $entry->raw($profile->asActivityActor());
503 $entry->raw($object->asActivityNoun('object'));
504 $entry->elmentEnd('entry');
506 $feed = $this->atomFeed($actor);
508 $feed->addEntry($entry);
509 $feed->renderEntries();
512 $xml = $feed->getString();
513 common_log(LOG_INFO, "Posting to Salmon endpoint $salmon: $xml");
515 $salmon = new Salmon(); // ?
516 $salmon->post($this->salmonuri, $xml);
520 function getBestName()
522 if ($this->isGroup()) {
523 return $this->localGroup()->getBestName();
525 return $this->localProfile()->getBestName();
529 function atomFeed($actor)
531 $feed = new Atom10Feed();
532 // @fixme should these be set up somewhere else?
533 $feed->addNamespace('activity', 'http://activitystrea.ms/spec/1.0/');
534 $feed->addNamesapce('thr', 'http://purl.org/syndication/thread/1.0');
535 $feed->addNamespace('georss', 'http://www.georss.org/georss');
536 $feed->addNamespace('ostatus', 'http://ostatus.org/schema/1.0');
538 $taguribase = common_config('integration', 'taguri');
539 $feed->setId("tag:{$taguribase}:UserTimeline:{$actor->id}"); // ???
541 $feed->setTitle($actor->getBestName() . ' timeline'); // @fixme
542 $feed->setUpdated(time());
543 $feed->setPublished(time());
545 $feed->addLink(common_url('ApiTimelineUser',
546 array('id' => $actor->id,
548 array('rel' => 'self',
549 'type' => 'application/atom+xml'));
551 $feed->addLink(common_url('userbyid',
552 array('id' => $actor->id)),
553 array('rel' => 'alternate',
554 'type' => 'text/html'));
560 * Read and post notices for updates from the feed.
561 * Currently assumes that all items in the feed are new,
562 * coming from a PuSH hub.
564 * @param string $xml source of Atom or RSS feed
565 * @param string $hmac X-Hub-Signature header, if present
567 public function postUpdates($xml, $hmac)
569 common_log(LOG_INFO, __METHOD__ . ": packet for \"$this->feeduri\"! $hmac $xml");
572 if (preg_match('/^sha1=([0-9a-fA-F]{40})$/', $hmac, $matches)) {
573 $their_hmac = strtolower($matches[1]);
574 $our_hmac = hash_hmac('sha1', $xml, $this->secret);
575 if ($their_hmac !== $our_hmac) {
576 common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bad SHA-1 HMAC: got $their_hmac, expected $our_hmac");
580 common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bogus HMAC '$hmac'");
584 common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with unexpected HMAC '$hmac'");
588 require_once "XML/Feed/Parser.php";
589 $feed = new XML_Feed_Parser($xml, false, false, true);
590 $munger = new FeedMunger($feed);
593 foreach ($feed as $index => $entry) {
594 // @fixme this might sort in wrong order if we get multiple updates
596 $notice = $munger->notice($index);
598 // Double-check for oldies
599 // @fixme this could explode horribly for multiple feeds on a blog. sigh
601 $dupe = Notice::staticGet('uri', $notice->uri);
604 common_log(LOG_WARNING, __METHOD__ . ": tried to save dupe notice for entry {$notice->uri} of feed {$this->feeduri}");
608 // @fixme need to ensure that groups get handled correctly
609 $saved = Notice::saveNew($notice->profile_id,
612 array('is_local' => Notice::REMOTE_OMB,
613 'uri' => $notice->uri,
614 'lat' => $notice->lat,
615 'lon' => $notice->lon,
616 'location_ns' => $notice->location_ns,
617 'location_id' => $notice->location_id));
620 common_log(LOG_DEBUG, "going to check group delivery...");
621 if ($this->group_id) {
622 $group = User_group::staticGet($this->group_id);
624 common_log(LOG_INFO, __METHOD__ . ": saving to local shadow group $group->id $group->nickname");
625 $groups = array($group);
627 common_log(LOG_INFO, __METHOD__ . ": lost the local shadow group?");
630 common_log(LOG_INFO, __METHOD__ . ": no local shadow groups");
633 common_log(LOG_DEBUG, "going to add to inboxes...");
634 $notice->addToInboxes($groups, array());
635 common_log(LOG_DEBUG, "added to inboxes.");
641 common_log(LOG_INFO, __METHOD__ . ": no updates in packet for \"$this->feeduri\"! $xml");