2010-07-05 05:45:56 +02:00
|
|
|
<?php
|
2016-11-28 16:23:47 +01:00
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 16:18:46 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2020-01-19 07:05:23 +01:00
|
|
|
* The dfrn notify endpoint
|
2020-02-09 16:18:46 +01:00
|
|
|
*
|
2020-06-15 19:47:23 +02:00
|
|
|
* @see PDF with dfrn specs: https://github.com/friendica/friendica/blob/stable/spec/dfrn2.pdf
|
2016-11-28 16:23:47 +01:00
|
|
|
*/
|
2017-04-30 06:07:00 +02:00
|
|
|
|
|
|
|
use Friendica\App;
|
2018-10-29 22:20:46 +01:00
|
|
|
use Friendica\Core\Logger;
|
2018-01-27 17:59:10 +01:00
|
|
|
use Friendica\Core\System;
|
2018-07-20 14:19:26 +02:00
|
|
|
use Friendica\Database\DBA;
|
2017-12-07 15:04:24 +01:00
|
|
|
use Friendica\Model\Contact;
|
2020-11-29 10:01:51 +01:00
|
|
|
use Friendica\Model\Conversation;
|
2021-07-15 15:28:32 +02:00
|
|
|
use Friendica\Network\HTTPException;
|
2017-11-08 04:57:46 +01:00
|
|
|
use Friendica\Protocol\DFRN;
|
2018-03-23 06:08:47 +01:00
|
|
|
use Friendica\Protocol\Diaspora;
|
2019-07-31 00:26:01 +02:00
|
|
|
use Friendica\Util\Network;
|
2017-04-30 06:07:00 +02:00
|
|
|
|
2017-01-09 13:12:54 +01:00
|
|
|
function dfrn_notify_post(App $a) {
|
2019-07-31 00:26:01 +02:00
|
|
|
$postdata = Network::postdata();
|
2018-03-23 06:08:47 +01:00
|
|
|
|
|
|
|
if (empty($_POST) || !empty($postdata)) {
|
|
|
|
$data = json_decode($postdata);
|
|
|
|
if (is_object($data)) {
|
2019-10-15 15:01:17 +02:00
|
|
|
$nick = $a->argv[1] ?? '';
|
2018-03-23 06:08:47 +01:00
|
|
|
|
2018-07-20 14:19:26 +02:00
|
|
|
$user = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
|
2018-07-21 14:46:04 +02:00
|
|
|
if (!DBA::isResult($user)) {
|
2019-05-02 05:16:10 +02:00
|
|
|
throw new \Friendica\Network\HTTPException\InternalServerErrorException();
|
2018-03-23 06:08:47 +01:00
|
|
|
}
|
2018-04-22 12:42:01 +02:00
|
|
|
dfrn_dispatch_private($user, $postdata);
|
|
|
|
} elseif (!dfrn_dispatch_public($postdata)) {
|
2018-03-23 06:08:47 +01:00
|
|
|
require_once 'mod/salmon.php';
|
|
|
|
salmon_post($a, $postdata);
|
|
|
|
}
|
2018-03-16 07:58:54 +01:00
|
|
|
}
|
2021-07-15 15:28:32 +02:00
|
|
|
throw new HTTPException\BadRequestException();
|
2010-07-05 05:45:56 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 12:42:01 +02:00
|
|
|
function dfrn_dispatch_public($postdata)
|
2018-04-21 11:55:41 +02:00
|
|
|
{
|
2019-10-20 13:00:08 +02:00
|
|
|
$msg = Diaspora::decodeRaw($postdata, '', true);
|
2018-04-22 12:42:01 +02:00
|
|
|
if (!$msg) {
|
|
|
|
// We have to fail silently to be able to hand it over to the salmon parser
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-21 11:55:41 +02:00
|
|
|
// Fetch the corresponding public contact
|
2019-06-22 06:17:38 +02:00
|
|
|
$contact_id = Contact::getIdForURL($msg['author']);
|
|
|
|
if (empty($contact_id)) {
|
2018-10-29 22:20:46 +01:00
|
|
|
Logger::log('Contact not found for address ' . $msg['author']);
|
2018-04-30 14:41:56 +02:00
|
|
|
System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
|
2018-04-21 11:55:41 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 06:17:38 +02:00
|
|
|
$importer = DFRN::getImporter($contact_id);
|
2018-04-21 11:55:41 +02:00
|
|
|
|
|
|
|
// This should never fail
|
2018-08-19 15:37:56 +02:00
|
|
|
if (empty($importer)) {
|
2018-10-29 22:20:46 +01:00
|
|
|
Logger::log('Contact not found for address ' . $msg['author']);
|
2018-04-30 14:41:56 +02:00
|
|
|
System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
|
2018-04-21 11:55:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('Importing post from ' . $msg['author'] . ' with the public envelope.', Logger::DEBUG);
|
2018-04-21 11:55:41 +02:00
|
|
|
|
|
|
|
// Now we should be able to import it
|
2021-01-09 17:56:42 +01:00
|
|
|
$ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY);
|
2018-04-21 11:55:41 +02:00
|
|
|
System::xmlExit($ret, 'Done');
|
|
|
|
}
|
|
|
|
|
2018-04-22 12:42:01 +02:00
|
|
|
function dfrn_dispatch_private($user, $postdata)
|
2018-04-21 11:55:41 +02:00
|
|
|
{
|
2019-10-21 11:34:47 +02:00
|
|
|
$msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
|
2018-04-22 12:42:01 +02:00
|
|
|
if (!$msg) {
|
|
|
|
System::xmlExit(4, 'Unable to parse message');
|
|
|
|
}
|
|
|
|
|
2018-04-21 11:55:41 +02:00
|
|
|
// Check if the user has got this contact
|
|
|
|
$cid = Contact::getIdForURL($msg['author'], $user['uid']);
|
|
|
|
if (!$cid) {
|
|
|
|
// Otherwise there should be a public contact
|
|
|
|
$cid = Contact::getIdForURL($msg['author']);
|
|
|
|
if (!$cid) {
|
2018-10-29 22:20:46 +01:00
|
|
|
Logger::log('Contact not found for address ' . $msg['author']);
|
2018-04-30 14:41:56 +02:00
|
|
|
System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
|
2018-04-21 11:55:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 15:37:56 +02:00
|
|
|
$importer = DFRN::getImporter($cid, $user['uid']);
|
2018-04-21 11:55:41 +02:00
|
|
|
|
|
|
|
// This should never fail
|
2018-08-19 15:37:56 +02:00
|
|
|
if (empty($importer)) {
|
2018-10-29 22:20:46 +01:00
|
|
|
Logger::log('Contact not found for address ' . $msg['author']);
|
2018-04-30 14:41:56 +02:00
|
|
|
System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
|
2018-04-21 11:55:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('Importing post from ' . $msg['author'] . ' to ' . $user['nickname'] . ' with the private envelope.', Logger::DEBUG);
|
2018-04-21 11:55:41 +02:00
|
|
|
|
|
|
|
// Now we should be able to import it
|
2021-01-09 13:59:30 +01:00
|
|
|
$ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH);
|
2018-04-21 11:55:41 +02:00
|
|
|
System::xmlExit($ret, 'Done');
|
|
|
|
}
|
2016-02-07 15:11:34 +01:00
|
|
|
|
2017-01-09 13:12:54 +01:00
|
|
|
function dfrn_notify_content(App $a) {
|
2021-07-15 15:28:32 +02:00
|
|
|
throw new HTTPException\NotFoundException();
|
2010-10-04 13:22:34 +02:00
|
|
|
}
|