X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FOStatus%2Fclasses%2FOstatus_profile.php;h=ee126c02a19dea1eca574d616724a3b643c70830;hb=99194e03fa50b61f99164674afc949b4bbefd44a;hp=34bff548dbf15d769a8133c84a05cb4055a91229;hpb=29a25848dee2863b380779d6b8f979b66365d924;p=quix0rs-gnu-social.git diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php index 34bff548db..ee126c02a1 100644 --- a/plugins/OStatus/classes/Ostatus_profile.php +++ b/plugins/OStatus/classes/Ostatus_profile.php @@ -22,7 +22,7 @@ * @maintainer Brion Vibber */ -class Ostatus_profile extends Memcached_DataObject +class Ostatus_profile extends Managed_DataObject { public $__table = 'ostatus_profile'; @@ -44,77 +44,35 @@ class Ostatus_profile extends Memcached_DataObject } /** - * 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 table definition for Schema setup and DB_DataObject usage. * * @return array array of column definitions */ - function table() - { - return array('uri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL, - 'profile_id' => DB_DATAOBJECT_INT, - 'group_id' => DB_DATAOBJECT_INT, - 'feeduri' => DB_DATAOBJECT_STR, - 'salmonuri' => DB_DATAOBJECT_STR, - 'avatar' => DB_DATAOBJECT_STR, - 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL, - 'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL); - } - static function schemaDef() { - return array(new ColumnDef('uri', 'varchar', - 255, false, 'PRI'), - new ColumnDef('profile_id', 'integer', - null, true, 'UNI'), - new ColumnDef('group_id', 'integer', - null, true, 'UNI'), - new ColumnDef('feeduri', 'varchar', - 255, true, 'UNI'), - new ColumnDef('salmonuri', 'text', - null, true), - new ColumnDef('avatar', 'text', - null, true), - new ColumnDef('created', 'datetime', - null, false), - new ColumnDef('modified', '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() - { - return array_keys($this->keyTypes()); - } - - /** - * 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() - { - return array('uri' => 'K', 'profile_id' => 'U', 'group_id' => 'U', 'feeduri' => 'U'); - } - - function sequenceKey() - { - return array(false, false, false); + return array( + 'fields' => array( + 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true), + 'profile_id' => array('type' => 'integer'), + 'group_id' => array('type' => 'integer'), + 'feeduri' => array('type' => 'varchar', 'length' => 255), + 'salmonuri' => array('type' => 'varchar', 'length' => 255), + 'avatar' => array('type' => 'text'), + 'created' => array('type' => 'datetime', 'not null' => true), + 'modified' => array('type' => 'datetime', 'not null' => true), + ), + 'primary key' => array('uri'), + 'unique keys' => array( + 'ostatus_profile_profile_id_idx' => array('profile_id'), + 'ostatus_profile_group_id_idx' => array('group_id'), + 'ostatus_profile_feeduri_idx' => array('feeduri'), + ), + 'foreign keys' => array( + 'profile_id' => array('profile' => 'id'), + 'group_id' => array('user_group' => 'id'), + ), + ); } /** @@ -188,9 +146,11 @@ class Ostatus_profile extends Memcached_DataObject } else if ($this->group_id && !$this->profile_id) { return true; } else if ($this->group_id && $this->profile_id) { - throw new ServerException("Invalid ostatus_profile state: both group and profile IDs set for $this->uri"); + // TRANS: Server exception. + throw new ServerException(sprintf(_m('Invalid ostatus_profile state: both group and profile IDs set for %s.'),$this->uri)); } else { - throw new ServerException("Invalid ostatus_profile state: both group and profile IDs empty for $this->uri"); + // TRANS: Server exception. + throw new ServerException(sprintf(_m('Invalid ostatus_profile state: both group and profile IDs empty for %s.'),$this->uri)); } } @@ -215,22 +175,13 @@ class Ostatus_profile extends Memcached_DataObject } /** - * Send a PuSH unsubscription request to the hub for this feed. - * The hub will later send us a confirmation POST to /main/push/callback. + * Check if this remote profile has any active local subscriptions, and + * if not drop the PuSH subscription feed. * * @return bool true on success, false on failure - * @throws ServerException if feed state is not valid */ public function unsubscribe() { - $feedsub = FeedSub::staticGet('uri', $this->feeduri); - if (!$feedsub || $feedsub->sub_state == '' || $feedsub->sub_state == 'inactive') { - // No active PuSH subscription, we can just leave it be. - return true; - } else { - // PuSH subscription is either active or in an indeterminate state. - // Send an unsubscribe. - return $feedsub->unsubscribe(); - } + $this->garbageCollect(); } /** @@ -240,6 +191,21 @@ class Ostatus_profile extends Memcached_DataObject * @return boolean */ public function garbageCollect() + { + $feedsub = FeedSub::staticGet('uri', $this->feeduri); + return $feedsub->garbageCollect(); + } + + /** + * Check if this remote profile has any active local subscriptions, so the + * PuSH subscription layer can decide if it can drop the feed. + * + * This gets called via the FeedSubSubscriberCount event when running + * FeedSub::garbageCollect(). + * + * @return int + */ + public function subscriberCount() { if ($this->isGroup()) { $members = $this->localGroup()->getMembers(0, 1); @@ -247,13 +213,14 @@ class Ostatus_profile extends Memcached_DataObject } else { $count = $this->localProfile()->subscriberCount(); } - if ($count == 0) { - common_log(LOG_INFO, "Unsubscribing from now-unused remote feed $this->feeduri"); - $this->unsubscribe(); - return true; - } else { - return false; - } + common_log(LOG_INFO, __METHOD__ . " SUB COUNT BEFORE: $count"); + + // Other plugins may be piggybacking on OStatus without having + // an active group or user-to-user subscription we know about. + Event::handle('Ostatus_profileSubscriberCount', array($this, &$count)); + common_log(LOG_INFO, __METHOD__ . " SUB COUNT AFTER: $count"); + + return $count; } /** @@ -271,7 +238,9 @@ class Ostatus_profile extends Memcached_DataObject if ($type == 'object') { $type = get_class($actor); } - throw new ServerException("Invalid actor passed to " . __METHOD__ . ": " . $type); + // TRANS: Server exception. + // TRANS: %1$s is the method name the exception occured in, %2$s is the actor type. + throw new ServerException(sprintf(_m('Invalid actor passed to %1$s: %2$s.'),__METHOD__,$type)); } if ($object == null) { $object = $this; @@ -363,7 +332,8 @@ class Ostatus_profile extends Memcached_DataObject } else if ($entry instanceof Notice) { return $preamble . $entry->asAtomEntry(true, true); } else { - throw new ServerException("Invalid type passed to Ostatus_profile::notify; must be XML string or Activity entry"); + // TRANS: Server exception. + throw new ServerException(_m('Invalid type passed to Ostatus_profile::notify. It must be XML string or Activity entry.')); } } @@ -393,7 +363,7 @@ class Ostatus_profile extends Memcached_DataObject } else if ($feed->localName == 'rss') { // @fixme check namespace $this->processRssFeed($feed, $source); } else { - throw new Exception("Unknown feed format."); + throw new Exception(_m('Unknown feed format.')); } } @@ -416,7 +386,7 @@ class Ostatus_profile extends Memcached_DataObject $channels = $rss->getElementsByTagName('channel'); if ($channels->length == 0) { - throw new Exception("RSS feed without a channel."); + throw new Exception(_m('RSS feed without a channel.')); } else if ($channels->length > 1) { common_log(LOG_WARNING, __METHOD__ . ": more than one channel in an RSS feed"); } @@ -438,26 +408,33 @@ class Ostatus_profile extends Memcached_DataObject * @param DOMElement $feed for context * @param string $source identifier ("push" or "salmon") */ + public function processEntry($entry, $feed, $source) { $activity = new Activity($entry, $feed); - // @todo process all activity objects - switch ($activity->objects[0]->type) { - case ActivityObject::ARTICLE: - case ActivityObject::BLOGENTRY: - case ActivityObject::NOTE: - case ActivityObject::STATUS: - case ActivityObject::COMMENT: - break; - default: - throw new ClientException("Can't handle that kind of post."); - } + if (Event::handle('StartHandleFeedEntry', array($activity))) { + + // @todo process all activity objects + switch ($activity->objects[0]->type) { + case ActivityObject::ARTICLE: + case ActivityObject::BLOGENTRY: + case ActivityObject::NOTE: + case ActivityObject::STATUS: + case ActivityObject::COMMENT: + case null: + if ($activity->verb == ActivityVerb::POST) { + $this->processPost($activity, $source); + } else { + common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb"); + } + break; + default: + // TRANS: Client exception. + throw new ClientException(_m('Can\'t handle that kind of post.')); + } - if ($activity->verb == ActivityVerb::POST) { - $this->processPost($activity, $source); - } else { - common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb"); + Event::handle('EndHandleFeedEntry', array($activity)); } } @@ -486,8 +463,17 @@ class Ostatus_profile extends Memcached_DataObject // OK here! assume the default } else if ($actor->id == $this->uri || $actor->link == $this->uri) { $this->updateFromActivityObject($actor); + } else if ($actor->id) { + // We have an ActivityStreams actor with an explicit ID that doesn't match the feed owner. + // This isn't what we expect from mainline OStatus person feeds! + // Group feeds go down another path, with different validation... + // Most likely this is a plain ol' blog feed of some kind which + // doesn't match our expectations. We'll take the entry, but ignore + // the info. + common_log(LOG_WARNING, "Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}"); } else { - throw new Exception("Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}"); + // Plain without ActivityStreams actor info. + // We'll just ignore this info for now and save the update under the feed's identity. } $oprofile = $this; @@ -527,13 +513,14 @@ class Ostatus_profile extends Memcached_DataObject $sourceContent = $note->title; } else { // @fixme fetch from $sourceUrl? - throw new ClientException("No content for notice {$sourceUri}"); + // TRANS: Client exception. %s is a source URL. + throw new ClientException(sprintf(_m('No content for notice %s.'),$sourceUri)); } // Get (safe!) HTML and text versions of the content $rendered = $this->purify($sourceContent); - $content = html_entity_decode(strip_tags($rendered)); + $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8'); $shortened = common_shorten_links($content); @@ -544,7 +531,7 @@ class Ostatus_profile extends Memcached_DataObject if (Notice::contentTooLong($shortened)) { $attachment = $this->saveHTMLFile($note->title, $rendered); - $summary = html_entity_decode(strip_tags($note->summary)); + $summary = html_entity_decode(strip_tags($note->summary), ENT_QUOTES, 'UTF-8'); if (empty($summary)) { $summary = $content; } @@ -558,12 +545,15 @@ class Ostatus_profile extends Memcached_DataObject // We mark up the attachment link specially for the HTML output // so we can fold-out the full version inline. + + // TRANS: Shown when a notice is longer than supported and/or when attachments are present. + $showMoreText = _m('Show more'); $attachUrl = common_local_url('attachment', array('attachment' => $attachment->id)); $rendered = common_render_text($shortSummary) . '' . + ' title="'. htmlspecialchars($showMoreText) . '">' . '…' . ''; } @@ -668,7 +658,7 @@ class Ostatus_profile extends Memcached_DataObject common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', $attention_uris)); $groups = array(); $replies = array(); - foreach ($attention_uris as $recipient) { + foreach (array_unique($attention_uris) as $recipient) { // Is the recipient a local user? $user = User::staticGet('uri', $recipient); if ($user) { @@ -677,21 +667,7 @@ class Ostatus_profile extends Memcached_DataObject continue; } - // Is the recipient a remote group? - $oprofile = Ostatus_profile::staticGet('uri', $recipient); - if ($oprofile) { - if ($oprofile->isGroup()) { - // Deliver to local members of this remote group. - // @fixme sender verification? - $groups[] = $oprofile->group_id; - } else { - common_log(LOG_DEBUG, "Skipping reply to remote profile $recipient"); - } - continue; - } - // Is the recipient a local group? - // @fixme uri on user_group isn't reliable yet // $group = User_group::staticGet('uri', $recipient); $id = OStatusPlugin::localGroupFromUrl($recipient); if ($id) { @@ -710,7 +686,22 @@ class Ostatus_profile extends Memcached_DataObject } } - common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient"); + // Is the recipient a remote user or group? + try { + $oprofile = Ostatus_profile::ensureProfileURI($recipient); + if ($oprofile->isGroup()) { + // Deliver to local members of this remote group. + // @fixme sender verification? + $groups[] = $oprofile->group_id; + } else { + // may be canonicalized or something + $replies[] = $oprofile->uri; + } + continue; + } catch (Exception $e) { + // Neither a recognizable local nor remote user! + common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient: " . $e->getMessage()); + } } $attention_uris = $replies; @@ -726,7 +717,8 @@ class Ostatus_profile extends Memcached_DataObject * * @param string $profile_url * @return Ostatus_profile - * @throws Exception + * @throws Exception on various error conditions + * @throws OStatusShadowException if this reference would obscure a local user/group */ public static function ensureProfileURL($profile_url, $hints=array()) @@ -747,7 +739,8 @@ class Ostatus_profile extends Memcached_DataObject $response = $client->get($profile_url); if (!$response->isOk()) { - throw new Exception("Could not reach profile page: " . $profile_url); + // TRANS: Exception. %s is a profile URL. + throw new Exception(sprintf(_m('Could not reach profile page %s.'),$profile_url)); } // Check if we have a non-canonical URL @@ -804,7 +797,8 @@ class Ostatus_profile extends Memcached_DataObject return self::ensureFeedURL($feedurl, $hints); } - throw new Exception("Could not find a feed URL for profile page " . $finalUrl); + // TRANS: Exception. + throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl)); } /** @@ -813,7 +807,7 @@ class Ostatus_profile extends Memcached_DataObject * remote profiles. * * @return mixed Ostatus_profile or null - * @throws Exception for local profiles + * @throws OStatusShadowException for local profiles */ static function getFromProfileURL($profile_url) { @@ -836,7 +830,8 @@ class Ostatus_profile extends Memcached_DataObject $user = User::staticGet('id', $profile->id); if (!empty($user)) { - throw new Exception("'$profile_url' is the profile for local user '{$user->nickname}'."); + // @todo i18n FIXME: use sprintf and add i18n (?) + throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'."); } // Continue discovery; it's a remote profile @@ -861,12 +856,12 @@ class Ostatus_profile extends Memcached_DataObject $feeduri = $discover->discoverFromFeedURL($feed_url); $hints['feedurl'] = $feeduri; - $huburi = $discover->getAtomLink('hub'); + $huburi = $discover->getHubLink(); $hints['hub'] = $huburi; $salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES); $hints['salmon'] = $salmonuri; - if (!$huburi) { + if (!$huburi && !common_config('feedsub', 'fallback_hub')) { // We can only deal with folks with a PuSH hub throw new FeedSubNoHubException(); } @@ -940,8 +935,7 @@ class Ostatus_profile extends Memcached_DataObject } // XXX: make some educated guesses here - - throw new FeedSubException("Can't find enough profile information to make a feed."); + throw new FeedSubException(_m('Can\'t find enough profile information to make a feed.')); } /** @@ -1000,7 +994,7 @@ class Ostatus_profile extends Memcached_DataObject return; } if (!common_valid_http_url($url)) { - throw new ServerException(_m("Invalid avatar URL %s"), $url); + throw new ServerException(sprintf(_m("Invalid avatar URL %s."), $url)); } if ($this->isGroup()) { @@ -1010,7 +1004,7 @@ class Ostatus_profile extends Memcached_DataObject } if (!$self) { throw new ServerException(sprintf( - _m("Tried to update avatar for unsaved remote profile %s"), + _m("Tried to update avatar for unsaved remote profile %s."), $this->uri)); } @@ -1018,7 +1012,7 @@ class Ostatus_profile extends Memcached_DataObject // ripped from oauthstore.php (for old OMB client) $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar'); if (!copy($url, $temp_filename)) { - throw new ServerException(sprintf(_m("Unable to fetch avatar from %s"), $url)); + throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url)); } if ($this->isGroup()) { @@ -1033,6 +1027,14 @@ class Ostatus_profile extends Memcached_DataObject null, common_timestamp()); rename($temp_filename, Avatar::path($filename)); + // @fixme hardcoded chmod is lame, but seems to be necessary to + // keep from accidentally saving images from command-line (queues) + // that can't be read from web server, which causes hard-to-notice + // problems later on: + // + // http://status.net/open-source/issues/2663 + chmod(Avatar::path($filename), 0644); + $self->setOriginal($filename); $orig = clone($this); @@ -1048,7 +1050,7 @@ class Ostatus_profile extends Memcached_DataObject * @return mixed URL string or false */ - protected static function getActivityObjectAvatar($object, $hints=array()) + public static function getActivityObjectAvatar($object, $hints=array()) { if ($object->avatarLinks) { $best = false; @@ -1201,7 +1203,7 @@ class Ostatus_profile extends Memcached_DataObject if ($object->link && common_valid_http_url($object->link)) { return $object->link; } - throw new ServerException("No author ID URI found"); + throw new ServerException("No author ID URI found."); } /** @@ -1231,11 +1233,13 @@ class Ostatus_profile extends Memcached_DataObject $user = User::staticGet('uri', $homeuri); if ($user) { - throw new Exception("Local user can't be referenced as remote."); + // TRANS: Exception. + throw new Exception(_m('Local user can\'t be referenced as remote.')); } if (OStatusPlugin::localGroupFromUrl($homeuri)) { - throw new Exception("Local group can't be referenced as remote."); + // TRANS: Exception. + throw new Exception(_m('Local group can\'t be referenced as remote.')); } if (array_key_exists('feedurl', $hints)) { @@ -1262,10 +1266,10 @@ class Ostatus_profile extends Memcached_DataObject $discover = new FeedDiscovery(); $discover->discoverFromFeedURL($hints['feedurl']); } - $huburi = $discover->getAtomLink('hub'); + $huburi = $discover->getHubLink(); } - if (!$huburi) { + if (!$huburi && !common_config('feedsub', 'fallback_hub')) { // We can only deal with folks with a PuSH hub throw new FeedSubNoHubException(); } @@ -1286,7 +1290,8 @@ class Ostatus_profile extends Memcached_DataObject $oprofile->profile_id = $profile->insert(); if (!$oprofile->profile_id) { - throw new ServerException("Can't save local profile"); + // TRANS: Exception. + throw new ServerException(_m('Can\'t save local profile.')); } } else { $group = new User_group(); @@ -1296,21 +1301,31 @@ class Ostatus_profile extends Memcached_DataObject $oprofile->group_id = $group->insert(); if (!$oprofile->group_id) { - throw new ServerException("Can't save local profile"); + // TRANS: Exception. + throw new ServerException(_m('Can\'t save local profile.')); } } $ok = $oprofile->insert(); - if ($ok) { - $avatar = self::getActivityObjectAvatar($object, $hints); - if ($avatar) { + if (!$ok) { + // TRANS: Exception. + throw new ServerException(_m('Can\'t save OStatus profile.')); + } + + $avatar = self::getActivityObjectAvatar($object, $hints); + + if ($avatar) { + try { $oprofile->updateAvatar($avatar); + } catch (Exception $ex) { + // Profile is saved, but Avatar is messed up. We're + // just going to continue. + common_log(LOG_WARNING, "Exception saving OStatus profile avatar: ". $ex->getMessage()); } - return $oprofile; - } else { - throw new ServerException("Can't save OStatus profile"); } + + return $oprofile; } /** @@ -1329,11 +1344,15 @@ class Ostatus_profile extends Memcached_DataObject } $avatar = self::getActivityObjectAvatar($object, $hints); if ($avatar) { - $this->updateAvatar($avatar); + try { + $this->updateAvatar($avatar); + } catch (Exception $ex) { + common_log(LOG_WARNING, "Exception saving OStatus profile avatar: " . $ex->getMessage()); + } } } - protected static function updateProfile($profile, $object, $hints=array()) + public static function updateProfile($profile, $object, $hints=array()) { $orig = clone($profile); @@ -1461,7 +1480,7 @@ class Ostatus_profile extends Memcached_DataObject return $bio; } - protected static function getActivityObjectNickname($object, $hints=array()) + public static function getActivityObjectNickname($object, $hints=array()) { if ($object->poco) { if (!empty($object->poco->preferredUsername)) { @@ -1538,6 +1557,7 @@ class Ostatus_profile extends Memcached_DataObject * @param string $addr webfinger address * @return Ostatus_profile * @throws Exception on error conditions + * @throws OStatusShadowException if this reference would obscure a local user/group */ public static function ensureWebfinger($addr) { @@ -1548,7 +1568,8 @@ class Ostatus_profile extends Memcached_DataObject if ($uri !== false) { if (is_null($uri)) { // Negative cache entry - throw new Exception('Not a valid webfinger address.'); + // TRANS: Exception. + throw new Exception(_m('Not a valid webfinger address.')); } $oprofile = Ostatus_profile::staticGet('uri', $uri); if (!empty($oprofile)) { @@ -1575,7 +1596,8 @@ class Ostatus_profile extends Memcached_DataObject // Save negative cache entry so we don't waste time looking it up again. // @fixme distinguish temporary failures? self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null); - throw new Exception('Not a valid webfinger address.'); + // TRANS: Exception. + throw new Exception(_m('Not a valid webfinger address.')); } $hints = array('webfinger' => $addr); @@ -1616,9 +1638,18 @@ class Ostatus_profile extends Memcached_DataObject $oprofile = self::ensureProfileURL($hints['profileurl'], $hints); self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri); return $oprofile; + } catch (OStatusShadowException $e) { + // We've ended up with a remote reference to a local user or group. + // @fixme ideally we should be able to say who it was so we can + // go back and refer to it the regular way + throw $e; } catch (Exception $e) { common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage()); // keep looking + // + // @fixme this means an error discovering from profile page + // may give us a corrupt entry using the webfinger URI, which + // will obscure the correct page-keyed profile later on. } } @@ -1647,7 +1678,8 @@ class Ostatus_profile extends Memcached_DataObject if (!$profile_id) { common_log_db_error($profile, 'INSERT', __FILE__); - throw new Exception("Couldn't save profile for '$addr'"); + // TRANS: Exception. %s is a webfinger address. + throw new Exception(sprintf(_m('Couldn\'t save profile for "%s".'),$addr)); } $oprofile = new Ostatus_profile(); @@ -1665,14 +1697,16 @@ class Ostatus_profile extends Memcached_DataObject if (!$result) { common_log_db_error($oprofile, 'INSERT', __FILE__); - throw new Exception("Couldn't save ostatus_profile for '$addr'"); + // TRANS: Exception. %s is a webfinger address. + throw new Exception(sprintf(_m('Couldn\'t save ostatus_profile for "%s".'),$addr)); } self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri); return $oprofile; } - throw new Exception("Couldn't find a valid profile for '$addr'"); + // TRANS: Exception. %s is a webfinger address. + throw new Exception(sprintf(_m('Couldn\'t find a valid profile for "%s".'),$addr)); } /** @@ -1685,7 +1719,11 @@ class Ostatus_profile extends Memcached_DataObject */ function saveHTMLFile($title, $rendered) { - $final = sprintf("\n%s". + $final = sprintf("\n" . + '' . + '' . + '%s' . + '' . '%s', htmlspecialchars($title), $rendered); @@ -1710,9 +1748,60 @@ class Ostatus_profile extends Memcached_DataObject if ($file_id === false) { common_log_db_error($file, "INSERT", __FILE__); - throw new ServerException(_('Could not store HTML content of long post as file.')); + throw new ServerException(_m('Could not store HTML content of long post as file.')); } return $file; } + + static function ensureProfileURI($uri) + { + $oprofile = null; + + // First, try to query it + + $oprofile = Ostatus_profile::staticGet('uri', $uri); + + // If unfound, do discovery stuff + + if (empty($oprofile)) { + if (preg_match("/^(\w+)\:(.*)/", $uri, $match)) { + $protocol = $match[1]; + switch ($protocol) { + case 'http': + case 'https': + $oprofile = Ostatus_profile::ensureProfileURL($uri); + break; + case 'acct': + case 'mailto': + $rest = $match[2]; + $oprofile = Ostatus_profile::ensureWebfinger($rest); + default: + common_log("Unrecognized URI protocol for profile: $protocol ($uri)"); + break; + } + } + } + return $oprofile; + } +} + +/** + * Exception indicating we've got a remote reference to a local user, + * not a remote user! + * + * If we can ue a local profile after all, it's available as $e->profile. + */ +class OStatusShadowException extends Exception +{ + public $profile; + + /** + * @param Profile $profile + * @param string $message + */ + function __construct($profile, $message) { + $this->profile = $profile; + parent::__construct($message); + } }