util.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. function linkback_lenient_target_match($body, $target)
  18. {
  19. return strpos('' . $body, str_replace(
  20. ['http://www.', 'http://', 'https://www.', 'https://'],
  21. '',
  22. preg_replace('/\/+$/', '', preg_replace('/#.*/', '', $target))
  23. ));
  24. }
  25. function linkback_get_source($source, $target)
  26. {
  27. // Check if we are pinging ourselves and ignore
  28. $localprefix = common_config('site', 'server') . '/' . common_config('site', 'path');
  29. if (linkback_lenient_target_match($source, $localprefix) === 0) {
  30. common_debug('Ignoring self ping from ' . $source . ' to ' . $target);
  31. return null;
  32. }
  33. $request = HTTPClient::start();
  34. try {
  35. $response = $request->get($source);
  36. } catch (Exception $ex) {
  37. return null;
  38. }
  39. $body = htmlspecialchars_decode($response->getBody());
  40. // We're slightly more lenient in our link detection than the spec requires
  41. if (linkback_lenient_target_match($body, $target) === false) {
  42. return null;
  43. }
  44. return $response;
  45. }
  46. function linkback_get_target($target)
  47. {
  48. // Resolve target (https://github.com/converspace/webmention/issues/43)
  49. $request = HTTPClient::start();
  50. try {
  51. $response = $request->head($target);
  52. } catch (Exception $ex) {
  53. return null;
  54. }
  55. try {
  56. $notice = Notice::fromUri($response->getEffectiveUrl());
  57. } catch (UnknownUriException $ex) {
  58. preg_match('/\/notice\/(\d+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
  59. $notice = Notice::getKV('id', $match[1]);
  60. }
  61. if ($notice instanceof Notice && $notice->isLocal()) {
  62. return $notice;
  63. } else {
  64. $user = User::getKV('uri', $response->getEffectiveUrl());
  65. if (!$user) {
  66. preg_match('/\/user\/(\d+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
  67. $user = User::getKV('id', $match[1]);
  68. }
  69. if (!$user) {
  70. preg_match('/\/([^\/\?#]+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
  71. if (linkback_lenient_target_match(
  72. common_profile_url($match[1]),
  73. $response->getEffectiveUrl()
  74. ) !== false) {
  75. $user = User::getKV('nickname', $match[1]);
  76. }
  77. }
  78. if ($user instanceof User) {
  79. return $user;
  80. }
  81. }
  82. return null;
  83. }
  84. function linkback_is_contained_in($entry, $target)
  85. {
  86. foreach ((array)$entry['properties'] as $key => $values) {
  87. if (count(array_filter($values, function ($x) use ($target) {
  88. return linkback_lenient_target_match($x, $target) !== false;
  89. })) > 0) {
  90. return $entry['properties'];
  91. }
  92. // check included h-* formats and their links
  93. foreach ($values as $obj) {
  94. if (
  95. array_key_exists('type', $obj)
  96. && array_intersect(['h-cite', 'h-entry'], $obj['type'])
  97. && array_key_exists('properties', $obj)
  98. && array_key_exists('url', $obj['properties'])
  99. && count(array_filter(
  100. $obj['properties']['url'],
  101. function ($x) use ($target) {
  102. return linkback_lenient_target_match($x, $target) !== false;
  103. }
  104. )) > 0
  105. ) {
  106. return $entry['properties'];
  107. }
  108. }
  109. // check content for the link
  110. if ($key == "content" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]['html']), $context)) {
  111. return $entry['properties'];
  112. // check summary for the link
  113. } elseif ($key == "summary" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]), $context)) {
  114. return $entry['properties'];
  115. }
  116. }
  117. foreach ((array) $entry['children'] as $mf2) {
  118. if (linkback_is_contained_in($mf2, $target)) {
  119. return $entry['properties'];
  120. }
  121. }
  122. return null;
  123. }
  124. // Based on https://github.com/acegiak/Semantic-Linkbacks/blob/master/semantic-linkbacks-microformats-handler.php, GPL-2.0+
  125. function linkback_find_entry($mf2, $target)
  126. {
  127. if (
  128. array_key_exists('type', $mf2['items'][0])
  129. && in_array('h-feed', $mf2['items'][0]['type'])
  130. && array_key_exists('children', $mf2['items'][0])
  131. ) {
  132. $mf2['items'] = $mf2['items'][0]['children'];
  133. }
  134. $entries = array_filter($mf2['items'], function ($x) {
  135. return array_key_exists('type', $x) && in_array('h-entry', $x['type']);
  136. });
  137. foreach ($entries as $entry) {
  138. if (($prop = linkback_is_contained_in($entry, $target))) {
  139. return $prop;
  140. }
  141. }
  142. // Default to first one
  143. if (count($entries) > 0) {
  144. return $entries[0]['properties'];
  145. }
  146. return null;
  147. }
  148. function linkback_entry_type($entry, $mf2, $target)
  149. {
  150. if (!$entry) {
  151. return 'mention';
  152. }
  153. if ($mf2['rels'] && $mf2['rels']['in-reply-to']) {
  154. foreach ($mf2['rels']['in-reply-to'] as $url) {
  155. if (linkback_lenient_target_match($url, $target) !== false) {
  156. return 'reply';
  157. }
  158. }
  159. }
  160. $classes = array(
  161. 'in-reply-to' => 'reply',
  162. 'repost-of' => 'repost',
  163. 'like-of' => 'like',
  164. 'tag-of' => 'tag'
  165. );
  166. foreach ((array) $entry as $key => $values) {
  167. if (count(array_filter($values, function ($x) use ($target) {
  168. return linkback_lenient_target_match($x, $target) !== false;
  169. })) > 0) {
  170. if ($classes[$key]) {
  171. return $classes[$key];
  172. }
  173. }
  174. foreach ($values as $obj) {
  175. if (
  176. array_key_exists('type', $obj)
  177. && array_intersect(['h-cite', 'h-entry'], $obj['type'])
  178. && array_key_exists('properties', $obj)
  179. && array_key_exists('url', $obj['properties'])
  180. && count(array_filter(
  181. $obj['properties']['url'],
  182. function ($x) use ($target) {
  183. return linkback_lenient_target_match($x, $target) !== false;
  184. }
  185. )) > 0
  186. ) {
  187. if ($classes[$key]) {
  188. return $classes[$key];
  189. }
  190. }
  191. }
  192. }
  193. return 'mention';
  194. }
  195. function linkback_is_dupe($key, $url)
  196. {
  197. $dupe = Notice::getKV($key, $url);
  198. if ($dupe instanceof Notice) {
  199. return $dupe;
  200. }
  201. return false;
  202. }
  203. function linkback_hcard($mf2, $url)
  204. {
  205. if (empty($mf2['items'])) {
  206. return null;
  207. }
  208. $hcards = array();
  209. foreach ($mf2['items'] as $item) {
  210. if (!in_array('h-card', $item['type'])) {
  211. continue;
  212. }
  213. // We found a match, return it immediately
  214. if (
  215. array_key_exists('url', $item['properties'])
  216. && in_array($url, $item['properties']['url'])
  217. ) {
  218. return $item['properties'];
  219. }
  220. // Let's keep all the hcards for later, to return one of them at least
  221. $hcards[] = $item['properties'];
  222. }
  223. // No match immediately for the url we expected, but there were h-cards found
  224. if (count($hcards) > 0) {
  225. return $hcards[0];
  226. }
  227. return null;
  228. }
  229. function linkback_notice($source, $notice_or_user, $entry, $author, $mf2)
  230. {
  231. $content = isset($entry['content']) ? $entry['content'][0]['html'] :
  232. (isset($entry['summary']) ? $entry['summary'][0] : $entry['name'][0]);
  233. $rendered = common_purify($content);
  234. if ($notice_or_user instanceof Notice && $entry['type'] === 'mention') {
  235. $name = isset($entry['name']) ? $entry['name'][0] : substr(common_strip_html($content), 0, 20).'…';
  236. $rendered = _m('linked to this from <a href="'.htmlspecialchars($source).'">'.htmlspecialchars($name).'</a>');
  237. }
  238. $content = common_strip_html($rendered);
  239. $shortened = common_shorten_links($content);
  240. if (Notice::contentTooLong($shortened)) {
  241. $content = substr(
  242. $content,
  243. 0,
  244. (Notice::maxContent() - (mb_strlen($source) + 2))
  245. );
  246. $rendered = $content . '<a href="'.htmlspecialchars($source).'">…</a>';
  247. $content .= ' ' . $source;
  248. }
  249. $options = array('is_local' => Notice::REMOTE,
  250. 'url' => $entry['url'][0],
  251. 'uri' => $entry['url'][0],
  252. 'rendered' => $rendered,
  253. 'replies' => array(),
  254. 'groups' => array(),
  255. 'peopletags' => array(),
  256. 'tags' => array(),
  257. 'urls' => array());
  258. if ($notice_or_user instanceof User) {
  259. $options['replies'][] = $notice_or_user->getUri();
  260. } else {
  261. if ($entry['type'] === 'repost') {
  262. $options['repeat_of'] = $notice_or_user->id;
  263. } else {
  264. $options['reply_to'] = $notice_or_user->id;
  265. }
  266. }
  267. if (isset($entry['published']) || isset($entry['updated'])) {
  268. $options['created'] = isset($entry['published'])
  269. ? common_sql_date(strtotime($entry['published'][0]))
  270. : common_sql_date(strtotime($entry['updated'][0]));
  271. }
  272. if (isset($entry['photo']) && common_valid_http_url($entry['photo'])) {
  273. $options['urls'][] = $entry['photo'][0];
  274. } elseif (isset($entry['photo'])) {
  275. common_debug('Linkback got invalid HTTP URL for photo: '._ve($entry['photo']));
  276. }
  277. foreach ((array) $entry['category'] as $tag) {
  278. $tag = common_canonical_tag($tag);
  279. if ($tag) {
  280. $options['tags'][] = $tag;
  281. }
  282. }
  283. if ($mf2['rels'] && $mf2['rels']['enclosure']) {
  284. foreach ($mf2['rels']['enclosure'] as $url) {
  285. $options['urls'][] = $url;
  286. }
  287. }
  288. if ($mf2['rels'] && $mf2['rels']['tag']) {
  289. foreach ($mf2['rels']['tag'] as $url) {
  290. preg_match('/\/([^\/]+)\/*$/', $url, $match);
  291. $tag = common_canonical_tag($match[1]);
  292. if ($tag) {
  293. $options['tags'][] = $tag;
  294. }
  295. }
  296. }
  297. if ($entry['type'] !== 'reply' && $entry['type'] !== 'repost') {
  298. $options['urls'] = [];
  299. }
  300. return [$content, $options];
  301. }
  302. function linkback_avatar($profile, $url)
  303. {
  304. // Ripped from OStatus plugin for now
  305. $tempfile = new TemporaryFile('gs-avatarlinback');
  306. $img_data = HTTPClient::quickGet($url);
  307. // Make sure it's at least an image file. ImageFile can do the rest.
  308. if (getimagesizefromstring($img_data) === false) {
  309. return false;
  310. }
  311. fwrite($tempfile->getResource(), $img_data);
  312. fflush($tempfile->getResource());
  313. // No need to carry this in memory.
  314. unset($img_data);
  315. $imagefile = new ImageFile(-1, $tempfile->getRealPath());
  316. $filename = Avatar::filename(
  317. $profile->id,
  318. image_type_to_extension($imagefile->type),
  319. null,
  320. common_timestamp()
  321. );
  322. $tempfile->commit(Avatar::path($filename));
  323. $profile->setOriginal($filename);
  324. }
  325. function linkback_profile($entry, $mf2, $response, $target)
  326. {
  327. if (
  328. array_key_exists('author', $entry)
  329. && array_key_exists('properties', $entry['author'][0])
  330. ) {
  331. $author = $entry['author'][0]['properties'];
  332. } else {
  333. $author = linkback_hcard($mf2, $response->getEffectiveUrl());
  334. }
  335. if (!$author) {
  336. $author = array('name' => $entry['name']);
  337. }
  338. if (!isset($author['url']) || empty($author['url'])) {
  339. $author['url'] = array($response->getEffectiveUrl());
  340. }
  341. $user = User::getKV('uri', $author['url'][0]);
  342. if ($user instanceof User) {
  343. common_log(LOG_INFO, "Linkback: ignoring linkback from local user: $url");
  344. return true;
  345. }
  346. try {
  347. $profile = Profile::fromUri($author['url'][0]);
  348. } catch (UnknownUriException $ex) {
  349. $profile = Profile::getKV('profileurl', $author['url'][0]);
  350. }
  351. // XXX: Is this a good way to create the profile?
  352. if (!$profile instanceof Profile) {
  353. $profile = new Profile();
  354. $profile->profileurl = $author['url'][0];
  355. $profile->fullname = $author['name'][0];
  356. $profile->nickname = isset($author['nickname']) ? $author['nickname'][0] : str_replace(' ', '', $author['name'][0]);
  357. $profile->created = common_sql_now();
  358. $profile->insert();
  359. if ($author['photo'] && $author['photo'][0]) {
  360. linkback_avatar($profile, $author['photo'][0]);
  361. }
  362. }
  363. return array($profile, $author);
  364. }
  365. function linkback_save($source, $target, $response, $notice_or_user)
  366. {
  367. $dupe = linkback_is_dupe('uri', $response->getEffectiveUrl());
  368. if (!$dupe) {
  369. $dupe = linkback_is_dupe('url', $response->getEffectiveUrl());
  370. }
  371. if (!$dupe) {
  372. $dupe = linkback_is_dupe('uri', $source);
  373. }
  374. if (!$dupe) {
  375. $dupe = linkback_is_dupe('url', $source);
  376. }
  377. $mf2 = new Mf2\Parser($response->getBody(), $response->getEffectiveUrl());
  378. $mf2 = $mf2->parse();
  379. $entry = linkback_find_entry($mf2, $target);
  380. if (!$entry) {
  381. preg_match('/<title>([^<]+)', $response->getBody(), $match);
  382. $entry = array(
  383. 'content' => array('html' => $response->getBody()),
  384. 'name' => $match[1] ? htmlspecialchars_decode($match[1]) : $source
  385. );
  386. }
  387. if (!$entry['url']) {
  388. $entry['url'] = array($response->getEffectiveUrl());
  389. }
  390. if (!$dupe) {
  391. $dupe = linkback_is_dupe('uri', $entry['url'][0]);
  392. }
  393. if (!$dupe) {
  394. $dupe = linkback_is_dupe('url', $entry['url'][0]);
  395. }
  396. $entry['type'] = linkback_entry_type($entry, $mf2, $target);
  397. list($profile, $author) = linkback_profile($entry, $mf2, $response, $target);
  398. list($content, $options) = linkback_notice($source, $notice_or_user, $entry, $author, $mf2);
  399. if ($dupe) {
  400. $orig = clone($dupe);
  401. try {
  402. // Ignore duplicate save error
  403. try {
  404. $dupe->saveKnownReplies($options['replies']);
  405. } catch (ServerException $ex) {
  406. }
  407. try {
  408. $dupe->saveKnownTags($options['tags']);
  409. } catch (ServerException $ex) {
  410. }
  411. try {
  412. $dupe->saveKnownUrls($options['urls']);
  413. } catch (ServerException $ex) {
  414. }
  415. if (isset($options['reply_to'])) {
  416. $dupe->reply_to = $options['reply_to'];
  417. }
  418. if (isset($options['repeat_of'])) {
  419. $dupe->repeat_of = $options['repeat_of'];
  420. }
  421. if ($dupe->reply_to != $orig->reply_to || $dupe->repeat_of != $orig->repeat_of) {
  422. $parent = Notice::getKV('id', $dupe->repeat_of ?: $dupe->reply_to);
  423. if ($parent instanceof Notice) {
  424. // If we changed the reply_to or repeat_of we might live in a new conversation now
  425. $dupe->conversation = $parent->conversation;
  426. }
  427. }
  428. if ($dupe->update($orig)) {
  429. $saved = $dupe;
  430. }
  431. if (
  432. $dupe->conversation !== $orig->conversation
  433. && Conversation::noticeCount($orig->conversation) < 1
  434. ) {
  435. // Delete empty conversation
  436. $emptyConversation = Conversation::getKV('id', $orig->conversation);
  437. $emptyConversation->delete();
  438. }
  439. } catch (Exception $e) {
  440. common_log(LOG_ERR, "Linkback update of remote message $source failed: " . $e->getMessage());
  441. return false;
  442. }
  443. common_log(LOG_INFO, "Linkback updated remote message $source as notice id $saved->id");
  444. } elseif (
  445. $entry['type'] === 'like'
  446. || ($entry['type'] === 'reply' && $entry['rsvp'])
  447. ) {
  448. $act = new Activity();
  449. $act->type = ActivityObject::ACTIVITY;
  450. $act->time = $options['created'] ? strtotime($options['created']) : time();
  451. $act->title = $entry["name"] ? $entry["name"][0] : _m("Favor");
  452. $act->actor = $profile->asActivityObject();
  453. $act->target = $notice_or_user->asActivityObject();
  454. $act->objects = array(clone($act->target));
  455. // TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited
  456. // notice's nickname and %3$s is the content of the favorited notice.)
  457. $act->content = sprintf(
  458. _('%1$s favorited something by %2$s: %3$s'),
  459. $profile->getNickname(),
  460. $notice_or_user->getProfile()->getNickname(),
  461. $notice_or_user->getRendered()
  462. );
  463. if ($entry['rsvp']) {
  464. $act->content = $options['rendered'];
  465. }
  466. $act->verb = ActivityVerb::FAVORITE;
  467. if (strtolower($entry['rsvp'][0]) === 'yes') {
  468. $act->verb = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
  469. } elseif (strtolower($entry['rsvp'][0]) === 'no') {
  470. $act->verb = 'http://activitystrea.ms/schema/1.0/rsvp-no';
  471. } elseif (strtolower($entry['rsvp'][0]) === 'maybe') {
  472. $act->verb = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
  473. }
  474. $act->id = $source;
  475. $act->link = $entry['url'][0];
  476. $options['source'] = 'linkback';
  477. $options['mentions'] = $options['replies'];
  478. unset($options['reply_to']);
  479. unset($options['repeat_of']);
  480. try {
  481. $saved = Notice::saveActivity($act, $profile, $options);
  482. } catch (Exception $e) {
  483. common_log(LOG_ERR, "Linkback save of remote message $source failed: " . $e->getMessage());
  484. return false;
  485. }
  486. common_log(LOG_INFO, "Linkback saved remote message $source as notice id $saved->id");
  487. } else {
  488. // Fallback is to make a notice manually
  489. try {
  490. $saved = Notice::saveNew(
  491. $profile->id,
  492. $content,
  493. 'linkback',
  494. $options
  495. );
  496. } catch (Exception $e) {
  497. common_log(LOG_ERR, "Linkback save of remote message $source failed: " . $e->getMessage());
  498. return false;
  499. }
  500. common_log(LOG_INFO, "Linkback saved remote message $source as notice id $saved->id");
  501. }
  502. return $saved->getLocalUrl();
  503. }