]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Linkback/lib/util.php
Use author-declared URL as URI
[quix0rs-gnu-social.git] / plugins / Linkback / lib / util.php
1 <?php
2
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))));
5 }
6
7 function linkback_get_source($source, $target) {
8     $request = HTTPClient::start();
9
10     try {
11         $response = $request->get($source);
12     } catch(Exception $ex) {
13         return NULL;
14     }
15
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)) {
19         return NULL;
20     }
21
22     return $response;
23 }
24
25 function linkback_get_target($target) {
26     // Resolve target (https://github.com/converspace/webmention/issues/43)
27     $request = HTTPClient::start();
28
29     try {
30         $response = $request->head($target);
31     } catch(Exception $ex) {
32         return NULL;
33     }
34
35     try {
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]);
40     }
41
42     if($notice instanceof Notice && $notice->isLocal()) {
43         return $notice;
44     } else {
45         $user = User::getKV('uri', $response->getEffectiveUrl());
46         if(!$user) {
47             preg_match('/\/user\/(\d+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
48             $user = User::getKV('id', $match[1]);
49         }
50         if(!$user) {
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]);
54             }
55         }
56         if($user instanceof User) {
57             return $user;
58         }
59     }
60
61     return NULL;
62 }
63
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'];
68         }
69
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
76             ) {
77                 return $entry['properties'];
78             }
79         }
80
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'];
87         }
88     }
89
90     foreach((array)$entry['children'] as $mf2) {
91         if(linkback_is_contained_in($mf2, $target)) {
92             return $entry['properties'];
93         }
94     }
95
96     return null;
97 }
98
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'];
103     }
104
105     $entries = array_filter($mf2['items'], function($x) { return isset($x['type']) && in_array('h-entry', $x['type']); });
106
107     foreach ($entries as $entry) {
108         if($prop = linkback_is_contained_in($entry, $target)) {
109             return $prop;
110         }
111     }
112
113     // Default to first one
114     if(count($entries) > 0) {
115         return $entries[0]['properties'];
116     }
117
118     return NULL;
119 }
120
121 function linkback_entry_type($entry, $mf2, $target) {
122     if(!$entry) { return 'mention'; }
123
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)) {
127                 return 'reply';
128             }
129         }
130     }
131
132     $classes = array(
133         'in-reply-to' => 'reply',
134         'repost-of' => 'repost',
135         'like-of' => 'like',
136         'tag-of' => 'tag'
137     );
138
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]; }
142         }
143
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
149             ) {
150                 if($classes[$key]) { return $classes[$key]; }
151             }
152         }
153     }
154
155     return 'mention';
156 }
157
158 function linkback_is_dupe($key, $url) {
159     $dupe = Notice::getKV($key, $url);
160     if ($dupe instanceof Notice) {
161         return $dupe;
162     }
163
164     return false;
165 }
166
167
168 function linkback_hcard($mf2, $url) {
169     if(empty($mf2['items'])) {
170         return null;
171     }
172   
173     $hcards = array();
174     foreach($mf2['items'] as $item) {
175         if(!in_array('h-card', $item['type'])) {
176             continue;
177         }
178       
179         // We found a match, return it immediately
180         if(isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
181             return $item['properties'];
182       
183             // Let's keep all the hcards for later, to return one of them at least
184             $hcards[] = $item['properties'];
185         }
186     }
187   
188     // No match immediately for the url we expected, but there were h-cards found
189     if (count($hcards) > 0) {
190         return $hcards[0];
191     }
192   
193     return null;
194 }
195
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]);
199
200     $rendered = common_purify($content);
201
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>');
205     }
206
207     $content = common_strip_html($rendered);
208     $shortened = common_shorten_links($content);
209     if(Notice::contentTooLong($shortened)) {
210         $content = substr($content,
211                           0,
212                           Notice::maxContent() - (mb_strlen($source) + 2));
213         $rendered = $content . '<a href="'.htmlspecialchars($source).'">…</a>';
214         $content .= ' ' . $source;
215     }
216
217     $options = array('is_local' => Notice::REMOTE,
218                     'url' => $entry['url'][0],
219                     'uri' => $entry['url'][0],
220                     'rendered' => $rendered,
221                     'replies' => array(),
222                     'groups' => array(),
223                     'peopletags' => array(),
224                     'tags' => array(),
225                     'urls' => array());
226
227     if($notice_or_user instanceof User) {
228         $options['replies'][] = $notice_or_user->getUri();
229     } else {
230         if($entry['type'] == 'repost') {
231             $options['repeat_of'] = $notice_or_user->id;
232         } else {
233             $options['reply_to'] = $notice_or_user->id;
234         }
235     }
236
237     if($entry['published'] || $entry['updated']) {
238         $options['created'] = $entry['published'] ? common_sql_date($entry['published'][0]) : common_sql_date($entry['updated'][0]);
239     }
240
241     if($entry['photo']) {
242         $options['urls'][] = $entry['photo'][0];
243     }
244
245     foreach((array)$entry['category'] as $tag) {
246         $tag = common_canonical_tag($tag);
247         if($tag) { $options['tags'][] = $tag; }
248     }
249
250
251     if($mf2['rels'] && $mf2['rels']['enclosure']) {
252         foreach($mf2['rels']['enclosure'] as $url) {
253             $options['urls'][] = $url;
254         }
255     }
256
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; }
262          }
263     }
264
265     if($entry['type'] != 'reply' && $entry['type'] != 'repost') {
266         $options['urls'] = array();
267     }
268
269     return array($content, $options);
270 }
271
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'];
275     } else {
276         $author = linkback_hcard($mf2, $response->getEffectiveUrl());
277     }
278
279     if(!$author) {
280         $author = array('name' => array($entry['name']));
281     }
282
283     if(!$author['url']) {
284         $author['url'] = array($response->getEffectiveUrl());
285     }
286
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");
290         return true;
291     }
292
293     try {
294         $profile = Profile::fromUri($author['url'][0]);
295     } catch(UnknownUriException $ex) {}
296
297     if(!($profile instanceof Profile)) {
298         $profile = Profile::getKV('profileurl', $author['url'][0]);
299     }
300
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();
307         $profile->insert();
308     }
309
310     return array($profile, $author);
311 }
312
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); }
318
319     $mf2 = new Mf2\Parser($response->getBody(), $response->getEffectiveUrl());
320     $mf2 = $mf2->parse();
321
322     $entry = linkback_find_entry($mf2, $target);
323     if(!$entry) {
324         preg_match('/<title>([^<]+)', $response->getBody(), $match);
325         $entry = array(
326             'content' => array('html' => $response->getBody()),
327             'name' => $match[1] ? htmlspecialchars_decode($match[1]) : $source
328         );
329     }
330
331     if(!$entry['url']) {
332         $entry['url'] = array($response->getEffectiveUrl());
333     }
334
335     if(!$dupe) { $dupe = linkback_is_dupe('uri', $entry['url'][0]); }
336     if(!$dupe) { $dupe = linkback_is_dupe('url', $entry['url'][0]); }
337
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);
341
342     if($dupe) {
343         $orig = clone($dupe);
344
345         try {
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) {}
350
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());
356             return false;
357         }
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));
367
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);
373         if($entry['rsvp']) {
374             $act->content = $options['rendered'];
375         }
376
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';
384         }
385
386         $act->id = $source;
387         $act->link = $entry['url'][0];
388
389         $options['source'] = 'linkback';
390         $options['mentions'] = $options['replies'];
391         unset($options['reply_to']);
392         unset($options['repeat_of']);
393
394         try {
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());
398             return false;
399         }
400         common_log(LOG_INFO, "Linkback saved remote message $source as notice id $saved->id");
401     } else {
402         // Fallback is to make a notice manually
403         try {
404             $saved = Notice::saveNew($profile->id,
405                                      $content,
406                                      'linkback',
407                                      $options);
408         } catch (Exception $e) {
409             common_log(LOG_ERR, "Linkback save of remote message $source failed: " . $e->getMessage());
410             return false;
411         }
412         common_log(LOG_INFO, "Linkback saved remote message $source as notice id $saved->id");
413     }
414
415     return $saved->getLocalUrl();
416 }