Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing

This commit is contained in:
Evan Prodromou 2010-02-26 11:40:12 -05:00
commit d9d1a77bb0
20 changed files with 443 additions and 349 deletions

View File

@ -32,8 +32,6 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php';
/** /**
* Gives a full dump of configuration variables for this instance * Gives a full dump of configuration variables for this instance
* of StatusNet, minus variables that may be security-sensitive (like * of StatusNet, minus variables that may be security-sensitive (like

View File

@ -31,8 +31,6 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1); exit(1);
} }
require_once INSTALLDIR.'/lib/api.php';
/** /**
* Action for outputting search results in Twitter compatible Atom * Action for outputting search results in Twitter compatible Atom
* format. * format.

View File

@ -31,7 +31,6 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1); exit(1);
} }
require_once INSTALLDIR.'/lib/api.php';
require_once INSTALLDIR.'/lib/jsonsearchresultslist.php'; require_once INSTALLDIR.'/lib/jsonsearchresultslist.php';
/** /**

View File

@ -31,8 +31,6 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1); exit(1);
} }
require_once INSTALLDIR.'/lib/api.php';
/** /**
* Returns the top ten queries that are currently trending * Returns the top ten queries that are currently trending
* *

View File

@ -38,7 +38,6 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php';
require_once INSTALLDIR . '/lib/apioauth.php'; require_once INSTALLDIR . '/lib/apioauth.php';
/** /**

View File

@ -39,7 +39,11 @@ class ProfileQueueHandler extends QueueHandler
if (Event::handle('StartBroadcastProfile', array($profile))) { if (Event::handle('StartBroadcastProfile', array($profile))) {
require_once(INSTALLDIR.'/lib/omb.php'); require_once(INSTALLDIR.'/lib/omb.php');
omb_broadcast_profile($profile); try {
omb_broadcast_profile($profile);
} catch (Exception $e) {
common_log(LOG_ERR, "Failed sending OMB profiles: " . $e->getMessage());
}
} }
Event::handle('EndBroadcastProfile', array($profile)); Event::handle('EndBroadcastProfile', array($profile));
return true; return true;

View File

@ -142,8 +142,6 @@ class MapAction extends OwnerDesignAction
// of refactoring from within a plugin, so I'm just abusing // of refactoring from within a plugin, so I'm just abusing
// the ApiAction method. Don't do this unless you're me! // the ApiAction method. Don't do this unless you're me!
require_once(INSTALLDIR.'/lib/api.php');
$act = new ApiAction('/dev/null'); $act = new ApiAction('/dev/null');
$arr = $act->twitterStatusArray($notice, true); $arr = $act->twitterStatusArray($notice, true);

View File

@ -43,8 +43,8 @@ class OStatusPlugin extends Plugin
// Discovery actions // Discovery actions
$m->connect('.well-known/host-meta', $m->connect('.well-known/host-meta',
array('action' => 'hostmeta')); array('action' => 'hostmeta'));
$m->connect('main/webfinger', $m->connect('main/xrd',
array('action' => 'webfinger')); array('action' => 'xrd'));
$m->connect('main/ostatus', $m->connect('main/ostatus',
array('action' => 'ostatusinit')); array('action' => 'ostatusinit'));
$m->connect('main/ostatus?nickname=:nickname', $m->connect('main/ostatus?nickname=:nickname',
@ -102,6 +102,20 @@ class OStatusPlugin extends Plugin
return true; return true;
} }
/**
* Add a link header for LRDD Discovery
*/
function onStartShowHTML($action)
{
if ($action instanceof ShowstreamAction) {
$acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server');
$url = common_local_url('xrd');
$url.= '?uri='. $acct;
header('Link: <'.$url.'>; rel="'. Discovery::LRDD_REL.'"; type="application/xrd+xml"');
}
}
/** /**
* Set up a PuSH hub link to our internal link for canonical timeline * Set up a PuSH hub link to our internal link for canonical timeline
* Atom feeds for users and groups. * Atom feeds for users and groups.
@ -644,7 +658,7 @@ class OStatusPlugin extends Plugin
function onStartUserGroupHomeUrl($group, &$url) function onStartUserGroupHomeUrl($group, &$url)
{ {
return $this->onStartUserGroupPermalink($group, &$url); return $this->onStartUserGroupPermalink($group, $url);
} }
function onStartUserGroupPermalink($group, &$url) function onStartUserGroupPermalink($group, &$url)

View File

@ -31,12 +31,18 @@ class HostMetaAction extends Action
{ {
parent::handle(); parent::handle();
$w = new Webfinger();
$domain = common_config('site', 'server'); $domain = common_config('site', 'server');
$url = common_local_url('webfinger'); $url = common_local_url('xrd');
$url.= '?uri={uri}'; $url.= '?uri={uri}';
print $w->getHostMeta($domain, $url);
$xrd = new XRD();
$xrd = new XRD();
$xrd->host = $domain;
$xrd->links[] = array('rel' => Discovery::LRDD_REL,
'template' => $url,
'title' => array('Resource Descriptor'));
print $xrd->toXML();
} }
} }

View File

@ -131,9 +131,9 @@ class OStatusInitAction extends Action
function connectWebfinger($acct) function connectWebfinger($acct)
{ {
$w = new Webfinger; $disco = new Discovery;
$result = $w->lookup($acct); $result = $disco->lookup($acct);
if (!$result) { if (!$result) {
$this->clientError(_m("Couldn't look up OStatus account profile.")); $this->clientError(_m("Couldn't look up OStatus account profile."));
} }
@ -144,7 +144,7 @@ class OStatusInitAction extends Action
$user = User::staticGet('nickname', $this->nickname); $user = User::staticGet('nickname', $this->nickname);
$target_profile = common_local_url('userbyid', array('id' => $user->id)); $target_profile = common_local_url('userbyid', array('id' => $user->id));
$url = $w->applyTemplate($link['template'], $target_profile); $url = Discovery::applyTemplate($link['template'], $target_profile);
common_log(LOG_INFO, "Sending remote subscriber $acct to $url"); common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
common_redirect($url, 303); common_redirect($url, 303);
} }

View File

@ -24,7 +24,7 @@
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
class WebfingerAction extends Action class XrdAction extends Action
{ {
public $uri; public $uri;
@ -40,11 +40,11 @@ class WebfingerAction extends Action
function handle() function handle()
{ {
$acct = Webfinger::normalize($this->uri); $acct = Discovery::normalize($this->uri);
$xrd = new XRD(); $xrd = new XRD();
list($nick, $domain) = explode('@', urldecode($acct)); list($nick, $domain) = explode('@', substr(urldecode($acct), 5));
$nick = common_canonical_nickname($nick); $nick = common_canonical_nickname($nick);
$this->user = User::staticGet('nickname', $nick); $this->user = User::staticGet('nickname', $nick);
@ -55,18 +55,18 @@ class WebfingerAction extends Action
$xrd->subject = $this->uri; $xrd->subject = $this->uri;
$xrd->alias[] = common_profile_url($nick); $xrd->alias[] = common_profile_url($nick);
$xrd->links[] = array('rel' => Webfinger::PROFILEPAGE, $xrd->links[] = array('rel' => Discovery::PROFILEPAGE,
'type' => 'text/html', 'type' => 'text/html',
'href' => common_profile_url($nick)); 'href' => common_profile_url($nick));
$xrd->links[] = array('rel' => Webfinger::UPDATESFROM, $xrd->links[] = array('rel' => Discovery::UPDATESFROM,
'href' => common_local_url('ApiTimelineUser', 'href' => common_local_url('ApiTimelineUser',
array('id' => $this->user->id, array('id' => $this->user->id,
'format' => 'atom')), 'format' => 'atom')),
'type' => 'application/atom+xml'); 'type' => 'application/atom+xml');
// hCard // hCard
$xrd->links[] = array('rel' => Webfinger::HCARD, $xrd->links[] = array('rel' => Discovery::HCARD,
'type' => 'text/html', 'type' => 'text/html',
'href' => common_local_url('hcard', array('nickname' => $nick))); 'href' => common_local_url('hcard', array('nickname' => $nick)));
@ -91,7 +91,7 @@ class WebfingerAction extends Action
if (!$magickey) { if (!$magickey) {
// No keypair yet, let's generate one. // No keypair yet, let's generate one.
$magickey = new Magicsig(); $magickey = new Magicsig();
$magickey->generate(); $magickey->generate($this->user->id);
} }
$xrd->links[] = array('rel' => Magicsig::PUBLICKEYREL, $xrd->links[] = array('rel' => Magicsig::PUBLICKEYREL,

View File

@ -90,7 +90,7 @@ class Magicsig extends Memcached_DataObject
return parent::insert(); return parent::insert();
} }
public function generate($key_length = 512) public function generate($user_id, $key_length = 512)
{ {
PEAR::pushErrorHandling(PEAR_ERROR_RETURN); PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
@ -101,6 +101,7 @@ class Magicsig extends Memcached_DataObject
$this->_rsa = new Crypt_RSA($params); $this->_rsa = new Crypt_RSA($params);
PEAR::popErrorHandling(); PEAR::popErrorHandling();
$this->user_id = $user_id;
$this->insert(); $this->insert();
} }

View File

@ -150,27 +150,7 @@ class Ostatus_profile extends Memcached_DataObject
function asActivityObject() function asActivityObject()
{ {
if ($this->isGroup()) { if ($this->isGroup()) {
$object = new ActivityObject(); return ActivityObject::fromGroup($this->localGroup());
$object->type = 'http://activitystrea.ms/schema/1.0/group';
$object->id = $this->uri;
$self = $this->localGroup();
// @fixme put a standard getAvatar() interface on groups too
if ($self->homepage_logo) {
$object->avatar = $self->homepage_logo;
$map = array('png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif');
$extension = pathinfo(parse_url($object->avatar, PHP_URL_PATH), PATHINFO_EXTENSION);
if (isset($map[$extension])) {
// @fixme this ain't used/saved yet
$object->avatarType = $map[$extension];
}
}
$object->link = $this->uri; // @fixme accurate?
return $object;
} else { } else {
return ActivityObject::fromProfile($this->localProfile()); return ActivityObject::fromProfile($this->localProfile());
} }
@ -189,57 +169,13 @@ class Ostatus_profile extends Memcached_DataObject
*/ */
function asActivityNoun($element) function asActivityNoun($element)
{ {
$xs = new XMLStringer(true);
$avatarHref = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
$avatarType = 'image/png';
if ($this->isGroup()) { if ($this->isGroup()) {
$type = 'http://activitystrea.ms/schema/1.0/group'; $noun = ActivityObject::fromGroup($this->localGroup());
$self = $this->localGroup(); return $noun->asString('activity:' . $element);
// @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 { } else {
$type = 'http://activitystrea.ms/schema/1.0/person'; $noun = ActivityObject::fromProfile($this->localProfile());
$self = $this->localProfile(); return $noun->asString('activity:' . $element);
$avatar = $self->getAvatar(AVATAR_PROFILE_SIZE);
if ($avatar) {
$avatarHref = $avatar->url;
$avatarType = $avatar->mediatype;
}
} }
$xs->elementStart('activity:' . $element);
$xs->element(
'activity:object-type',
null,
$type
);
$xs->element(
'id',
null,
$this->uri); // ?
$xs->element('title', null, $self->getBestName());
$xs->element(
'link', array(
'type' => $avatarType,
'href' => $avatarHref
),
''
);
$xs->elementEnd('activity:' . $element);
return $xs->getString();
} }
/** /**
@ -486,36 +422,6 @@ class Ostatus_profile extends Memcached_DataObject
} }
} }
function atomFeed($actor)
{
$feed = new Atom10Feed();
// @fixme should these be set up somewhere else?
$feed->addNamespace('activity', 'http://activitystrea.ms/spec/1.0/');
$feed->addNamespace('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_local_url('ApiTimelineUser',
array('id' => $actor->id,
'type' => 'atom')),
array('rel' => 'self',
'type' => 'application/atom+xml'));
$feed->addLink(common_local_url('userbyid',
array('id' => $actor->id)),
array('rel' => 'alternate',
'type' => 'text/html'));
return $feed;
}
/** /**
* Read and post notices for updates from the feed. * Read and post notices for updates from the feed.
* Currently assumes that all items in the feed are new, * Currently assumes that all items in the feed are new,
@ -791,11 +697,18 @@ class Ostatus_profile extends Memcached_DataObject
{ {
// Get the canonical feed URI and check it // Get the canonical feed URI and check it
$discover = new FeedDiscovery(); $discover = new FeedDiscovery();
$feeduri = $discover->discoverFromURL($profile_uri); if ($hints['feedurl']) {
$feeduri = $hints['feedurl'];
$feeduri = $discover->discoverFromFeedURL($feeduri);
} else {
$feeduri = $discover->discoverFromURL($profile_uri);
$hints['feedurl'] = $feeduri;
}
//$feedsub = FeedSub::ensureFeed($feeduri, $discover->feed);
$huburi = $discover->getAtomLink('hub'); $huburi = $discover->getAtomLink('hub');
$hints['hub'] = $huburi;
$salmonuri = $discover->getAtomLink('salmon'); $salmonuri = $discover->getAtomLink('salmon');
$hints['salmon'] = $salmonuri;
if (!$huburi) { if (!$huburi) {
// We can only deal with folks with a PuSH hub // We can only deal with folks with a PuSH hub
@ -810,7 +723,7 @@ class Ostatus_profile extends Memcached_DataObject
if (!empty($subject)) { if (!empty($subject)) {
$subjObject = new ActivityObject($subject); $subjObject = new ActivityObject($subject);
return self::ensureActivityObjectProfile($subjObject, $feeduri, $salmonuri, $hints); return self::ensureActivityObjectProfile($subjObject, $hints);
} }
// Otherwise, try the feed author // Otherwise, try the feed author
@ -819,7 +732,7 @@ class Ostatus_profile extends Memcached_DataObject
if (!empty($author)) { if (!empty($author)) {
$authorObject = new ActivityObject($author); $authorObject = new ActivityObject($author);
return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri, $hints); return self::ensureActivityObjectProfile($authorObject, $hints);
} }
// Sheesh. Not a very nice feed! Let's try fingerpoken in the // Sheesh. Not a very nice feed! Let's try fingerpoken in the
@ -835,7 +748,7 @@ class Ostatus_profile extends Memcached_DataObject
if (!empty($actor)) { if (!empty($actor)) {
$actorObject = new ActivityObject($actor); $actorObject = new ActivityObject($actor);
return self::ensureActivityObjectProfile($actorObject, $feeduri, $salmonuri, $hints); return self::ensureActivityObjectProfile($actorObject, $hints);
} }
@ -843,7 +756,7 @@ class Ostatus_profile extends Memcached_DataObject
if (!empty($author)) { if (!empty($author)) {
$authorObject = new ActivityObject($author); $authorObject = new ActivityObject($author);
return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri, $hints); return self::ensureActivityObjectProfile($authorObject, $hints);
} }
} }
@ -988,18 +901,18 @@ class Ostatus_profile extends Memcached_DataObject
* @return Ostatus_profile * @return Ostatus_profile
*/ */
public static function ensureActorProfile($activity, $feeduri=null, $salmonuri=null) public static function ensureActorProfile($activity, $hints=array())
{ {
return self::ensureActivityObjectProfile($activity->actor, $feeduri, $salmonuri); return self::ensureActivityObjectProfile($activity->actor, $hints);
} }
public static function ensureActivityObjectProfile($object, $feeduri=null, $salmonuri=null, $hints=array()) public static function ensureActivityObjectProfile($object, $hints=array())
{ {
$profile = self::getActivityObjectProfile($object); $profile = self::getActivityObjectProfile($object);
if ($profile) { if ($profile) {
$profile->updateFromActivityObject($object, $hints); $profile->updateFromActivityObject($object, $hints);
} else { } else {
$profile = self::createActivityObjectProfile($object, $feeduri, $salmonuri, $hints); $profile = self::createActivityObjectProfile($object, $hints);
} }
return $profile; return $profile;
} }
@ -1045,58 +958,55 @@ class Ostatus_profile extends Memcached_DataObject
* @fixme validate stuff somewhere * @fixme validate stuff somewhere
*/ */
protected static function createActorProfile($activity, $feeduri=null, $salmonuri=null)
{
$actor = $activity->actor;
self::createActivityObjectProfile($actor, $feeduri, $salmonuri);
}
/** /**
* Create local ostatus_profile and profile/user_group entries for * Create local ostatus_profile and profile/user_group entries for
* the provided remote user or group. * the provided remote user or group.
* *
* @param ActivityObject $object * @param ActivityObject $object
* @param string $feeduri
* @param string $salmonuri
* @param array $hints * @param array $hints
* *
* @fixme fold $feeduri/$salmonuri into $hints
* @return Ostatus_profile * @return Ostatus_profile
*/ */
protected static function createActivityObjectProfile($object, $feeduri=null, $salmonuri=null, $hints=array()) protected static function createActivityObjectProfile($object, $hints=array())
{ {
$homeuri = $object->id; $homeuri = $object->id;
$discover = false;
if (!$homeuri) { if (!$homeuri) {
common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true)); common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
throw new ServerException("No profile URI"); throw new ServerException("No profile URI");
} }
if (empty($feeduri)) { if (array_key_exists('feedurl', $hints)) {
if (array_key_exists('feedurl', $hints)) { $feeduri = $hints['feedurl'];
$feeduri = $hints['feedurl']; } else {
}
}
if (empty($salmonuri)) {
if (array_key_exists('salmon', $hints)) {
$salmonuri = $hints['salmon'];
}
}
if (!$feeduri || !$salmonuri) {
// Get the canonical feed URI and check it
$discover = new FeedDiscovery(); $discover = new FeedDiscovery();
$feeduri = $discover->discoverFromURL($homeuri); $feeduri = $discover->discoverFromURL($homeuri);
}
$huburi = $discover->getAtomLink('hub'); if (array_key_exists('salmon', $hints)) {
$salmonuri = $discover->getAtomLink('salmon'); $salmonuri = $hints['salmon'];
} else {
if (!$huburi) { if (!$discover) {
// We can only deal with folks with a PuSH hub $discover = new FeedDiscovery();
throw new FeedSubNoHubException(); $discover->discoverFromFeedURL($hints['feedurl']);
} }
$salmonuri = $discover->getAtomLink('salmon');
}
if (array_key_exists('hub', $hints)) {
$huburi = $hints['hub'];
} else {
if (!$discover) {
$discover = new FeedDiscovery();
$discover->discoverFromFeedURL($hints['feedurl']);
}
$huburi = $discover->getAtomLink('hub');
}
if (!$huburi) {
// We can only deal with folks with a PuSH hub
throw new FeedSubNoHubException();
} }
$oprofile = new Ostatus_profile(); $oprofile = new Ostatus_profile();
@ -1375,9 +1285,9 @@ class Ostatus_profile extends Memcached_DataObject
// Now, try some discovery // Now, try some discovery
$wf = new Webfinger(); $disco = new Discovery();
$result = $wf->lookup($addr); $result = $disco->lookup($addr);
if (!$result) { if (!$result) {
self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null); self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
@ -1386,16 +1296,16 @@ class Ostatus_profile extends Memcached_DataObject
foreach ($result->links as $link) { foreach ($result->links as $link) {
switch ($link['rel']) { switch ($link['rel']) {
case Webfinger::PROFILEPAGE: case Discovery::PROFILEPAGE:
$profileUrl = $link['href']; $profileUrl = $link['href'];
break; break;
case 'salmon': case 'salmon':
$salmonEndpoint = $link['href']; $salmonEndpoint = $link['href'];
break; break;
case Webfinger::UPDATESFROM: case Discovery::UPDATESFROM:
$feedUrl = $link['href']; $feedUrl = $link['href'];
break; break;
case Webfinger::HCARD: case Discovery::HCARD:
$hcardUrl = $link['href']; $hcardUrl = $link['href'];
break; break;
default: default:

View File

@ -0,0 +1,310 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* A sample module to show best practices for StatusNet plugins
*
* PHP version 5
*
* 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 StatusNet
* @author James Walker <james@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
/**
* This class implements LRDD-based service discovery based on the "Hammer Draft"
* (including webfinger)
*
* @see http://groups.google.com/group/webfinger/browse_thread/thread/9f3d93a479e91bbf
*/
class Discovery
{
const LRDD_REL = 'lrdd';
const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
const HCARD = 'http://microformats.org/profile/hcard';
public $methods = array();
public function __construct()
{
$this->registerMethod('Discovery_LRDD_Host_Meta');
$this->registerMethod('Discovery_LRDD_Link_Header');
$this->registerMethod('Discovery_LRDD_Link_HTML');
}
public function registerMethod($class)
{
$this->methods[] = $class;
}
/**
* Given a "user id" make sure it's normalized to either a webfinger
* acct: uri or a profile HTTP URL.
*/
public static function normalize($user_id)
{
if (substr($user_id, 0, 5) == 'http:' ||
substr($user_id, 0, 6) == 'https:' ||
substr($user_id, 0, 5) == 'acct:') {
return $user_id;
}
if (strpos($user_id, '@') !== FALSE) {
return 'acct:' . $user_id;
}
return 'http://' . $user_id;
}
public static function isWebfinger($user_id)
{
$uri = Discovery::normalize($user_id);
return (substr($uri, 0, 5) == 'acct:');
}
/**
* This implements the actual lookup procedure
*/
public function lookup($id)
{
// Normalize the incoming $id to make sure we have a uri
$uri = $this->normalize($id);
foreach ($this->methods as $class) {
$links = call_user_func(array($class, 'discover'), $uri);
if ($link = Discovery::getService($links, Discovery::LRDD_REL)) {
// Load the LRDD XRD
if ($link['template']) {
$xrd_uri = Discovery::applyTemplate($link['template'], $uri);
} else {
$xrd_uri = $link['href'];
}
$xrd = $this->fetchXrd($xrd_uri);
if ($xrd) {
return $xrd;
}
}
}
throw new Exception('Unable to find services for '. $id);
}
public static function getService($links, $service) {
if (!is_array($links)) {
return false;
}
foreach ($links as $link) {
if ($link['rel'] == $service) {
return $link;
}
}
}
public static function applyTemplate($template, $id)
{
$template = str_replace('{uri}', urlencode($id), $template);
return $template;
}
public static function fetchXrd($url)
{
try {
$client = new HTTPClient();
$response = $client->get($url);
} catch (HTTP_Request2_Exception $e) {
return false;
}
if ($response->getStatus() != 200) {
return false;
}
return XRD::parse($response->getBody());
}
}
interface Discovery_LRDD
{
public function discover($uri);
}
class Discovery_LRDD_Host_Meta implements Discovery_LRDD
{
public function discover($uri)
{
if (!Discovery::isWebfinger($uri)) {
return false;
}
// We have a webfinger acct: - start with host-meta
list($name, $domain) = explode('@', $uri);
$url = 'http://'. $domain .'/.well-known/host-meta';
$xrd = Discovery::fetchXrd($url);
if ($xrd) {
if ($xrd->host != $domain) {
return false;
}
return $xrd->links;
}
}
}
class Discovery_LRDD_Link_Header implements Discovery_LRDD
{
public function discover($uri)
{
try {
$client = new HTTPClient();
$response = $client->get($uri);
} catch (HTTP_Request2_Exception $e) {
return false;
}
if ($response->getStatus() != 200) {
return false;
}
$link_header = $response->getHeader('Link');
if (!$link_header) {
// return false;
}
return Discovery_LRDD_Link_Header::parseHeader($link_header);
}
protected static function parseHeader($header)
{
preg_match('/^<[^>]+>/', $header, $uri_reference);
//if (empty($uri_reference)) return;
$links = array();
$link_uri = trim($uri_reference[0], '<>');
$link_rel = array();
$link_type = null;
// remove uri-reference from header
$header = substr($header, strlen($uri_reference[0]));
// parse link-params
$params = explode(';', $header);
foreach ($params as $param) {
if (empty($param)) continue;
list($param_name, $param_value) = explode('=', $param, 2);
$param_name = trim($param_name);
$param_value = preg_replace('(^"|"$)', '', trim($param_value));
// for now we only care about 'rel' and 'type' link params
// TODO do something with the other links-params
switch ($param_name) {
case 'rel':
$link_rel = trim($param_value);
break;
case 'type':
$link_type = trim($param_value);
}
}
$links[] = array(
'href' => $link_uri,
'rel' => $link_rel,
'type' => $link_type);
return $links;
}
}
class Discovery_LRDD_Link_HTML implements Discovery_LRDD
{
public function discover($uri)
{
try {
$client = new HTTPClient();
$response = $client->get($uri);
} catch (HTTP_Request2_Exception $e) {
return false;
}
if ($response->getStatus() != 200) {
return false;
}
return Discovery_LRDD_Link_HTML::parse($response->getBody());
}
public function parse($html)
{
$links = array();
preg_match('/<head(\s[^>]*)?>(.*?)<\/head>/is', $html, $head_matches);
$head_html = $head_matches[2];
preg_match_all('/<link\s[^>]*>/i', $head_html, $link_matches);
foreach ($link_matches[0] as $link_html) {
$link_url = null;
$link_rel = null;
$link_type = null;
preg_match('/\srel=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $rel_matches);
if ( isset($rel_matches[3]) ) {
$link_rel = $rel_matches[3];
} else if ( isset($rel_matches[1]) ) {
$link_rel = $rel_matches[1];
}
preg_match('/\shref=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $href_matches);
if ( isset($href_matches[3]) ) {
$link_uri = $href_matches[3];
} else if ( isset($href_matches[1]) ) {
$link_uri = $href_matches[1];
}
preg_match('/\stype=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $type_matches);
if ( isset($type_matches[3]) ) {
$link_type = $type_matches[3];
} else if ( isset($type_matches[1]) ) {
$link_type = $type_matches[1];
}
$links[] = array(
'href' => $link_url,
'rel' => $link_rel,
'type' => $link_type,
);
}
return $links;
}
}

View File

@ -50,7 +50,20 @@ class MagicEnvelope
public function getKeyPair($signer_uri) public function getKeyPair($signer_uri)
{ {
return 'RSA.79_L2gq-TD72Nsb5yGS0r9stLLpJZF5AHXyxzWmQmlqKl276LEJEs8CppcerLcR90MbYQUwt-SX9slx40Yq3vA==.AQAB.AR-jo5KMfSISmDAT2iMs2_vNFgWRjl5rbJVvA0SpGIEWyPdCGxlPtCbTexp8-0ZEIe8a4SyjatBECH5hxgMTpw=='; $disco = new Discovery();
try {
$xrd = $disco->lookup($signer_uri);
} catch (Exception $e) {
return false;
}
if ($xrd->links) {
if ($link = Discovery::getService($xrd->links, Magicsig::PUBLICKEYREL)) {
list($type, $keypair) = explode(';', $link['href']);
return $keypair;
}
}
throw new Exception('Unable to locate signer public key');
} }
@ -59,10 +72,14 @@ class MagicEnvelope
$signer_uri = $this->normalizeUser($signer_uri); $signer_uri = $this->normalizeUser($signer_uri);
if (!$this->checkAuthor($text, $signer_uri)) { if (!$this->checkAuthor($text, $signer_uri)) {
return false; throw new Exception("Unable to determine entry author.");
} }
$signature_alg = Magicsig::fromString($this->getKeyPair($signer_uri)); $keypair = $this->getKeyPair($signer_uri);
if (!$keypair) {
throw new Exception("Unable to retrive keypair for ". $signer_uri);
}
$signature_alg = Magicsig::fromString($keypair);
$armored_text = base64_encode($text); $armored_text = base64_encode($text);
return array( return array(

View File

@ -72,8 +72,12 @@ class Salmon
// TODO: Should probably be getting the signer uri as an argument? // TODO: Should probably be getting the signer uri as an argument?
$signer_uri = $magic_env->getAuthor($text); $signer_uri = $magic_env->getAuthor($text);
$env = $magic_env->signMessage($text, 'application/atom+xml', $signer_uri); try {
$env = $magic_env->signMessage($text, 'application/atom+xml', $signer_uri);
} catch (Exception $e) {
common_log(LOG_ERR, "Salmon signing failed: ". $e->getMessage());
return $text;
}
return $magic_env->unfold($env); return $magic_env->unfold($env);
} }

View File

@ -1,164 +0,0 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* A sample module to show best practices for StatusNet plugins
*
* PHP version 5
*
* 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 StatusNet
* @author James Walker <james@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
define('WEBFINGER_SERVICE_REL_VALUE', 'lrdd');
/**
* Implement the webfinger protocol.
*/
class Webfinger
{
const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
const HCARD = 'http://microformats.org/profile/hcard';
/**
* Perform a webfinger lookup given an account.
*/
public function lookup($id)
{
$id = $this->normalize($id);
list($name, $domain) = explode('@', $id);
$links = $this->getServiceLinks($domain);
if (!$links) {
return false;
}
$services = array();
foreach ($links as $link) {
if ($link['template']) {
return $this->getServiceDescription($link['template'], $id);
}
if ($link['href']) {
return $this->getServiceDescription($link['href'], $id);
}
}
}
/**
* Normalize an account ID
*/
function normalize($id)
{
if (substr($id, 0, 7) == 'acct://') {
return substr($id, 7);
} else if (substr($id, 0, 5) == 'acct:') {
return substr($id, 5);
}
return $id;
}
function getServiceLinks($domain)
{
$url = 'http://'. $domain .'/.well-known/host-meta';
$content = $this->fetchURL($url);
if (empty($content)) {
common_log(LOG_DEBUG, 'Error fetching host-meta');
return false;
}
$result = XRD::parse($content);
// Ensure that the host == domain (spec may include signing later)
if ($result->host != $domain) {
return false;
}
$links = array();
foreach ($result->links as $link) {
if ($link['rel'] == WEBFINGER_SERVICE_REL_VALUE) {
$links[] = $link;
}
}
return $links;
}
function getServiceDescription($template, $id)
{
$url = $this->applyTemplate($template, 'acct:' . $id);
$content = $this->fetchURL($url);
if (!$content) {
return false;
}
return XRD::parse($content);
}
function fetchURL($url)
{
try {
$c = Cache::instance();
$content = $c->get('webfinger:url:'.$url);
if ($content !== false) {
return $content;
}
$client = new HTTPClient();
$response = $client->get($url);
} catch (HTTP_Request2_Exception $e) {
return false;
}
if ($response->getStatus() != 200) {
return false;
}
$body = $response->getBody();
$c->set('webfinger:url:'.$url, $body);
return $body;
}
function applyTemplate($template, $id)
{
$template = str_replace('{uri}', urlencode($id), $template);
return $template;
}
function getHostMeta($domain, $template) {
$xrd = new XRD();
$xrd->host = $domain;
$xrd->links[] = array('rel' => 'lrdd',
'template' => $template,
'title' => array('Resource Descriptor'));
return $xrd->toXML();
}
}

View File

@ -244,8 +244,6 @@ class RealtimePlugin extends Plugin
// of refactoring from within a plugin, so I'm just abusing // of refactoring from within a plugin, so I'm just abusing
// the ApiAction method. Don't do this unless you're me! // the ApiAction method. Don't do this unless you're me!
require_once(INSTALLDIR.'/lib/api.php');
$act = new ApiAction('/dev/null'); $act = new ApiAction('/dev/null');
$arr = $act->twitterStatusArray($notice, true); $arr = $act->twitterStatusArray($notice, true);

View File

@ -121,10 +121,14 @@ class ActivityParseTests extends PHPUnit_Framework_TestCase
$this->assertEquals($act->actor->title, 'Test User'); $this->assertEquals($act->actor->title, 'Test User');
$this->assertEquals($act->actor->id, 'http://example.net/mysite/user/3'); $this->assertEquals($act->actor->id, 'http://example.net/mysite/user/3');
$this->assertEquals($act->actor->link, 'http://example.net/mysite/testuser'); $this->assertEquals($act->actor->link, 'http://example.net/mysite/testuser');
$avatars = $act->actor->avatarLinks;
$this->assertEquals( $this->assertEquals(
$act->actor->avatar, $avatars[0]->url,
'http://example.net/mysite/avatar/3-96-20100224004207.jpeg' 'http://example.net/mysite/avatar/3-96-20100224004207.jpeg'
); );
$this->assertEquals($act->actor->displayName, 'Test User'); $this->assertEquals($act->actor->displayName, 'Test User');
$poco = $act->actor->poco; $poco = $act->actor->poco;