2009-11-21 02:42:19 +09:00
|
|
|
<?php
|
|
|
|
/*
|
2010-02-09 04:06:03 +09:00
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2009-2010, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package FeedSubPlugin
|
|
|
|
* @maintainer Brion Vibber <brion@status.net>
|
|
|
|
*/
|
2009-11-21 02:42:19 +09:00
|
|
|
|
2010-02-09 04:06:03 +09:00
|
|
|
/*
|
|
|
|
PuSH subscription flow:
|
2009-11-21 02:42:19 +09:00
|
|
|
|
|
|
|
$feedinfo->subscribe()
|
|
|
|
generate random verification token
|
|
|
|
save to verify_token
|
|
|
|
sends a sub request to the hub...
|
|
|
|
|
|
|
|
feedsub/callback
|
|
|
|
hub sends confirmation back to us via GET
|
|
|
|
We verify the request, then echo back the challenge.
|
|
|
|
On our end, we save the time we subscribed and the lease expiration
|
|
|
|
|
|
|
|
feedsub/callback
|
|
|
|
hub sends us updates via POST
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
class FeedDBException extends FeedSubException
|
|
|
|
{
|
|
|
|
public $obj;
|
|
|
|
|
|
|
|
function __construct($obj)
|
|
|
|
{
|
|
|
|
parent::__construct('Database insert failure');
|
|
|
|
$this->obj = $obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-05 03:30:19 +09:00
|
|
|
class Feedinfo extends Memcached_DataObject
|
2009-11-21 02:42:19 +09:00
|
|
|
{
|
|
|
|
public $__table = 'feedinfo';
|
|
|
|
|
|
|
|
public $id;
|
|
|
|
public $profile_id;
|
|
|
|
|
|
|
|
public $feeduri;
|
|
|
|
public $homeuri;
|
|
|
|
public $huburi;
|
|
|
|
|
|
|
|
// PuSH subscription data
|
2010-02-09 04:06:03 +09:00
|
|
|
public $secret;
|
2009-11-21 02:42:19 +09:00
|
|
|
public $verify_token;
|
|
|
|
public $sub_start;
|
|
|
|
public $sub_end;
|
|
|
|
|
|
|
|
public $created;
|
|
|
|
public $lastupdate;
|
|
|
|
|
|
|
|
|
|
|
|
public /*static*/ function staticGet($k, $v=null)
|
|
|
|
{
|
|
|
|
return parent::staticGet(__CLASS__, $k, $v);
|
|
|
|
}
|
|
|
|
|
2010-01-05 03:30:19 +09:00
|
|
|
/**
|
|
|
|
* return table definition for DB_DataObject
|
|
|
|
*
|
|
|
|
* DB_DataObject needs to know something about the table to manipulate
|
|
|
|
* instances. This method provides all the DB_DataObject needs to know.
|
|
|
|
*
|
|
|
|
* @return array array of column definitions
|
|
|
|
*/
|
|
|
|
|
|
|
|
function table()
|
|
|
|
{
|
|
|
|
return array('id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
|
2010-02-10 11:32:52 +09:00
|
|
|
'profile_id' => DB_DATAOBJECT_INT,
|
|
|
|
'group_id' => DB_DATAOBJECT_INT,
|
2010-01-05 03:30:19 +09:00
|
|
|
'feeduri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
|
|
|
|
'homeuri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
|
|
|
|
'huburi' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
|
2010-02-09 04:06:03 +09:00
|
|
|
'secret' => DB_DATAOBJECT_STR,
|
2010-01-05 03:30:19 +09:00
|
|
|
'verify_token' => DB_DATAOBJECT_STR,
|
|
|
|
'sub_start' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
|
|
|
|
'sub_end' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
|
|
|
|
'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
|
|
|
|
'lastupdate' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function schemaDef()
|
|
|
|
{
|
|
|
|
return array(new ColumnDef('id', 'integer',
|
|
|
|
/*size*/ null,
|
|
|
|
/*nullable*/ false,
|
|
|
|
/*key*/ 'PRI',
|
|
|
|
/*default*/ '0',
|
|
|
|
/*extra*/ null,
|
|
|
|
/*auto_increment*/ true),
|
|
|
|
new ColumnDef('profile_id', 'integer',
|
2010-02-11 09:09:20 +09:00
|
|
|
null, true, 'UNI'),
|
2010-02-10 11:32:52 +09:00
|
|
|
new ColumnDef('group_id', 'integer',
|
2010-02-11 09:09:20 +09:00
|
|
|
null, true, 'UNI'),
|
2010-01-05 03:30:19 +09:00
|
|
|
new ColumnDef('feeduri', 'varchar',
|
|
|
|
255, false, 'UNI'),
|
|
|
|
new ColumnDef('homeuri', 'varchar',
|
|
|
|
255, false),
|
|
|
|
new ColumnDef('huburi', 'varchar',
|
|
|
|
255, false),
|
|
|
|
new ColumnDef('verify_token', 'varchar',
|
|
|
|
32, true),
|
2010-02-09 04:06:03 +09:00
|
|
|
new ColumnDef('secret', 'varchar',
|
|
|
|
64, true),
|
2010-01-05 03:30:19 +09:00
|
|
|
new ColumnDef('sub_start', 'datetime',
|
|
|
|
null, true),
|
|
|
|
new ColumnDef('sub_end', 'datetime',
|
|
|
|
null, true),
|
|
|
|
new ColumnDef('created', 'datetime',
|
|
|
|
null, false),
|
|
|
|
new ColumnDef('lastupdate', 'datetime',
|
|
|
|
null, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return key definitions for DB_DataObject
|
|
|
|
*
|
|
|
|
* DB_DataObject needs to know about keys that the table has; this function
|
|
|
|
* defines them.
|
|
|
|
*
|
|
|
|
* @return array key definitions
|
|
|
|
*/
|
|
|
|
|
|
|
|
function keys()
|
|
|
|
{
|
2010-02-09 04:06:03 +09:00
|
|
|
return array_keys($this->keyTypes());
|
2010-01-05 03:30:19 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return key definitions for Memcached_DataObject
|
|
|
|
*
|
|
|
|
* Our caching system uses the same key definitions, but uses a different
|
|
|
|
* method to get them.
|
|
|
|
*
|
|
|
|
* @return array key definitions
|
|
|
|
*/
|
|
|
|
|
|
|
|
function keyTypes()
|
2009-11-21 02:42:19 +09:00
|
|
|
{
|
2010-02-11 09:09:20 +09:00
|
|
|
return array('id' => 'K', 'profile_id' => 'U', 'group_id' => 'U', 'feeduri' => 'U');
|
2010-02-09 04:06:03 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function sequenceKey()
|
|
|
|
{
|
|
|
|
return array('id', true, false);
|
2009-11-21 02:42:19 +09:00
|
|
|
}
|
|
|
|
|
2010-01-05 03:30:19 +09:00
|
|
|
/**
|
|
|
|
* Fetch the StatusNet-side profile for this feed
|
|
|
|
* @return Profile
|
|
|
|
*/
|
2009-11-21 02:42:19 +09:00
|
|
|
public function getProfile()
|
|
|
|
{
|
|
|
|
return Profile::staticGet('id', $this->profile_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FeedMunger $munger
|
2010-02-10 11:32:52 +09:00
|
|
|
* @param boolean $isGroup is this a group record?
|
2009-11-21 02:42:19 +09:00
|
|
|
* @return Feedinfo
|
|
|
|
*/
|
|
|
|
public static function ensureProfile($munger)
|
|
|
|
{
|
|
|
|
$feedinfo = $munger->feedinfo();
|
|
|
|
|
|
|
|
$current = self::staticGet('feeduri', $feedinfo->feeduri);
|
|
|
|
if ($current) {
|
|
|
|
// @fixme we should probably update info as necessary
|
|
|
|
return $current;
|
|
|
|
}
|
|
|
|
|
|
|
|
$feedinfo->query('BEGIN');
|
|
|
|
|
2010-02-09 04:06:03 +09:00
|
|
|
// Awful hack! Awful hack!
|
|
|
|
$feedinfo->verify = common_good_rand(16);
|
|
|
|
$feedinfo->secret = common_good_rand(32);
|
|
|
|
|
2009-11-21 02:42:19 +09:00
|
|
|
try {
|
|
|
|
$profile = $munger->profile();
|
|
|
|
$result = $profile->insert();
|
|
|
|
if (empty($result)) {
|
|
|
|
throw new FeedDBException($profile);
|
|
|
|
}
|
|
|
|
|
2010-02-09 04:06:03 +09:00
|
|
|
$avatar = $munger->getAvatar();
|
|
|
|
if ($avatar) {
|
|
|
|
// @fixme this should be better encapsulated
|
|
|
|
// ripped from oauthstore.php (for old OMB client)
|
|
|
|
$temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
|
|
|
|
copy($avatar, $temp_filename);
|
|
|
|
$imagefile = new ImageFile($profile->id, $temp_filename);
|
|
|
|
$filename = Avatar::filename($profile->id,
|
|
|
|
image_type_to_extension($imagefile->type),
|
|
|
|
null,
|
|
|
|
common_timestamp());
|
|
|
|
rename($temp_filename, Avatar::path($filename));
|
|
|
|
$profile->setOriginal($filename);
|
|
|
|
}
|
|
|
|
|
2009-11-21 02:42:19 +09:00
|
|
|
$feedinfo->profile_id = $profile->id;
|
2010-02-10 11:32:52 +09:00
|
|
|
if ($feedinfo->isGroup()) {
|
|
|
|
$group = new User_group();
|
|
|
|
$group->nickname = $profile->nickname . '@remote'; // @fixme
|
|
|
|
$group->fullname = $profile->fullname;
|
|
|
|
$group->homepage = $profile->homepage;
|
|
|
|
$group->location = $profile->location;
|
|
|
|
$group->created = $profile->created;
|
|
|
|
$group->insert();
|
|
|
|
|
|
|
|
if ($avatar) {
|
|
|
|
$group->setOriginal($filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
$feedinfo->group_id = $group->id;
|
|
|
|
}
|
|
|
|
|
2009-11-21 02:42:19 +09:00
|
|
|
$result = $feedinfo->insert();
|
|
|
|
if (empty($result)) {
|
|
|
|
throw new FeedDBException($feedinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
$feedinfo->query('COMMIT');
|
|
|
|
} catch (FeedDBException $e) {
|
|
|
|
common_log_db_error($e->obj, 'INSERT', __FILE__);
|
|
|
|
$feedinfo->query('ROLLBACK');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $feedinfo;
|
|
|
|
}
|
|
|
|
|
2010-02-10 11:32:52 +09:00
|
|
|
/**
|
|
|
|
* Damn dirty hack!
|
|
|
|
*/
|
|
|
|
function isGroup()
|
|
|
|
{
|
|
|
|
return (strpos($this->feeduri, '/groups/') !== false);
|
|
|
|
}
|
|
|
|
|
2009-11-21 02:42:19 +09:00
|
|
|
/**
|
|
|
|
* Send a subscription request to the hub for this feed.
|
2010-02-11 09:09:20 +09:00
|
|
|
* The hub will later send us a confirmation POST to /main/push/callback.
|
2009-11-21 02:42:19 +09:00
|
|
|
*
|
|
|
|
* @return bool true on success, false on failure
|
|
|
|
*/
|
2010-02-11 09:09:20 +09:00
|
|
|
public function subscribe($mode='subscribe')
|
2009-11-21 02:42:19 +09:00
|
|
|
{
|
2010-02-09 04:06:03 +09:00
|
|
|
if (common_config('feedsub', 'nohub')) {
|
|
|
|
// Fake it! We're just testing remote feeds w/o hubs.
|
|
|
|
return true;
|
|
|
|
}
|
2009-11-21 02:42:19 +09:00
|
|
|
// @fixme use the verification token
|
|
|
|
#$token = md5(mt_rand() . ':' . $this->feeduri);
|
|
|
|
#$this->verify_token = $token;
|
|
|
|
#$this->update(); // @fixme
|
|
|
|
try {
|
2010-02-09 07:06:36 +09:00
|
|
|
$callback = common_local_url('pushcallback', array('feed' => $this->id));
|
2009-11-21 02:42:19 +09:00
|
|
|
$headers = array('Content-Type: application/x-www-form-urlencoded');
|
2010-02-11 09:09:20 +09:00
|
|
|
$post = array('hub.mode' => $mode,
|
2009-11-21 02:42:19 +09:00
|
|
|
'hub.callback' => $callback,
|
|
|
|
'hub.verify' => 'async',
|
2010-02-09 04:06:03 +09:00
|
|
|
'hub.verify_token' => $this->verify_token,
|
|
|
|
'hub.secret' => $this->secret,
|
2009-11-21 02:42:19 +09:00
|
|
|
//'hub.lease_seconds' => 0,
|
|
|
|
'hub.topic' => $this->feeduri);
|
|
|
|
$client = new HTTPClient();
|
|
|
|
$response = $client->post($this->huburi, $headers, $post);
|
2010-02-09 04:06:03 +09:00
|
|
|
$status = $response->getStatus();
|
|
|
|
if ($status == 202) {
|
|
|
|
common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
|
|
|
|
return true;
|
|
|
|
} else if ($status == 204) {
|
|
|
|
common_log(LOG_INFO, __METHOD__ . ': sub req ok and verified');
|
2009-11-21 02:42:19 +09:00
|
|
|
return true;
|
2010-02-09 04:06:03 +09:00
|
|
|
} else if ($status >= 200 && $status < 300) {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
|
|
|
|
return false;
|
2009-11-21 02:42:19 +09:00
|
|
|
} else {
|
2010-02-09 04:06:03 +09:00
|
|
|
common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
|
2009-11-21 02:42:19 +09:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// wtf!
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to $this->feeduri");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-11 09:09:20 +09:00
|
|
|
/**
|
|
|
|
* Send an unsubscription request to the hub for this feed.
|
|
|
|
* The hub will later send us a confirmation POST to /main/push/callback.
|
|
|
|
*
|
|
|
|
* @return bool true on success, false on failure
|
|
|
|
*/
|
|
|
|
public function unsubscribe() {
|
|
|
|
return $this->subscribe('unsubscribe');
|
|
|
|
}
|
|
|
|
|
2009-11-21 02:42:19 +09:00
|
|
|
/**
|
|
|
|
* Read and post notices for updates from the feed.
|
|
|
|
* Currently assumes that all items in the feed are new,
|
|
|
|
* coming from a PuSH hub.
|
|
|
|
*
|
|
|
|
* @param string $xml source of Atom or RSS feed
|
2010-02-09 04:06:03 +09:00
|
|
|
* @param string $hmac X-Hub-Signature header, if present
|
2009-11-21 02:42:19 +09:00
|
|
|
*/
|
2010-02-09 04:06:03 +09:00
|
|
|
public function postUpdates($xml, $hmac)
|
2009-11-21 02:42:19 +09:00
|
|
|
{
|
2010-02-09 04:06:03 +09:00
|
|
|
common_log(LOG_INFO, __METHOD__ . ": packet for \"$this->feeduri\"! $hmac $xml");
|
|
|
|
|
|
|
|
if ($this->secret) {
|
|
|
|
if (preg_match('/^sha1=([0-9a-fA-F]{40})$/', $hmac, $matches)) {
|
|
|
|
$their_hmac = strtolower($matches[1]);
|
2010-02-11 07:58:39 +09:00
|
|
|
$our_hmac = hash_hmac('sha1', $xml, $this->secret);
|
2010-02-09 04:06:03 +09:00
|
|
|
if ($their_hmac !== $our_hmac) {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bad SHA-1 HMAC: got $their_hmac, expected $our_hmac");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bogus HMAC '$hmac'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if ($hmac) {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with unexpected HMAC '$hmac'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-21 02:42:19 +09:00
|
|
|
require_once "XML/Feed/Parser.php";
|
|
|
|
$feed = new XML_Feed_Parser($xml, false, false, true);
|
|
|
|
$munger = new FeedMunger($feed);
|
|
|
|
|
|
|
|
$hits = 0;
|
|
|
|
foreach ($feed as $index => $entry) {
|
|
|
|
// @fixme this might sort in wrong order if we get multiple updates
|
2010-02-11 06:18:53 +09:00
|
|
|
|
2009-11-21 02:42:19 +09:00
|
|
|
$notice = $munger->notice($index);
|
2010-02-11 06:18:53 +09:00
|
|
|
|
2009-11-21 02:42:19 +09:00
|
|
|
// Double-check for oldies
|
|
|
|
// @fixme this could explode horribly for multiple feeds on a blog. sigh
|
|
|
|
$dupe = new Notice();
|
|
|
|
$dupe->uri = $notice->uri;
|
2010-02-09 04:06:03 +09:00
|
|
|
if ($dupe->find(true)) {
|
2009-11-21 02:42:19 +09:00
|
|
|
common_log(LOG_WARNING, __METHOD__ . ": tried to save dupe notice for entry {$notice->uri} of feed {$this->feeduri}");
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-10 11:32:52 +09:00
|
|
|
|
2010-02-11 06:18:53 +09:00
|
|
|
// @fixme need to ensure that groups get handled correctly
|
2010-02-12 04:44:03 +09:00
|
|
|
$saved = Notice::saveNew($notice->profile_id,
|
2010-02-11 06:18:53 +09:00
|
|
|
$notice->content,
|
|
|
|
'ostatus',
|
|
|
|
array('is_local' => Notice::REMOTE_OMB,
|
|
|
|
'uri' => $notice->uri,
|
|
|
|
'lat' => $notice->lat,
|
|
|
|
'lon' => $notice->lon,
|
|
|
|
'location_ns' => $notice->location_ns,
|
|
|
|
'location_id' => $notice->location_id));
|
|
|
|
|
|
|
|
/*
|
2010-02-10 11:32:52 +09:00
|
|
|
common_log(LOG_DEBUG, "going to check group delivery...");
|
|
|
|
if ($this->group_id) {
|
|
|
|
$group = User_group::staticGet($this->group_id);
|
|
|
|
if ($group) {
|
|
|
|
common_log(LOG_INFO, __METHOD__ . ": saving to local shadow group $group->id $group->nickname");
|
|
|
|
$groups = array($group);
|
|
|
|
} else {
|
|
|
|
common_log(LOG_INFO, __METHOD__ . ": lost the local shadow group?");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
common_log(LOG_INFO, __METHOD__ . ": no local shadow groups");
|
|
|
|
$groups = array();
|
|
|
|
}
|
|
|
|
common_log(LOG_DEBUG, "going to add to inboxes...");
|
|
|
|
$notice->addToInboxes($groups, array());
|
|
|
|
common_log(LOG_DEBUG, "added to inboxes.");
|
2010-02-11 06:18:53 +09:00
|
|
|
*/
|
2010-02-10 11:32:52 +09:00
|
|
|
|
2009-11-21 02:42:19 +09:00
|
|
|
$hits++;
|
|
|
|
}
|
|
|
|
if ($hits == 0) {
|
|
|
|
common_log(LOG_INFO, __METHOD__ . ": no updates in packet for \"$this->feeduri\"! $xml");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|