Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2160 lines
74 KiB

5 years ago
5 years ago
  1. <?php
  2. /**
  3. * @file include/ostatus.php
  4. */
  5. require_once("include/Contact.php");
  6. require_once("include/threads.php");
  7. require_once("include/html2bbcode.php");
  8. require_once("include/bbcode.php");
  9. require_once("include/items.php");
  10. require_once("mod/share.php");
  11. require_once("include/enotify.php");
  12. require_once("include/socgraph.php");
  13. require_once("include/Photo.php");
  14. require_once("include/Scrape.php");
  15. require_once("include/follow.php");
  16. require_once("include/api.php");
  17. require_once("mod/proxy.php");
  18. require_once("include/xml.php");
  19. /**
  20. * @brief This class contain functions for the OStatus protocol
  21. *
  22. */
  23. class ostatus {
  24. const OSTATUS_DEFAULT_POLL_INTERVAL = 30; // given in minutes
  25. const OSTATUS_DEFAULT_POLL_TIMEFRAME = 1440; // given in minutes
  26. const OSTATUS_DEFAULT_POLL_TIMEFRAME_MENTIONS = 14400; // given in minutes
  27. /**
  28. * @brief Fetches author data
  29. *
  30. * @param object $xpath The xpath object
  31. * @param object $context The xml context of the author detals
  32. * @param array $importer user record of the importing user
  33. * @param array $contact Called by reference, will contain the fetched contact
  34. * @param bool $onlyfetch Only fetch the header without updating the contact entries
  35. *
  36. * @return array Array of author related entries for the item
  37. */
  38. private function fetchauthor($xpath, $context, $importer, &$contact, $onlyfetch) {
  39. $author = array();
  40. $author["author-link"] = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
  41. $author["author-name"] = $xpath->evaluate('atom:author/atom:name/text()', $context)->item(0)->nodeValue;
  42. $aliaslink = $author["author-link"];
  43. $alternate = $xpath->query("atom:author/atom:link[@rel='alternate']", $context)->item(0)->attributes;
  44. if (is_object($alternate))
  45. foreach($alternate AS $attributes)
  46. if ($attributes->name == "href")
  47. $author["author-link"] = $attributes->textContent;
  48. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` IN ('%s', '%s') AND `network` != '%s'",
  49. intval($importer["uid"]), dbesc(normalise_link($author["author-link"])),
  50. dbesc(normalise_link($aliaslink)), dbesc(NETWORK_STATUSNET));
  51. if ($r) {
  52. $contact = $r[0];
  53. $author["contact-id"] = $r[0]["id"];
  54. } else
  55. $author["contact-id"] = $contact["id"];
  56. $avatarlist = array();
  57. $avatars = $xpath->query("atom:author/atom:link[@rel='avatar']", $context);
  58. foreach($avatars AS $avatar) {
  59. $href = "";
  60. $width = 0;
  61. foreach($avatar->attributes AS $attributes) {
  62. if ($attributes->name == "href")
  63. $href = $attributes->textContent;
  64. if ($attributes->name == "width")
  65. $width = $attributes->textContent;
  66. }
  67. if (($width > 0) AND ($href != ""))
  68. $avatarlist[$width] = $href;
  69. }
  70. if (count($avatarlist) > 0) {
  71. krsort($avatarlist);
  72. $author["author-avatar"] = current($avatarlist);
  73. }
  74. $displayname = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
  75. if ($displayname != "")
  76. $author["author-name"] = $displayname;
  77. $author["owner-name"] = $author["author-name"];
  78. $author["owner-link"] = $author["author-link"];
  79. $author["owner-avatar"] = $author["author-avatar"];
  80. // Only update the contacts if it is an OStatus contact
  81. if ($r AND !$onlyfetch AND ($contact["network"] == NETWORK_OSTATUS)) {
  82. // Update contact data
  83. // This query doesn't seem to work
  84. // $value = $xpath->query("atom:link[@rel='salmon']", $context)->item(0)->nodeValue;
  85. // if ($value != "")
  86. // $contact["notify"] = $value;
  87. // This query doesn't seem to work as well - I hate these queries
  88. // $value = $xpath->query("atom:link[@rel='self' and @type='application/atom+xml']", $context)->item(0)->nodeValue;
  89. // if ($value != "")
  90. // $contact["poll"] = $value;
  91. $value = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
  92. if ($value != "")
  93. $contact["alias"] = $value;
  94. $value = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
  95. if ($value != "")
  96. $contact["name"] = $value;
  97. $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()', $context)->item(0)->nodeValue;
  98. if ($value != "")
  99. $contact["nick"] = $value;
  100. $value = $xpath->evaluate('atom:author/poco:note/text()', $context)->item(0)->nodeValue;
  101. if ($value != "")
  102. $contact["about"] = html2bbcode($value);
  103. $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
  104. if ($value != "")
  105. $contact["location"] = $value;
  106. if (($contact["name"] != $r[0]["name"]) OR ($contact["nick"] != $r[0]["nick"]) OR ($contact["about"] != $r[0]["about"]) OR
  107. ($contact["alias"] != $r[0]["alias"]) OR ($contact["location"] != $r[0]["location"])) {
  108. logger("Update contact data for contact ".$contact["id"], LOGGER_DEBUG);
  109. q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `alias` = '%s', `about` = '%s', `location` = '%s', `name-date` = '%s' WHERE `id` = %d",
  110. dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["alias"]),
  111. dbesc($contact["about"]), dbesc($contact["location"]),
  112. dbesc(datetime_convert()), intval($contact["id"]));
  113. poco_check($contact["url"], $contact["name"], $contact["network"], $author["author-avatar"], $contact["about"], $contact["location"],
  114. "", "", "", datetime_convert(), 2, $contact["id"], $contact["uid"]);
  115. }
  116. if (isset($author["author-avatar"]) AND ($author["author-avatar"] != $r[0]['avatar'])) {
  117. logger("Update profile picture for contact ".$contact["id"], LOGGER_DEBUG);
  118. update_contact_avatar($author["author-avatar"], $importer["uid"], $contact["id"]);
  119. }
  120. // Ensure that we are having this contact (with uid=0)
  121. $cid = get_contact($author["author-link"], 0);
  122. if ($cid) {
  123. // Update it with the current values
  124. q("UPDATE `contact` SET `url` = '%s', `name` = '%s', `nick` = '%s', `alias` = '%s',
  125. `about` = '%s', `location` = '%s',
  126. `success_update` = '%s', `last-update` = '%s'
  127. WHERE `id` = %d",
  128. dbesc($author["author-link"]), dbesc($contact["name"]), dbesc($contact["nick"]),
  129. dbesc($contact["alias"]), dbesc($contact["about"]), dbesc($contact["location"]),
  130. dbesc(datetime_convert()), dbesc(datetime_convert()), intval($cid));
  131. // Update the avatar
  132. update_contact_avatar($author["author-avatar"], 0, $cid);
  133. }
  134. $contact["generation"] = 2;
  135. $contact["hide"] = false; // OStatus contacts are never hidden
  136. $contact["photo"] = $author["author-avatar"];
  137. update_gcontact($contact);
  138. }
  139. return($author);
  140. }
  141. /**
  142. * @brief Fetches author data from a given XML string
  143. *
  144. * @param string $xml The XML
  145. * @param array $importer user record of the importing user
  146. *
  147. * @return array Array of author related entries for the item
  148. */
  149. public static function salmon_author($xml, $importer) {
  150. if ($xml == "")
  151. return;
  152. $doc = new DOMDocument();
  153. @$doc->loadXML($xml);
  154. $xpath = new DomXPath($doc);
  155. $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
  156. $xpath->registerNamespace('thr', NAMESPACE_THREAD);
  157. $xpath->registerNamespace('georss', NAMESPACE_GEORSS);
  158. $xpath->registerNamespace('activity', NAMESPACE_ACTIVITY);
  159. $xpath->registerNamespace('media', NAMESPACE_MEDIA);
  160. $xpath->registerNamespace('poco', NAMESPACE_POCO);
  161. $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
  162. $xpath->registerNamespace('statusnet', NAMESPACE_STATUSNET);
  163. $entries = $xpath->query('/atom:entry');
  164. foreach ($entries AS $entry) {
  165. // fetch the author
  166. $author = self::fetchauthor($xpath, $entry, $importer, $contact, true);
  167. return $author;
  168. }
  169. }
  170. /**
  171. * @brief Imports an XML string containing OStatus elements
  172. *
  173. * @param string $xml The XML
  174. * @param array $importer user record of the importing user
  175. * @param $contact
  176. * @param array $hub Called by reference, returns the fetched hub data
  177. */
  178. public static function import($xml,$importer,&$contact, &$hub) {
  179. /// @todo this function is too long. It has to be split in many parts
  180. logger("Import OStatus message", LOGGER_DEBUG);
  181. if ($xml == "")
  182. return;
  183. //$tempfile = tempnam(get_temppath(), "import");
  184. //file_put_contents($tempfile, $xml);
  185. $doc = new DOMDocument();
  186. @$doc->loadXML($xml);
  187. $xpath = new DomXPath($doc);
  188. $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
  189. $xpath->registerNamespace('thr', NAMESPACE_THREAD);
  190. $xpath->registerNamespace('georss', NAMESPACE_GEORSS);
  191. $xpath->registerNamespace('activity', NAMESPACE_ACTIVITY);
  192. $xpath->registerNamespace('media', NAMESPACE_MEDIA);
  193. $xpath->registerNamespace('poco', NAMESPACE_POCO);
  194. $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
  195. $xpath->registerNamespace('statusnet', NAMESPACE_STATUSNET);
  196. $gub = "";
  197. $hub_attributes = $xpath->query("/atom:feed/atom:link[@rel='hub']")->item(0)->attributes;
  198. if (is_object($hub_attributes))
  199. foreach($hub_attributes AS $hub_attribute)
  200. if ($hub_attribute->name == "href") {
  201. $hub = $hub_attribute->textContent;
  202. logger("Found hub ".$hub, LOGGER_DEBUG);
  203. }
  204. $header = array();
  205. $header["uid"] = $importer["uid"];
  206. $header["network"] = NETWORK_OSTATUS;
  207. $header["type"] = "remote";
  208. $header["wall"] = 0;
  209. $header["origin"] = 0;
  210. $header["gravity"] = GRAVITY_PARENT;
  211. // it could either be a received post or a post we fetched by ourselves
  212. // depending on that, the first node is different
  213. $first_child = $doc->firstChild->tagName;
  214. if ($first_child == "feed")
  215. $entries = $xpath->query('/atom:feed/atom:entry');
  216. else
  217. $entries = $xpath->query('/atom:entry');
  218. $conversation = "";
  219. $conversationlist = array();
  220. $item_id = 0;
  221. // Reverse the order of the entries
  222. $entrylist = array();
  223. foreach ($entries AS $entry)
  224. $entrylist[] = $entry;
  225. foreach (array_reverse($entrylist) AS $entry) {
  226. $mention = false;
  227. // fetch the author
  228. if ($first_child == "feed")
  229. $author = self::fetchauthor($xpath, $doc->firstChild, $importer, $contact, false);
  230. else
  231. $author = self::fetchauthor($xpath, $entry, $importer, $contact, false);
  232. $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()', $context)->item(0)->nodeValue;
  233. if ($value != "")
  234. $nickname = $value;
  235. else
  236. $nickname = $author["author-name"];
  237. $item = array_merge($header, $author);
  238. // Now get the item
  239. $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
  240. $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
  241. intval($importer["uid"]), dbesc($item["uri"]));
  242. if ($r) {
  243. logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
  244. continue;
  245. }
  246. $item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
  247. $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
  248. if (($item["object-type"] == ACTIVITY_OBJ_BOOKMARK) OR ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
  249. $item["title"] = $xpath->query('atom:title/text()', $entry)->item(0)->nodeValue;
  250. $item["body"] = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
  251. } elseif ($item["object-type"] == ACTIVITY_OBJ_QUESTION)
  252. $item["title"] = $xpath->query('atom:title/text()', $entry)->item(0)->nodeValue;
  253. $item["object"] = $xml;
  254. $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
  255. /// @TODO
  256. /// Delete a message
  257. if ($item["verb"] == "qvitter-delete-notice") {
  258. // ignore "Delete" messages (by now)
  259. logger("Ignore delete message ".print_r($item, true));
  260. continue;
  261. }
  262. if ($item["verb"] == ACTIVITY_JOIN) {
  263. // ignore "Join" messages
  264. logger("Ignore join message ".print_r($item, true));
  265. continue;
  266. }
  267. if ($item["verb"] == ACTIVITY_FOLLOW) {
  268. new_follower($importer, $contact, $item, $nickname);
  269. continue;
  270. }
  271. if ($item["verb"] == NAMESPACE_OSTATUS."/unfollow") {
  272. lose_follower($importer, $contact, $item, $dummy);
  273. continue;
  274. }
  275. if ($item["verb"] == ACTIVITY_FAVORITE) {
  276. $orig_uri = $xpath->query("activity:object/atom:id", $entry)->item(0)->nodeValue;
  277. logger("Favorite ".$orig_uri." ".print_r($item, true));
  278. $item["verb"] = ACTIVITY_LIKE;
  279. $item["parent-uri"] = $orig_uri;
  280. $item["gravity"] = GRAVITY_LIKE;
  281. }
  282. if ($item["verb"] == NAMESPACE_OSTATUS."/unfavorite") {
  283. // Ignore "Unfavorite" message
  284. logger("Ignore unfavorite message ".print_r($item, true));
  285. continue;
  286. }
  287. // http://activitystrea.ms/schema/1.0/rsvp-yes
  288. if (!in_array($item["verb"], array(ACTIVITY_POST, ACTIVITY_LIKE, ACTIVITY_SHARE)))
  289. logger("Unhandled verb ".$item["verb"]." ".print_r($item, true));
  290. $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
  291. $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
  292. $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
  293. $related = "";
  294. $inreplyto = $xpath->query('thr:in-reply-to', $entry);
  295. if (is_object($inreplyto->item(0))) {
  296. foreach($inreplyto->item(0)->attributes AS $attributes) {
  297. if ($attributes->name == "ref")
  298. $item["parent-uri"] = $attributes->textContent;
  299. if ($attributes->name == "href")
  300. $related = $attributes->textContent;
  301. }
  302. }
  303. $georsspoint = $xpath->query('georss:point', $entry);
  304. if ($georsspoint)
  305. $item["coord"] = $georsspoint->item(0)->nodeValue;
  306. $categories = $xpath->query('atom:category', $entry);
  307. if ($categories) {
  308. foreach ($categories AS $category) {
  309. foreach($category->attributes AS $attributes)
  310. if ($attributes->name == "term") {
  311. $term = $attributes->textContent;
  312. if(strlen($item["tag"]))
  313. $item["tag"] .= ',';
  314. $item["tag"] .= "#[url=".App::get_baseurl()."/search?tag=".$term."]".$term."[/url]";
  315. }
  316. }
  317. }
  318. $self = "";
  319. $enclosure = "";
  320. $links = $xpath->query('atom:link', $entry);
  321. if ($links) {
  322. $rel = "";
  323. $href = "";
  324. $type = "";
  325. $length = "0";
  326. $title = "";
  327. foreach ($links AS $link) {
  328. foreach($link->attributes AS $attributes) {
  329. if ($attributes->name == "href")
  330. $href = $attributes->textContent;
  331. if ($attributes->name == "rel")
  332. $rel = $attributes->textContent;
  333. if ($attributes->name == "type")
  334. $type = $attributes->textContent;
  335. if ($attributes->name == "length")
  336. $length = $attributes->textContent;
  337. if ($attributes->name == "title")
  338. $title = $attributes->textContent;
  339. }
  340. if (($rel != "") AND ($href != ""))
  341. switch($rel) {
  342. case "alternate":
  343. $item["plink"] = $href;
  344. if (($item["object-type"] == ACTIVITY_OBJ_QUESTION) OR
  345. ($item["object-type"] == ACTIVITY_OBJ_EVENT))
  346. $item["body"] .= add_page_info($href);
  347. break;
  348. case "ostatus:conversation":
  349. $conversation = $href;
  350. break;
  351. case "enclosure":
  352. $enclosure = $href;
  353. if(strlen($item["attach"]))
  354. $item["attach"] .= ',';
  355. $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
  356. break;
  357. case "related":
  358. if ($item["object-type"] != ACTIVITY_OBJ_BOOKMARK) {
  359. if (!isset($item["parent-uri"]))
  360. $item["parent-uri"] = $href;
  361. if ($related == "")
  362. $related = $href;
  363. } else
  364. $item["body"] .= add_page_info($href);
  365. break;
  366. case "self":
  367. $self = $href;
  368. break;
  369. case "mentioned":
  370. // Notification check
  371. if ($importer["nurl"] == normalise_link($href))
  372. $mention = true;
  373. break;
  374. }
  375. }
  376. }
  377. $local_id = "";
  378. $repeat_of = "";
  379. $notice_info = $xpath->query('statusnet:notice_info', $entry);
  380. if ($notice_info AND ($notice_info->length > 0)) {
  381. foreach($notice_info->item(0)->attributes AS $attributes) {
  382. if ($attributes->name == "source")
  383. $item["app"] = strip_tags($attributes->textContent);
  384. if ($attributes->name == "local_id")
  385. $local_id = $attributes->textContent;
  386. if ($attributes->name == "repeat_of")
  387. $repeat_of = $attributes->textContent;
  388. }
  389. }
  390. // Is it a repeated post?
  391. if (($repeat_of != "") OR ($item["verb"] == ACTIVITY_SHARE)) {
  392. $activityobjects = $xpath->query('activity:object', $entry)->item(0);
  393. if (is_object($activityobjects)) {
  394. $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
  395. if (!isset($orig_uri))
  396. $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
  397. $orig_links = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
  398. if ($orig_links AND ($orig_links->length > 0))
  399. foreach($orig_links->item(0)->attributes AS $attributes)
  400. if ($attributes->name == "href")
  401. $orig_link = $attributes->textContent;
  402. if (!isset($orig_link))
  403. $orig_link = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
  404. if (!isset($orig_link))
  405. $orig_link = self::convert_href($orig_uri);
  406. $orig_body = $xpath->query('activity:object/atom:content/text()', $activityobjects)->item(0)->nodeValue;
  407. if (!isset($orig_body))
  408. $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
  409. $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
  410. $orig_edited = $xpath->query('atom:updated/text()', $activityobjects)->item(0)->nodeValue;
  411. $orig_contact = $contact;
  412. $orig_author = self::fetchauthor($xpath, $activityobjects, $importer, $orig_contact, false);
  413. $item["author-name"] = $orig_author["author-name"];
  414. $item["author-link"] = $orig_author["author-link"];
  415. $item["author-avatar"] = $orig_author["author-avatar"];
  416. $item["body"] = add_page_info_to_body(html2bbcode($orig_body));
  417. $item["created"] = $orig_created;
  418. $item["edited"] = $orig_edited;
  419. $item["uri"] = $orig_uri;
  420. $item["plink"] = $orig_link;
  421. $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
  422. $item["object-type"] = $xpath->query('activity:object/activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
  423. if (!isset($item["object-type"]))
  424. $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
  425. }
  426. }
  427. //if ($enclosure != "")
  428. // $item["body"] .= add_page_info($enclosure);
  429. if (isset($item["parent-uri"])) {
  430. $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
  431. intval($importer["uid"]), dbesc($item["parent-uri"]));
  432. // Only fetch missing stuff if it is a comment or reshare.
  433. if (in_array($item["verb"], array(ACTIVITY_POST, ACTIVITY_SHARE)) AND
  434. !dbm::is_result($r) AND ($related != "")) {
  435. $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
  436. if ($reply_path != $related) {
  437. logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
  438. $reply_xml = fetch_url($reply_path);
  439. $reply_contact = $contact;
  440. self::import($reply_xml,$importer,$reply_contact, $reply_hub);
  441. // After the import try to fetch the parent item again
  442. $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
  443. intval($importer["uid"]), dbesc($item["parent-uri"]));
  444. }
  445. }
  446. if ($r) {
  447. $item["type"] = 'remote-comment';
  448. $item["gravity"] = GRAVITY_COMMENT;
  449. }
  450. } else
  451. $item["parent-uri"] = $item["uri"];
  452. $item_id = self::completion($conversation, $importer["uid"], $item, $self);
  453. if (!$item_id) {
  454. logger("Error storing item", LOGGER_DEBUG);
  455. continue;
  456. }
  457. logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
  458. }
  459. }
  460. /**
  461. * @brief Create an url out of an uri
  462. *
  463. * @param string $href URI in the format "parameter1:parameter1:..."
  464. *
  465. * @return string URL in the format http(s)://....
  466. */
  467. public static function convert_href($href) {
  468. $elements = explode(":",$href);
  469. if ((count($elements) <= 2) OR ($elements[0] != "tag"))
  470. return $href;
  471. $server = explode(",", $elements[1]);
  472. $conversation = explode("=", $elements[2]);
  473. if ((count($elements) == 4) AND ($elements[2] == "post"))
  474. return "http://".$server[0]."/notice/".$elements[3];
  475. if ((count($conversation) != 2) OR ($conversation[1] ==""))
  476. return $href;
  477. if ($elements[3] == "objectType=thread")
  478. return "http://".$server[0]."/conversation/".$conversation[1];
  479. else
  480. return "http://".$server[0]."/notice/".$conversation[1];
  481. return $href;
  482. }
  483. /**
  484. * @brief Checks if there are entries in conversations that aren't present on our side
  485. *
  486. * @param bool $mentions Fetch conversations where we are mentioned
  487. * @param bool $override Override the interval setting
  488. */
  489. public static function check_conversations($mentions = false, $override = false) {
  490. $last = get_config('system','ostatus_last_poll');
  491. $poll_interval = intval(get_config('system','ostatus_poll_interval'));
  492. if (!$poll_interval) {
  493. $poll_interval = self::OSTATUS_DEFAULT_POLL_INTERVAL;
  494. }
  495. // Don't poll if the interval is set negative
  496. if (($poll_interval < 0) AND !$override) {
  497. return;
  498. }
  499. if (!$mentions) {
  500. $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
  501. if (!$poll_timeframe) {
  502. $poll_timeframe = self::OSTATUS_DEFAULT_POLL_TIMEFRAME;
  503. }
  504. } else {
  505. $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
  506. if (!$poll_timeframe) {
  507. $poll_timeframe = self::OSTATUS_DEFAULT_POLL_TIMEFRAME_MENTIONS;
  508. }
  509. }
  510. if ($last AND !$override) {
  511. $next = $last + ($poll_interval * 60);
  512. if ($next > time()) {
  513. logger('poll interval not reached');
  514. return;
  515. }
  516. }
  517. logger('cron_start');
  518. $start = date("Y-m-d H:i:s", time() - ($poll_timeframe * 60));
  519. if ($mentions) {
  520. $conversations = q("SELECT `term`.`oid`, `term`.`url`, `term`.`uid` FROM `term`
  521. STRAIGHT_JOIN `thread` ON `thread`.`iid` = `term`.`oid` AND `thread`.`uid` = `term`.`uid`
  522. WHERE `term`.`type` = 7 AND `term`.`term` > '%s' AND `thread`.`mention`
  523. GROUP BY `term`.`url`, `term`.`uid` ORDER BY `term`.`term` DESC", dbesc($start));
  524. } else {
  525. $conversations = q("SELECT `oid`, `url`, `uid` FROM `term`
  526. WHERE `type` = 7 AND `term` > '%s'
  527. GROUP BY `url`, `uid` ORDER BY `term` DESC", dbesc($start));
  528. }
  529. foreach ($conversations AS $conversation) {
  530. self::completion($conversation['url'], $conversation['uid']);
  531. }
  532. logger('cron_end');
  533. set_config('system','ostatus_last_poll', time());
  534. }
  535. /**
  536. * @brief Updates the gcontact table with actor data from the conversation
  537. *
  538. * @param object $actor The actor object that contains the contact data
  539. */
  540. private function conv_fetch_actor($actor) {
  541. // We set the generation to "3" since the data here is not as reliable as the data we get on other occasions
  542. $contact = array("network" => NETWORK_OSTATUS, "generation" => 3);
  543. if (isset($actor->url))
  544. $contact["url"] = $actor->url;
  545. if (isset($actor->displayName))
  546. $contact["name"] = $actor->displayName;
  547. if (isset($actor->portablecontacts_net->displayName))
  548. $contact["name"] = $actor->portablecontacts_net->displayName;
  549. if (isset($actor->portablecontacts_net->preferredUsername))
  550. $contact["nick"] = $actor->portablecontacts_net->preferredUsername;
  551. if (isset($actor->id))
  552. $contact["alias"] = $actor->id;
  553. if (isset($actor->summary))
  554. $contact["about"] = $actor->summary;
  555. if (isset($actor->portablecontacts_net->note))
  556. $contact["about"] = $actor->portablecontacts_net->note;
  557. if (isset($actor->portablecontacts_net->addresses->formatted))
  558. $contact["location"] = $actor->portablecontacts_net->addresses->formatted;
  559. if (isset($actor->image->url))
  560. $contact["photo"] = $actor->image->url;
  561. if (isset($actor->image->width))
  562. $avatarwidth = $actor->image->width;
  563. if (is_array($actor->status_net->avatarLinks))
  564. foreach ($actor->status_net->avatarLinks AS $avatar) {
  565. if ($avatarsize < $avatar->width) {
  566. $contact["photo"] = $avatar->url;
  567. $avatarsize = $avatar->width;
  568. }
  569. }
  570. $contact["hide"] = false; // OStatus contacts are never hidden
  571. update_gcontact($contact);
  572. }
  573. /**
  574. * @brief Fetches the conversation url for a given item link or conversation id
  575. *
  576. * @param string $self The link to the posting
  577. * @param string $conversation_id The conversation id
  578. *
  579. * @return string The conversation url
  580. */
  581. private function fetch_conversation($self, $conversation_id = "") {
  582. if ($conversation_id != "") {
  583. $elements = explode(":", $conversation_id);
  584. if ((count($elements) <= 2) OR ($elements[0] != "tag"))
  585. return $conversation_id;
  586. }
  587. if ($self == "")
  588. return "";
  589. $json = str_replace(".atom", ".json", $self);
  590. $raw = fetch_url($json);
  591. if ($raw == "")
  592. return "";
  593. $data = json_decode($raw);
  594. if (!is_object($data))
  595. return "";
  596. $conversation_id = $data->statusnet_conversation_id;
  597. $pos = strpos($self, "/api/statuses/show/");
  598. $base_url = substr($self, 0, $pos);
  599. return $base_url."/conversation/".$conversation_id;
  600. }
  601. /**
  602. * @brief Fetches actor details of a given actor and user id
  603. *
  604. * @param string $actor The actor url
  605. * @param int $uid The user id
  606. * @param int $contact_id The default contact-id
  607. *
  608. * @return array Array with actor details
  609. */
  610. private function get_actor_details($actor, $uid, $contact_id) {
  611. $details = array();
  612. $contact = q("SELECT `id`, `rel`, `network` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
  613. $uid, normalise_link($actor), NETWORK_STATUSNET);
  614. if (!$contact)
  615. $contact = q("SELECT `id`, `rel`, `network` FROM `contact` WHERE `uid` = %d AND `alias` IN ('%s', '%s') AND `network` != '%s'",
  616. $uid, $actor, normalise_link($actor), NETWORK_STATUSNET);
  617. if ($contact) {
  618. logger("Found contact for url ".$actor, LOGGER_DEBUG);
  619. $details["contact_id"] = $contact[0]["id"];
  620. $details["network"] = $contact[0]["network"];
  621. $details["not_following"] = !in_array($contact[0]["rel"], array(CONTACT_IS_SHARING, CONTACT_IS_FRIEND));
  622. } else {
  623. logger("No contact found for user ".$uid." and url ".$actor, LOGGER_DEBUG);
  624. // Adding a global contact
  625. /// @TODO Use this data for the post
  626. $details["global_contact_id"] = get_contact($actor, 0);
  627. logger("Global contact ".$global_contact_id." found for url ".$actor, LOGGER_DEBUG);
  628. $details["contact_id"] = $contact_id;
  629. $details["network"] = NETWORK_OSTATUS;
  630. $details["not_following"] = true;
  631. }
  632. return $details;
  633. }
  634. /**
  635. * @brief Stores an item and completes the thread
  636. *
  637. * @param string $conversation_url The URI of the conversation
  638. * @param integer $uid The user id
  639. * @param array $item Data of the item that is to be posted
  640. *
  641. * @return integer The item id of the posted item array
  642. */
  643. private function completion($conversation_url, $uid, $item = array(), $self = "") {
  644. /// @todo This function is totally ugly and has to be rewritten totally
  645. $item_stored = -1;
  646. $conversation_url = self::fetch_conversation($self, $conversation_url);
  647. // If the thread shouldn't be completed then store the item and go away
  648. // Don't do a completion on liked content
  649. if (((intval(get_config('system','ostatus_poll_interval')) == -2) AND (count($item) > 0)) OR
  650. ($item["verb"] == ACTIVITY_LIKE) OR ($conversation_url == "")) {
  651. $item_stored = item_store($item, true);
  652. return($item_stored);
  653. }
  654. // Get the parent
  655. $parents = q("SELECT `item`.`id`, `item`.`parent`, `item`.`uri`, `item`.`contact-id`, `item`.`type`,
  656. `item`.`verb`, `item`.`visible` FROM `term`
  657. STRAIGHT_JOIN `item` AS `thritem` ON `thritem`.`parent` = `term`.`oid`
  658. STRAIGHT_JOIN `item` ON `item`.`parent` = `thritem`.`parent`
  659. WHERE `term`.`uid` = %d AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`url` = '%s'",
  660. intval($uid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($conversation_url));
  661. /* 2016-10-23: The old query will be kept until we are sure that the query above is a good and fast replacement
  662. $parents = q("SELECT `id`, `parent`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `id` IN
  663. (SELECT `parent` FROM `item` WHERE `id` IN
  664. (SELECT `oid` FROM `term` WHERE `uid` = %d AND `otype` = %d AND `type` = %d AND `url` = '%s'))",
  665. intval($uid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($conversation_url));
  666. */
  667. if ($parents)
  668. $parent = $parents[0];
  669. elseif (count($item) > 0) {
  670. $parent = $item;
  671. $parent["type"] = "remote";
  672. $parent["verb"] = ACTIVITY_POST;
  673. $parent["visible"] = 1;
  674. } else {
  675. // Preset the parent
  676. $r = q("SELECT `id` FROM `contact` WHERE `self` AND `uid`=%d", $uid);
  677. if (!$r)
  678. return(-2);
  679. $parent = array();
  680. $parent["id"] = 0;
  681. $parent["parent"] = 0;
  682. $parent["uri"] = "";
  683. $parent["contact-id"] = $r[0]["id"];
  684. $parent["type"] = "remote";
  685. $parent["verb"] = ACTIVITY_POST;
  686. $parent["visible"] = 1;
  687. }
  688. $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
  689. $pageno = 1;
  690. $items = array();
  691. logger('fetching conversation url '.$conv.' (Self: '.$self.') for user '.$uid);
  692. do {
  693. $conv_arr = z_fetch_url($conv."?page=".$pageno);
  694. // If it is a non-ssl site and there is an error, then try ssl or vice versa
  695. if (!$conv_arr["success"] AND (substr($conv, 0, 7) == "http://")) {
  696. $conv = str_replace("http://", "https://", $conv);
  697. $conv_as = fetch_url($conv."?page=".$pageno);
  698. } elseif (!$conv_arr["success"] AND (substr($conv, 0, 8) == "https://")) {
  699. $conv = str_replace("https://", "http://", $conv);
  700. $conv_as = fetch_url($conv."?page=".$pageno);
  701. } else
  702. $conv_as = $conv_arr["body"];
  703. $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
  704. $conv_as = json_decode($conv_as);
  705. $no_of_items = sizeof($items);
  706. if (@is_array($conv_as->items))
  707. foreach ($conv_as->items AS $single_item)
  708. $items[$single_item->id] = $single_item;
  709. if ($no_of_items == sizeof($items))
  710. break;
  711. $pageno++;
  712. } while (true);
  713. logger('fetching conversation done. Found '.count($items).' items');
  714. if (!sizeof($items)) {
  715. if (count($item) > 0) {
  716. $item_stored = item_store($item, true);
  717. if ($item_stored) {
  718. logger("Conversation ".$conversation_url." couldn't be fetched. Item uri ".$item["uri"]." stored: ".$item_stored, LOGGER_DEBUG);
  719. self::store_conversation($item_id, $conversation_url);
  720. }
  721. return($item_stored);
  722. } else
  723. return(-3);
  724. }
  725. $items = array_reverse($items);
  726. $r = q("SELECT `nurl` FROM `contact` WHERE `uid` = %d AND `self`", intval($uid));
  727. $importer = $r[0];
  728. $new_parent = true;
  729. foreach ($items as $single_conv) {
  730. // Update the gcontact table
  731. self::conv_fetch_actor($single_conv->actor);
  732. // Test - remove before flight
  733. //$tempfile = tempnam(get_temppath(), "conversation");
  734. //file_put_contents($tempfile, json_encode($single_conv));
  735. $mention = false;
  736. if (isset($single_conv->object->id))
  737. $single_conv->id = $single_conv->object->id;
  738. $plink = self::convert_href($single_conv->id);
  739. if (isset($single_conv->object->url))
  740. $plink = self::convert_href($single_conv->object->url);
  741. if (@!$single_conv->id)
  742. continue;
  743. logger("Got id ".$single_conv->id, LOGGER_DEBUG);
  744. if ($first_id == "") {
  745. $first_id = $single_conv->id;
  746. // The first post of the conversation isn't our first post. There are three options:
  747. // 1. Our conversation hasn't the "real" thread starter
  748. // 2. This first post is a post inside our thread
  749. // 3. This first post is a post inside another thread
  750. if (($first_id != $parent["uri"]) AND ($parent["uri"] != "")) {
  751. $new_parent = true;
  752. $new_parents = q("SELECT `id`, `parent`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `id` IN
  753. (SELECT `parent` FROM `item`
  754. WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s')) LIMIT 1",
  755. intval($uid), dbesc($first_id), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
  756. if ($new_parents) {
  757. if ($new_parents[0]["parent"] == $parent["parent"]) {
  758. // Option 2: This post is already present inside our thread - but not as thread starter
  759. logger("Option 2: uri present in our thread: ".$first_id, LOGGER_DEBUG);
  760. $first_id = $parent["uri"];
  761. } else {
  762. // Option 3: Not so good. We have mixed parents. We have to see how to clean this up.
  763. // For now just take the new parent.
  764. $parent = $new_parents[0];
  765. $first_id = $parent["uri"];
  766. logger("Option 3: mixed parents for uri ".$first_id, LOGGER_DEBUG);
  767. }
  768. } else {
  769. // Option 1: We hadn't got the real thread starter
  770. // We have to clean up our existing messages.
  771. $parent["id"] = 0;
  772. $parent["uri"] = $first_id;
  773. logger("Option 1: we have a new parent: ".$first_id, LOGGER_DEBUG);
  774. }
  775. } elseif ($parent["uri"] == "") {
  776. $parent["id"] = 0;
  777. $parent["uri"] = $first_id;
  778. }
  779. }
  780. $parent_uri = $parent["uri"];
  781. // "context" only seems to exist on older servers
  782. if (isset($single_conv->context->inReplyTo->id)) {
  783. $parent_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
  784. intval($uid), dbesc($single_conv->context->inReplyTo->id), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
  785. if ($parent_exists)
  786. $parent_uri = $single_conv->context->inReplyTo->id;
  787. }
  788. // This is the current way
  789. if (isset($single_conv->object->inReplyTo->id)) {
  790. $parent_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
  791. intval($uid), dbesc($single_conv->object->inReplyTo->id), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
  792. if ($parent_exists)
  793. $parent_uri = $single_conv->object->inReplyTo->id;
  794. }
  795. $message_exists = q("SELECT `id`, `parent`, `uri` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
  796. intval($uid), dbesc($single_conv->id),
  797. dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
  798. if ($message_exists) {
  799. logger("Message ".$single_conv->id." already existed on the system", LOGGER_DEBUG);
  800. if ($parent["id"] != 0) {
  801. $existing_message = $message_exists[0];
  802. // We improved the way we fetch OStatus messages, this shouldn't happen very often now
  803. /// @TODO We have to change the shadow copies as well. This way here is really ugly.
  804. if ($existing_message["parent"] != $parent["id"]) {
  805. logger('updating id '.$existing_message["id"].' with parent '.$existing_message["parent"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
  806. // Update the parent id of the selected item
  807. $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `id` = %d",
  808. intval($parent["id"]), dbesc($parent["uri"]), intval($existing_message["id"]));
  809. // Update the parent uri in the thread - but only if it points to itself
  810. $r = q("UPDATE `item` SET `thr-parent` = '%s' WHERE `id` = %d AND `uri` = `thr-parent`",
  811. dbesc($parent_uri), intval($existing_message["id"]));
  812. // try to change all items of the same parent
  813. $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
  814. intval($parent["id"]), dbesc($parent["uri"]), intval($existing_message["parent"]));
  815. // Update the parent uri in the thread - but only if it points to itself
  816. $r = q("UPDATE `item` SET `thr-parent` = '%s' WHERE (`parent` = %d) AND (`uri` = `thr-parent`)",
  817. dbesc($parent["uri"]), intval($existing_message["parent"]));
  818. // Now delete the thread
  819. delete_thread($existing_message["parent"]);
  820. }
  821. }
  822. // The item we are having on the system is the one that we wanted to store via the item array
  823. if (isset($item["uri"]) AND ($item["uri"] == $existing_message["uri"])) {
  824. $item = array();
  825. $item_stored = 0;
  826. }
  827. continue;
  828. }
  829. if (is_array($single_conv->to))
  830. foreach($single_conv->to AS $to)
  831. if ($importer["nurl"] == normalise_link($to->id))
  832. $mention = true;
  833. $actor = $single_conv->actor->id;
  834. if (isset($single_conv->actor->url))
  835. $actor = $single_conv->actor->url;
  836. $details = self::get_actor_details($actor, $uid, $parent["contact-id"]);
  837. // Do we only want to import threads that were started by our contacts?
  838. if ($details["not_following"] AND $new_parent AND get_config('system','ostatus_full_threads')) {
  839. logger("Don't import uri ".$first_id." because user ".$uid." doesn't follow the person ".$actor, LOGGER_DEBUG);
  840. continue;
  841. }
  842. $arr = array();
  843. $arr["network"] = $details["network"];
  844. $arr["uri"] = $single_conv->id;
  845. $arr["plink"] = $plink;
  846. $arr["uid"] = $uid;
  847. $arr["contact-id"] = $details["contact_id"];
  848. $arr["parent-uri"] = $parent_uri;
  849. $arr["created"] = $single_conv->published;
  850. $arr["edited"] = $single_conv->published;
  851. $arr["owner-name"] = $single_conv->actor->displayName;
  852. if ($arr["owner-name"] == '')
  853. $arr["owner-name"] = $single_conv->actor->contact->displayName;
  854. if ($arr["owner-name"] == '')
  855. $arr["owner-name"] = $single_conv->actor->portablecontacts_net->displayName;
  856. $arr["owner-link"] = $actor;
  857. $arr["owner-avatar"] = $single_conv->actor->image->url;
  858. $arr["author-name"] = $arr["owner-name"];
  859. $arr["author-link"] = $actor;
  860. $arr["author-avatar"] = $single_conv->actor->image->url;
  861. $arr["body"] = add_page_info_to_body(html2bbcode($single_conv->content));
  862. if (isset($single_conv->status_net->notice_info->source))
  863. $arr["app"] = strip_tags($single_conv->status_net->notice_info->source);
  864. elseif (isset($single_conv->statusnet->notice_info->source))
  865. $arr["app"] = strip_tags($single_conv->statusnet->notice_info->source);
  866. elseif (isset($single_conv->statusnet_notice_info->source))
  867. $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
  868. elseif (isset($single_conv->provider->displayName))
  869. $arr["app"] = $single_conv->provider->displayName;
  870. else
  871. $arr["app"] = "OStatus";
  872. $arr["object"] = json_encode($single_conv);
  873. $arr["verb"] = $parent["verb"];
  874. $arr["visible"] = $parent["visible"];
  875. $arr["location"] = $single_conv->location->displayName;
  876. $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
  877. // Is it a reshared item?
  878. if (isset($single_conv->verb) AND ($single_conv->verb == "share") AND isset($single_conv->object)) {
  879. if (is_array($single_conv->object))
  880. $single_conv->object = $single_conv->object[0];
  881. logger("Found reshared item ".$single_conv->object->id);
  882. // $single_conv->object->context->conversation;
  883. if (isset($single_conv->object->object->id))
  884. $arr["uri"] = $single_conv->object->object->id;
  885. else
  886. $arr["uri"] = $single_conv->object->id;
  887. if (isset($single_conv->object->object->url))
  888. $plink = self::convert_href($single_conv->object->object->url);
  889. else
  890. $plink = self::convert_href($single_conv->object->url);
  891. if (isset($single_conv->object->object->content))
  892. $arr["body"] = add_page_info_to_body(html2bbcode($single_conv->object->object->content));
  893. else
  894. $arr["body"] = add_page_info_to_body(html2bbcode($single_conv->object->content));
  895. $arr["plink"] = $plink;
  896. $arr["created"] = $single_conv->object->published;
  897. $arr["edited"] = $single_conv->object->published;
  898. $arr["author-name"] = $single_conv->object->actor->displayName;
  899. if ($arr["owner-name"] == '')
  900. $arr["author-name"] = $single_conv->object->actor->contact->displayName;
  901. $arr["author-link"] = $single_conv->object->actor->url;
  902. $arr["author-avatar"] = $single_conv->object->actor->image->url;
  903. $arr["app"] = $single_conv->object->provider->displayName."#";
  904. //$arr["verb"] = $single_conv->object->verb;
  905. $arr["location"] = $single_conv->object->location->displayName;
  906. $arr["coord"] = trim($single_conv->object->location->lat." ".$single_conv->object->location->lon);
  907. }
  908. if ($arr["location"] == "")
  909. unset($arr["location"]);
  910. if ($arr["coord"] == "")
  911. unset($arr["coord"]);
  912. // Copy fields from given item array
  913. if (isset($item["uri"]) AND (($item["uri"] == $arr["uri"]) OR ($item["uri"] == $single_conv->id))) {
  914. $copy_fields = array("owner-name", "owner-link", "owner-avatar", "author-name", "author-link", "author-avatar",
  915. "gravity", "body", "object-type", "object", "verb", "created", "edited", "coord", "tag",
  916. "title", "attach", "app", "type", "location", "contact-id", "uri");
  917. foreach ($copy_fields AS $field)
  918. if (isset($item[$field]))
  919. $arr[$field] = $item[$field];
  920. }
  921. $newitem = item_store($arr);
  922. if (!$newitem) {
  923. logger("Item wasn't stored ".print_r($arr, true), LOGGER_DEBUG);
  924. continue;
  925. }
  926. if (isset($item["uri"]) AND ($item["uri"] == $arr["uri"])) {
  927. $item = array();
  928. $item_stored = $newitem;
  929. }
  930. logger('Stored new item '.$plink.' for parent '.$arr["parent-uri"].' under id '.$newitem, LOGGER_DEBUG);
  931. // Add the conversation entry (but don't fetch the whole conversation)
  932. self::store_conversation($newitem, $conversation_url);
  933. // If the newly created item is the top item then change the parent settings of the thread
  934. // This shouldn't happen anymore. This is supposed to be absolote.
  935. if ($arr["uri"] == $first_id) {
  936. logger('setting new parent to id '.$newitem);
  937. $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
  938. intval($uid), intval($newitem));
  939. if ($new_parents)
  940. $parent = $new_parents[0];
  941. }
  942. }
  943. if (($item_stored < 0) AND (count($item) > 0)) {
  944. if (get_config('system','ostatus_full_threads')) {
  945. $details = self::get_actor_details($item["owner-link"], $uid, $item["contact-id"]);
  946. if ($details["not_following"]) {
  947. logger("Don't import uri ".$item["uri"]." because user ".$uid." doesn't follow the person ".$item["owner-link"], LOGGER_DEBUG);
  948. return false;
  949. }
  950. }
  951. $item_stored = item_store($item, true);
  952. if ($item_stored) {
  953. logger("Uri ".$item["uri"]." wasn't found in conversation ".$conversation_url, LOGGER_DEBUG);
  954. self::store_conversation($item_stored, $conversation_url);
  955. }
  956. }
  957. return($item_stored);
  958. }
  959. /**
  960. * @brief Stores conversation data into the database
  961. *
  962. * @param integer $itemid The id of the item
  963. * @param string $conversation_url The uri of the conversation
  964. */
  965. private function store_conversation($itemid, $conversation_url) {
  966. $conversation_url = self::convert_href($conversation_url);
  967. $messages = q("SELECT `uid`, `parent`, `created`, `received`, `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
  968. if (!$messages)
  969. return;
  970. $message = $messages[0];
  971. // Store conversation url if not done before
  972. $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
  973. intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
  974. if (!$conversation) {
  975. $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `created`, `received`, `guid`) VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s')",
  976. intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION),
  977. dbesc($message["created"]), dbesc($conversation_url), dbesc($message["created"]), dbesc($message["received"]), dbesc($message["guid"]));
  978. logger('Storing conversation url '.$conversation_url.' for id '.$itemid);
  979. }
  980. }
  981. /**
  982. * @brief Checks if the current post is a reshare
  983. *
  984. * @param array $item The item array of thw post
  985. *
  986. * @return string The guid if the post is a reshare
  987. */
  988. private function get_reshared_guid($item) {
  989. $body = trim($item["body"]);
  990. // Skip if it isn't a pure repeated messages
  991. // Does it start with a share?
  992. if (strpos($body, "[share") > 0)
  993. return("");
  994. // Does it end with a share?
  995. if (strlen($body) > (strrpos($body, "[/share]") + 8))
  996. return("");
  997. $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
  998. // Skip if there is no shared message in there
  999. if ($body == $attributes)
  1000. return(false);
  1001. $guid = "";
  1002. preg_match("/guid='(.*?)'/ism", $attributes, $matches);
  1003. if ($matches[1] != "")
  1004. $guid = $matches[1];
  1005. preg_match('/guid="(.*?)"/ism', $attributes, $matches);
  1006. if ($matches[1] != "")
  1007. $guid = $matches[1];
  1008. return $guid;
  1009. }
  1010. /**
  1011. * @brief Cleans the body of a post if it contains picture links
  1012. *
  1013. * @param string $body The body
  1014. *
  1015. * @return string The cleaned body
  1016. */
  1017. private function format_picture_post($body) {
  1018. $siteinfo = get_attached_data($body);
  1019. if (($siteinfo["type"] == "photo")) {
  1020. if (isset($siteinfo["preview"]))
  1021. $preview = $siteinfo["preview"];
  1022. else
  1023. $preview = $siteinfo["image"];
  1024. // Is it a remote picture? Then make a smaller preview here
  1025. $preview = proxy_url($preview, false, PROXY_SIZE_SMALL);
  1026. // Is it a local picture? Then make it smaller here
  1027. $preview = str_replace(array("-0.jpg", "-0.png"), array("-2.jpg", "-2.png"), $preview);
  1028. $preview = str_replace(array("-1.jpg", "-1.png"), array("-2.jpg", "-2.png"), $preview);
  1029. if (isset($siteinfo["url"]))
  1030. $url = $siteinfo["url"];
  1031. else
  1032. $url = $siteinfo["image"];
  1033. $body = trim($siteinfo["text"])." [url]".$url."[/url]\n[img]".$preview."[/img]";
  1034. }
  1035. return $body;
  1036. }
  1037. /**
  1038. * @brief Adds the header elements to the XML document
  1039. *
  1040. * @param object $doc XML document
  1041. * @param array $owner Contact data of the poster
  1042. *
  1043. * @return object header root element
  1044. */
  1045. private function add_header($doc, $owner) {
  1046. $a = get_app();
  1047. $root = $doc->createElementNS(NAMESPACE_ATOM1, 'feed');
  1048. $doc->appendChild($root);
  1049. $root->setAttribute("xmlns:thr", NAMESPACE_THREAD);
  1050. $root->setAttribute("xmlns:georss", NAMESPACE_GEORSS);
  1051. $root->setAttribute("xmlns:activity", NAMESPACE_ACTIVITY);
  1052. $root->setAttribute("xmlns:media", NAMESPACE_MEDIA);
  1053. $root->setAttribute("xmlns:poco", NAMESPACE_POCO);
  1054. $root->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
  1055. $root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
  1056. $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
  1057. xml::add_element($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
  1058. xml::add_element($doc, $root, "id", App::get_baseurl()."/profile/".$owner["nick"]);
  1059. xml::add_element($doc, $root, "title", sprintf("%s timeline", $owner["name"]));
  1060. xml::add_element($doc, $root, "subtitle", sprintf("Updates from %s on %s", $owner["name"], $a->config["sitename"]));
  1061. xml::add_element($doc, $root, "logo", $owner["photo"]);
  1062. xml::add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
  1063. $author = self::add_author($doc, $owner);
  1064. $root->appendChild($author);
  1065. $attributes = array("href" => $owner["url"], "rel" => "alternate", "type" => "text/html");
  1066. xml::add_element($doc, $root, "link", "", $attributes);
  1067. /// @TODO We have to find out what this is
  1068. /// $attributes = array("href" => App::get_baseurl()."/sup",
  1069. /// "rel" => "http://api.friendfeed.com/2008/03#sup",
  1070. /// "type" => "application/json");
  1071. /// xml::add_element($doc, $root, "link", "", $attributes);
  1072. self::hublinks($doc, $root);
  1073. $attributes = array("href" => App::get_baseurl()."/salmon/".$owner["nick"], "rel" => "salmon");
  1074. xml::add_element($doc, $root, "link", "", $attributes);
  1075. $attributes = array("href" => App::get_baseurl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-replies");
  1076. xml::add_element($doc, $root, "link", "", $attributes);
  1077. $attributes = array("href" => App::get_baseurl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention");
  1078. xml::add_element($doc, $root, "link", "", $attributes);
  1079. $attributes = array("href" => App::get_baseurl()."/api/statuses/user_timeline/".$owner["nick"].".atom",
  1080. "rel" => "self", "type" => "application/atom+xml");
  1081. xml::add_element($doc, $root, "link", "", $attributes);
  1082. return $root;
  1083. }
  1084. /**
  1085. * @brief Add the link to the push hubs to the XML document
  1086. *
  1087. * @param object $doc XML document
  1088. * @param object $root XML root element where the hub links are added
  1089. */
  1090. public static function hublinks($doc, $root) {
  1091. $hub = get_config('system','huburl');
  1092. $hubxml = '';
  1093. if(strlen($hub)) {
  1094. $hubs = explode(',', $hub);
  1095. if(count($hubs)) {
  1096. foreach($hubs as $h) {
  1097. $h = trim($h);
  1098. if(! strlen($h))
  1099. continue;
  1100. if ($h === '[internal]')
  1101. $h = App::get_baseurl() . '/pubsubhubbub';
  1102. xml::add_element($doc, $root, "link", "", array("href" => $h, "rel" => "hub"));
  1103. }
  1104. }
  1105. }
  1106. }
  1107. /**
  1108. * @brief Adds attachement data to the XML document
  1109. *
  1110. * @param object $doc XML document
  1111. * @param object $root XML root element where the hub links are added
  1112. * @param array $item Data of the item that is to be posted
  1113. */
  1114. private function get_attachment($doc, $root, $item) {
  1115. $o = "";
  1116. $siteinfo = get_attached_data($item["body"]);
  1117. switch($siteinfo["type"]) {
  1118. case 'link':
  1119. $attributes = array("rel" => "enclosure",
  1120. "href" => $siteinfo["url"],
  1121. "type" => "text/html; charset=UTF-8",
  1122. "length" => "",
  1123. "title" => $siteinfo["title"]);
  1124. xml::add_element($doc, $root, "link", "", $attributes);
  1125. break;
  1126. case 'photo':
  1127. $imgdata = get_photo_info($siteinfo["image"]);
  1128. $attributes = array("rel" => "enclosure",
  1129. "href" => $siteinfo["image"],
  1130. "type" => $imgdata["mime"],
  1131. "length" => intval($imgdata["size"]));
  1132. xml::add_element($doc, $root, "link", "", $attributes);
  1133. break;
  1134. case 'video':
  1135. $attributes = array("rel" => "enclosure",
  1136. "href" => $siteinfo["url"],
  1137. "type" => "text/html; charset=UTF-8",
  1138. "length" => "",
  1139. "title" => $siteinfo["title"]);
  1140. xml::add_element($doc, $root, "link", "", $attributes);
  1141. break;
  1142. default:
  1143. break;
  1144. }
  1145. if (($siteinfo["type"] != "photo") AND isset($siteinfo["image"])) {
  1146. $photodata = get_photo_info($siteinfo["image"]);
  1147. $attributes = array("rel" => "preview", "href" => $siteinfo["image"], "media:width" => $photodata[0], "media:height" => $photodata[1]);
  1148. xml::add_element($doc, $root, "link", "", $attributes);
  1149. }
  1150. $arr = explode('[/attach],',$item['attach']);
  1151. if(count($arr)) {
  1152. foreach($arr as $r) {
  1153. $matches = false;
  1154. $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches);
  1155. if($cnt) {
  1156. $attributes = array("rel" => "enclosure",
  1157. "href" => $matches[1],
  1158. "type" => $matches[3]);
  1159. if(intval($matches[2]))
  1160. $attributes["length"] = intval($matches[2]);
  1161. if(trim($matches[4]) != "")
  1162. $attributes["title"] = trim($matches[4]);
  1163. xml::add_element($doc, $root, "link", "", $attributes);
  1164. }
  1165. }
  1166. }
  1167. }
  1168. /**
  1169. * @brief Adds the author element to the XML document
  1170. *
  1171. * @param object $doc XML document
  1172. * @param array $owner Contact data of the poster
  1173. *
  1174. * @return object author element
  1175. */
  1176. private function add_author($doc, $owner) {
  1177. $r = q("SELECT `homepage` FROM `profile` WHERE `uid` = %d AND `is-default` LIMIT 1", intval($owner["uid"]));
  1178. if ($r)
  1179. $profile = $r[0];
  1180. $author = $doc->createElement("author");
  1181. xml::add_element($doc, $author, "activity:object-type", ACTIVITY_OBJ_PERSON);
  1182. xml::add_element($doc, $author, "uri", $owner["url"]);
  1183. xml::add_element($doc, $author, "name", $owner["name"]);
  1184. xml::add_element($doc, $author, "summary", bbcode($owner["about"], false, false, 7));
  1185. $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $owner["url"]);
  1186. xml::add_element($doc, $author, "link", "", $attributes);
  1187. $attributes = array(
  1188. "rel" => "avatar",
  1189. "type" => "image/jpeg", // To-Do?
  1190. "media:width" => 175,
  1191. "media:height" => 175,
  1192. "href" => $owner["photo"]);
  1193. xml::add_element($doc, $author, "link", "", $attributes);
  1194. if (isset($owner["thumb"])) {
  1195. $attributes = array(
  1196. "rel" => "avatar",
  1197. "type" => "image/jpeg", // To-Do?
  1198. "media:width" => 80,
  1199. "media:height" => 80,
  1200. "href" => $owner["thumb"]);
  1201. xml::add_element($doc, $author, "link", "", $attributes);
  1202. }
  1203. xml::add_element($doc, $author, "poco:preferredUsername", $owner["nick"]);
  1204. xml::add_element($doc, $author, "poco:displayName", $owner["name"]);
  1205. xml::add_element($doc, $author, "poco:note", bbcode($owner["about"], false, false, 7));
  1206. if (trim($owner["location"]) != "") {
  1207. $element = $doc->createElement("poco:address");
  1208. xml::add_element($doc, $element, "poco:formatted", $owner["location"]);
  1209. $author->appendChild($element);
  1210. }
  1211. if (trim($profile["homepage"]) != "") {
  1212. $urls = $doc->createElement("poco:urls");
  1213. xml::add_element($doc, $urls, "poco:type", "homepage");
  1214. xml::add_element($doc, $urls, "poco:value", $profile["homepage"]);
  1215. xml::add_element($doc, $urls, "poco:primary", "true");
  1216. $author->appendChild($urls);
  1217. }
  1218. if (count($profile)) {
  1219. xml::add_element($doc, $author, "followers", "", array("url" => App::get_baseurl()."/viewcontacts/".$owner["nick"]));
  1220. xml::add_element($doc, $author, "statusnet:profile_info", "", array("local_id" => $owner["uid"]));
  1221. }
  1222. return $author;
  1223. }
  1224. /**
  1225. * @TODO Picture attachments should look like this:
  1226. * <a href="https://status.pirati.ca/attachment/572819" title="https://status.pirati.ca/file/heluecht-20151202T222602-rd3u49p.gif"
  1227. * class="attachment thumbnail" id="attachment-572819" rel="nofollow external">https://status.pirati.ca/attachment/572819</a>
  1228. *
  1229. */
  1230. /**
  1231. * @brief Returns the given activity if present - otherwise returns the "post" activity
  1232. *
  1233. *