2009-11-20 17:42:19 +00:00
|
|
|
<?php
|
|
|
|
/*
|
2010-02-08 19:06:03 +00: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-20 17:42:19 +00:00
|
|
|
|
2010-02-08 19:06:03 +00:00
|
|
|
/*
|
|
|
|
PuSH subscription flow:
|
2009-11-20 17:42:19 +00:00
|
|
|
|
2010-02-12 00:22:16 +00:00
|
|
|
$profile->subscribe()
|
2009-11-20 17:42:19 +00:00
|
|
|
generate random verification token
|
|
|
|
save to verify_token
|
|
|
|
sends a sub request to the hub...
|
2010-02-16 14:58:33 +00:00
|
|
|
|
2010-02-12 00:22:16 +00:00
|
|
|
main/push/callback
|
2009-11-20 17:42:19 +00:00
|
|
|
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
|
2010-02-16 14:58:33 +00:00
|
|
|
|
2010-02-12 00:22:16 +00:00
|
|
|
main/push/callback
|
2009-11-20 17:42:19 +00:00
|
|
|
hub sends us updates via POST
|
2010-02-16 14:58:33 +00:00
|
|
|
|
2009-11-20 17:42:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
class FeedDBException extends FeedSubException
|
|
|
|
{
|
|
|
|
public $obj;
|
|
|
|
|
|
|
|
function __construct($obj)
|
|
|
|
{
|
|
|
|
parent::__construct('Database insert failure');
|
|
|
|
$this->obj = $obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 00:22:16 +00:00
|
|
|
class Ostatus_profile extends Memcached_DataObject
|
2009-11-20 17:42:19 +00:00
|
|
|
{
|
2010-02-12 00:22:16 +00:00
|
|
|
public $__table = 'ostatus_profile';
|
2009-11-20 17:42:19 +00:00
|
|
|
|
|
|
|
public $id;
|
|
|
|
public $profile_id;
|
2010-02-12 00:22:16 +00:00
|
|
|
public $group_id;
|
2009-11-20 17:42:19 +00:00
|
|
|
|
|
|
|
public $feeduri;
|
|
|
|
public $homeuri;
|
|
|
|
|
|
|
|
// PuSH subscription data
|
2010-02-12 00:22:16 +00:00
|
|
|
public $huburi;
|
2010-02-08 19:06:03 +00:00
|
|
|
public $secret;
|
2009-11-20 17:42:19 +00:00
|
|
|
public $verify_token;
|
2010-02-12 00:22:16 +00:00
|
|
|
public $sub_state; // subscribe, active, unsubscribe
|
2009-11-20 17:42:19 +00:00
|
|
|
public $sub_start;
|
|
|
|
public $sub_end;
|
|
|
|
|
2010-02-12 00:22:16 +00:00
|
|
|
public $salmonuri;
|
|
|
|
|
2009-11-20 17:42:19 +00:00
|
|
|
public $created;
|
|
|
|
public $lastupdate;
|
|
|
|
|
|
|
|
public /*static*/ function staticGet($k, $v=null)
|
|
|
|
{
|
|
|
|
return parent::staticGet(__CLASS__, $k, $v);
|
|
|
|
}
|
|
|
|
|
2010-01-04 18:30:19 +00: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 02:32:52 +00:00
|
|
|
'profile_id' => DB_DATAOBJECT_INT,
|
|
|
|
'group_id' => DB_DATAOBJECT_INT,
|
2010-01-04 18:30:19 +00:00
|
|
|
'feeduri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
|
|
|
|
'homeuri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
|
2010-02-11 20:12:48 +00:00
|
|
|
'huburi' => DB_DATAOBJECT_STR,
|
2010-02-08 19:06:03 +00:00
|
|
|
'secret' => DB_DATAOBJECT_STR,
|
2010-01-04 18:30:19 +00:00
|
|
|
'verify_token' => DB_DATAOBJECT_STR,
|
2010-02-12 00:22:16 +00:00
|
|
|
'sub_state' => DB_DATAOBJECT_STR,
|
2010-01-04 18:30:19 +00:00
|
|
|
'sub_start' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
|
|
|
|
'sub_end' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
|
2010-02-11 20:12:48 +00:00
|
|
|
'salmonuri' => DB_DATAOBJECT_STR,
|
2010-01-04 18:30:19 +00:00
|
|
|
'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);
|
|
|
|
}
|
2010-02-16 14:58:33 +00:00
|
|
|
|
2010-01-04 18:30:19 +00:00
|
|
|
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 00:09:20 +00:00
|
|
|
null, true, 'UNI'),
|
2010-02-10 02:32:52 +00:00
|
|
|
new ColumnDef('group_id', 'integer',
|
2010-02-11 00:09:20 +00:00
|
|
|
null, true, 'UNI'),
|
2010-01-04 18:30:19 +00:00
|
|
|
new ColumnDef('feeduri', 'varchar',
|
|
|
|
255, false, 'UNI'),
|
|
|
|
new ColumnDef('homeuri', 'varchar',
|
|
|
|
255, false),
|
2010-02-11 20:12:48 +00:00
|
|
|
new ColumnDef('huburi', 'text',
|
|
|
|
null, true),
|
2010-01-04 18:30:19 +00:00
|
|
|
new ColumnDef('verify_token', 'varchar',
|
|
|
|
32, true),
|
2010-02-08 19:06:03 +00:00
|
|
|
new ColumnDef('secret', 'varchar',
|
|
|
|
64, true),
|
2010-02-12 00:22:16 +00:00
|
|
|
new ColumnDef('sub_state', "enum('subscribe','active','unsubscribe')",
|
|
|
|
null, true),
|
2010-01-04 18:30:19 +00:00
|
|
|
new ColumnDef('sub_start', 'datetime',
|
|
|
|
null, true),
|
|
|
|
new ColumnDef('sub_end', 'datetime',
|
|
|
|
null, true),
|
2010-02-11 20:12:48 +00:00
|
|
|
new ColumnDef('salmonuri', 'text',
|
|
|
|
null, true),
|
2010-01-04 18:30:19 +00:00
|
|
|
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-08 19:06:03 +00:00
|
|
|
return array_keys($this->keyTypes());
|
2010-01-04 18:30:19 +00: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-20 17:42:19 +00:00
|
|
|
{
|
2010-02-11 00:09:20 +00:00
|
|
|
return array('id' => 'K', 'profile_id' => 'U', 'group_id' => 'U', 'feeduri' => 'U');
|
2010-02-08 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sequenceKey()
|
|
|
|
{
|
|
|
|
return array('id', true, false);
|
2009-11-20 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 18:30:19 +00:00
|
|
|
/**
|
|
|
|
* Fetch the StatusNet-side profile for this feed
|
|
|
|
* @return Profile
|
|
|
|
*/
|
2010-02-12 01:11:46 +00:00
|
|
|
public function localProfile()
|
2009-11-20 17:42:19 +00:00
|
|
|
{
|
2010-02-12 01:11:46 +00:00
|
|
|
if ($this->profile_id) {
|
|
|
|
return Profile::staticGet('id', $this->profile_id);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the StatusNet-side profile for this feed
|
|
|
|
* @return Profile
|
|
|
|
*/
|
|
|
|
public function localGroup()
|
|
|
|
{
|
|
|
|
if ($this->group_id) {
|
|
|
|
return User_group::staticGet('id', $this->group_id);
|
|
|
|
}
|
|
|
|
return null;
|
2009-11-20 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FeedMunger $munger
|
2010-02-10 02:32:52 +00:00
|
|
|
* @param boolean $isGroup is this a group record?
|
2010-02-12 00:22:16 +00:00
|
|
|
* @return Ostatus_profile
|
2009-11-20 17:42:19 +00:00
|
|
|
*/
|
|
|
|
public static function ensureProfile($munger)
|
|
|
|
{
|
2010-02-12 01:11:46 +00:00
|
|
|
$profile = $munger->ostatusProfile();
|
2009-11-20 17:42:19 +00:00
|
|
|
|
2010-02-12 01:11:46 +00:00
|
|
|
$current = self::staticGet('feeduri', $profile->feeduri);
|
2009-11-20 17:42:19 +00:00
|
|
|
if ($current) {
|
|
|
|
// @fixme we should probably update info as necessary
|
|
|
|
return $current;
|
|
|
|
}
|
|
|
|
|
2010-02-12 01:11:46 +00:00
|
|
|
$profile->query('BEGIN');
|
2009-11-20 17:42:19 +00:00
|
|
|
|
2010-02-08 19:06:03 +00:00
|
|
|
// Awful hack! Awful hack!
|
2010-02-12 01:11:46 +00:00
|
|
|
$profile->verify = common_good_rand(16);
|
|
|
|
$profile->secret = common_good_rand(32);
|
2010-02-08 19:06:03 +00:00
|
|
|
|
2009-11-20 17:42:19 +00:00
|
|
|
try {
|
2010-02-12 01:11:46 +00:00
|
|
|
$local = $munger->profile();
|
2010-02-08 19:06:03 +00:00
|
|
|
|
2010-02-16 20:11:48 +00:00
|
|
|
if ($profile->isGroup()) {
|
2010-02-10 02:32:52 +00:00
|
|
|
$group = new User_group();
|
2010-02-12 01:11:46 +00:00
|
|
|
$group->nickname = $local->nickname . '@remote'; // @fixme
|
|
|
|
$group->fullname = $local->fullname;
|
|
|
|
$group->homepage = $local->homepage;
|
|
|
|
$group->location = $local->location;
|
|
|
|
$group->created = $local->created;
|
2010-02-10 02:32:52 +00:00
|
|
|
$group->insert();
|
2010-02-12 01:11:46 +00:00
|
|
|
if (empty($result)) {
|
|
|
|
throw new FeedDBException($group);
|
2010-02-10 02:32:52 +00:00
|
|
|
}
|
2010-02-12 01:11:46 +00:00
|
|
|
$profile->group_id = $group->id;
|
|
|
|
} else {
|
|
|
|
$result = $local->insert();
|
|
|
|
if (empty($result)) {
|
|
|
|
throw new FeedDBException($local);
|
|
|
|
}
|
|
|
|
$profile->profile_id = $local->id;
|
2010-02-10 02:32:52 +00:00
|
|
|
}
|
|
|
|
|
2010-02-16 20:11:48 +00:00
|
|
|
$profile->created = common_sql_now();
|
|
|
|
$profile->lastupdate = common_sql_now();
|
2010-02-12 01:11:46 +00:00
|
|
|
$result = $profile->insert();
|
2009-11-20 17:42:19 +00:00
|
|
|
if (empty($result)) {
|
2010-02-12 01:11:46 +00:00
|
|
|
throw new FeedDBException($profile);
|
2009-11-20 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2010-02-16 20:11:48 +00:00
|
|
|
$profile->query('COMMIT');
|
2009-11-20 17:42:19 +00:00
|
|
|
} catch (FeedDBException $e) {
|
|
|
|
common_log_db_error($e->obj, 'INSERT', __FILE__);
|
2010-02-16 20:11:48 +00:00
|
|
|
$profile->query('ROLLBACK');
|
2009-11-20 17:42:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-02-12 01:11:46 +00:00
|
|
|
|
|
|
|
$avatar = $munger->getAvatar();
|
|
|
|
if ($avatar) {
|
|
|
|
try {
|
2010-02-16 22:03:24 +00:00
|
|
|
$profile->updateAvatar($avatar);
|
2010-02-12 01:11:46 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
common_log(LOG_ERR, "Exception setting OStatus avatar: " .
|
|
|
|
$e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-16 20:11:48 +00:00
|
|
|
return $profile;
|
2009-11-20 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 01:11:46 +00:00
|
|
|
/**
|
|
|
|
* Download and update given avatar image
|
|
|
|
* @param string $url
|
|
|
|
* @throws Exception in various failure cases
|
|
|
|
*/
|
|
|
|
public function updateAvatar($url)
|
|
|
|
{
|
|
|
|
// @fixme this should be better encapsulated
|
|
|
|
// ripped from oauthstore.php (for old OMB client)
|
|
|
|
$temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
|
|
|
|
copy($url, $temp_filename);
|
2010-02-16 22:03:24 +00:00
|
|
|
|
|
|
|
// @fixme should we be using different ids?
|
|
|
|
$imagefile = new ImageFile($this->id, $temp_filename);
|
|
|
|
$filename = Avatar::filename($this->id,
|
2010-02-12 01:11:46 +00:00
|
|
|
image_type_to_extension($imagefile->type),
|
|
|
|
null,
|
|
|
|
common_timestamp());
|
|
|
|
rename($temp_filename, Avatar::path($filename));
|
|
|
|
if ($this->isGroup()) {
|
|
|
|
$group = $this->localGroup();
|
|
|
|
$group->setOriginal($filename);
|
|
|
|
} else {
|
|
|
|
$profile = $this->localProfile();
|
|
|
|
$profile->setOriginal($filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 18:54:48 +00:00
|
|
|
/**
|
|
|
|
* Returns an XML string fragment with profile information as an
|
|
|
|
* Activity Streams noun object with the given element type.
|
|
|
|
*
|
|
|
|
* Assumes that 'activity' namespace has been previously defined.
|
|
|
|
*
|
|
|
|
* @param string $element one of 'actor', 'subject', 'object', 'target'
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function asActivityNoun($element)
|
|
|
|
{
|
|
|
|
$xs = new XMLStringer(true);
|
|
|
|
|
|
|
|
$avatarHref = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
|
|
|
|
$avatarType = 'image/png';
|
|
|
|
if ($this->isGroup()) {
|
|
|
|
$type = 'http://activitystrea.ms/schema/1.0/group';
|
|
|
|
$self = $this->localGroup();
|
|
|
|
|
|
|
|
// @fixme put a standard getAvatar() interface on groups too
|
|
|
|
if ($self->homepage_logo) {
|
|
|
|
$avatarHref = $self->homepage_logo;
|
|
|
|
$map = array('png' => 'image/png',
|
|
|
|
'jpg' => 'image/jpeg',
|
|
|
|
'jpeg' => 'image/jpeg',
|
|
|
|
'gif' => 'image/gif');
|
|
|
|
$extension = pathinfo(parse_url($avatarHref, PHP_URL_PATH), PATHINFO_EXTENSION);
|
|
|
|
if (isset($map[$extension])) {
|
|
|
|
$avatarType = $map[$extension];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$type = 'http://activitystrea.ms/schema/1.0/person';
|
|
|
|
$self = $this->localProfile();
|
|
|
|
$avatar = $self->getAvatar(AVATAR_PROFILE_SIZE);
|
|
|
|
if ($avatar) {
|
|
|
|
$avatarHref = $avatar->
|
|
|
|
$avatarType = $avatar->mediatype;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$xs->elementStart('activity:' . $element);
|
|
|
|
$xs->element(
|
|
|
|
'activity:object-type',
|
|
|
|
null,
|
|
|
|
$type
|
|
|
|
);
|
|
|
|
$xs->element(
|
|
|
|
'id',
|
|
|
|
null,
|
|
|
|
$this->homeuri); // ?
|
|
|
|
$xs->element('title', null, $self->getBestName());
|
|
|
|
|
|
|
|
$xs->element(
|
|
|
|
'link', array(
|
|
|
|
'type' => $avatarType,
|
|
|
|
'href' => $avatarHref
|
|
|
|
),
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
$xs->elementEnd('activity:' . $element);
|
|
|
|
|
|
|
|
return $xs->getString();
|
|
|
|
}
|
|
|
|
|
2010-02-10 02:32:52 +00:00
|
|
|
/**
|
|
|
|
* Damn dirty hack!
|
|
|
|
*/
|
|
|
|
function isGroup()
|
|
|
|
{
|
|
|
|
return (strpos($this->feeduri, '/groups/') !== false);
|
|
|
|
}
|
|
|
|
|
2009-11-20 17:42:19 +00:00
|
|
|
/**
|
|
|
|
* Send a subscription request to the hub for this feed.
|
2010-02-11 00:09:20 +00:00
|
|
|
* The hub will later send us a confirmation POST to /main/push/callback.
|
2009-11-20 17:42:19 +00:00
|
|
|
*
|
|
|
|
* @return bool true on success, false on failure
|
2010-02-16 22:03:24 +00:00
|
|
|
* @throws ServerException if feed state is not valid
|
2009-11-20 17:42:19 +00:00
|
|
|
*/
|
2010-02-11 00:09:20 +00:00
|
|
|
public function subscribe($mode='subscribe')
|
2009-11-20 17:42:19 +00:00
|
|
|
{
|
2010-02-16 22:03:24 +00:00
|
|
|
if ($this->sub_state != '') {
|
|
|
|
throw new ServerException("Attempting to start PuSH subscription to feed in state $this->sub_state");
|
2010-02-08 19:06:03 +00:00
|
|
|
}
|
2010-02-16 22:03:24 +00:00
|
|
|
if (empty($this->huburi)) {
|
|
|
|
if (common_config('feedsub', 'nohub')) {
|
|
|
|
// Fake it! We're just testing remote feeds w/o hubs.
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
throw new ServerException("Attempting to start PuSH subscription for feed with no hub");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->doSubscribe('subscribe');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a PuSH 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
|
|
|
|
* @throws ServerException if feed state is not valid
|
|
|
|
*/
|
|
|
|
public function unsubscribe() {
|
|
|
|
if ($this->sub_state != 'active') {
|
|
|
|
throw new ServerException("Attempting to end PuSH subscription to feed in state $this->sub_state");
|
|
|
|
}
|
|
|
|
if (empty($this->huburi)) {
|
|
|
|
if (common_config('feedsub', 'nohub')) {
|
|
|
|
// Fake it! We're just testing remote feeds w/o hubs.
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
throw new ServerException("Attempting to end PuSH subscription for feed with no hub");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->doSubscribe('unsubscribe');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doSubscribe($mode)
|
|
|
|
{
|
|
|
|
$orig = clone($this);
|
|
|
|
$this->verify_token = md5(mt_rand() . ':' . $this->feeduri);
|
|
|
|
$this->sub_state = $mode;
|
|
|
|
$this->update($orig);
|
|
|
|
unset($orig);
|
|
|
|
|
2009-11-20 17:42:19 +00:00
|
|
|
try {
|
2010-02-08 22:06:36 +00:00
|
|
|
$callback = common_local_url('pushcallback', array('feed' => $this->id));
|
2009-11-20 17:42:19 +00:00
|
|
|
$headers = array('Content-Type: application/x-www-form-urlencoded');
|
2010-02-11 00:09:20 +00:00
|
|
|
$post = array('hub.mode' => $mode,
|
2009-11-20 17:42:19 +00:00
|
|
|
'hub.callback' => $callback,
|
|
|
|
'hub.verify' => 'async',
|
2010-02-08 19:06:03 +00:00
|
|
|
'hub.verify_token' => $this->verify_token,
|
|
|
|
'hub.secret' => $this->secret,
|
2009-11-20 17:42:19 +00:00
|
|
|
//'hub.lease_seconds' => 0,
|
|
|
|
'hub.topic' => $this->feeduri);
|
|
|
|
$client = new HTTPClient();
|
|
|
|
$response = $client->post($this->huburi, $headers, $post);
|
2010-02-08 19:06:03 +00: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-20 17:42:19 +00:00
|
|
|
return true;
|
2010-02-08 19:06:03 +00:00
|
|
|
} else if ($status >= 200 && $status < 300) {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
|
|
|
|
return false;
|
2009-11-20 17:42:19 +00:00
|
|
|
} else {
|
2010-02-08 19:06:03 +00:00
|
|
|
common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
|
2009-11-20 17:42:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// wtf!
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to $this->feeduri");
|
2010-02-16 22:03:24 +00:00
|
|
|
|
|
|
|
$orig = clone($this);
|
|
|
|
$this->verify_token = null;
|
|
|
|
$this->sub_state = null;
|
|
|
|
$this->update($orig);
|
|
|
|
unset($orig);
|
|
|
|
|
2009-11-20 17:42:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 01:11:46 +00:00
|
|
|
/**
|
|
|
|
* Save PuSH subscription confirmation.
|
|
|
|
* Sets approximate lease start and end times and finalizes state.
|
|
|
|
*
|
|
|
|
* @param int $lease_seconds provided hub.lease_seconds parameter, if given
|
|
|
|
*/
|
|
|
|
public function confirmSubscribe($lease_seconds=0)
|
|
|
|
{
|
|
|
|
$original = clone($this);
|
|
|
|
|
|
|
|
$this->sub_state = 'active';
|
|
|
|
$this->sub_start = common_sql_date(time());
|
|
|
|
if ($lease_seconds > 0) {
|
|
|
|
$this->sub_end = common_sql_date(time() + $lease_seconds);
|
|
|
|
} else {
|
|
|
|
$this->sub_end = null;
|
|
|
|
}
|
|
|
|
$this->lastupdate = common_sql_date();
|
|
|
|
|
|
|
|
return $this->update($original);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save PuSH unsubscription confirmation.
|
|
|
|
* Wipes active PuSH sub info and resets state.
|
|
|
|
*/
|
|
|
|
public function confirmUnsubscribe()
|
|
|
|
{
|
|
|
|
$original = clone($this);
|
|
|
|
|
|
|
|
$this->verify_token = null;
|
|
|
|
$this->secret = null;
|
|
|
|
$this->sub_state = null;
|
|
|
|
$this->sub_start = null;
|
|
|
|
$this->sub_end = null;
|
|
|
|
$this->lastupdate = common_sql_date();
|
|
|
|
|
|
|
|
return $this->update($original);
|
|
|
|
}
|
|
|
|
|
2010-02-12 18:54:48 +00:00
|
|
|
/**
|
|
|
|
* Send an Activity Streams notification to the remote Salmon endpoint,
|
|
|
|
* if so configured.
|
|
|
|
*
|
|
|
|
* @param Profile $actor
|
|
|
|
* @param $verb eg Activity::SUBSCRIBE or Activity::JOIN
|
|
|
|
* @param $object object of the action; if null, the remote entity itself is assumed
|
|
|
|
*/
|
|
|
|
public function notify(Profile $actor, $verb, $object=null)
|
|
|
|
{
|
|
|
|
if ($object == null) {
|
|
|
|
$object = $this;
|
|
|
|
}
|
|
|
|
if ($this->salmonuri) {
|
|
|
|
$text = 'update'; // @fixme
|
2010-02-16 14:58:33 +00:00
|
|
|
$id = 'tag:' . common_config('site', 'server') .
|
2010-02-12 18:54:48 +00:00
|
|
|
':' . $verb .
|
|
|
|
':' . $actor->id .
|
|
|
|
':' . time(); // @fixme
|
|
|
|
|
|
|
|
$entry = new Atom10Entry();
|
|
|
|
$entry->elementStart('entry');
|
|
|
|
$entry->element('id', null, $id);
|
|
|
|
$entry->element('title', null, $text);
|
|
|
|
$entry->element('summary', null, $text);
|
|
|
|
$entry->element('published', null, common_date_w3dtf());
|
|
|
|
|
|
|
|
$entry->element('activity:verb', null, $verb);
|
|
|
|
$entry->raw($profile->asAtomAuthor());
|
|
|
|
$entry->raw($profile->asActivityActor());
|
|
|
|
$entry->raw($object->asActivityNoun('object'));
|
|
|
|
$entry->elmentEnd('entry');
|
|
|
|
|
|
|
|
$feed = $this->atomFeed($actor);
|
|
|
|
$feed->initFeed();
|
|
|
|
$feed->addEntry($entry);
|
|
|
|
$feed->renderEntries();
|
|
|
|
$feed->endFeed();
|
|
|
|
|
|
|
|
$xml = $feed->getString();
|
|
|
|
common_log(LOG_INFO, "Posting to Salmon endpoint $salmon: $xml");
|
|
|
|
|
|
|
|
$salmon = new Salmon(); // ?
|
|
|
|
$salmon->post($this->salmonuri, $xml);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBestName()
|
|
|
|
{
|
|
|
|
if ($this->isGroup()) {
|
|
|
|
return $this->localGroup()->getBestName();
|
|
|
|
} else {
|
|
|
|
return $this->localProfile()->getBestName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function atomFeed($actor)
|
|
|
|
{
|
|
|
|
$feed = new Atom10Feed();
|
|
|
|
// @fixme should these be set up somewhere else?
|
|
|
|
$feed->addNamespace('activity', 'http://activitystrea.ms/spec/1.0/');
|
|
|
|
$feed->addNamesapce('thr', 'http://purl.org/syndication/thread/1.0');
|
|
|
|
$feed->addNamespace('georss', 'http://www.georss.org/georss');
|
|
|
|
$feed->addNamespace('ostatus', 'http://ostatus.org/schema/1.0');
|
|
|
|
|
|
|
|
$taguribase = common_config('integration', 'taguri');
|
|
|
|
$feed->setId("tag:{$taguribase}:UserTimeline:{$actor->id}"); // ???
|
|
|
|
|
|
|
|
$feed->setTitle($actor->getBestName() . ' timeline'); // @fixme
|
|
|
|
$feed->setUpdated(time());
|
|
|
|
$feed->setPublished(time());
|
|
|
|
|
|
|
|
$feed->addLink(common_url('ApiTimelineUser',
|
|
|
|
array('id' => $actor->id,
|
|
|
|
'type' => 'atom')),
|
|
|
|
array('rel' => 'self',
|
|
|
|
'type' => 'application/atom+xml'));
|
|
|
|
|
|
|
|
$feed->addLink(common_url('userbyid',
|
|
|
|
array('id' => $actor->id)),
|
|
|
|
array('rel' => 'alternate',
|
|
|
|
'type' => 'text/html'));
|
|
|
|
|
|
|
|
return $feed;
|
|
|
|
}
|
|
|
|
|
2009-11-20 17:42:19 +00: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.
|
|
|
|
*
|
2010-02-16 23:31:11 +00:00
|
|
|
* @param string $post source of Atom or RSS feed
|
2010-02-08 19:06:03 +00:00
|
|
|
* @param string $hmac X-Hub-Signature header, if present
|
2009-11-20 17:42:19 +00:00
|
|
|
*/
|
2010-02-16 23:31:11 +00:00
|
|
|
public function postUpdates($post, $hmac)
|
2009-11-20 17:42:19 +00:00
|
|
|
{
|
2010-02-16 23:31:11 +00:00
|
|
|
common_log(LOG_INFO, __METHOD__ . ": packet for \"$this->feeduri\"! $hmac $post");
|
2010-02-08 19:06:03 +00:00
|
|
|
|
2010-02-16 22:03:24 +00:00
|
|
|
if ($this->sub_state != 'active') {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH for inactive feed $this->feeduri (in state '$this->sub_state')");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-16 23:31:11 +00:00
|
|
|
if ($post === '') {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": ignoring empty post");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->validatePushSig($post, $hmac)) {
|
|
|
|
// Per spec we silently drop input with a bad sig,
|
|
|
|
// while reporting receipt to the server.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$feed = new DOMDocument();
|
|
|
|
if (!$feed->loadXML($post)) {
|
|
|
|
// @fixme might help to include the err message
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": ignoring invalid XML");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
|
|
|
|
if ($entries->length == 0) {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": no entries in feed update, ignoring");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = 0; $i < $entries->length; $i++) {
|
|
|
|
$entry = $entries->item($i);
|
|
|
|
$this->processEntry($entry, $feed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the given Atom chunk and HMAC signature against our
|
|
|
|
* shared secret that was set up at subscription time.
|
|
|
|
*
|
|
|
|
* If we don't have a shared secret, there should be no signature.
|
|
|
|
* If we we do, our the calculated HMAC should match theirs.
|
|
|
|
*
|
|
|
|
* @param string $post raw XML source as POSTed to us
|
|
|
|
* @param string $hmac X-Hub-Signature HTTP header value, or empty
|
|
|
|
* @return boolean true for a match
|
|
|
|
*/
|
|
|
|
protected function validatePushSig($post, $hmac)
|
|
|
|
{
|
2010-02-08 19:06:03 +00:00
|
|
|
if ($this->secret) {
|
|
|
|
if (preg_match('/^sha1=([0-9a-fA-F]{40})$/', $hmac, $matches)) {
|
|
|
|
$their_hmac = strtolower($matches[1]);
|
2010-02-16 23:31:11 +00:00
|
|
|
$our_hmac = hash_hmac('sha1', $post, $this->secret);
|
|
|
|
if ($their_hmac === $our_hmac) {
|
|
|
|
return true;
|
2010-02-08 19:06:03 +00:00
|
|
|
}
|
2010-02-16 23:31:11 +00:00
|
|
|
common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bad SHA-1 HMAC: got $their_hmac, expected $our_hmac");
|
2010-02-08 19:06:03 +00:00
|
|
|
} else {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bogus HMAC '$hmac'");
|
|
|
|
}
|
2010-02-16 23:31:11 +00:00
|
|
|
} else {
|
|
|
|
if (empty($hmac)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with unexpected HMAC '$hmac'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a posted entry from this feed source.
|
|
|
|
*
|
|
|
|
* @param DOMElement $entry
|
|
|
|
* @param DOMElement $feed for context
|
|
|
|
*/
|
|
|
|
protected function processEntry($entry, $feed)
|
|
|
|
{
|
|
|
|
$activity = new Activity($entry, $feed);
|
|
|
|
|
|
|
|
$debug = var_export($activity, true);
|
|
|
|
common_log(LOG_DEBUG, $debug);
|
|
|
|
|
|
|
|
if ($activity->verb == ActivityVerb::POST) {
|
|
|
|
$this->processPost($activity);
|
|
|
|
} else {
|
|
|
|
common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process an incoming post activity from this remote feed.
|
|
|
|
* @param Activity $activity
|
|
|
|
*/
|
|
|
|
protected function processPost($activity)
|
|
|
|
{
|
|
|
|
// @fixme pull profile reference from actor for group feeds
|
|
|
|
$actor = $this;
|
|
|
|
$localProfile = $actor->localProfile();
|
|
|
|
if (empty($localProfile)) {
|
|
|
|
common_log(LOG_INFO, "OStatus: ignoring post with invalid author");
|
2010-02-08 19:06:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-16 23:31:11 +00:00
|
|
|
if (empty($activity->object)) {
|
|
|
|
// This shouldn't happen!
|
|
|
|
common_log(LOG_INFO, "OStatus: ignoring post with missing post object.");
|
|
|
|
return;
|
|
|
|
}
|
2010-02-16 14:58:33 +00:00
|
|
|
|
2010-02-16 23:31:11 +00:00
|
|
|
if ($activity->object->link) {
|
|
|
|
$sourceUri = $activity->object->link;
|
|
|
|
} else if (preg_match('!^https?://!', $activity->object->id)) {
|
|
|
|
$sourceUri = $activity->object->id;
|
|
|
|
} else {
|
|
|
|
common_log(LOG_INFO, "OStatus: ignoring post with no source link: id $activity->object->id");
|
|
|
|
return;
|
|
|
|
}
|
2010-02-10 21:18:53 +00:00
|
|
|
|
2010-02-16 23:31:11 +00:00
|
|
|
$dupe = Notice::staticGet('uri', $sourceUri);
|
|
|
|
if ($dupe) {
|
|
|
|
common_log(LOG_INFO, "OStatus: ignoring duplicate post: $noticeLink");
|
|
|
|
return;
|
|
|
|
}
|
2010-02-10 21:18:53 +00:00
|
|
|
|
2010-02-16 23:31:11 +00:00
|
|
|
// @fixme sanitize and save HTML content if available
|
|
|
|
$content = $activity->object->title;
|
2010-02-16 14:58:33 +00:00
|
|
|
|
2010-02-16 23:31:11 +00:00
|
|
|
$params = array('is_local' => Notice::REMOTE_OMB,
|
|
|
|
'uri' => $sourceUri);
|
2010-02-16 14:58:33 +00:00
|
|
|
|
2010-02-16 23:31:11 +00:00
|
|
|
$location = $this->getEntryLocation($activity->entry);
|
|
|
|
if ($location) {
|
|
|
|
$params['lat'] = $location->lat;
|
|
|
|
$params['lon'] = $location->lon;
|
|
|
|
if ($location->location_id) {
|
|
|
|
$params['location_ns'] = $location->location_ns;
|
|
|
|
$params['location_id'] = $location->location_id;
|
2009-11-20 17:42:19 +00:00
|
|
|
}
|
2010-02-16 23:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @fixme save detailed ostatus source info
|
|
|
|
// @fixme ensure that groups get handled correctly
|
2010-02-10 02:32:52 +00:00
|
|
|
|
2010-02-16 23:31:11 +00:00
|
|
|
$saved = Notice::saveNew($localProfile->id,
|
|
|
|
$content,
|
|
|
|
'ostatus',
|
|
|
|
$params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse location given as a GeoRSS-simple point, if provided.
|
|
|
|
* http://www.georss.org/simple
|
|
|
|
*
|
|
|
|
* @param feed item $entry
|
|
|
|
* @return mixed Location or false
|
|
|
|
*/
|
|
|
|
function getLocation($dom)
|
|
|
|
{
|
|
|
|
$points = $dom->getElementsByTagNameNS('http://www.georss.org/georss', 'point');
|
|
|
|
|
|
|
|
for ($i = 0; $i < $points->length; $i++) {
|
|
|
|
$point = $points->item(0)->textContent;
|
|
|
|
$point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
|
|
|
|
$point = preg_replace('/\s+/', ' ', $point);
|
|
|
|
$point = trim($point);
|
|
|
|
$coords = explode(' ', $point);
|
|
|
|
if (count($coords) == 2) {
|
|
|
|
list($lat, $lon) = $coords;
|
|
|
|
if (is_numeric($lat) && is_numeric($lon)) {
|
|
|
|
common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
|
|
|
|
return Location::fromLatLon($lat, $lon);
|
2010-02-10 02:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-16 23:31:11 +00:00
|
|
|
common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
|
2009-11-20 17:42:19 +00:00
|
|
|
}
|
2010-02-16 23:31:11 +00:00
|
|
|
|
|
|
|
return false;
|
2009-11-20 17:42:19 +00:00
|
|
|
}
|
|
|
|
}
|