2018-07-10 01:26:02 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* GNU social - a federating social network
|
|
|
|
*
|
|
|
|
* ActivityPubPlugin implementation for GNU Social
|
|
|
|
*
|
|
|
|
* 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 Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @author Daniel Supernault <danielsupernault@gmail.com>
|
|
|
|
* @copyright 2018 Free Software Foundation http://fsf.org
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
|
|
|
* @link https://www.gnu.org/software/social/
|
|
|
|
*/
|
2018-07-26 22:12:13 +01:00
|
|
|
if (!defined('GNUSOCIAL')) {
|
|
|
|
exit(1);
|
2018-07-10 01:26:02 +01:00
|
|
|
}
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
$valid_object_types = array("Note");
|
2018-07-10 01:26:02 +01:00
|
|
|
|
|
|
|
// Validate data
|
2018-07-29 02:35:04 +01:00
|
|
|
if (!isset($data->object->id)) {
|
|
|
|
ActivityPubReturn::error("Object ID not specified.");
|
|
|
|
} elseif (!filter_var($data->object->id, FILTER_VALIDATE_URL)) {
|
|
|
|
ActivityPubReturn::error("Invalid Object ID.");
|
2018-07-10 01:26:02 +01:00
|
|
|
}
|
2018-07-26 22:12:13 +01:00
|
|
|
if (!(isset($data->object->type) && in_array($data->object->type, $valid_object_types))) {
|
|
|
|
ActivityPubReturn::error("Invalid Object type.");
|
2018-07-10 01:26:02 +01:00
|
|
|
}
|
2018-07-26 22:12:13 +01:00
|
|
|
if (!isset($data->object->content)) {
|
|
|
|
ActivityPubReturn::error("Object content was not specified.");
|
|
|
|
}
|
|
|
|
if (!isset($data->object->url)) {
|
|
|
|
ActivityPubReturn::error("Object url was not specified.");
|
|
|
|
} elseif (!filter_var($data->object->url, FILTER_VALIDATE_URL)) {
|
2018-07-28 02:11:58 +01:00
|
|
|
ActivityPubReturn::error("Invalid Object URL.");
|
2018-07-26 22:12:13 +01:00
|
|
|
}
|
|
|
|
if (!isset($data->object->to)) {
|
|
|
|
ActivityPubReturn::error("Object To was not specified.");
|
2018-07-15 02:13:46 +01:00
|
|
|
}
|
2018-07-10 01:26:02 +01:00
|
|
|
|
|
|
|
$content = $data->object->content;
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
$act = new Activity();
|
2018-07-10 01:26:02 +01:00
|
|
|
$act->verb = ActivityVerb::POST;
|
2018-07-26 22:12:13 +01:00
|
|
|
$act->time = time();
|
|
|
|
$act->actor = $actor_profile->asActivityObject();
|
2018-07-10 01:26:02 +01:00
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
$act->context = new ActivityContext();
|
2018-07-10 01:26:02 +01:00
|
|
|
|
|
|
|
// Is this a reply?
|
2018-07-29 02:35:04 +01:00
|
|
|
if (isset($data->object->inReplyTo)) {
|
2018-07-26 22:12:13 +01:00
|
|
|
try {
|
2018-07-29 02:35:04 +01:00
|
|
|
$inReplyTo = ActivityPubPlugin::get_local_notice_from_url($data->object->inReplyTo);
|
2018-07-26 22:12:13 +01:00
|
|
|
} catch (Exception $e) {
|
2018-07-29 02:35:04 +01:00
|
|
|
ActivityPubReturn::error("Invalid Object inReplyTo value.");
|
2018-07-26 22:12:13 +01:00
|
|
|
}
|
2018-07-29 02:35:04 +01:00
|
|
|
$act->context->replyToID = $inReplyTo->getUri();
|
|
|
|
$act->context->replyToUrl = $inReplyTo->getUrl();
|
2018-07-26 22:12:13 +01:00
|
|
|
} else {
|
2018-07-29 02:35:04 +01:00
|
|
|
$inReplyTo = null;
|
2018-07-26 22:12:13 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 02:35:04 +01:00
|
|
|
$act->context->attention = common_get_attentions($content, $actor_profile, $inReplyTo);
|
2018-07-26 22:12:13 +01:00
|
|
|
|
|
|
|
$discovery = new Activitypub_explorer;
|
|
|
|
if ($to_profiles == "https://www.w3.org/ns/activitystreams#Public") {
|
|
|
|
$to_profiles = array();
|
|
|
|
}
|
|
|
|
// Generate To objects
|
|
|
|
if (is_array($data->object->to)) {
|
|
|
|
// Remove duplicates from To actors set
|
|
|
|
array_unique($data->object->to);
|
|
|
|
foreach ($data->object->to as $to_url) {
|
2018-07-15 02:13:46 +01:00
|
|
|
try {
|
2018-07-26 22:12:13 +01:00
|
|
|
$to_profiles = array_merge($to_profiles, $discovery->lookup($to_url));
|
2018-07-15 02:13:46 +01:00
|
|
|
} catch (Exception $e) {
|
2018-07-29 02:35:04 +01:00
|
|
|
// Invalid actor found, just let it go.
|
2018-07-15 02:13:46 +01:00
|
|
|
}
|
2018-07-26 22:12:13 +01:00
|
|
|
}
|
|
|
|
} elseif (empty($data->object->to) || in_array($data->object->to, $public_to)) {
|
|
|
|
// No need to do anything else at this point, let's just break out the if
|
2018-07-10 01:26:02 +01:00
|
|
|
} else {
|
2018-07-26 22:12:13 +01:00
|
|
|
try {
|
2018-07-29 02:35:04 +01:00
|
|
|
$to_profiles = array_merge($to_profiles, $discovery->lookup($data->object->to));
|
2018-07-26 22:12:13 +01:00
|
|
|
} catch (Exception $e) {
|
2018-07-29 02:35:04 +01:00
|
|
|
// Invalid actor found, just let it go.
|
2018-07-26 22:12:13 +01:00
|
|
|
}
|
2018-07-10 01:26:02 +01:00
|
|
|
}
|
2018-07-29 02:35:04 +01:00
|
|
|
// Generate Cc objects
|
|
|
|
if (isset($data->object->cc) && is_array($data->object->cc)) {
|
|
|
|
// Remove duplicates from Cc actors set
|
|
|
|
array_unique($data->object->to);
|
|
|
|
foreach ($data->object->cc as $cc_url) {
|
|
|
|
try {
|
|
|
|
$to_profiles = array_merge($to_profiles, $discovery->lookup($cc_url));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Invalid actor found, just let it go.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif (empty($data->object->cc) || in_array($data->object->cc, $public_to)) {
|
|
|
|
// No need to do anything else at this point, let's just break out the if
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$to_profiles = array_merge($to_profiles, $discovery->lookup($data->object->cc));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Invalid actor found, just let it go.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
unset($discovery);
|
2018-07-10 01:26:02 +01:00
|
|
|
|
2018-07-29 02:35:04 +01:00
|
|
|
foreach ($to_profiles as $tp) {
|
|
|
|
$act->context->attention[ActivityPubPlugin::actor_uri($tp)] = "http://activitystrea.ms/schema/1.0/person";
|
2018-07-10 01:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reject notice if it is too long (without the HTML)
|
|
|
|
// This is done after MediaFile::fromUpload etc. just to act the same as the ApiStatusesUpdateAction
|
2018-07-26 22:12:13 +01:00
|
|
|
if (Notice::contentTooLong($content)) {
|
|
|
|
ActivityPubReturn::error("That's too long. Maximum notice size is %d character.");
|
2018-07-10 01:26:02 +01:00
|
|
|
}
|
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
$options = array('source' => 'ActivityPub', 'uri' => isset($data->id) ? $data->id : $data->object->url, 'url' => $data->object->url);
|
2018-07-10 01:26:02 +01:00
|
|
|
// $options gets filled with possible scoping settings
|
2018-07-26 22:12:13 +01:00
|
|
|
ToSelector::fillActivity($this, $act, $options);
|
2018-07-10 01:26:02 +01:00
|
|
|
|
2018-07-26 22:12:13 +01:00
|
|
|
$actobj = new ActivityObject();
|
2018-07-10 01:26:02 +01:00
|
|
|
$actobj->type = ActivityObject::NOTE;
|
2018-07-29 02:35:04 +01:00
|
|
|
$actobj->content = common_render_content($content, $actor_profile, $inReplyTo);
|
2018-07-10 01:26:02 +01:00
|
|
|
|
|
|
|
// Finally add the activity object to our activity
|
|
|
|
$act->objects[] = $actobj;
|
|
|
|
|
|
|
|
try {
|
2018-07-26 22:12:13 +01:00
|
|
|
$res = Activitypub_create::create_to_array(
|
|
|
|
$data->id,
|
|
|
|
$data->actor,
|
|
|
|
Activitypub_notice::notice_to_array(Notice::saveActivity($act, $actor_profile, $options))
|
|
|
|
);
|
|
|
|
ActivityPubReturn::answer($res);
|
2018-07-10 01:26:02 +01:00
|
|
|
} catch (Exception $e) {
|
2018-07-26 22:12:13 +01:00
|
|
|
ActivityPubReturn::error($e->getMessage());
|
2018-07-10 01:26:02 +01:00
|
|
|
}
|