2015-10-23 02:17:14 +09:00
|
|
|
<?php
|
2020-09-04 19:15:23 +09:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
defined('GNUSOCIAL') || die();
|
|
|
|
|
|
|
|
function linkback_lenient_target_match($body, $target)
|
|
|
|
{
|
|
|
|
return strpos('' . $body, str_replace(
|
|
|
|
['http://www.', 'http://', 'https://www.', 'https://'],
|
|
|
|
'',
|
|
|
|
preg_replace('/\/+$/', '', preg_replace('/#.*/', '', $target))
|
|
|
|
));
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_get_source($source, $target)
|
|
|
|
{
|
2015-10-26 02:30:51 +09:00
|
|
|
// Check if we are pinging ourselves and ignore
|
|
|
|
$localprefix = common_config('site', 'server') . '/' . common_config('site', 'path');
|
2020-09-04 19:15:23 +09:00
|
|
|
if (linkback_lenient_target_match($source, $localprefix) === 0) {
|
2015-10-26 02:30:51 +09:00
|
|
|
common_debug('Ignoring self ping from ' . $source . ' to ' . $target);
|
2020-09-04 19:15:23 +09:00
|
|
|
return null;
|
2015-10-26 02:30:51 +09:00
|
|
|
}
|
|
|
|
|
2015-10-23 02:17:14 +09:00
|
|
|
$request = HTTPClient::start();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $request->get($source);
|
2020-09-04 19:15:23 +09:00
|
|
|
} catch (Exception $ex) {
|
|
|
|
return null;
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
$body = htmlspecialchars_decode($response->getBody());
|
|
|
|
// We're slightly more lenient in our link detection than the spec requires
|
2020-09-04 19:15:23 +09:00
|
|
|
if (linkback_lenient_target_match($body, $target) === false) {
|
|
|
|
return null;
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_get_target($target)
|
|
|
|
{
|
2015-10-23 02:17:14 +09:00
|
|
|
// Resolve target (https://github.com/converspace/webmention/issues/43)
|
|
|
|
$request = HTTPClient::start();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $request->head($target);
|
2020-09-04 19:15:23 +09:00
|
|
|
} catch (Exception $ex) {
|
|
|
|
return null;
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-10-23 02:20:03 +09:00
|
|
|
$notice = Notice::fromUri($response->getEffectiveUrl());
|
2020-09-04 19:15:23 +09:00
|
|
|
} catch (UnknownUriException $ex) {
|
2015-10-23 02:17:14 +09:00
|
|
|
preg_match('/\/notice\/(\d+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
|
2015-10-23 02:20:03 +09:00
|
|
|
$notice = Notice::getKV('id', $match[1]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($notice instanceof Notice && $notice->isLocal()) {
|
2015-10-23 02:20:03 +09:00
|
|
|
return $notice;
|
2015-10-23 02:39:15 +09:00
|
|
|
} else {
|
|
|
|
$user = User::getKV('uri', $response->getEffectiveUrl());
|
2020-09-04 19:15:23 +09:00
|
|
|
if (!$user) {
|
2015-10-23 02:39:15 +09:00
|
|
|
preg_match('/\/user\/(\d+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
|
|
|
|
$user = User::getKV('id', $match[1]);
|
|
|
|
}
|
2020-09-04 19:15:23 +09:00
|
|
|
if (!$user) {
|
2015-10-23 02:39:15 +09:00
|
|
|
preg_match('/\/([^\/\?#]+)(?:#.*)?$/', $response->getEffectiveUrl(), $match);
|
2020-09-04 19:15:23 +09:00
|
|
|
if (linkback_lenient_target_match(
|
|
|
|
common_profile_url($match[1]),
|
|
|
|
$response->getEffectiveUrl()
|
|
|
|
) !== false) {
|
2015-10-23 02:39:15 +09:00
|
|
|
$user = User::getKV('nickname', $match[1]);
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($user instanceof User) {
|
2015-10-23 02:39:15 +09:00
|
|
|
return $user;
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
return null;
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_is_contained_in($entry, $target)
|
|
|
|
{
|
2015-10-23 04:29:04 +09:00
|
|
|
foreach ((array)$entry['properties'] as $key => $values) {
|
2020-09-04 19:15:23 +09:00
|
|
|
if (count(array_filter($values, function ($x) use ($target) {
|
|
|
|
return linkback_lenient_target_match($x, $target) !== false;
|
|
|
|
})) > 0) {
|
2015-10-23 04:29:04 +09:00
|
|
|
return $entry['properties'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// check included h-* formats and their links
|
|
|
|
foreach ($values as $obj) {
|
2020-09-04 19:15:23 +09:00
|
|
|
if (
|
|
|
|
array_key_exists('type', $obj)
|
|
|
|
&& array_intersect(['h-cite', 'h-entry'], $obj['type'])
|
|
|
|
&& array_key_exists('properties', $obj)
|
|
|
|
&& array_key_exists('url', $obj['properties'])
|
|
|
|
&& count(array_filter(
|
|
|
|
$obj['properties']['url'],
|
|
|
|
function ($x) use ($target) {
|
|
|
|
return linkback_lenient_target_match($x, $target) !== false;
|
|
|
|
}
|
|
|
|
)) > 0
|
2015-10-23 04:29:04 +09:00
|
|
|
) {
|
|
|
|
return $entry['properties'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check content for the link
|
|
|
|
if ($key == "content" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]['html']), $context)) {
|
|
|
|
return $entry['properties'];
|
|
|
|
// check summary for the link
|
|
|
|
} elseif ($key == "summary" && preg_match_all("/<a[^>]+?".preg_quote($target, "/")."[^>]*>([^>]+?)<\/a>/i", htmlspecialchars_decode($values[0]), $context)) {
|
|
|
|
return $entry['properties'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
foreach ((array) $entry['children'] as $mf2) {
|
|
|
|
if (linkback_is_contained_in($mf2, $target)) {
|
2015-10-23 04:29:04 +09:00
|
|
|
return $entry['properties'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-10-23 02:17:14 +09:00
|
|
|
// Based on https://github.com/acegiak/Semantic-Linkbacks/blob/master/semantic-linkbacks-microformats-handler.php, GPL-2.0+
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_find_entry($mf2, $target)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
array_key_exists('type', $mf2['items'][0])
|
|
|
|
&& in_array('h-feed', $mf2['items'][0]['type'])
|
|
|
|
&& array_key_exists('children', $mf2['items'][0])
|
|
|
|
) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$mf2['items'] = $mf2['items'][0]['children'];
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
$entries = array_filter($mf2['items'], function ($x) {
|
|
|
|
return array_key_exists('type', $x) && in_array('h-entry', $x['type']);
|
|
|
|
});
|
2015-10-23 02:17:14 +09:00
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
2020-09-04 19:15:23 +09:00
|
|
|
if (($prop = linkback_is_contained_in($entry, $target))) {
|
2015-10-23 04:29:04 +09:00
|
|
|
return $prop;
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to first one
|
2020-09-04 19:15:23 +09:00
|
|
|
if (count($entries) > 0) {
|
2015-10-23 02:17:14 +09:00
|
|
|
return $entries[0]['properties'];
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
return null;
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_entry_type($entry, $mf2, $target)
|
|
|
|
{
|
|
|
|
if (!$entry) {
|
|
|
|
return 'mention';
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($mf2['rels'] && $mf2['rels']['in-reply-to']) {
|
|
|
|
foreach ($mf2['rels']['in-reply-to'] as $url) {
|
|
|
|
if (linkback_lenient_target_match($url, $target) !== false) {
|
2015-10-23 02:17:14 +09:00
|
|
|
return 'reply';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$classes = array(
|
|
|
|
'in-reply-to' => 'reply',
|
|
|
|
'repost-of' => 'repost',
|
|
|
|
'like-of' => 'like',
|
|
|
|
'tag-of' => 'tag'
|
|
|
|
);
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
foreach ((array) $entry as $key => $values) {
|
|
|
|
if (count(array_filter($values, function ($x) use ($target) {
|
|
|
|
return linkback_lenient_target_match($x, $target) !== false;
|
|
|
|
})) > 0) {
|
|
|
|
if ($classes[$key]) {
|
|
|
|
return $classes[$key];
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($values as $obj) {
|
2020-09-04 19:15:23 +09:00
|
|
|
if (
|
|
|
|
array_key_exists('type', $obj)
|
|
|
|
&& array_intersect(['h-cite', 'h-entry'], $obj['type'])
|
|
|
|
&& array_key_exists('properties', $obj)
|
|
|
|
&& array_key_exists('url', $obj['properties'])
|
|
|
|
&& count(array_filter(
|
|
|
|
$obj['properties']['url'],
|
|
|
|
function ($x) use ($target) {
|
|
|
|
return linkback_lenient_target_match($x, $target) !== false;
|
|
|
|
}
|
|
|
|
)) > 0
|
2015-10-23 02:17:14 +09:00
|
|
|
) {
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($classes[$key]) {
|
|
|
|
return $classes[$key];
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'mention';
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_is_dupe($key, $url)
|
|
|
|
{
|
2015-10-23 04:13:16 +09:00
|
|
|
$dupe = Notice::getKV($key, $url);
|
2015-10-23 02:17:14 +09:00
|
|
|
if ($dupe instanceof Notice) {
|
|
|
|
return $dupe;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_hcard($mf2, $url)
|
|
|
|
{
|
|
|
|
if (empty($mf2['items'])) {
|
2015-10-23 02:17:14 +09:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hcards = array();
|
2020-09-04 19:15:23 +09:00
|
|
|
foreach ($mf2['items'] as $item) {
|
|
|
|
if (!in_array('h-card', $item['type'])) {
|
2015-10-23 02:17:14 +09:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-04 19:15:23 +09:00
|
|
|
|
2015-10-23 02:17:14 +09:00
|
|
|
// We found a match, return it immediately
|
2020-09-04 19:15:23 +09:00
|
|
|
if (
|
|
|
|
array_key_exists('url', $item['properties'])
|
|
|
|
&& in_array($url, $item['properties']['url'])
|
|
|
|
) {
|
2015-10-23 02:17:14 +09:00
|
|
|
return $item['properties'];
|
|
|
|
}
|
2015-10-26 01:50:59 +09:00
|
|
|
|
|
|
|
// Let's keep all the hcards for later, to return one of them at least
|
|
|
|
$hcards[] = $item['properties'];
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// No match immediately for the url we expected, but there were h-cards found
|
|
|
|
if (count($hcards) > 0) {
|
|
|
|
return $hcards[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_notice($source, $notice_or_user, $entry, $author, $mf2)
|
|
|
|
{
|
2016-05-01 18:36:07 +09:00
|
|
|
$content = isset($entry['content']) ? $entry['content'][0]['html'] :
|
|
|
|
(isset($entry['summary']) ? $entry['summary'][0] : $entry['name'][0]);
|
2015-10-23 02:17:14 +09:00
|
|
|
|
|
|
|
$rendered = common_purify($content);
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($notice_or_user instanceof Notice && $entry['type'] === 'mention') {
|
2016-05-01 18:36:07 +09:00
|
|
|
$name = isset($entry['name']) ? $entry['name'][0] : substr(common_strip_html($content), 0, 20).'…';
|
2015-10-23 02:17:14 +09:00
|
|
|
$rendered = _m('linked to this from <a href="'.htmlspecialchars($source).'">'.htmlspecialchars($name).'</a>');
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = common_strip_html($rendered);
|
|
|
|
$shortened = common_shorten_links($content);
|
2020-09-04 19:15:23 +09:00
|
|
|
if (Notice::contentTooLong($shortened)) {
|
|
|
|
$content = substr(
|
|
|
|
$content,
|
|
|
|
0,
|
|
|
|
(Notice::maxContent() - (mb_strlen($source) + 2))
|
|
|
|
);
|
2015-10-23 02:17:14 +09:00
|
|
|
$rendered = $content . '<a href="'.htmlspecialchars($source).'">…</a>';
|
|
|
|
$content .= ' ' . $source;
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = array('is_local' => Notice::REMOTE,
|
|
|
|
'url' => $entry['url'][0],
|
2015-10-24 03:41:00 +09:00
|
|
|
'uri' => $entry['url'][0],
|
2015-10-23 02:17:14 +09:00
|
|
|
'rendered' => $rendered,
|
|
|
|
'replies' => array(),
|
|
|
|
'groups' => array(),
|
|
|
|
'peopletags' => array(),
|
|
|
|
'tags' => array(),
|
|
|
|
'urls' => array());
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($notice_or_user instanceof User) {
|
2015-10-23 02:39:15 +09:00
|
|
|
$options['replies'][] = $notice_or_user->getUri();
|
2015-10-23 02:17:14 +09:00
|
|
|
} else {
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($entry['type'] === 'repost') {
|
2015-10-23 02:39:15 +09:00
|
|
|
$options['repeat_of'] = $notice_or_user->id;
|
|
|
|
} else {
|
|
|
|
$options['reply_to'] = $notice_or_user->id;
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2016-05-01 18:36:07 +09:00
|
|
|
if (isset($entry['published']) || isset($entry['updated'])) {
|
|
|
|
$options['created'] = isset($entry['published'])
|
2016-06-11 06:02:08 +09:00
|
|
|
? common_sql_date(strtotime($entry['published'][0]))
|
|
|
|
: common_sql_date(strtotime($entry['updated'][0]));
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2016-05-01 18:36:07 +09:00
|
|
|
if (isset($entry['photo']) && common_valid_http_url($entry['photo'])) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$options['urls'][] = $entry['photo'][0];
|
2016-05-01 18:36:07 +09:00
|
|
|
} elseif (isset($entry['photo'])) {
|
|
|
|
common_debug('Linkback got invalid HTTP URL for photo: '._ve($entry['photo']));
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
foreach ((array) $entry['category'] as $tag) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$tag = common_canonical_tag($tag);
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($tag) {
|
|
|
|
$options['tags'][] = $tag;
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($mf2['rels'] && $mf2['rels']['enclosure']) {
|
|
|
|
foreach ($mf2['rels']['enclosure'] as $url) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$options['urls'][] = $url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($mf2['rels'] && $mf2['rels']['tag']) {
|
|
|
|
foreach ($mf2['rels']['tag'] as $url) {
|
2015-10-23 02:17:14 +09:00
|
|
|
preg_match('/\/([^\/]+)\/*$/', $url, $match);
|
|
|
|
$tag = common_canonical_tag($match[1]);
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($tag) {
|
|
|
|
$options['tags'][] = $tag;
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($entry['type'] !== 'reply' && $entry['type'] !== 'repost') {
|
|
|
|
$options['urls'] = [];
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
return [$content, $options];
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_avatar($profile, $url)
|
|
|
|
{
|
2016-06-11 06:02:50 +09:00
|
|
|
// Ripped from OStatus plugin for now
|
2020-09-04 19:15:23 +09:00
|
|
|
$tempfile = new TemporaryFile('gs-avatarlinback');
|
|
|
|
$img_data = HTTPClient::quickGet($url);
|
|
|
|
// Make sure it's at least an image file. ImageFile can do the rest.
|
|
|
|
if (getimagesizefromstring($img_data) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fwrite($tempfile->getResource(), $img_data);
|
|
|
|
fflush($tempfile->getResource());
|
|
|
|
// No need to carry this in memory.
|
|
|
|
unset($img_data);
|
|
|
|
|
|
|
|
$imagefile = new ImageFile(-1, $tempfile->getRealPath());
|
|
|
|
$filename = Avatar::filename(
|
|
|
|
$profile->id,
|
|
|
|
image_type_to_extension($imagefile->type),
|
|
|
|
null,
|
|
|
|
common_timestamp()
|
|
|
|
);
|
|
|
|
$tempfile->commit(Avatar::path($filename));
|
2016-06-11 06:02:50 +09:00
|
|
|
|
|
|
|
$profile->setOriginal($filename);
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_profile($entry, $mf2, $response, $target)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
array_key_exists('author', $entry)
|
|
|
|
&& array_key_exists('properties', $entry['author'][0])
|
|
|
|
) {
|
2016-06-11 06:02:34 +09:00
|
|
|
$author = $entry['author'][0]['properties'];
|
2015-10-23 02:17:14 +09:00
|
|
|
} else {
|
|
|
|
$author = linkback_hcard($mf2, $response->getEffectiveUrl());
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if (!$author) {
|
2016-02-27 07:06:04 +09:00
|
|
|
$author = array('name' => $entry['name']);
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
2016-05-01 18:36:07 +09:00
|
|
|
if (!isset($author['url']) || empty($author['url'])) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$author['url'] = array($response->getEffectiveUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::getKV('uri', $author['url'][0]);
|
|
|
|
if ($user instanceof User) {
|
|
|
|
common_log(LOG_INFO, "Linkback: ignoring linkback from local user: $url");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-23 04:13:56 +09:00
|
|
|
try {
|
|
|
|
$profile = Profile::fromUri($author['url'][0]);
|
2020-09-04 19:15:23 +09:00
|
|
|
} catch (UnknownUriException $ex) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$profile = Profile::getKV('profileurl', $author['url'][0]);
|
|
|
|
}
|
|
|
|
|
2016-05-01 18:36:07 +09:00
|
|
|
// XXX: Is this a good way to create the profile?
|
|
|
|
if (!$profile instanceof Profile) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$profile = new Profile();
|
|
|
|
$profile->profileurl = $author['url'][0];
|
|
|
|
$profile->fullname = $author['name'][0];
|
2016-05-01 18:36:07 +09:00
|
|
|
$profile->nickname = isset($author['nickname']) ? $author['nickname'][0] : str_replace(' ', '', $author['name'][0]);
|
2015-10-23 02:17:14 +09:00
|
|
|
$profile->created = common_sql_now();
|
|
|
|
$profile->insert();
|
2016-06-11 06:02:50 +09:00
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($author['photo'] && $author['photo'][0]) {
|
2016-06-11 06:02:50 +09:00
|
|
|
linkback_avatar($profile, $author['photo'][0]);
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return array($profile, $author);
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
function linkback_save($source, $target, $response, $notice_or_user)
|
|
|
|
{
|
2015-10-23 03:21:46 +09:00
|
|
|
$dupe = linkback_is_dupe('uri', $response->getEffectiveUrl());
|
2020-09-04 19:15:23 +09:00
|
|
|
if (!$dupe) {
|
|
|
|
$dupe = linkback_is_dupe('url', $response->getEffectiveUrl());
|
|
|
|
}
|
|
|
|
if (!$dupe) {
|
|
|
|
$dupe = linkback_is_dupe('uri', $source);
|
|
|
|
}
|
|
|
|
if (!$dupe) {
|
|
|
|
$dupe = linkback_is_dupe('url', $source);
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
|
|
|
|
$mf2 = new Mf2\Parser($response->getBody(), $response->getEffectiveUrl());
|
|
|
|
$mf2 = $mf2->parse();
|
|
|
|
|
|
|
|
$entry = linkback_find_entry($mf2, $target);
|
2020-09-04 19:15:23 +09:00
|
|
|
if (!$entry) {
|
2015-10-23 02:17:14 +09:00
|
|
|
preg_match('/<title>([^<]+)', $response->getBody(), $match);
|
|
|
|
$entry = array(
|
|
|
|
'content' => array('html' => $response->getBody()),
|
|
|
|
'name' => $match[1] ? htmlspecialchars_decode($match[1]) : $source
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if (!$entry['url']) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$entry['url'] = array($response->getEffectiveUrl());
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if (!$dupe) {
|
|
|
|
$dupe = linkback_is_dupe('uri', $entry['url'][0]);
|
|
|
|
}
|
|
|
|
if (!$dupe) {
|
|
|
|
$dupe = linkback_is_dupe('url', $entry['url'][0]);
|
|
|
|
}
|
2015-10-23 02:17:14 +09:00
|
|
|
|
|
|
|
$entry['type'] = linkback_entry_type($entry, $mf2, $target);
|
|
|
|
list($profile, $author) = linkback_profile($entry, $mf2, $response, $target);
|
2015-10-23 02:39:15 +09:00
|
|
|
list($content, $options) = linkback_notice($source, $notice_or_user, $entry, $author, $mf2);
|
2015-10-23 02:17:14 +09:00
|
|
|
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($dupe) {
|
2015-10-23 03:21:46 +09:00
|
|
|
$orig = clone($dupe);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Ignore duplicate save error
|
2020-09-04 19:15:23 +09:00
|
|
|
try {
|
|
|
|
$dupe->saveKnownReplies($options['replies']);
|
|
|
|
} catch (ServerException $ex) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$dupe->saveKnownTags($options['tags']);
|
|
|
|
} catch (ServerException $ex) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$dupe->saveKnownUrls($options['urls']);
|
|
|
|
} catch (ServerException $ex) {
|
|
|
|
}
|
2015-10-23 03:21:46 +09:00
|
|
|
|
2016-06-18 06:53:05 +09:00
|
|
|
if (isset($options['reply_to'])) {
|
|
|
|
$dupe->reply_to = $options['reply_to'];
|
|
|
|
}
|
|
|
|
if (isset($options['repeat_of'])) {
|
|
|
|
$dupe->repeat_of = $options['repeat_of'];
|
|
|
|
}
|
|
|
|
if ($dupe->reply_to != $orig->reply_to || $dupe->repeat_of != $orig->repeat_of) {
|
|
|
|
$parent = Notice::getKV('id', $dupe->repeat_of ?: $dupe->reply_to);
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($parent instanceof Notice) {
|
2015-10-27 12:15:38 +09:00
|
|
|
// If we changed the reply_to or repeat_of we might live in a new conversation now
|
|
|
|
$dupe->conversation = $parent->conversation;
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 19:15:23 +09:00
|
|
|
if ($dupe->update($orig)) {
|
|
|
|
$saved = $dupe;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
$dupe->conversation !== $orig->conversation
|
|
|
|
&& Conversation::noticeCount($orig->conversation) < 1
|
|
|
|
) {
|
2015-10-27 12:15:38 +09:00
|
|
|
// Delete empty conversation
|
|
|
|
$emptyConversation = Conversation::getKV('id', $orig->conversation);
|
|
|
|
$emptyConversation->delete();
|
|
|
|
}
|
2015-10-23 03:21:46 +09:00
|
|
|
} catch (Exception $e) {
|
|
|
|
common_log(LOG_ERR, "Linkback update of remote message $source failed: " . $e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
common_log(LOG_INFO, "Linkback updated remote message $source as notice id $saved->id");
|
2020-09-04 19:15:23 +09:00
|
|
|
} elseif (
|
|
|
|
$entry['type'] === 'like'
|
|
|
|
|| ($entry['type'] === 'reply' && $entry['rsvp'])
|
|
|
|
) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$act = new Activity();
|
|
|
|
$act->type = ActivityObject::ACTIVITY;
|
|
|
|
$act->time = $options['created'] ? strtotime($options['created']) : time();
|
|
|
|
$act->title = $entry["name"] ? $entry["name"][0] : _m("Favor");
|
|
|
|
$act->actor = $profile->asActivityObject();
|
2015-10-23 02:39:15 +09:00
|
|
|
$act->target = $notice_or_user->asActivityObject();
|
2015-10-23 02:17:14 +09:00
|
|
|
$act->objects = array(clone($act->target));
|
|
|
|
|
|
|
|
// TRANS: Message that is the "content" of a favorite (%1$s is the actor's nickname, %2$ is the favorited
|
|
|
|
// notice's nickname and %3$s is the content of the favorited notice.)
|
2020-09-04 19:15:23 +09:00
|
|
|
$act->content = sprintf(
|
|
|
|
_('%1$s favorited something by %2$s: %3$s'),
|
|
|
|
$profile->getNickname(),
|
|
|
|
$notice_or_user->getProfile()->getNickname(),
|
|
|
|
$notice_or_user->getRendered()
|
|
|
|
);
|
|
|
|
if ($entry['rsvp']) {
|
2015-10-23 02:17:14 +09:00
|
|
|
$act->content = $options['rendered'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$act->verb = ActivityVerb::FAVORITE;
|
2020-09-04 19:15:23 +09:00
|
|
|
if (strtolower($entry['rsvp'][0]) === 'yes') {
|
2015-10-23 02:17:14 +09:00
|
|
|
$act->verb = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
|
2020-09-04 19:15:23 +09:00
|
|
|
} elseif (strtolower($entry['rsvp'][0]) === 'no') {
|
2015-10-23 02:17:14 +09:00
|
|
|
$act->verb = 'http://activitystrea.ms/schema/1.0/rsvp-no';
|
2020-09-04 19:15:23 +09:00
|
|
|
} elseif (strtolower($entry['rsvp'][0]) === 'maybe') {
|
2015-10-23 02:17:14 +09:00
|
|
|
$act->verb = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
|
|
|
|
}
|
|
|
|
|
|
|
|
$act->id = $source;
|
|
|
|
$act->link = $entry['url'][0];
|
|
|
|
|
|
|
|
$options['source'] = 'linkback';
|
|
|
|
$options['mentions'] = $options['replies'];
|
|
|
|
unset($options['reply_to']);
|
|
|
|
unset($options['repeat_of']);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$saved = Notice::saveActivity($act, $profile, $options);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
common_log(LOG_ERR, "Linkback save of remote message $source failed: " . $e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
common_log(LOG_INFO, "Linkback saved remote message $source as notice id $saved->id");
|
|
|
|
} else {
|
|
|
|
// Fallback is to make a notice manually
|
|
|
|
try {
|
2020-09-04 19:15:23 +09:00
|
|
|
$saved = Notice::saveNew(
|
|
|
|
$profile->id,
|
|
|
|
$content,
|
|
|
|
'linkback',
|
|
|
|
$options
|
|
|
|
);
|
2015-10-23 02:17:14 +09:00
|
|
|
} catch (Exception $e) {
|
|
|
|
common_log(LOG_ERR, "Linkback save of remote message $source failed: " . $e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
common_log(LOG_INFO, "Linkback saved remote message $source as notice id $saved->id");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $saved->getLocalUrl();
|
|
|
|
}
|