Ostatus_profile.php 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2009-2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * @package OStatusPlugin
  22. * @author Brion Vibber <brion@status.net>
  23. * @maintainer Mikael Nordfeldth <mmn@hethane.se>
  24. */
  25. class Ostatus_profile extends Managed_DataObject
  26. {
  27. public $__table = 'ostatus_profile';
  28. public $uri;
  29. public $profile_id;
  30. public $group_id;
  31. public $peopletag_id;
  32. public $feeduri;
  33. public $salmonuri;
  34. public $avatar; // remote URL of the last avatar we saved
  35. public $created;
  36. public $modified;
  37. /**
  38. * Return table definition for Schema setup and DB_DataObject usage.
  39. *
  40. * @return array array of column definitions
  41. */
  42. static function schemaDef()
  43. {
  44. return array(
  45. 'fields' => array(
  46. 'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true),
  47. 'profile_id' => array('type' => 'integer'),
  48. 'group_id' => array('type' => 'integer'),
  49. 'peopletag_id' => array('type' => 'integer'),
  50. 'feeduri' => array('type' => 'varchar', 'length' => 191),
  51. 'salmonuri' => array('type' => 'varchar', 'length' => 191),
  52. 'avatar' => array('type' => 'text'),
  53. 'created' => array('type' => 'datetime', 'not null' => true),
  54. 'modified' => array('type' => 'datetime', 'not null' => true),
  55. ),
  56. 'primary key' => array('uri'),
  57. 'unique keys' => array(
  58. 'ostatus_profile_profile_id_key' => array('profile_id'),
  59. 'ostatus_profile_group_id_key' => array('group_id'),
  60. 'ostatus_profile_peopletag_id_key' => array('peopletag_id'),
  61. 'ostatus_profile_feeduri_key' => array('feeduri'),
  62. ),
  63. 'foreign keys' => array(
  64. 'ostatus_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  65. 'ostatus_profile_group_id_fkey' => array('user_group', array('group_id' => 'id')),
  66. 'ostatus_profile_peopletag_id_fkey' => array('profile_list', array('peopletag_id' => 'id')),
  67. ),
  68. );
  69. }
  70. public function getUri()
  71. {
  72. return $this->uri;
  73. }
  74. public function getFeedSub()
  75. {
  76. return FeedSub::getByUri($this->feeduri);
  77. }
  78. static function fromProfile(Profile $profile)
  79. {
  80. $oprofile = Ostatus_profile::getKV('profile_id', $profile->getID());
  81. if (!$oprofile instanceof Ostatus_profile) {
  82. throw new Exception('No Ostatus_profile for Profile ID: '.$profile->getID());
  83. }
  84. return $oprofile;
  85. }
  86. /**
  87. * Fetch the locally stored profile for this feed
  88. * @return Profile
  89. * @throws NoProfileException if it was not found
  90. */
  91. public function localProfile()
  92. {
  93. if ($this->isGroup()) {
  94. return $this->localGroup()->getProfile();
  95. }
  96. $profile = Profile::getKV('id', $this->profile_id);
  97. if (!$profile instanceof Profile) {
  98. throw new NoProfileException($this->profile_id);
  99. }
  100. return $profile;
  101. }
  102. /**
  103. * Fetch the StatusNet-side profile for this feed
  104. * @return Profile
  105. */
  106. public function localGroup()
  107. {
  108. $group = User_group::getKV('id', $this->group_id);
  109. if (!$group instanceof User_group) {
  110. throw new NoSuchGroupException(array('id'=>$this->group_id));
  111. }
  112. return $group;
  113. }
  114. /**
  115. * Fetch the StatusNet-side peopletag for this feed
  116. * @return Profile
  117. */
  118. public function localPeopletag()
  119. {
  120. if ($this->peopletag_id) {
  121. return Profile_list::getKV('id', $this->peopletag_id);
  122. }
  123. return null;
  124. }
  125. /**
  126. * Returns an ActivityObject describing this remote user or group profile.
  127. * Can then be used to generate Atom chunks.
  128. *
  129. * @return ActivityObject
  130. */
  131. function asActivityObject()
  132. {
  133. if ($this->isGroup()) {
  134. return ActivityObject::fromGroup($this->localGroup());
  135. } else if ($this->isPeopletag()) {
  136. return ActivityObject::fromPeopletag($this->localPeopletag());
  137. } else {
  138. return $this->localProfile()->asActivityObject();
  139. }
  140. }
  141. /**
  142. * Returns an XML string fragment with profile information as an
  143. * Activity Streams noun object with the given element type.
  144. *
  145. * Assumes that 'activity' namespace has been previously defined.
  146. *
  147. * @todo FIXME: Replace with wrappers on asActivityObject when it's got everything.
  148. *
  149. * @param string $element one of 'actor', 'subject', 'object', 'target'
  150. * @return string
  151. */
  152. function asActivityNoun($element)
  153. {
  154. if ($this->isGroup()) {
  155. $noun = ActivityObject::fromGroup($this->localGroup());
  156. return $noun->asString('activity:' . $element);
  157. } else if ($this->isPeopletag()) {
  158. $noun = ActivityObject::fromPeopletag($this->localPeopletag());
  159. return $noun->asString('activity:' . $element);
  160. } else {
  161. $noun = $this->localProfile()->asActivityObject();
  162. return $noun->asString('activity:' . $element);
  163. }
  164. }
  165. /**
  166. * @return boolean true if this is a remote group
  167. */
  168. function isGroup()
  169. {
  170. if ($this->profile_id || $this->peopletag_id && !$this->group_id) {
  171. return false;
  172. } else if ($this->group_id && !$this->profile_id && !$this->peopletag_id) {
  173. return true;
  174. } else if ($this->group_id && ($this->profile_id || $this->peopletag_id)) {
  175. // TRANS: Server exception. %s is a URI
  176. throw new ServerException(sprintf(_m('Invalid ostatus_profile state: Two or more IDs set for %s.'), $this->getUri()));
  177. } else {
  178. // TRANS: Server exception. %s is a URI
  179. throw new ServerException(sprintf(_m('Invalid ostatus_profile state: All IDs empty for %s.'), $this->getUri()));
  180. }
  181. }
  182. public function isPerson()
  183. {
  184. return $this->localProfile()->isPerson();
  185. }
  186. /**
  187. * @return boolean true if this is a remote peopletag
  188. */
  189. function isPeopletag()
  190. {
  191. if ($this->profile_id || $this->group_id && !$this->peopletag_id) {
  192. return false;
  193. } else if ($this->peopletag_id && !$this->profile_id && !$this->group_id) {
  194. return true;
  195. } else if ($this->peopletag_id && ($this->profile_id || $this->group_id)) {
  196. // TRANS: Server exception. %s is a URI
  197. throw new ServerException(sprintf(_m('Invalid ostatus_profile state: Two or more IDs set for %s.'), $this->getUri()));
  198. } else {
  199. // TRANS: Server exception. %s is a URI
  200. throw new ServerException(sprintf(_m('Invalid ostatus_profile state: All IDs empty for %s.'), $this->getUri()));
  201. }
  202. }
  203. /**
  204. * Send a subscription request to the hub for this feed.
  205. * The hub will later send us a confirmation POST to /main/push/callback.
  206. *
  207. * @return void
  208. * @throws ServerException if feed state is not valid or subscription fails.
  209. */
  210. public function subscribe()
  211. {
  212. $feedsub = FeedSub::ensureFeed($this->feeduri);
  213. if ($feedsub->sub_state == 'active') {
  214. // Active subscription, we don't need to do anything.
  215. return;
  216. }
  217. // Inactive or we got left in an inconsistent state.
  218. // Run a subscription request to make sure we're current!
  219. return $feedsub->subscribe();
  220. }
  221. /**
  222. * Check if this remote profile has any active local subscriptions, and
  223. * if not drop the WebSub subscription feed.
  224. *
  225. * @return boolean true if subscription is removed, false if there are still subscribers to the feed
  226. * @throws Exception of various kinds on failure.
  227. */
  228. public function unsubscribe() {
  229. return $this->garbageCollect();
  230. }
  231. /**
  232. * Check if this remote profile has any active local subscriptions, and
  233. * if not drop the WebSub subscription feed.
  234. *
  235. * @return boolean true if subscription is removed, false if there are still subscribers to the feed
  236. * @throws Exception of various kinds on failure.
  237. */
  238. public function garbageCollect()
  239. {
  240. $feedsub = FeedSub::getKV('uri', $this->feeduri);
  241. if ($feedsub instanceof FeedSub) {
  242. return $feedsub->garbageCollect();
  243. }
  244. // Since there's no FeedSub we can assume it's already garbage collected
  245. return true;
  246. }
  247. /**
  248. * Check if this remote profile has any active local subscriptions, so the
  249. * WebSub subscription layer can decide if it can drop the feed.
  250. *
  251. * This gets called via the FeedSubSubscriberCount event when running
  252. * FeedSub::garbageCollect().
  253. *
  254. * @return int
  255. * @throws NoProfileException if there is no local profile for the object
  256. */
  257. public function subscriberCount()
  258. {
  259. if ($this->isGroup()) {
  260. $members = $this->localGroup()->getMembers(0, 1);
  261. $count = $members->N;
  262. } else if ($this->isPeopletag()) {
  263. $subscribers = $this->localPeopletag()->getSubscribers(0, 1);
  264. $count = $subscribers->N;
  265. } else {
  266. $profile = $this->localProfile();
  267. if ($profile->hasLocalTags()) {
  268. $count = 1;
  269. } else {
  270. $count = $profile->subscriberCount();
  271. }
  272. }
  273. common_log(LOG_INFO, __METHOD__ . " SUB COUNT BEFORE: $count");
  274. // Other plugins may be piggybacking on OStatus without having
  275. // an active group or user-to-user subscription we know about.
  276. Event::handle('Ostatus_profileSubscriberCount', array($this, &$count));
  277. common_log(LOG_INFO, __METHOD__ . " SUB COUNT AFTER: $count");
  278. return $count;
  279. }
  280. /**
  281. * Send an Activity Streams notification to the remote Salmon endpoint,
  282. * if so configured.
  283. *
  284. * @param Profile $actor Actor who did the activity
  285. * @param string $verb Activity::SUBSCRIBE or Activity::JOIN
  286. * @param Object $object object of the action; must define asActivityNoun($tag)
  287. */
  288. public function notify(Profile $actor, $verb, $object=null, $target=null)
  289. {
  290. if ($object == null) {
  291. $object = $this;
  292. }
  293. if (empty($this->salmonuri)) {
  294. return false;
  295. }
  296. $text = 'update';
  297. $id = TagURI::mint('%s:%s:%s',
  298. $verb,
  299. $actor->getURI(),
  300. common_date_iso8601(time()));
  301. // @todo FIXME: Consolidate all these NS settings somewhere.
  302. $attributes = array('xmlns' => Activity::ATOM,
  303. 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
  304. 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
  305. 'xmlns:georss' => 'http://www.georss.org/georss',
  306. 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
  307. 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
  308. 'xmlns:media' => 'http://purl.org/syndication/atommedia');
  309. $entry = new XMLStringer();
  310. $entry->elementStart('entry', $attributes);
  311. $entry->element('id', null, $id);
  312. $entry->element('title', null, $text);
  313. $entry->element('summary', null, $text);
  314. $entry->element('published', null, common_date_w3dtf(common_sql_now()));
  315. $entry->element('activity:verb', null, $verb);
  316. $entry->raw($actor->asAtomAuthor());
  317. $entry->raw($actor->asActivityActor());
  318. $entry->raw($object->asActivityNoun('object'));
  319. if ($target != null) {
  320. $entry->raw($target->asActivityNoun('target'));
  321. }
  322. $entry->elementEnd('entry');
  323. $xml = $entry->getString();
  324. common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
  325. Salmon::post($this->salmonuri, $xml, $actor);
  326. }
  327. /**
  328. * Send a Salmon notification ping immediately, and confirm that we got
  329. * an acceptable response from the remote site.
  330. *
  331. * @param mixed $entry XML string, Notice, or Activity
  332. * @param Profile $actor
  333. * @return boolean success
  334. */
  335. public function notifyActivity($entry, Profile $actor)
  336. {
  337. if ($this->salmonuri) {
  338. return Salmon::post($this->salmonuri, $this->notifyPrepXml($entry), $actor, $this->localProfile());
  339. }
  340. common_debug(__CLASS__.' error: No salmonuri for Ostatus_profile uri: '._ve($this->getUri()));
  341. return false;
  342. }
  343. /**
  344. * Queue a Salmon notification for later. If queues are disabled we'll
  345. * send immediately but won't get the return value.
  346. *
  347. * @param mixed $entry XML string, Notice, or Activity
  348. * @return boolean success
  349. */
  350. public function notifyDeferred($entry, $actor)
  351. {
  352. if ($this->salmonuri) {
  353. try {
  354. common_debug("OSTATUS: user {$actor->getNickname()} ({$actor->getID()}) wants to ping {$this->localProfile()->getNickname()} on {$this->salmonuri}");
  355. $data = array('salmonuri' => $this->salmonuri,
  356. 'entry' => $this->notifyPrepXml($entry),
  357. 'actor' => $actor->getID(),
  358. 'target' => $this->localProfile()->getID());
  359. $qm = QueueManager::get();
  360. return $qm->enqueue($data, 'salmon');
  361. } catch (Exception $e) {
  362. common_log(LOG_ERR, 'OSTATUS: Something went wrong when creating a Salmon slap: '._ve($e->getMessage()));
  363. return false;
  364. }
  365. }
  366. return false;
  367. }
  368. protected function notifyPrepXml($entry)
  369. {
  370. $preamble = '<?xml version="1.0" encoding="UTF-8" ?' . ">\n";
  371. if (is_string($entry)) {
  372. return $entry;
  373. } else if ($entry instanceof Activity) {
  374. return $preamble . $entry->asString(true);
  375. } else if ($entry instanceof Notice) {
  376. return $preamble . $entry->asAtomEntry(true, true);
  377. } else {
  378. // TRANS: Server exception.
  379. throw new ServerException(_m('Invalid type passed to Ostatus_profile::notify. It must be XML string or Activity entry.'));
  380. }
  381. }
  382. function getBestName()
  383. {
  384. if ($this->isGroup()) {
  385. return $this->localGroup()->getBestName();
  386. } else if ($this->isPeopletag()) {
  387. return $this->localPeopletag()->getBestName();
  388. } else {
  389. return $this->localProfile()->getBestName();
  390. }
  391. }
  392. /**
  393. * Read and post notices for updates from the feed.
  394. * Currently assumes that all items in the feed are new,
  395. * coming from a WebSub hub.
  396. *
  397. * @param DOMDocument $doc
  398. * @param string $source identifier ("push")
  399. */
  400. public function processFeed(DOMDocument $doc, $source)
  401. {
  402. $feed = $doc->documentElement;
  403. if ($feed->localName == 'feed' && $feed->namespaceURI == Activity::ATOM) {
  404. $this->processAtomFeed($feed, $source);
  405. } else if ($feed->localName == 'rss') { // @todo FIXME: Check namespace.
  406. $this->processRssFeed($feed, $source);
  407. } else {
  408. // TRANS: Exception.
  409. throw new Exception(_m('Unknown feed format.'));
  410. }
  411. }
  412. public function processAtomFeed(DOMElement $feed, $source)
  413. {
  414. $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
  415. if ($entries->length == 0) {
  416. common_log(LOG_ERR, __METHOD__ . ": no entries in feed update, ignoring");
  417. return;
  418. }
  419. $this->processEntries($entries, $feed, $source);
  420. }
  421. public function processRssFeed(DOMElement $rss, $source)
  422. {
  423. $channels = $rss->getElementsByTagName('channel');
  424. if ($channels->length == 0) {
  425. // TRANS: Exception.
  426. throw new Exception(_m('RSS feed without a channel.'));
  427. } else if ($channels->length > 1) {
  428. common_log(LOG_WARNING, __METHOD__ . ": more than one channel in an RSS feed");
  429. }
  430. $channel = $channels->item(0);
  431. $items = $channel->getElementsByTagName('item');
  432. $this->processEntries($items, $channel, $source);
  433. }
  434. public function processEntries(DOMNodeList $entries, DOMElement $feed, $source)
  435. {
  436. for ($i = 0; $i < $entries->length; $i++) {
  437. $entry = $entries->item($i);
  438. try {
  439. $this->processEntry($entry, $feed, $source);
  440. } catch (AlreadyFulfilledException $e) {
  441. common_debug('We already had this entry: '.$e->getMessage());
  442. } catch (ServerException $e) {
  443. // FIXME: This should be UnknownUriException and the ActivityUtils:: findLocalObject should only test one URI
  444. common_log(LOG_ERR, 'Entry threw exception while processing a feed from '.$source.': '.$e->getMessage());
  445. }
  446. }
  447. }
  448. /**
  449. * Process a posted entry from this feed source.
  450. *
  451. * @param DOMElement $entry
  452. * @param DOMElement $feed for context
  453. * @param string $source identifier ("push" or "salmon")
  454. *
  455. * @return Notice Notice representing the new (or existing) activity
  456. */
  457. public function processEntry(DOMElement $entry, DOMElement $feed, $source)
  458. {
  459. $activity = new Activity($entry, $feed);
  460. return $this->processActivity($activity, $source);
  461. }
  462. // TODO: Make this throw an exception
  463. public function processActivity(Activity $activity, $source)
  464. {
  465. $notice = null;
  466. // The "WithProfile" events were added later.
  467. if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this->localProfile(), &$notice)) &&
  468. Event::handle('StartHandleFeedEntry', array($activity))) {
  469. common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
  470. Event::handle('EndHandleFeedEntry', array($activity));
  471. Event::handle('EndHandleFeedEntryWithProfile', array($activity, $this, $notice));
  472. }
  473. return $notice;
  474. }
  475. /**
  476. * Process an incoming post activity from this remote feed.
  477. * @param Activity $activity
  478. * @param string $method 'push' or 'salmon'
  479. * @return mixed saved Notice or false
  480. */
  481. public function processPost($activity, $method)
  482. {
  483. $actor = ActivityUtils::checkAuthorship($activity, $this->localProfile());
  484. $options = array('is_local' => Notice::REMOTE);
  485. try {
  486. $stored = Notice::saveActivity($activity, $actor, $options);
  487. } catch (Exception $e) {
  488. common_log(LOG_ERR, "OStatus save of remote message $sourceUri failed: " . $e->getMessage());
  489. throw $e;
  490. }
  491. return $stored;
  492. }
  493. /**
  494. * Filters a list of recipient ID URIs to just those for local delivery.
  495. * @param Profile local profile of sender
  496. * @param array in/out &$attention_uris set of URIs, will be pruned on output
  497. * @return array of group IDs
  498. */
  499. static public function filterAttention(Profile $sender, array $attention)
  500. {
  501. common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', array_keys($attention)));
  502. $groups = array();
  503. $replies = array();
  504. foreach ($attention as $recipient=>$type) {
  505. // Is the recipient a local user?
  506. $user = User::getKV('uri', $recipient);
  507. if ($user instanceof User) {
  508. // @todo FIXME: Sender verification, spam etc?
  509. $replies[] = $recipient;
  510. continue;
  511. }
  512. // Is the recipient a local group?
  513. // TODO: $group = User_group::getKV('uri', $recipient);
  514. $id = OStatusPlugin::localGroupFromUrl($recipient);
  515. if ($id) {
  516. $group = User_group::getKV('id', $id);
  517. if ($group instanceof User_group) {
  518. // Deliver to all members of this local group if allowed.
  519. if ($sender->isMember($group)) {
  520. $groups[] = $group->id;
  521. } else {
  522. common_log(LOG_DEBUG, sprintf('Skipping reply to local group %s as sender %d is not a member', $group->getNickname(), $sender->id));
  523. }
  524. continue;
  525. } else {
  526. common_log(LOG_DEBUG, "Skipping reply to bogus group $recipient");
  527. }
  528. }
  529. // Is the recipient a remote user or group?
  530. try {
  531. $oprofile = self::ensureProfileURI($recipient);
  532. if ($oprofile->isGroup()) {
  533. // Deliver to local members of this remote group.
  534. // @todo FIXME: Sender verification?
  535. $groups[] = $oprofile->group_id;
  536. } else {
  537. // may be canonicalized or something
  538. $replies[] = $oprofile->getUri();
  539. }
  540. continue;
  541. } catch (Exception $e) {
  542. // Neither a recognizable local nor remote user!
  543. common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient: " . $e->getMessage());
  544. }
  545. }
  546. common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
  547. common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
  548. return array($groups, $replies);
  549. }
  550. /**
  551. * Look up and if necessary create an Ostatus_profile for the remote entity
  552. * with the given profile page URL. This should never return null -- you
  553. * will either get an object or an exception will be thrown.
  554. *
  555. * @param string $profile_url
  556. * @return Ostatus_profile
  557. * @throws Exception on various error conditions
  558. * @throws OStatusShadowException if this reference would obscure a local user/group
  559. */
  560. public static function ensureProfileURL($profile_url, array $hints=array())
  561. {
  562. $oprofile = self::getFromProfileURL($profile_url);
  563. if ($oprofile instanceof Ostatus_profile) {
  564. return $oprofile;
  565. }
  566. $hints['profileurl'] = $profile_url;
  567. // Fetch the URL
  568. // XXX: HTTP caching
  569. $client = new HTTPClient();
  570. $client->setHeader('Accept', 'text/html,application/xhtml+xml');
  571. $response = $client->get($profile_url);
  572. if (!$response->isOk()) {
  573. // TRANS: Exception. %s is a profile URL.
  574. throw new Exception(sprintf(_m('Could not reach profile page %s.'),$profile_url));
  575. }
  576. // Check if we have a non-canonical URL
  577. $finalUrl = $response->getEffectiveUrl();
  578. if ($finalUrl != $profile_url) {
  579. $hints['profileurl'] = $finalUrl;
  580. $oprofile = self::getFromProfileURL($finalUrl);
  581. if ($oprofile instanceof Ostatus_profile) {
  582. return $oprofile;
  583. }
  584. }
  585. if (in_array(
  586. preg_replace('/\s*;.*$/', '', $response->getHeader('Content-Type')),
  587. array('application/rss+xml', 'application/atom+xml', 'application/xml', 'text/xml'))
  588. ) {
  589. $hints['feedurl'] = $response->getEffectiveUrl();
  590. } else {
  591. // Try to get some hCard data
  592. $body = $response->getBody();
  593. $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
  594. if (!empty($hcardHints)) {
  595. $hints = array_merge($hints, $hcardHints);
  596. }
  597. }
  598. // Check if they've got an LRDD header
  599. $lrdd = LinkHeader::getLink($response, 'lrdd');
  600. try {
  601. $xrd = new XML_XRD();
  602. $xrd->loadFile($lrdd);
  603. $xrdHints = DiscoveryHints::fromXRD($xrd);
  604. $hints = array_merge($hints, $xrdHints);
  605. } catch (Exception $e) {
  606. // No hints available from XRD
  607. }
  608. // If discovery found a feedurl (probably from LRDD), use it.
  609. if (array_key_exists('feedurl', $hints)) {
  610. return self::ensureFeedURL($hints['feedurl'], $hints);
  611. }
  612. // Get the feed URL from HTML
  613. $discover = new FeedDiscovery();
  614. $feedurl = $discover->discoverFromHTML($finalUrl, $body);
  615. if (!empty($feedurl)) {
  616. $hints['feedurl'] = $feedurl;
  617. return self::ensureFeedURL($feedurl, $hints);
  618. }
  619. // TRANS: Exception. %s is a URL.
  620. throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
  621. }
  622. /**
  623. * Look up the Ostatus_profile, if present, for a remote entity with the
  624. * given profile page URL. Will return null for both unknown and invalid
  625. * remote profiles.
  626. *
  627. * @return mixed Ostatus_profile or null
  628. * @throws OStatusShadowException for local profiles
  629. */
  630. static function getFromProfileURL($profile_url)
  631. {
  632. $profile = Profile::getKV('profileurl', $profile_url);
  633. if (!$profile instanceof Profile) {
  634. return null;
  635. }
  636. try {
  637. $oprofile = self::getFromProfile($profile);
  638. // We found the profile, return it!
  639. return $oprofile;
  640. } catch (NoResultException $e) {
  641. // Could not find an OStatus profile, is it instead a local user?
  642. $user = User::getKV('id', $profile->id);
  643. if ($user instanceof User) {
  644. // @todo i18n FIXME: use sprintf and add i18n (?)
  645. throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'.");
  646. }
  647. }
  648. // Continue discovery; it's a remote profile
  649. // for OMB or some other protocol, may also
  650. // support OStatus
  651. return null;
  652. }
  653. static function getFromProfile(Profile $profile)
  654. {
  655. $oprofile = new Ostatus_profile();
  656. $oprofile->profile_id = $profile->id;
  657. if (!$oprofile->find(true)) {
  658. throw new NoResultException($oprofile);
  659. }
  660. return $oprofile;
  661. }
  662. /**
  663. * Look up and if necessary create an Ostatus_profile for remote entity
  664. * with the given update feed. This should never return null -- you will
  665. * either get an object or an exception will be thrown.
  666. *
  667. * @return Ostatus_profile
  668. * @throws Exception
  669. */
  670. public static function ensureFeedURL($feed_url, array $hints=array())
  671. {
  672. $oprofile = Ostatus_profile::getKV('feeduri', $feed_url);
  673. if ($oprofile instanceof Ostatus_profile) {
  674. return $oprofile;
  675. }
  676. $discover = new FeedDiscovery();
  677. $feeduri = $discover->discoverFromFeedURL($feed_url);
  678. $hints['feedurl'] = $feeduri;
  679. $huburi = $discover->getHubLink();
  680. $hints['hub'] = $huburi;
  681. // XXX: NS_REPLIES is deprecated anyway, so let's remove it in the future.
  682. $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
  683. ?: $discover->getAtomLink(Salmon::NS_REPLIES);
  684. $hints['salmon'] = $salmonuri;
  685. if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
  686. // We can only deal with folks with a WebSub hub
  687. // unless we have something similar available locally.
  688. throw new FeedSubNoHubException();
  689. }
  690. $feedEl = $discover->root;
  691. if ($feedEl->tagName == 'feed') {
  692. return self::ensureAtomFeed($feedEl, $hints);
  693. } else if ($feedEl->tagName == 'channel') {
  694. return self::ensureRssChannel($feedEl, $hints);
  695. } else {
  696. throw new FeedSubBadXmlException($feeduri);
  697. }
  698. }
  699. /**
  700. * Look up and, if necessary, create an Ostatus_profile for the remote
  701. * profile with the given Atom feed - actually loaded from the feed.
  702. * This should never return null -- you will either get an object or
  703. * an exception will be thrown.
  704. *
  705. * @param DOMElement $feedEl root element of a loaded Atom feed
  706. * @param array $hints additional discovery information passed from higher levels
  707. * @todo FIXME: Should this be marked public?
  708. * @return Ostatus_profile
  709. * @throws Exception
  710. */
  711. public static function ensureAtomFeed(DOMElement $feedEl, array $hints)
  712. {
  713. $author = ActivityUtils::getFeedAuthor($feedEl);
  714. if (empty($author)) {
  715. // XXX: make some educated guesses here
  716. // TRANS: Feed sub exception.
  717. throw new FeedSubException(_m('Cannot find enough profile '.
  718. 'information to make a feed.'));
  719. }
  720. return self::ensureActivityObjectProfile($author, $hints);
  721. }
  722. /**
  723. * Look up and, if necessary, create an Ostatus_profile for the remote
  724. * profile with the given RSS feed - actually loaded from the feed.
  725. * This should never return null -- you will either get an object or
  726. * an exception will be thrown.
  727. *
  728. * @param DOMElement $feedEl root element of a loaded RSS feed
  729. * @param array $hints additional discovery information passed from higher levels
  730. * @todo FIXME: Should this be marked public?
  731. * @return Ostatus_profile
  732. * @throws Exception
  733. */
  734. public static function ensureRssChannel(DOMElement $feedEl, array $hints)
  735. {
  736. // Special-case for Posterous. They have some nice metadata in their
  737. // posterous:author elements. We should use them instead of the channel.
  738. $items = $feedEl->getElementsByTagName('item');
  739. if ($items->length > 0) {
  740. $item = $items->item(0);
  741. $authorEl = ActivityUtils::child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS);
  742. if (!empty($authorEl)) {
  743. $obj = ActivityObject::fromPosterousAuthor($authorEl);
  744. // Posterous has multiple authors per feed, and multiple feeds
  745. // per author. We check if this is the "main" feed for this author.
  746. if (array_key_exists('profileurl', $hints) &&
  747. !empty($obj->poco) &&
  748. common_url_to_nickname($hints['profileurl']) == $obj->poco->preferredUsername) {
  749. return self::ensureActivityObjectProfile($obj, $hints);
  750. }
  751. }
  752. }
  753. $obj = ActivityUtils::getFeedAuthor($feedEl);
  754. // @todo FIXME: We should check whether this feed has elements
  755. // with different <author> or <dc:creator> elements, and... I dunno.
  756. // Do something about that.
  757. if(empty($obj)) { $obj = ActivityObject::fromRssChannel($feedEl); }
  758. return self::ensureActivityObjectProfile($obj, $hints);
  759. }
  760. /**
  761. * Download and update given avatar image
  762. *
  763. * @param string $url
  764. * @return Avatar The Avatar we have on disk. (seldom used)
  765. * @throws Exception in various failure cases
  766. */
  767. public function updateAvatar($url, $force=false)
  768. {
  769. try {
  770. // If avatar URL differs: update. If URLs were identical but we're forced: update.
  771. if ($url == $this->avatar && !$force) {
  772. // If there's no locally stored avatar, throw an exception and continue fetching below.
  773. $avatar = Avatar::getUploaded($this->localProfile()) instanceof Avatar;
  774. return $avatar;
  775. }
  776. } catch (NoAvatarException $e) {
  777. // No avatar available, let's fetch it.
  778. }
  779. if (!common_valid_http_url($url)) {
  780. // TRANS: Server exception. %s is a URL.
  781. throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
  782. }
  783. $self = $this->localProfile();
  784. // @todo FIXME: This should be better encapsulated
  785. // ripped from oauthstore.php (for old OMB client)
  786. $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
  787. try {
  788. $imgData = HTTPClient::quickGet($url);
  789. // Make sure it's at least an image file. ImageFile can do the rest.
  790. if (false === getimagesizefromstring($imgData)) {
  791. throw new UnsupportedMediaException(_('Downloaded group avatar was not an image.'));
  792. }
  793. file_put_contents($temp_filename, $imgData);
  794. unset($imgData); // No need to carry this in memory.
  795. if ($this->isGroup()) {
  796. $id = $this->group_id;
  797. } else {
  798. $id = $this->profile_id;
  799. }
  800. $imagefile = new ImageFile(null, $temp_filename);
  801. $filename = Avatar::filename($id,
  802. image_type_to_extension($imagefile->type),
  803. null,
  804. common_timestamp());
  805. rename($temp_filename, Avatar::path($filename));
  806. } catch (Exception $e) {
  807. unlink($temp_filename);
  808. throw $e;
  809. }
  810. // @todo FIXME: Hardcoded chmod is lame, but seems to be necessary to
  811. // keep from accidentally saving images from command-line (queues)
  812. // that can't be read from web server, which causes hard-to-notice
  813. // problems later on:
  814. //
  815. // http://status.net/open-source/issues/2663
  816. chmod(Avatar::path($filename), 0644);
  817. $self->setOriginal($filename);
  818. $orig = clone($this);
  819. $this->avatar = $url;
  820. $this->update($orig);
  821. return Avatar::getUploaded($self);
  822. }
  823. /**
  824. * Pull avatar URL from ActivityObject or profile hints
  825. *
  826. * @param ActivityObject $object
  827. * @param array $hints
  828. * @return mixed URL string or false
  829. */
  830. public static function getActivityObjectAvatar(ActivityObject $object, array $hints=array())
  831. {
  832. if ($object->avatarLinks) {
  833. $best = false;
  834. // Take the exact-size avatar, or the largest avatar, or the first avatar if all sizeless
  835. foreach ($object->avatarLinks as $avatar) {
  836. if ($avatar->width == AVATAR_PROFILE_SIZE && $avatar->height = AVATAR_PROFILE_SIZE) {
  837. // Exact match!
  838. $best = $avatar;
  839. break;
  840. }
  841. if (!$best || $avatar->width > $best->width) {
  842. $best = $avatar;
  843. }
  844. }
  845. return $best->url;
  846. } else if (array_key_exists('avatar', $hints)) {
  847. return $hints['avatar'];
  848. }
  849. return false;
  850. }
  851. /**
  852. * Get an appropriate avatar image source URL, if available.
  853. *
  854. * @param ActivityObject $actor
  855. * @param DOMElement $feed
  856. * @return string
  857. */
  858. protected static function getAvatar(ActivityObject $actor, DOMElement $feed)
  859. {
  860. $url = '';
  861. $icon = '';
  862. if ($actor->avatar) {
  863. $url = trim($actor->avatar);
  864. }
  865. if (!$url) {
  866. // Check <atom:logo> and <atom:icon> on the feed
  867. $els = $feed->childNodes();
  868. if ($els && $els->length) {
  869. for ($i = 0; $i < $els->length; $i++) {
  870. $el = $els->item($i);
  871. if ($el->namespaceURI == Activity::ATOM) {
  872. if (empty($url) && $el->localName == 'logo') {
  873. $url = trim($el->textContent);
  874. break;
  875. }
  876. if (empty($icon) && $el->localName == 'icon') {
  877. // Use as a fallback
  878. $icon = trim($el->textContent);
  879. }
  880. }
  881. }
  882. }
  883. if ($icon && !$url) {
  884. $url = $icon;
  885. }
  886. }
  887. if ($url) {
  888. $opts = array('allowed_schemes' => array('http', 'https'));
  889. if (common_valid_http_url($url)) {
  890. return $url;
  891. }
  892. }
  893. return Plugin::staticPath('OStatus', 'images/96px-Feed-icon.svg.png');
  894. }
  895. /**
  896. * Fetch, or build if necessary, an Ostatus_profile for the actor
  897. * in a given Activity Streams activity.
  898. * This should never return null -- you will either get an object or
  899. * an exception will be thrown.
  900. *
  901. * @param Activity $activity
  902. * @param string $feeduri if we already know the canonical feed URI!
  903. * @param string $salmonuri if we already know the salmon return channel URI
  904. * @return Ostatus_profile
  905. * @throws Exception
  906. */
  907. public static function ensureActorProfile(Activity $activity, array $hints=array())
  908. {
  909. return self::ensureActivityObjectProfile($activity->actor, $hints);
  910. }
  911. /**
  912. * Fetch, or build if necessary, an Ostatus_profile for the profile
  913. * in a given Activity Streams object (can be subject, actor, or object).
  914. * This should never return null -- you will either get an object or
  915. * an exception will be thrown.
  916. *
  917. * @param ActivityObject $object
  918. * @param array $hints additional discovery information passed from higher levels
  919. * @return Ostatus_profile
  920. * @throws Exception
  921. */
  922. public static function ensureActivityObjectProfile(ActivityObject $object, array $hints=array())
  923. {
  924. $profile = self::getActivityObjectProfile($object);
  925. if ($profile instanceof Ostatus_profile) {
  926. $profile->updateFromActivityObject($object, $hints);
  927. } else {
  928. $profile = self::createActivityObjectProfile($object, $hints);
  929. }
  930. return $profile;
  931. }
  932. /**
  933. * @param Activity $activity
  934. * @return mixed matching Ostatus_profile or false if none known
  935. * @throws ServerException if feed info invalid
  936. */
  937. public static function getActorProfile(Activity $activity)
  938. {
  939. return self::getActivityObjectProfile($activity->actor);
  940. }
  941. /**
  942. * @param ActivityObject $activity
  943. * @return mixed matching Ostatus_profile or false if none known
  944. * @throws ServerException if feed info invalid
  945. */
  946. protected static function getActivityObjectProfile(ActivityObject $object)
  947. {
  948. $uri = self::getActivityObjectProfileURI($object);
  949. return Ostatus_profile::getKV('uri', $uri);
  950. }
  951. /**
  952. * Get the identifier URI for the remote entity described
  953. * by this ActivityObject. This URI is *not* guaranteed to be
  954. * a resolvable HTTP/HTTPS URL.
  955. *
  956. * @param ActivityObject $object
  957. * @return string
  958. * @throws ServerException if feed info invalid
  959. */
  960. protected static function getActivityObjectProfileURI(ActivityObject $object)
  961. {
  962. if ($object->id) {
  963. if (ActivityUtils::validateUri($object->id)) {
  964. return $object->id;
  965. }
  966. }
  967. // If the id is missing or invalid (we've seen feeds mistakenly listing
  968. // things like local usernames in that field) then we'll use the profile
  969. // page link, if valid.
  970. if ($object->link && common_valid_http_url($object->link)) {
  971. return $object->link;
  972. }
  973. // TRANS: Server exception.
  974. throw new ServerException(_m('No author ID URI found.'));
  975. }
  976. /**
  977. * @todo FIXME: Validate stuff somewhere.
  978. */
  979. /**
  980. * Create local ostatus_profile and profile/user_group entries for
  981. * the provided remote user or group.
  982. * This should never return null -- you will either get an object or
  983. * an exception will be thrown.
  984. *
  985. * @param ActivityObject $object
  986. * @param array $hints
  987. *
  988. * @return Ostatus_profile
  989. */
  990. protected static function createActivityObjectProfile(ActivityObject $object, array $hints=array())
  991. {
  992. common_debug('Attempting to create an Ostatus_profile from an ActivityObject with ID: '._ve($object->id));
  993. $homeuri = $object->id;
  994. $discover = false;
  995. if (!$homeuri) {
  996. common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
  997. // TRANS: Exception.
  998. throw new Exception(_m('No profile URI.'));
  999. }
  1000. $user = User::getKV('uri', $homeuri);
  1001. if ($user instanceof User) {
  1002. // TRANS: Exception.
  1003. throw new Exception(_m('Local user cannot be referenced as remote.'));
  1004. }
  1005. if (OStatusPlugin::localGroupFromUrl($homeuri)) {
  1006. // TRANS: Exception.
  1007. throw new Exception(_m('Local group cannot be referenced as remote.'));
  1008. }
  1009. $ptag = Profile_list::getKV('uri', $homeuri);
  1010. if ($ptag instanceof Profile_list) {
  1011. $local_user = User::getKV('id', $ptag->tagger);
  1012. if ($local_user instanceof User) {
  1013. // TRANS: Exception.
  1014. throw new Exception(_m('Local list cannot be referenced as remote.'));
  1015. }
  1016. }
  1017. if (!array_key_exists('feedurl', $hints)) {
  1018. $discover = new FeedDiscovery();
  1019. $hints['feedurl'] = $discover->discoverFromURL($homeuri);
  1020. common_debug(__METHOD__.' did not have a "feedurl" hint, FeedDiscovery found '._ve($hints['feedurl']));
  1021. }
  1022. $feeduri = $hints['feedurl'];
  1023. if (array_key_exists('salmon', $hints)) {
  1024. $salmonuri = $hints['salmon'];
  1025. } else {
  1026. if (!$discover) {
  1027. $discover = new FeedDiscovery();
  1028. $discover->discoverFromFeedURL($hints['feedurl']);
  1029. }
  1030. // XXX: NS_REPLIES is deprecated anyway, so let's remove it in the future.
  1031. $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
  1032. ?: $discover->getAtomLink(Salmon::NS_REPLIES);
  1033. }
  1034. if (array_key_exists('hub', $hints)) {
  1035. $huburi = $hints['hub'];
  1036. } else {
  1037. if (!$discover) {
  1038. $discover = new FeedDiscovery();
  1039. $discover->discoverFromFeedURL($hints['feedurl']);
  1040. }
  1041. $huburi = $discover->getHubLink();
  1042. }
  1043. if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
  1044. // We can only deal with folks with a WebSub hub
  1045. throw new FeedSubNoHubException();
  1046. }
  1047. $oprofile = new Ostatus_profile();
  1048. $oprofile->uri = $homeuri;
  1049. $oprofile->feeduri = $feeduri;
  1050. $oprofile->salmonuri = $salmonuri;
  1051. $oprofile->created = common_sql_now();
  1052. $oprofile->modified = common_sql_now();
  1053. if ($object->type == ActivityObject::PERSON) {
  1054. $profile = new Profile();
  1055. $profile->created = common_sql_now();
  1056. self::updateProfile($profile, $object, $hints);
  1057. $oprofile->profile_id = $profile->insert();
  1058. if ($oprofile->profile_id === false) {
  1059. // TRANS: Server exception.
  1060. throw new ServerException(_m('Cannot save local profile.'));
  1061. }
  1062. } else if ($object->type == ActivityObject::GROUP) {
  1063. $profile = new Profile();
  1064. $profile->query('BEGIN');
  1065. $group = new User_group();
  1066. $group->uri = $homeuri;
  1067. $group->created = common_sql_now();
  1068. self::updateGroup($group, $object, $hints);
  1069. // TODO: We should do this directly in User_group->insert()!
  1070. // currently it's duplicated in User_group->update()
  1071. // AND User_group->register()!!!
  1072. $fields = array(/*group field => profile field*/
  1073. 'nickname' => 'nickname',
  1074. 'fullname' => 'fullname',
  1075. 'mainpage' => 'profileurl',
  1076. 'homepage' => 'homepage',
  1077. 'description' => 'bio',
  1078. 'location' => 'location',
  1079. 'created' => 'created',
  1080. 'modified' => 'modified',
  1081. );
  1082. foreach ($fields as $gf=>$pf) {
  1083. $profile->$pf = $group->$gf;
  1084. }
  1085. $profile_id = $profile->insert();
  1086. if ($profile_id === false) {
  1087. $profile->query('ROLLBACK');
  1088. throw new ServerException(_('Profile insertion failed.'));
  1089. }
  1090. $group->profile_id = $profile_id;
  1091. $oprofile->group_id = $group->insert();
  1092. if ($oprofile->group_id === false) {
  1093. $profile->query('ROLLBACK');
  1094. // TRANS: Server exception.
  1095. throw new ServerException(_m('Cannot save local profile.'));
  1096. }
  1097. $profile->query('COMMIT');
  1098. } else if ($object->type == ActivityObject::_LIST) {
  1099. $ptag = new Profile_list();
  1100. $ptag->uri = $homeuri;
  1101. $ptag->created = common_sql_now();
  1102. self::updatePeopletag($ptag, $object, $hints);
  1103. $oprofile->peopletag_id = $ptag->insert();
  1104. if ($oprofile->peopletag_id === false) {
  1105. // TRANS: Server exception.
  1106. throw new ServerException(_m('Cannot save local list.'));
  1107. }
  1108. }
  1109. $ok = $oprofile->insert();
  1110. if ($ok === false) {
  1111. // TRANS: Server exception.
  1112. throw new ServerException(_m('Cannot save OStatus profile.'));
  1113. }
  1114. $avatar = self::getActivityObjectAvatar($object, $hints);
  1115. if ($avatar) {
  1116. try {
  1117. $oprofile->updateAvatar($avatar);
  1118. } catch (Exception $ex) {
  1119. // Profile is saved, but Avatar is messed up. We're
  1120. // just going to continue.
  1121. common_log(LOG_WARNING, "Exception saving OStatus profile avatar: ". $ex->getMessage());
  1122. }
  1123. }
  1124. return $oprofile;
  1125. }
  1126. /**
  1127. * Save any updated profile information to our local copy.
  1128. * @param ActivityObject $object
  1129. * @param array $hints
  1130. */
  1131. public function updateFromActivityObject(ActivityObject $object, array $hints=array())
  1132. {
  1133. if (self::getActivityObjectProfileURI($object) !== $this->getUri()) {
  1134. common_log(LOG_ERR, 'Trying to update profile with URI '._ve($this->getUri()).' from ActivityObject with URI: '._ve(self::getActivityObjectProfileURI($object)));
  1135. // FIXME: Maybe not AuthorizationException?
  1136. throw new AuthorizationException('Trying to update profile from ActivityObject with different URI.');
  1137. }
  1138. common_debug('Updating Ostatus_profile with URI '._ve($this->getUri()).' from ActivityObject');
  1139. if ($this->isGroup()) {
  1140. $group = $this->localGroup();
  1141. self::updateGroup($group, $object, $hints);
  1142. } else if ($this->isPeopletag()) {
  1143. $ptag = $this->localPeopletag();
  1144. self::updatePeopletag($ptag, $object, $hints);
  1145. } else {
  1146. $profile = $this->localProfile();
  1147. self::updateProfile($profile, $object, $hints);
  1148. }
  1149. $avatar = self::getActivityObjectAvatar($object, $hints);
  1150. if ($avatar && !isset($ptag)) {
  1151. try {
  1152. $this->updateAvatar($avatar);
  1153. } catch (Exception $e) {
  1154. common_log(LOG_WARNING, 'Exception ('.get_class($e).') updating OStatus profile avatar: ' . $e->getMessage());
  1155. }
  1156. }
  1157. }
  1158. public static function updateProfile(Profile $profile, ActivityObject $object, array $hints=array())
  1159. {
  1160. $orig = clone($profile);
  1161. // Existing nickname is better than nothing.
  1162. if (!array_key_exists('nickname', $hints)) {
  1163. $hints['nickname'] = $profile->nickname;
  1164. }
  1165. $nickname = self::getActivityObjectNickname($object, $hints);
  1166. if (!empty($nickname)) {
  1167. $profile->nickname = $nickname;
  1168. }
  1169. if (!empty($object->title)) {
  1170. $profile->fullname = $object->title;
  1171. } else if (array_key_exists('fullname', $hints)) {
  1172. $profile->fullname = $hints['fullname'];
  1173. }
  1174. if (!empty($object->link)) {
  1175. $profile->profileurl = $object->link;
  1176. } else if (array_key_exists('profileurl', $hints)) {
  1177. $profile->profileurl = $hints['profileurl'];
  1178. } else if (common_valid_http_url($object->id)) {
  1179. $profile->profileurl = $object->id;
  1180. }
  1181. $bio = self::getActivityObjectBio($object, $hints);
  1182. if (!empty($bio)) {
  1183. $profile->bio = $bio;
  1184. }
  1185. $location = self::getActivityObjectLocation($object, $hints);
  1186. if (!empty($location)) {
  1187. $profile->location = $location;
  1188. }
  1189. $homepage = self::getActivityObjectHomepage($object, $hints);
  1190. if (!empty($homepage)) {
  1191. $profile->homepage = $homepage;
  1192. }
  1193. if (!empty($object->geopoint)) {
  1194. $location = ActivityContext::locationFromPoint($object->geopoint);
  1195. if (!empty($location)) {
  1196. $profile->lat = $location->lat;
  1197. $profile->lon = $location->lon;
  1198. }
  1199. }
  1200. // @todo FIXME: tags/categories
  1201. // @todo tags from categories
  1202. if ($profile->id) {
  1203. //common_debug('Updating OStatus profile '._ve($profile->getID().' from remote info '._ve($object->id).': ' . _ve($object) . _ve($hints));
  1204. common_debug('Updating OStatus profile '._ve($profile->getID()).' from remote info '._ve($object->id).' gathered from hints: '._ve($hints));
  1205. $profile->update($orig);
  1206. }
  1207. }
  1208. protected static function updateGroup(User_group $group, ActivityObject $object, array $hints=array())
  1209. {
  1210. $orig = clone($group);
  1211. $group->nickname = self::getActivityObjectNickname($object, $hints);
  1212. $group->fullname = $object->title;
  1213. if (!empty($object->link)) {
  1214. $group->mainpage = $object->link;
  1215. } else if (array_key_exists('profileurl', $hints)) {
  1216. $group->mainpage = $hints['profileurl'];
  1217. }
  1218. // @todo tags from categories
  1219. $group->description = self::getActivityObjectBio($object, $hints);
  1220. $group->location = self::getActivityObjectLocation($object, $hints);
  1221. $group->homepage = self::getActivityObjectHomepage($object, $hints);
  1222. if ($group->id) { // If no id, we haven't called insert() yet, so don't run update()
  1223. //common_debug('Updating OStatus group '._ve($group->getID().' from remote info '._ve($object->id).': ' . _ve($object) . _ve($hints));
  1224. common_debug('Updating OStatus group '._ve($group->getID()).' from remote info '._ve($object->id).' gathered from hints: '._ve($hints));
  1225. $group->update($orig);
  1226. }
  1227. }
  1228. protected static function updatePeopletag($tag, ActivityObject $object, array $hints=array()) {
  1229. $orig = clone($tag);
  1230. $tag->tag = $object->title;
  1231. if (!empty($object->link)) {
  1232. $tag->mainpage = $object->link;
  1233. } else if (array_key_exists('profileurl', $hints)) {
  1234. $tag->mainpage = $hints['profileurl'];
  1235. }
  1236. $tag->description = $object->summary;
  1237. $tagger = self::ensureActivityObjectProfile($object->owner);
  1238. $tag->tagger = $tagger->profile_id;
  1239. if ($tag->id) {
  1240. //common_debug('Updating OStatus peopletag '._ve($tag->getID().' from remote info '._ve($object->id).': ' . _ve($object) . _ve($hints));
  1241. common_debug('Updating OStatus peopletag '._ve($tag->getID()).' from remote info '._ve($object->id).' gathered from hints: '._ve($hints));
  1242. $tag->update($orig);
  1243. }
  1244. }
  1245. protected static function getActivityObjectHomepage(ActivityObject $object, array $hints=array())
  1246. {
  1247. $homepage = null;
  1248. $poco = $object->poco;
  1249. if (!empty($poco)) {
  1250. $url = $poco->getPrimaryURL();
  1251. if ($url && $url->type == 'homepage') {
  1252. $homepage = $url->value;
  1253. }
  1254. }
  1255. // @todo Try for a another PoCo URL?
  1256. return $homepage;
  1257. }
  1258. protected static function getActivityObjectLocation(ActivityObject $object, array $hints=array())
  1259. {
  1260. $location = null;
  1261. if (!empty($object->poco) &&
  1262. isset($object->poco->address->formatted)) {
  1263. $location = $object->poco->address->formatted;
  1264. } else if (array_key_exists('location', $hints)) {
  1265. $location = $hints['location'];
  1266. }
  1267. if (!empty($location)) {
  1268. if (mb_strlen($location) > 191) { // not 255 because utf8mb4 takes more space
  1269. $location = mb_substr($note, 0, 191 - 3) . ' … ';
  1270. }
  1271. }
  1272. // @todo Try to find location some othe way? Via goerss point?
  1273. return $location;
  1274. }
  1275. protected static function getActivityObjectBio(ActivityObject $object, array $hints=array())
  1276. {
  1277. $bio = null;
  1278. if (!empty($object->poco)) {
  1279. $note = $object->poco->note;
  1280. } else if (array_key_exists('bio', $hints)) {
  1281. $note = $hints['bio'];
  1282. }
  1283. if (!empty($note)) {
  1284. if (Profile::bioTooLong($note)) {
  1285. // XXX: truncate ok?
  1286. $bio = mb_substr($note, 0, Profile::maxBio() - 3) . ' … ';
  1287. } else {
  1288. $bio = $note;
  1289. }
  1290. }
  1291. // @todo Try to get bio info some other way?
  1292. return $bio;
  1293. }
  1294. public static function getActivityObjectNickname(ActivityObject $object, array $hints=array())
  1295. {
  1296. if ($object->poco) {
  1297. if (!empty($object->poco->preferredUsername)) {
  1298. return common_nicknamize($object->poco->preferredUsername);
  1299. }
  1300. }
  1301. if (!empty($object->nickname)) {
  1302. return common_nicknamize($object->nickname);
  1303. }
  1304. if (array_key_exists('nickname', $hints)) {
  1305. return $hints['nickname'];
  1306. }
  1307. // Try the profile url (like foo.example.com or example.com/user/foo)
  1308. if (!empty($object->link)) {
  1309. $profileUrl = $object->link;
  1310. } else if (!empty($hints['profileurl'])) {
  1311. $profileUrl = $hints['profileurl'];
  1312. }
  1313. if (!empty($profileUrl)) {
  1314. $nickname = self::nicknameFromURI($profileUrl);
  1315. }
  1316. // Try the URI (may be a tag:, http:, acct:, ...
  1317. if (empty($nickname)) {
  1318. $nickname = self::nicknameFromURI($object->id);
  1319. }
  1320. // Try a Webfinger if one was passed (way) down
  1321. if (empty($nickname)) {
  1322. if (array_key_exists('webfinger', $hints)) {
  1323. $nickname = self::nicknameFromURI($hints['webfinger']);
  1324. }
  1325. }
  1326. // Try the name
  1327. if (empty($nickname)) {
  1328. $nickname = common_nicknamize($object->title);
  1329. }
  1330. return $nickname;
  1331. }
  1332. protected static function nicknameFromURI($uri)
  1333. {
  1334. if (preg_match('/(\w+):/', $uri, $matches)) {
  1335. $protocol = $matches[1];
  1336. } else {
  1337. return null;
  1338. }
  1339. switch ($protocol) {
  1340. case 'acct':
  1341. case 'mailto':
  1342. if (preg_match("/^$protocol:(.*)?@.*\$/", $uri, $matches)) {
  1343. return common_canonical_nickname($matches[1]);
  1344. }
  1345. return null;
  1346. case 'http':
  1347. return common_url_to_nickname($uri);
  1348. break;
  1349. default:
  1350. return null;
  1351. }
  1352. }
  1353. /**
  1354. * Look up, and if necessary create, an Ostatus_profile for the remote
  1355. * entity with the given webfinger address.
  1356. * This should never return null -- you will either get an object or
  1357. * an exception will be thrown.
  1358. *
  1359. * @param string $addr webfinger address
  1360. * @return Ostatus_profile
  1361. * @throws Exception on error conditions
  1362. * @throws OStatusShadowException if this reference would obscure a local user/group
  1363. */
  1364. public static function ensureWebfinger($addr)
  1365. {
  1366. // Normalize $addr, i.e. add 'acct:' if missing
  1367. $addr = Discovery::normalize($addr);
  1368. // Try the cache
  1369. $uri = self::cacheGet(sprintf('ostatus_profile:webfinger:%s', $addr));
  1370. if ($uri !== false) {
  1371. if (is_null($uri)) {
  1372. // Negative cache entry
  1373. // TRANS: Exception.
  1374. throw new Exception(_m('Not a valid webfinger address (via cache).'));
  1375. }
  1376. $oprofile = Ostatus_profile::getKV('uri', $uri);
  1377. if ($oprofile instanceof Ostatus_profile) {
  1378. return $oprofile;
  1379. }
  1380. common_log(LOG_ERR, sprintf(__METHOD__ . ': Webfinger address cache inconsistent with database, did not find Ostatus_profile uri==%s', $uri));
  1381. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), false);
  1382. }
  1383. // Try looking it up
  1384. $oprofile = Ostatus_profile::getKV('uri', $addr);
  1385. if ($oprofile instanceof Ostatus_profile) {
  1386. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
  1387. return $oprofile;
  1388. }
  1389. // Now, try some discovery
  1390. $disco = new Discovery();
  1391. try {
  1392. $xrd = $disco->lookup($addr);
  1393. } catch (Exception $e) {
  1394. // Save negative cache entry so we don't waste time looking it up again.
  1395. // @todo FIXME: Distinguish temporary failures?
  1396. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
  1397. // TRANS: Exception.
  1398. throw new Exception(_m('Not a valid webfinger address.'));
  1399. }
  1400. $hints = array_merge(array('webfinger' => $addr),
  1401. DiscoveryHints::fromXRD($xrd));
  1402. // If there's an Hcard, let's grab its info
  1403. if (array_key_exists('hcard', $hints)) {
  1404. if (!array_key_exists('profileurl', $hints) ||
  1405. $hints['hcard'] != $hints['profileurl']) {
  1406. $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
  1407. $hints = array_merge($hcardHints, $hints);
  1408. }
  1409. }
  1410. // If we got a feed URL, try that
  1411. $feedUrl = null;
  1412. if (array_key_exists('feedurl', $hints)) {
  1413. $feedUrl = $hints['feedurl'];
  1414. try {
  1415. common_log(LOG_INFO, "Discovery on acct:$addr with feed URL " . $hints['feedurl']);
  1416. $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
  1417. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
  1418. return $oprofile;
  1419. } catch (Exception $e) {
  1420. common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
  1421. // keep looking
  1422. }
  1423. }
  1424. // If we got a profile page, try that!
  1425. $profileUrl = null;
  1426. if (array_key_exists('profileurl', $hints)) {
  1427. $profileUrl = $hints['profileurl'];
  1428. try {
  1429. common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
  1430. $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
  1431. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
  1432. return $oprofile;
  1433. } catch (OStatusShadowException $e) {
  1434. // We've ended up with a remote reference to a local user or group.
  1435. // @todo FIXME: Ideally we should be able to say who it was so we can
  1436. // go back and refer to it the regular way
  1437. throw $e;
  1438. } catch (Exception $e) {
  1439. common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
  1440. // keep looking
  1441. //
  1442. // @todo FIXME: This means an error discovering from profile page
  1443. // may give us a corrupt entry using the webfinger URI, which
  1444. // will obscure the correct page-keyed profile later on.
  1445. }
  1446. }
  1447. // XXX: try hcard
  1448. // XXX: try FOAF
  1449. if (array_key_exists('salmon', $hints)) {
  1450. $salmonEndpoint = $hints['salmon'];
  1451. // An account URL, a salmon endpoint, and a dream? Not much to go
  1452. // on, but let's give it a try
  1453. $uri = 'acct:'.$addr;
  1454. $profile = new Profile();
  1455. $profile->nickname = self::nicknameFromUri($uri);
  1456. $profile->created = common_sql_now();
  1457. if (!is_null($profileUrl)) {
  1458. $profile->profileurl = $profileUrl;
  1459. }
  1460. $profile_id = $profile->insert();
  1461. if ($profile_id === false) {
  1462. common_log_db_error($profile, 'INSERT', __FILE__);
  1463. // TRANS: Exception. %s is a webfinger address.
  1464. throw new Exception(sprintf(_m('Could not save profile for "%s".'),$addr));
  1465. }
  1466. $oprofile = new Ostatus_profile();
  1467. $oprofile->uri = $uri;
  1468. $oprofile->salmonuri = $salmonEndpoint;
  1469. $oprofile->profile_id = $profile_id;
  1470. $oprofile->created = common_sql_now();
  1471. if (!is_null($feedUrl)) {
  1472. $oprofile->feeduri = $feedUrl;
  1473. }
  1474. $result = $oprofile->insert();
  1475. if ($result === false) {
  1476. $profile->delete();
  1477. common_log_db_error($oprofile, 'INSERT', __FILE__);
  1478. // TRANS: Exception. %s is a webfinger address.
  1479. throw new Exception(sprintf(_m('Could not save OStatus profile for "%s".'),$addr));
  1480. }
  1481. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
  1482. return $oprofile;
  1483. }
  1484. // TRANS: Exception. %s is a webfinger address.
  1485. throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'),$addr));
  1486. }
  1487. /**
  1488. * Store the full-length scrubbed HTML of a remote notice to an attachment
  1489. * file on our server. We'll link to this at the end of the cropped version.
  1490. *
  1491. * @param string $title plaintext for HTML page's title
  1492. * @param string $rendered HTML fragment for HTML page's body
  1493. * @return File
  1494. */
  1495. function saveHTMLFile($title, $rendered)
  1496. {
  1497. $final = sprintf("<!DOCTYPE html>\n" .
  1498. '<html><head>' .
  1499. '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' .
  1500. '<title>%s</title>' .
  1501. '</head>' .
  1502. '<body>%s</body></html>',
  1503. htmlspecialchars($title),
  1504. $rendered);
  1505. $filename = File::filename($this->localProfile(),
  1506. 'ostatus', // ignored?
  1507. 'text/html');
  1508. $filepath = File::path($filename);
  1509. $fileurl = File::url($filename);
  1510. file_put_contents($filepath, $final);
  1511. $file = new File;
  1512. $file->filename = $filename;
  1513. $file->urlhash = File::hashurl($fileurl);
  1514. $file->url = $fileurl;
  1515. $file->size = filesize($filepath);
  1516. $file->date = time();
  1517. $file->mimetype = 'text/html';
  1518. $file_id = $file->insert();
  1519. if ($file_id === false) {
  1520. common_log_db_error($file, "INSERT", __FILE__);
  1521. // TRANS: Server exception.
  1522. throw new ServerException(_m('Could not store HTML content of long post as file.'));
  1523. }
  1524. return $file;
  1525. }
  1526. static function ensureProfileURI($uri)
  1527. {
  1528. $oprofile = null;
  1529. // First, try to query it
  1530. $oprofile = Ostatus_profile::getKV('uri', $uri);
  1531. if ($oprofile instanceof Ostatus_profile) {
  1532. return $oprofile;
  1533. }
  1534. // If unfound, do discovery stuff
  1535. if (preg_match("/^(\w+)\:(.*)/", $uri, $match)) {
  1536. $protocol = $match[1];
  1537. switch ($protocol) {
  1538. case 'http':
  1539. case 'https':
  1540. $oprofile = self::ensureProfileURL($uri);
  1541. break;
  1542. case 'acct':
  1543. case 'mailto':
  1544. $rest = $match[2];
  1545. $oprofile = self::ensureWebfinger($rest);
  1546. break;
  1547. default:
  1548. // TRANS: Server exception.
  1549. // TRANS: %1$s is a protocol, %2$s is a URI.
  1550. throw new ServerException(sprintf(_m('Unrecognized URI protocol for profile: %1$s (%2$s).'),
  1551. $protocol,
  1552. $uri));
  1553. }
  1554. } else {
  1555. // TRANS: Server exception. %s is a URI.
  1556. throw new ServerException(sprintf(_m('No URI protocol for profile: %s.'),$uri));
  1557. }
  1558. return $oprofile;
  1559. }
  1560. public function checkAuthorship(Activity $activity)
  1561. {
  1562. if ($this->isGroup() || $this->isPeopletag()) {
  1563. // A group or propletag feed will contain posts from multiple authors.
  1564. $oprofile = self::ensureActorProfile($activity);
  1565. if ($oprofile->isGroup() || $oprofile->isPeopletag()) {
  1566. // Groups can't post notices in StatusNet.
  1567. common_log(LOG_WARNING,
  1568. "OStatus: skipping post with group listed ".
  1569. "as author: " . $oprofile->getUri() . " in feed from " . $this->getUri());
  1570. throw new ServerException('Activity author is a non-actor');
  1571. }
  1572. } else {
  1573. $actor = $activity->actor;
  1574. if (empty($actor)) {
  1575. // OK here! assume the default
  1576. } else if ($actor->id == $this->getUri() || $actor->link == $this->getUri()) {
  1577. $this->updateFromActivityObject($actor);
  1578. } else if ($actor->id) {
  1579. // We have an ActivityStreams actor with an explicit ID that doesn't match the feed owner.
  1580. // This isn't what we expect from mainline OStatus person feeds!
  1581. // Group feeds go down another path, with different validation...
  1582. // Most likely this is a plain ol' blog feed of some kind which
  1583. // doesn't match our expectations. We'll take the entry, but ignore
  1584. // the <author> info.
  1585. common_log(LOG_WARNING, "Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for " . $this->getUri());
  1586. } else {
  1587. // Plain <author> without ActivityStreams actor info.
  1588. // We'll just ignore this info for now and save the update under the feed's identity.
  1589. }
  1590. $oprofile = $this;
  1591. }
  1592. return $oprofile->localProfile();
  1593. }
  1594. public function updateUriKeys($profile_uri, array $hints=array())
  1595. {
  1596. $orig = clone($this);
  1597. common_debug('URIFIX These identities both say they are each other: '._ve($orig->uri).' and '._ve($profile_uri));
  1598. $this->uri = $profile_uri;
  1599. if (array_key_exists('feedurl', $hints) && common_valid_http_url($hints['feedurl'])) {
  1600. try {
  1601. $feedsub = $this->getFeedSub();
  1602. common_debug('URIFIX Changing FeedSub id==['._ve($feedsub->id).'] feeduri '._ve($feedsub->uri).' to '._ve($hints['feedurl']));
  1603. $feedorig = clone($feedsub);
  1604. $feedsub->uri = $hints['feedurl'];
  1605. $feedsub->updateWithKeys($feedorig);
  1606. } catch (EmptyPkeyValueException $e) {
  1607. common_debug('URIFIX Old Ostatus_profile did not have feedurl set, ensuring new feedurl: '._ve($hints['feedurl']));
  1608. FeedSub::ensureFeed($hints['feedurl']);
  1609. } catch (NoResultException $e) {
  1610. common_debug('URIFIX Missing FeedSub entry for the Ostatus_profile, ensuring new feedurl: '._ve($hints['feedurl']));
  1611. FeedSub::ensureFeed($hints['feedurl']);
  1612. }
  1613. $this->feeduri = $hints['feedurl'];
  1614. } elseif (array_key_exists('feedurl')) {
  1615. common_log(LOG_WARN, 'The feedurl hint we got was not a valid HTTP URL: '._ve($hints['feedurl']));
  1616. }
  1617. if (array_key_exists('salmon', $hints)) {
  1618. common_debug('URIFIX Changing Ostatus_profile salmonuri from "'.$this->salmonuri.'" to "'.$hints['salmon'].'"');
  1619. $this->salmonuri = $hints['salmon'];
  1620. }
  1621. common_debug('URIFIX Updating Ostatus_profile URI for '.$orig->uri.' to '.$this->uri);
  1622. $this->updateWithKeys($orig); // Will use the PID column(s) in the 'UPDATE ... WHERE [unique selector]'
  1623. common_debug('URIFIX Subscribing/renewing feedsub for Ostatus_profile '.$this->uri);
  1624. $this->subscribe();
  1625. }
  1626. }