2018-04-29 13:23:54 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* GNU social - a federating social network
|
|
|
|
*
|
2018-07-09 23:43:34 +01:00
|
|
|
* ActivityPubPlugin implementation for GNU Social
|
2018-04-29 13:23:54 +01:00
|
|
|
*
|
|
|
|
* LICENCE: This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category Plugin
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Daniel Supernault <danielsupernault@gmail.com>
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2018-07-09 23:43:34 +01:00
|
|
|
* @copyright 2018 Free Software Foundation http://fsf.org
|
2018-04-29 13:23:54 +01:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2018-07-09 23:43:34 +01:00
|
|
|
* @link https://www.gnu.org/software/social/
|
2018-04-29 13:23:54 +01:00
|
|
|
*/
|
2018-07-26 22:12:13 +01:00
|
|
|
if (!defined('GNUSOCIAL')) {
|
|
|
|
exit(1);
|
2018-07-09 23:43:34 +01:00
|
|
|
}
|
2018-04-29 13:23:54 +01:00
|
|
|
|
2018-07-09 23:43:34 +01:00
|
|
|
/**
|
2018-07-13 12:32:27 +01:00
|
|
|
* ActivityPub notice representation
|
|
|
|
*
|
2018-07-09 23:43:34 +01:00
|
|
|
* @category Plugin
|
|
|
|
* @package GNUsocial
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
|
|
|
* @link http://www.gnu.org/software/social/
|
|
|
|
*/
|
2018-04-29 13:23:54 +01:00
|
|
|
class Activitypub_notice extends Managed_DataObject
|
|
|
|
{
|
2018-07-26 22:12:13 +01:00
|
|
|
/**
|
|
|
|
* Generates a pretty notice from a Notice object
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @param Notice $notice
|
|
|
|
* @return pretty array to be used in a response
|
|
|
|
*/
|
|
|
|
public static function notice_to_array($notice)
|
|
|
|
{
|
2018-07-29 02:35:04 +01:00
|
|
|
$profile = $notice->getProfile();
|
2018-07-31 20:09:47 +01:00
|
|
|
$attachments = [];
|
2018-07-26 22:12:13 +01:00
|
|
|
foreach ($notice->attachments() as $attachment) {
|
|
|
|
$attachments[] = Activitypub_attachment::attachment_to_array($attachment);
|
|
|
|
}
|
2018-04-29 13:23:54 +01:00
|
|
|
|
2018-07-31 20:09:47 +01:00
|
|
|
$tags = [];
|
2018-07-26 22:12:13 +01:00
|
|
|
foreach ($notice->getTags() as $tag) {
|
|
|
|
if ($tag != "") { // Hacky workaround to avoid stupid outputs
|
|
|
|
$tags[] = Activitypub_tag::tag_to_array($tag);
|
|
|
|
}
|
|
|
|
}
|
2018-05-05 01:14:27 +01:00
|
|
|
|
2018-08-01 01:50:27 +01:00
|
|
|
$cc = [common_local_url('apActorFollowers', ['id' => $profile->getID()])];
|
2018-07-26 22:12:13 +01:00
|
|
|
foreach ($notice->getAttentionProfiles() as $to_profile) {
|
2018-08-01 01:50:27 +01:00
|
|
|
$cc[] = $href = $to_profile->getUri();
|
2018-07-31 20:09:47 +01:00
|
|
|
$tags[] = Activitypub_mention_tag::mention_tag_to_array_from_values($href, $to_profile->getNickname().'@'.parse_url($href, PHP_URL_HOST));
|
2018-07-26 22:12:13 +01:00
|
|
|
}
|
2018-07-09 23:43:34 +01:00
|
|
|
|
2018-07-31 20:09:47 +01:00
|
|
|
// In a world without walls and fences, we should make everything Public!
|
|
|
|
$to[]= 'https://www.w3.org/ns/activitystreams#Public';
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
$item = [
|
2018-08-06 02:08:30 +01:00
|
|
|
'@context' => 'https://www.w3.org/ns/activitystreams',
|
2018-08-06 08:09:25 +01:00
|
|
|
'id' => common_local_url('apNotice', ['id' => $notice->getID()]),
|
2018-07-31 22:16:30 +01:00
|
|
|
'type' => 'Note',
|
|
|
|
'published' => str_replace(' ', 'T', $notice->getCreated()).'Z',
|
|
|
|
'url' => $notice->getUrl(),
|
2018-08-04 04:11:17 +01:00
|
|
|
'attributedTo' => ActivityPubPlugin::actor_uri($profile),
|
2018-08-01 01:50:27 +01:00
|
|
|
'to' => ['https://www.w3.org/ns/activitystreams#Public'],
|
|
|
|
'cc' => $cc,
|
2018-07-31 22:16:30 +01:00
|
|
|
'atomUri' => $notice->getUrl(),
|
|
|
|
'conversation' => $notice->getConversationUrl(),
|
2018-08-01 01:09:34 +01:00
|
|
|
'content' => $notice->getRendered(),
|
2018-07-31 22:16:30 +01:00
|
|
|
'isLocal' => $notice->isLocal(),
|
|
|
|
'attachment' => $attachments,
|
|
|
|
'tag' => $tags
|
2018-07-29 02:57:46 +01:00
|
|
|
];
|
|
|
|
|
2018-07-31 20:09:47 +01:00
|
|
|
// Is this a reply?
|
|
|
|
if (!empty($notice->reply_to)) {
|
2018-08-06 08:35:48 +01:00
|
|
|
$item['inReplyTo'] = common_local_url('apNotice', ['id' => $notice->getID()]);
|
2018-07-31 20:09:47 +01:00
|
|
|
$item['inReplyToAtomUri'] = Notice::getById($notice->reply_to)->getUrl();
|
|
|
|
}
|
2018-07-31 22:16:30 +01:00
|
|
|
|
2018-07-29 02:57:46 +01:00
|
|
|
// Do we have a location for this notice?
|
|
|
|
try {
|
|
|
|
$location = Notice_location::locFromStored($notice);
|
|
|
|
$item['latitude'] = $location->lat;
|
|
|
|
$item['longitude'] = $location->lon;
|
2018-07-31 20:28:52 +01:00
|
|
|
} catch (Exception $e) {
|
2018-07-29 02:57:46 +01:00
|
|
|
// Apparently no.
|
|
|
|
}
|
2018-07-09 20:07:35 +01:00
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
return $item;
|
|
|
|
}
|
2018-08-02 01:42:15 +01:00
|
|
|
|
|
|
|
/**
|
2018-08-04 04:11:17 +01:00
|
|
|
* Create a Notice via ActivityPub Note Object.
|
2018-08-02 01:42:15 +01:00
|
|
|
* Returns created Notice.
|
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2018-08-04 04:11:17 +01:00
|
|
|
* @param Array $object
|
|
|
|
* @param Profile|null $actor_profile
|
2018-08-02 01:42:15 +01:00
|
|
|
* @return Notice
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2018-08-04 04:11:17 +01:00
|
|
|
public static function create_notice($object, $actor_profile = null)
|
2018-08-02 01:42:15 +01:00
|
|
|
{
|
2018-08-04 04:11:17 +01:00
|
|
|
$id = $object['id']; // int32
|
|
|
|
$url = $object['url']; // string
|
|
|
|
$content = $object['content']; // string
|
|
|
|
|
|
|
|
// possible keys: ['inReplyTo', 'latitude', 'longitude', 'attachment']
|
|
|
|
$settings = [];
|
|
|
|
if (isset($object['inReplyTo'])) {
|
|
|
|
$settings['inReplyTo'] = $object['inReplyTo'];
|
|
|
|
}
|
|
|
|
if (isset($object['latitude'])) {
|
|
|
|
$settings['latitude'] = $object['latitude'];
|
|
|
|
}
|
|
|
|
if (isset($object['longitude'])) {
|
|
|
|
$settings['longitude'] = $object['longitude'];
|
|
|
|
}
|
|
|
|
if (isset($object['attachment'])) {
|
|
|
|
$settings['attachment'] = $object['attachment'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure Actor Profile
|
|
|
|
if (is_null($actor_profile)) {
|
|
|
|
$actor_profile = ActivityPub_explorer::get_profile_from_url($object['actor']);
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:42:15 +01:00
|
|
|
$act = new Activity();
|
|
|
|
$act->verb = ActivityVerb::POST;
|
|
|
|
$act->time = time();
|
|
|
|
$act->actor = $actor_profile->asActivityObject();
|
|
|
|
$act->context = new ActivityContext();
|
2018-08-03 05:24:21 +01:00
|
|
|
$options = ['source' => 'ActivityPub', 'uri' => $id, 'url' => $url];
|
|
|
|
|
|
|
|
// Do we have an attachment?
|
|
|
|
if (isset($settings['attachment'][0])) {
|
|
|
|
$attach = $settings['attachment'][0];
|
|
|
|
$attach_url = $settings['attachment'][0]['url'];
|
|
|
|
// Is it an image?
|
|
|
|
if (ActivityPubPlugin::$store_images_from_remote_notes_attachments && substr($attach["mediaType"], 0, 5) == "image") {
|
|
|
|
$temp_filename = tempnam(sys_get_temp_dir(), 'apCreateNoteAttach_');
|
|
|
|
try {
|
|
|
|
$imgData = HTTPClient::quickGet($attach_url);
|
|
|
|
// Make sure it's at least an image file. ImageFile can do the rest.
|
|
|
|
if (false === getimagesizefromstring($imgData)) {
|
|
|
|
common_debug('ActivityPub Create Notice: Failed because the downloaded image: '.$attach_url. 'is not valid.');
|
|
|
|
throw new UnsupportedMediaException('Downloaded image was not an image.');
|
|
|
|
}
|
|
|
|
file_put_contents($temp_filename, $imgData);
|
|
|
|
common_debug('ActivityPub Create Notice: Stored dowloaded image in: '.$temp_filename);
|
|
|
|
|
|
|
|
$id = $actor_profile->getID();
|
|
|
|
|
|
|
|
$imagefile = new ImageFile(null, $temp_filename);
|
|
|
|
$filename = hash(File::FILEHASH_ALG, $imgData).image_type_to_extension($imagefile->type);
|
2018-08-04 04:11:17 +01:00
|
|
|
|
2018-08-03 18:47:20 +01:00
|
|
|
unset($imgData); // No need to carry this in memory.
|
2018-08-03 05:24:21 +01:00
|
|
|
rename($temp_filename, File::path($filename));
|
|
|
|
common_debug('ActivityPub Create Notice: Moved image from: '.$temp_filename.' to '.$filename);
|
|
|
|
$mediaFile = new MediaFile($filename, $attach['mediaType']);
|
|
|
|
$act->enclosures[] = $mediaFile->getEnclosure();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
common_debug('ActivityPub Create Notice: Something went wrong while processing the image from: '.$attach_url.' details: '.$e->getMessage());
|
|
|
|
unlink($temp_filename);
|
|
|
|
}
|
|
|
|
}
|
2018-08-04 04:11:17 +01:00
|
|
|
$content .= ($content==='' ? '' : ' ') . '<br><a href="'.$attach_url.'">Remote Attachment Source</a>';
|
2018-08-03 05:24:21 +01:00
|
|
|
}
|
2018-08-02 01:42:15 +01:00
|
|
|
|
|
|
|
// Is this a reply?
|
|
|
|
if (isset($settings['inReplyTo'])) {
|
|
|
|
try {
|
|
|
|
$inReplyTo = ActivityPubPlugin::grab_notice_from_url($settings['inReplyTo']);
|
|
|
|
} catch (Exception $e) {
|
2018-08-02 07:05:50 +01:00
|
|
|
throw new Exception('Invalid Object inReplyTo value: '.$e->getMessage());
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
|
|
|
$act->context->replyToID = $inReplyTo->getUri();
|
|
|
|
$act->context->replyToUrl = $inReplyTo->getUrl();
|
|
|
|
} else {
|
|
|
|
$inReplyTo = null;
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:29:47 +01:00
|
|
|
// Mentions
|
|
|
|
$mentions = [];
|
2018-08-05 23:56:05 +01:00
|
|
|
if (isset($object['tag']) && is_array($object['tag'])) {
|
2018-08-05 22:29:47 +01:00
|
|
|
foreach ($object['tag'] as $tag) {
|
|
|
|
if ($tag['type'] == 'Mention') {
|
|
|
|
$mentions[] = $tag['href'];
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-05 22:29:47 +01:00
|
|
|
}
|
|
|
|
$mentions_profiles = [];
|
|
|
|
$discovery = new Activitypub_explorer;
|
|
|
|
foreach ($mentions as $mention) {
|
2018-08-02 01:42:15 +01:00
|
|
|
try {
|
2018-08-06 08:09:25 +01:00
|
|
|
$mentions_profiles[] = $discovery->lookup($mention)[0];
|
2018-08-02 01:42:15 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
// Invalid actor found, just let it go. // TODO: Fallback to OStatus
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($discovery);
|
|
|
|
|
2018-08-05 22:29:47 +01:00
|
|
|
foreach ($mentions_profiles as $mp) {
|
|
|
|
$act->context->attention[ActivityPubPlugin::actor_uri($mp)] = 'http://activitystrea.ms/schema/1.0/person';
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add location if that is set
|
|
|
|
if (isset($settings['latitude'], $settings['longitude'])) {
|
|
|
|
$act->context->location = Location::fromLatLon($settings['latitude'], $settings['longitude']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject notice if it is too long (without the HTML)
|
|
|
|
if (Notice::contentTooLong($content)) {
|
2018-08-02 05:00:35 +01:00
|
|
|
//throw new Exception('That\'s too long. Maximum notice size is %d character.');
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$actobj = new ActivityObject();
|
|
|
|
$actobj->type = ActivityObject::NOTE;
|
|
|
|
$actobj->content = strip_tags($content, '<p><b><i><u><a><ul><ol><li>');
|
|
|
|
|
|
|
|
// Finally add the activity object to our activity
|
|
|
|
$act->objects[] = $actobj;
|
|
|
|
|
2018-08-04 04:11:17 +01:00
|
|
|
$note = Notice::saveActivity($act, $actor_profile, $options);
|
|
|
|
if (ActivityPubPlugin::$store_images_from_remote_notes_attachments && isset($mediaFile)) {
|
|
|
|
$mediaFile->attachToNotice($note);
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
2018-08-04 04:11:17 +01:00
|
|
|
return $note;
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-04 04:11:17 +01:00
|
|
|
* Validates a note.
|
2018-08-02 01:42:15 +01:00
|
|
|
*
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2018-08-04 04:11:17 +01:00
|
|
|
* @param Array $object
|
2018-08-02 01:42:15 +01:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2018-08-04 04:11:17 +01:00
|
|
|
public static function validate_note($object)
|
2018-08-02 01:42:15 +01:00
|
|
|
{
|
2018-08-04 04:11:17 +01:00
|
|
|
if (!isset($object['attributedTo'])) {
|
2018-08-02 02:34:08 +01:00
|
|
|
common_debug('ActivityPub Notice Validator: Rejected because attributedTo was not specified.');
|
2018-08-02 07:01:39 +01:00
|
|
|
throw new Exception('No attributedTo specified.');
|
2018-08-02 02:34:08 +01:00
|
|
|
}
|
2018-08-04 04:11:17 +01:00
|
|
|
if (!isset($object['id'])) {
|
2018-08-02 02:34:08 +01:00
|
|
|
common_debug('ActivityPub Notice Validator: Rejected because Object ID was not specified.');
|
2018-08-02 07:01:39 +01:00
|
|
|
throw new Exception('Object ID not specified.');
|
2018-08-04 04:11:17 +01:00
|
|
|
} elseif (!filter_var($object['id'], FILTER_VALIDATE_URL)) {
|
2018-08-02 02:34:08 +01:00
|
|
|
common_debug('ActivityPub Notice Validator: Rejected because Object ID is invalid.');
|
2018-08-02 07:01:39 +01:00
|
|
|
throw new Exception('Invalid Object ID.');
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
2018-08-04 04:11:17 +01:00
|
|
|
if (!isset($object['type']) || $object['type'] !== 'Note') {
|
2018-08-02 02:34:08 +01:00
|
|
|
common_debug('ActivityPub Notice Validator: Rejected because of Type.');
|
2018-08-02 07:01:39 +01:00
|
|
|
throw new Exception('Invalid Object type.');
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
2018-08-04 04:11:17 +01:00
|
|
|
if (!isset($object['content'])) {
|
2018-08-02 02:34:08 +01:00
|
|
|
common_debug('ActivityPub Notice Validator: Rejected because Content was not specified.');
|
2018-08-02 07:01:39 +01:00
|
|
|
throw new Exception('Object content was not specified.');
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
2018-08-04 04:11:17 +01:00
|
|
|
if (!isset($object['url'])) {
|
2018-08-02 07:01:39 +01:00
|
|
|
throw new Exception('Object URL was not specified.');
|
2018-08-04 04:11:17 +01:00
|
|
|
} elseif (!filter_var($object['url'], FILTER_VALIDATE_URL)) {
|
2018-08-02 02:34:08 +01:00
|
|
|
common_debug('ActivityPub Notice Validator: Rejected because Object URL is invalid.');
|
2018-08-02 07:01:39 +01:00
|
|
|
throw new Exception('Invalid Object URL.');
|
2018-08-02 01:42:15 +01:00
|
|
|
}
|
2018-08-04 04:11:17 +01:00
|
|
|
if (!isset($object['cc'])) {
|
2018-08-02 02:34:08 +01:00
|
|
|
common_debug('ActivityPub Notice Validator: Rejected because Object CC was not specified.');
|
2018-08-02 07:01:39 +01:00
|
|
|
throw new Exception('Object CC was not specified.');
|
2018-08-04 04:11:17 +01:00
|
|
|
}
|
2018-08-02 01:42:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-05 01:14:27 +01:00
|
|
|
}
|