streams/Zotlabs/Zot6/Zot6Handler.php

194 lines
4.2 KiB
PHP
Raw Normal View History

2018-04-23 01:51:46 +00:00
<?php
namespace Zotlabs\Zot6;
2018-06-01 02:42:13 +00:00
use Zotlabs\Lib\Libzot;
2018-07-03 05:43:41 +00:00
use Zotlabs\Lib\Queue;
2018-06-01 02:42:13 +00:00
2018-04-23 01:51:46 +00:00
class Zot6Handler implements IHandler {
2018-06-26 03:55:53 +00:00
function Notify($data,$hub) {
return self::reply_notify($data,$hub);
2018-04-23 01:51:46 +00:00
}
2018-06-26 03:55:53 +00:00
function Rekey($sender,$data,$hub) {
return self::reply_rekey_request($sender,$data,$hub);
2018-04-23 01:51:46 +00:00
}
2018-06-26 03:55:53 +00:00
function Refresh($sender,$recipients,$hub) {
return self::reply_refresh($sender,$recipients,$hub);
2018-04-23 01:51:46 +00:00
}
2018-06-26 03:55:53 +00:00
function Purge($sender,$recipients,$hub) {
return self::reply_purge($sender,$recipients,$hub);
2018-06-01 02:42:13 +00:00
}
2018-07-10 23:42:16 +00:00
2018-06-01 02:42:13 +00:00
// Implementation of specific methods follows;
// These generally do a small amout of validation and call Libzot
// to do any heavy lifting
2018-06-26 03:55:53 +00:00
static function reply_notify($data,$hub) {
2018-06-01 02:42:13 +00:00
$ret = [ 'success' => false ];
2018-06-26 03:55:53 +00:00
logger('notify received from ' . $hub['hubloc_url']);
2018-06-01 02:42:13 +00:00
$x = Libzot::fetch($data,$hub);
2018-06-01 02:42:13 +00:00
$ret['delivery_report'] = $x;
$ret['success'] = true;
2018-06-25 01:54:29 +00:00
return $ret;
2018-06-01 02:42:13 +00:00
}
/**
* @brief Remote channel info (such as permissions or photo or something)
* has been updated. Grab a fresh copy and sync it.
*
* The difference between refresh and force_refresh is that force_refresh
* unconditionally creates a directory update record, even if no changes were
* detected upon processing.
*
* @param array $sender
* @param array $recipients
*
* @return json_return_and_die()
*/
2018-06-26 03:55:53 +00:00
static function reply_refresh($sender, $recipients,$hub) {
2018-06-01 02:42:13 +00:00
$ret = array('success' => false);
if($recipients) {
// This would be a permissions update, typically for one connection
foreach ($recipients as $recip) {
$r = q("select channel.*,xchan.* from channel
left join xchan on channel_hash = xchan_hash
2018-06-04 00:49:48 +00:00
where channel_hash ='%s' limit 1",
2018-06-26 03:55:53 +00:00
dbesc($recip)
2018-06-01 02:42:13 +00:00
);
2018-06-26 03:55:53 +00:00
$x = Libzot::refresh( [ 'hubloc_id_url' => $hub['hubloc_id_url'] ], $r[0], (($msgtype === 'force_refresh') ? true : false));
2018-06-01 02:42:13 +00:00
}
}
else {
// system wide refresh
2018-06-26 03:55:53 +00:00
$x = Libzot::refresh( [ 'hubloc_id_url' => $hub['hubloc_id_url'] ], null, (($msgtype === 'force_refresh') ? true : false));
2018-06-01 02:42:13 +00:00
}
$ret['success'] = true;
2018-06-25 01:54:29 +00:00
return $ret;
2018-06-01 02:42:13 +00:00
}
2018-06-26 03:55:53 +00:00
static function rekey_request($sender,$data,$hub) {
2018-06-01 02:42:13 +00:00
$ret = array('success' => false);
// newsig is newkey signed with oldkey
// The original xchan will remain. In Zot/Receiver we will have imported the new xchan and hubloc to verify
// the packet authenticity. What we will do now is verify that the keychange operation was signed by the
// oldkey, and if so change all the abook, abconfig, group, and permission elements which reference the
// old xchan_hash.
if((! $data['old_key']) && (! $data['new_key']) && (! $data['new_sig']))
2018-06-25 01:54:29 +00:00
return $ret;
2018-06-01 02:42:13 +00:00
$old = null;
if(Libzot::verify($data['old_guid'],$data['old_guid_sig'],$data['old_key'])) {
$oldhash = make_xchan_hash($data['old_guid'],$data['old_key']);
$old = q("select * from xchan where xchan_hash = '%s' limit 1",
dbesc($oldhash)
);
}
else
2018-06-25 01:54:29 +00:00
return $ret;
2018-06-01 02:42:13 +00:00
if(! $old) {
2018-06-25 01:54:29 +00:00
return $ret;
2018-06-01 02:42:13 +00:00
}
$xchan = $old[0];
if(! Libzot::verify($data['new_key'],$data['new_sig'],$xchan['xchan_pubkey'])) {
2018-06-25 01:54:29 +00:00
return $ret;
2018-06-01 02:42:13 +00:00
}
$r = q("select * from xchan where xchan_hash = '%s' limit 1",
2018-06-26 03:55:53 +00:00
dbesc($sender)
2018-06-01 02:42:13 +00:00
);
$newxchan = $r[0];
2018-07-10 23:42:16 +00:00
// @todo
// if ! $update create a linked identity
2018-06-01 02:42:13 +00:00
xchan_change_key($xchan,$newxchan,$data);
$ret['success'] = true;
2018-06-25 01:54:29 +00:00
return $ret;
2018-04-23 01:51:46 +00:00
}
2018-06-01 02:42:13 +00:00
/**
* @brief
*
* @param array $sender
* @param array $recipients
*
* return json_return_and_die()
*/
2018-06-26 03:55:53 +00:00
static function reply_purge($sender, $recipients, $hub) {
2018-06-01 02:42:13 +00:00
$ret = array('success' => false);
if ($recipients) {
// basically this means "unfriend"
foreach ($recipients as $recip) {
$r = q("select channel.*,xchan.* from channel
left join xchan on channel_hash = xchan_hash
where channel_hash = '%s' limit 1",
2018-06-26 03:55:53 +00:00
dbesc($recip)
2018-06-01 02:42:13 +00:00
);
if ($r) {
$r = q("select abook_id from abook where uid = %d and abook_xchan = '%s' limit 1",
intval($r[0]['channel_id']),
2018-06-26 03:55:53 +00:00
dbesc($sender)
2018-06-01 02:42:13 +00:00
);
if ($r) {
contact_remove($r[0]['channel_id'],$r[0]['abook_id']);
}
}
}
$ret['success'] = true;
}
else {
2018-06-04 00:49:48 +00:00
2018-06-01 02:42:13 +00:00
// Unfriend everybody - basically this means the channel has committed suicide
2018-06-26 03:55:53 +00:00
remove_all_xchan_resources($sender);
2018-06-01 02:42:13 +00:00
$ret['success'] = true;
}
2018-06-25 01:54:29 +00:00
return $ret;
2018-06-01 02:42:13 +00:00
}
2018-04-23 01:51:46 +00:00
}