3 function linkback_lenient_target_match($body, $target) {
4 return strpos(''.$body, str_replace(array('http://www.', 'http://', 'https://www.', 'https://'), '', preg_replace('/\/+$/', '', preg_replace( '/#.*/', '', $target))));
7 function linkback_get_source($source, $target) {
8 $request = HTTPClient::start();
11 $response = $request->get($source);
12 } catch(Exception $ex) {
16 $body = htmlspecialchars_decode($response->getBody());
17 // We're slightly more lenient in our link detection than the spec requires
18 if(!linkback_lenient_target_match($body, $target)) {
25 function linkback_get_target($target) {
26 // Resolve target (https://github.com/converspace/webmention/issues/43)
27 $request = HTTPClient::start();
30 $response = $request->head($target);
31 } catch(Exception $ex) {
36 $notice = Notice::fromUri($response->getEffectiveUrl());
37 } catch(UnknownUriException $ex) {
38 preg_match('/\/notice\/(\d+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
39 $notice = Notice::getKV('id', $match[1]);
42 if($notice instanceof Notice && $notice->isLocal()) {
45 $user = User::getKV('uri', $response->getEffectiveUrl());
47 preg_match('/\/user\/(\d+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
48 $user = User::getKV('id', $match[1]);
51 preg_match('/\/([^\/\?#]+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
52 if(linkback_lenient_target_match(common_profile_url($match[1]), $response->getEffectiveUrl())) {
53 $user = User::getKV('nickname', $match[1]);
56 if($user instanceof User) {
64 function linkback_is_contained_in($entry, $target) {
65 foreach ((array)$entry['properties'] as $key => $values) {
66 if(count(array_filter($values, function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0) {
67 return $entry['properties'];
70 // check included h-* formats and their links
71 foreach ($values as $obj) {
72 if(isset($obj['type']) && array_intersect(array('h-cite', 'h-entry'), $obj['type']) &&
73 isset($obj['properties']) && isset($obj['properties']['url']) &&
74 count(array_filter($obj['properties']['url'],
75 function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0
77 return $entry['properties'];
81 // check content for the link
82 if ($key == "content" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]['html']), $context)) {
83 return $entry['properties'];
84 // check summary for the link
85 } elseif ($key == "summary" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]), $context)) {
86 return $entry['properties'];
90 foreach((array)$entry['children'] as $mf2) {
91 if(linkback_is_contained_in($mf2, $target)) {
92 return $entry['properties'];
99 // Based on https://github.com/acegiak/Semantic-Linkbacks/blob/master/semantic-linkbacks-microformats-handler.php, GPL-2.0+
100 function linkback_find_entry($mf2, $target) {
101 if(isset($mf2['items'][0]['type']) && in_array("h-feed", $mf2['items'][0]["type"]) && isset($mf2['items'][0]['children'])) {
102 $mf2['items'] = $mf2['items'][0]['children'];
105 $entries = array_filter($mf2['items'], function($x) { return isset($x['type']) && in_array('h-entry', $x['type']); });
107 foreach ($entries as $entry) {
108 if($prop = linkback_is_contained_in($entry, $target)) {
113 // Default to first one
114 if(count($entries) > 0) {
115 return $entries[0]['properties'];
121 function linkback_entry_type($entry, $mf2, $target) {
122 if(!$entry) { return 'mention'; }
124 if($mf2['rels'] && $mf2['rels']['in-reply-to']) {
125 foreach($mf2['rels']['in-reply-to'] as $url) {
126 if(linkback_lenient_target_match($url, $target)) {
133 'in-reply-to' => 'reply',
134 'repost-of' => 'repost',
139 foreach((array)$entry as $key => $values) {
140 if(count(array_filter($values, function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0) {
141 if($classes[$key]) { return $classes[$key]; }
144 foreach ($values as $obj) {
145 if(isset($obj['type']) && array_intersect(array('h-cite', 'h-entry'), $obj['type']) &&
146 isset($obj['properties']) && isset($obj['properties']['url']) &&
147 count(array_filter($obj['properties']['url'],
148 function($x) use ($target) { return linkback_lenient_target_match($x, $target); })) > 0
150 if($classes[$key]) { return $classes[$key]; }
158 function linkback_is_dupe($key, $url) {
159 $dupe = Notice::getKV($key, $url);
160 if ($dupe instanceof Notice) {
168 function linkback_hcard($mf2, $url) {
169 if(empty($mf2['items'])) {
174 foreach($mf2['items'] as $item) {
175 if(!in_array('h-card', $item['type'])) {
179 // We found a match, return it immediately
180 if(isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
181 return $item['properties'];
184 // Let's keep all the hcards for later, to return one of them at least
185 $hcards[] = $item['properties'];
188 // No match immediately for the url we expected, but there were h-cards found
189 if (count($hcards) > 0) {
196 function linkback_notice($source, $notice_or_user, $entry, $author, $mf2) {
197 $content = $entry['content'] ? $entry['content'][0]['html'] :
198 ($entry['summary'] ? $entry['sumary'][0] : $entry['name'][0]);
200 $rendered = common_purify($content);
202 if($notice_or_user instanceof Notice && $entry['type'] == 'mention') {
203 $name = $entry['name'] ? $entry['name'][0] : substr(common_strip_html($content), 0, 20).'…';
204 $rendered = _m('linked to this from <a href="'.htmlspecialchars($source).'">'.htmlspecialchars($name).'</a>');
207 $content = common_strip_html($rendered);
208 $shortened = common_shorten_links($content);
209 if(Notice::contentTooLong($shortened)) {
210 $content = substr($content,
212 Notice::maxContent() - (mb_strlen($source) + 2));
213 $rendered = $content . '<a href="'.htmlspecialchars($source).'">…</a>';
214 $content .= ' ' . $source;
217 $options = array('is_local' => Notice::REMOTE,
218 'url' => $entry['url'][0],
219 'uri' => $entry['url'][0],
220 'rendered' => $rendered,
221 'replies' => array(),
223 'peopletags' => array(),
227 if($notice_or_user instanceof User) {
228 $options['replies'][] = $notice_or_user->getUri();
230 if($entry['type'] == 'repost') {
231 $options['repeat_of'] = $notice_or_user->id;
233 $options['reply_to'] = $notice_or_user->id;
237 if($entry['published'] || $entry['updated']) {
238 $options['created'] = $entry['published'] ? common_sql_date($entry['published'][0]) : common_sql_date($entry['updated'][0]);
241 if($entry['photo']) {
242 $options['urls'][] = $entry['photo'][0];
245 foreach((array)$entry['category'] as $tag) {
246 $tag = common_canonical_tag($tag);
247 if($tag) { $options['tags'][] = $tag; }
251 if($mf2['rels'] && $mf2['rels']['enclosure']) {
252 foreach($mf2['rels']['enclosure'] as $url) {
253 $options['urls'][] = $url;
257 if($mf2['rels'] && $mf2['rels']['tag']) {
258 foreach($mf2['rels']['tag'] as $url) {
259 preg_match('/\/([^\/]+)\/*$/', $url, $match);
260 $tag = common_canonical_tag($match[1]);
261 if($tag) { $options['tags'][] = $tag; }
265 if($entry['type'] != 'reply' && $entry['type'] != 'repost') {
266 $options['urls'] = array();
269 return array($content, $options);
272 function linkback_profile($entry, $mf2, $response, $target) {
273 if(isset($entry['properties']['author']) && isset($entry['properties']['author'][0]['properties'])) {
274 $author = $entry['properties']['author'][0]['properties'];
276 $author = linkback_hcard($mf2, $response->getEffectiveUrl());
280 $author = array('name' => array($entry['name']));
283 if(!$author['url']) {
284 $author['url'] = array($response->getEffectiveUrl());
287 $user = User::getKV('uri', $author['url'][0]);
288 if ($user instanceof User) {
289 common_log(LOG_INFO, "Linkback: ignoring linkback from local user: $url");
294 $profile = Profile::fromUri($author['url'][0]);
295 } catch(UnknownUriException $ex) {}
297 if(!($profile instanceof Profile)) {
298 $profile = Profile::getKV('profileurl', $author['url'][0]);
301 if(!($profile instanceof Profile)) {
302 $profile = new Profile();
303 $profile->profileurl = $author['url'][0];
304 $profile->fullname = $author['name'][0];
305 $profile->nickname = $author['nickname'] ? $author['nickname'][0] : str_replace(' ', '', $author['name'][0]);
306 $profile->created = common_sql_now();
310 return array($profile, $author);
313 function linkback_save($source, $target, $response, $notice_or_user) {
314 $dupe = linkback_is_dupe('uri', $response->getEffectiveUrl());
315 if(!$dupe) { $dupe = linkback_is_dupe('url', $response->getEffectiveUrl()); }
316 if(!$dupe) { $dupe = linkback_is_dupe('uri', $source); }
317 if(!$dupe) { $dupe = linkback_is_dupe('url', $source); }
319 $mf2 = new Mf2\Parser($response->getBody(), $response->getEffectiveUrl());
320 $mf2 = $mf2->parse();
322 $entry = linkback_find_entry($mf2, $target);
324 preg_match('/<title>([^<]+)', $response->getBody(), $match);
326 'content' => array('html' => $response->getBody()),
327 'name' => $match[1] ? htmlspecialchars_decode($match[1]) : $source
332 $entry['url'] = array($response->getEffectiveUrl());
335 if(!$dupe) { $dupe = linkback_is_dupe('uri', $entry['url'][0]); }
336 if(!$dupe) { $dupe = linkback_is_dupe('url', $entry['url'][0]); }
338 $entry['type'] = linkback_entry_type($entry, $mf2, $target);
339 list($profile, $author) = linkback_profile($entry, $mf2, $response, $target);
340 list($content, $options) = linkback_notice($source, $notice_or_user, $entry, $author, $mf2);
343 $orig = clone($dupe);
346 // Ignore duplicate save error
347 try { $dupe->saveKnownReplies($options['replies']); } catch (ServerException $ex) {}
348 try { $dupe->saveKnownTags($options['tags']); } catch (ServerException $ex) {}
349 try { $dupe->saveKnownUrls($options['urls']); } catch (ServerException $ex) {}
351 if($options['reply_to']) { $dupe->reply_to = $options['reply_to']; }
352 if($options['repost_of']) { $dupe->repost_of = $options['repost_of']; }
353 if($dupe->update($orig)) { $saved = $dupe; }
354 } catch (Exception $e) {
355 common_log(LOG_ERR, "Linkback update of remote message $source failed: " . $e->getMessage());
358 common_log(LOG_INFO, "Linkback updated remote message $source as notice id $saved->id");
359 } else if($entry['type'] == 'like' || ($entry['type'] == 'reply' && $entry['rsvp'])) {
360 $act = new Activity();
361 $act->type = ActivityObject::ACTIVITY;
362 $act->time = $options['created'] ? strtotime($options['created']) : time();
363 $act->title = $entry["name"] ? $entry["name"][0] : _m("Favor");
364 $act->actor = $profile->asActivityObject();
365 $act->target = $notice_or_user->asActivityObject();
366 $act->objects = array(clone($act->target));
368 // TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited
369 // notice's nickname and %3$s is the content of the favorited notice.)
370 $act->content = sprintf(_('%1$s favorited something by %2$s: %3$s'),
371 $profile->getNickname(), $notice_or_user->getProfile()->getNickname(),
372 $notice_or_user->rendered ?: $notice_or_user->content);
374 $act->content = $options['rendered'];
377 $act->verb = ActivityVerb::FAVORITE;
378 if(strtolower($entry['rsvp'][0]) == 'yes') {
379 $act->verb = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
380 } else if(strtolower($entry['rsvp'][0]) == 'no') {
381 $act->verb = 'http://activitystrea.ms/schema/1.0/rsvp-no';
382 } else if(strtolower($entry['rsvp'][0]) == 'maybe') {
383 $act->verb = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
387 $act->link = $entry['url'][0];
389 $options['source'] = 'linkback';
390 $options['mentions'] = $options['replies'];
391 unset($options['reply_to']);
392 unset($options['repeat_of']);
395 $saved = Notice::saveActivity($act, $profile, $options);
396 } catch (Exception $e) {
397 common_log(LOG_ERR, "Linkback save of remote message $source failed: " . $e->getMessage());
400 common_log(LOG_INFO, "Linkback saved remote message $source as notice id $saved->id");
402 // Fallback is to make a notice manually
404 $saved = Notice::saveNew($profile->id,
408 } catch (Exception $e) {
409 common_log(LOG_ERR, "Linkback save of remote message $source failed: " . $e->getMessage());
412 common_log(LOG_INFO, "Linkback saved remote message $source as notice id $saved->id");
415 return $saved->getLocalUrl();