streams/Zotlabs/Module/Follow.php

137 lines
3.4 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
namespace Zotlabs\Module;
use App;
use Zotlabs\Web\Controller;
2018-06-05 01:40:11 +00:00
use Zotlabs\Lib\Libsync;
2018-08-23 06:04:37 +00:00
use Zotlabs\Lib\ActivityStreams;
use Zotlabs\Lib\Activity;
use Zotlabs\Web\HTTPSignatures;
use Zotlabs\Lib\LDSignatures;
use Zotlabs\Lib\Connect;
use Zotlabs\Daemon\Master;
2016-04-19 03:38:38 +00:00
class Follow extends Controller {
2016-04-19 03:38:38 +00:00
function init() {
if (ActivityStreams::is_as_request() && argc() == 2) {
$abook_id = intval(argv(1));
2018-08-23 06:04:37 +00:00
if(! $abook_id)
return;
$r = q("select * from abook left join xchan on abook_xchan = xchan_hash where abook_id = %d",
intval($abook_id)
);
if (! $r) {
2018-08-23 06:04:37 +00:00
return;
}
2018-08-23 06:04:37 +00:00
$chan = channelx_by_n($r[0]['abook_channel']);
if (! $chan) {
2018-08-23 06:04:37 +00:00
http_status_exit(404, 'Not found');
}
$actor = Activity::encode_person($chan,true,true);
if (! $actor) {
2018-08-23 06:04:37 +00:00
http_status_exit(404, 'Not found');
}
2018-08-23 06:04:37 +00:00
$x = array_merge(['@context' => [
ACTIVITYSTREAMS_JSONLD_REV,
'https://w3id.org/security/v1',
z_root() . ZOT_APSCHEMA_REV
]],
[
'id' => z_root() . '/follow/' . $r[0]['abook_id'],
'type' => 'Follow',
'actor' => $actor,
'object' => $r[0]['xchan_url']
]);
$headers = [];
$headers['Content-Type'] = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' ;
$x['signature'] = LDSignatures::sign($x,$chan);
2018-08-23 06:04:37 +00:00
$ret = json_encode($x, JSON_UNESCAPED_SLASHES);
$headers['Date'] = datetime_convert('UTC','UTC', 'now', 'D, d M Y h:i:s \\G\\M\\T');
2018-08-23 06:04:37 +00:00
$headers['Digest'] = HTTPSig::generate_digest_header($ret);
2018-10-11 00:58:51 +00:00
$headers['(request-target)'] = strtolower($_SERVER['REQUEST_METHOD']) . ' ' . $_SERVER['REQUEST_URI'];
2018-08-23 06:04:37 +00:00
$h = HTTPSig::create_sig($headers,$chan['channel_prvkey'],channel_url($chan));
HTTPSig::set_headers($h);
echo $ret;
killme();
}
if (! local_channel()) {
return;
}
2016-04-19 03:38:38 +00:00
$uid = local_channel();
2018-03-15 01:42:07 +00:00
$url = notags(trim(punify($_REQUEST['url'])));
2016-04-19 03:38:38 +00:00
$return_url = $_SESSION['return_url'];
$confirm = intval($_REQUEST['confirm']);
$interactive = (($_REQUEST['interactive']) ? intval($_REQUEST['interactive']) : 1);
$channel = App::get_channel();
$result = Connect::connect($channel,$url);
2016-04-19 03:38:38 +00:00
if ($result['success'] == false) {
if ($result['message']) {
2016-04-19 03:38:38 +00:00
notice($result['message']);
}
if ($interactive) {
goaway($return_url);
}
else {
json_return_and_die($result);
}
2016-04-19 03:38:38 +00:00
}
info( t('Connection added.') . EOL);
2016-04-19 03:38:38 +00:00
$clone = array();
foreach ($result['abook'] as $k => $v) {
if (strpos($k,'abook_') === 0) {
2016-04-19 03:38:38 +00:00
$clone[$k] = $v;
}
}
unset($clone['abook_id']);
unset($clone['abook_account']);
unset($clone['abook_channel']);
$abconfig = load_abconfig($channel['channel_id'],$clone['abook_xchan']);
if ($abconfig) {
2016-04-19 03:38:38 +00:00
$clone['abconfig'] = $abconfig;
}
Libsync::build_sync_packet(0, [ 'abook' => [ $clone ] ], true);
2016-04-19 03:38:38 +00:00
2018-05-26 22:12:02 +00:00
$can_view_stream = their_perms_contains($channel['channel_id'],$clone['abook_xchan'],'view_stream');
2016-04-19 03:38:38 +00:00
// If we can view their stream, pull in some posts
if (($can_view_stream) || ($result['abook']['xchan_network'] === 'rss')) {
Master::Summon([ 'Onepoll', $result['abook']['abook_id'] ]);
}
2016-04-19 03:38:38 +00:00
if ($interactive) {
goaway(z_root() . '/connedit/' . $result['abook']['abook_id'] . '?follow=1');
}
else {
json_return_and_die([ 'success' => true ]);
}
2016-04-19 03:38:38 +00:00
}
2016-05-20 05:26:37 +00:00
function get() {
if (! local_channel()) {
2016-04-19 03:38:38 +00:00
return login();
}
}
}