2019-01-24 01:55:39 +00:00
|
|
|
<?php /** @file */
|
|
|
|
|
|
|
|
namespace Zotlabs\Lib;
|
|
|
|
|
2019-10-03 19:56:49 +00:00
|
|
|
use App;
|
2019-01-24 01:55:39 +00:00
|
|
|
use Zotlabs\Access\Permissions;
|
2020-06-15 07:29:56 +00:00
|
|
|
use Zotlabs\Daemon\Run;
|
2019-01-24 01:55:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Connect {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a $channel and a $url/handle and adds a new connection
|
|
|
|
*
|
|
|
|
* Returns array
|
|
|
|
* $return['success'] boolean true if successful
|
|
|
|
* $return['abook'] Address book entry joined with xchan if successful
|
|
|
|
* $return['message'] error text if success is false.
|
|
|
|
*
|
|
|
|
* This function does NOT send sync packets to clones. The caller is responsible for doing this
|
|
|
|
*/
|
|
|
|
|
2019-04-03 23:22:03 +00:00
|
|
|
static function connect($channel, $url, $sub_channel = false) {
|
2019-01-24 01:55:39 +00:00
|
|
|
|
|
|
|
$uid = $channel['channel_id'];
|
|
|
|
|
2019-09-24 00:52:03 +00:00
|
|
|
if (strpos($url,'@') === false && strpos($url,'/') === false) {
|
|
|
|
$url = $url . '@' . App::get_hostname();
|
|
|
|
}
|
|
|
|
|
2019-01-24 01:55:39 +00:00
|
|
|
$result = [ 'success' => false, 'message' => '' ];
|
|
|
|
|
|
|
|
$my_perms = false;
|
|
|
|
$protocol = '';
|
|
|
|
|
2020-11-27 00:17:07 +00:00
|
|
|
$ap_allowed = get_config('system','activitypub', ACTIVITYPUB_ENABLED) && get_pconfig($uid,'system','activitypub', ACTIVITYPUB_ENABLED);
|
2020-07-15 01:47:23 +00:00
|
|
|
|
2019-09-11 02:31:08 +00:00
|
|
|
if (substr($url,0,1) === '[') {
|
2019-01-24 01:55:39 +00:00
|
|
|
$x = strpos($url,']');
|
2019-09-11 02:31:08 +00:00
|
|
|
if ($x) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$protocol = substr($url,1,$x-1);
|
|
|
|
$url = substr($url,$x+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 02:31:08 +00:00
|
|
|
if (! check_siteallowed($url)) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$result['message'] = t('Channel is blocked on this site.');
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-09-11 02:31:08 +00:00
|
|
|
if (! $url) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$result['message'] = t('Channel location missing.');
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check service class limits
|
|
|
|
|
|
|
|
$r = q("select count(*) as total from abook where abook_channel = %d and abook_self = 0 ",
|
|
|
|
intval($uid)
|
|
|
|
);
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($r) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$total_channels = $r[0]['total'];
|
|
|
|
}
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if (! service_class_allows($uid,'total_channels',$total_channels)) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$result['message'] = upgrade_message();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
$xchan_hash = '';
|
|
|
|
$sql_options = (($protocol) ? " and xchan_network = '" . dbesc($protocol) . "' " : '');
|
2019-07-12 05:03:17 +00:00
|
|
|
|
2019-11-05 03:34:22 +00:00
|
|
|
$r = q("select * from xchan where ( xchan_hash = '%s' or xchan_url = '%s' or xchan_addr = '%s') $sql_options ",
|
2019-01-24 01:55:39 +00:00
|
|
|
dbesc($url),
|
|
|
|
dbesc($url),
|
|
|
|
dbesc($url)
|
|
|
|
);
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($r) {
|
2019-01-24 01:55:39 +00:00
|
|
|
|
|
|
|
// reset results to the best record or the first if we don't have the best
|
2020-12-22 23:30:52 +00:00
|
|
|
// note: this returns a single record and not an array of records
|
2019-01-24 01:55:39 +00:00
|
|
|
|
|
|
|
$r = Libzot::zot_record_preferred($r,'xchan_network');
|
2019-07-12 05:03:17 +00:00
|
|
|
|
|
|
|
// Some Hubzilla records were originally stored as activitypub. If we find one, force rediscovery
|
|
|
|
// since Zap cannot connect with them.
|
|
|
|
|
2020-07-15 01:47:23 +00:00
|
|
|
if (($r['xchan_network'] === 'activitypub') && (! $ap_allowed)) {
|
2019-07-12 05:03:17 +00:00
|
|
|
$r = null;
|
|
|
|
}
|
2019-01-24 01:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$singleton = false;
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if (! $r) {
|
2019-01-24 01:55:39 +00:00
|
|
|
|
|
|
|
// not in cache - try discovery
|
|
|
|
|
2020-08-07 21:57:59 +00:00
|
|
|
$wf = discover_by_webbie($url,$protocol,false);
|
2019-01-24 01:55:39 +00:00
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if (! $wf) {
|
2020-02-17 22:58:21 +00:00
|
|
|
$result['message'] = t('Remote channel or protocol unavailable.');
|
|
|
|
return $result;
|
2019-01-24 01:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:58:21 +00:00
|
|
|
if ($wf) {
|
2019-01-24 01:55:39 +00:00
|
|
|
|
|
|
|
// something was discovered - find the record which was just created.
|
|
|
|
|
2019-11-05 03:34:22 +00:00
|
|
|
$r = q("select * from xchan where ( xchan_hash = '%s' or xchan_url = '%s' or xchan_addr = '%s' ) $sql_options",
|
2019-01-24 01:55:39 +00:00
|
|
|
dbesc(($wf) ? $wf : $url),
|
|
|
|
dbesc($url),
|
|
|
|
dbesc($url)
|
|
|
|
);
|
|
|
|
|
|
|
|
// convert to a single record (once again preferring a zot solution in the case of multiples)
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($r) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$r = Libzot::zot_record_preferred($r,'xchan_network');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if discovery was a success or the channel was already cached we should have an xchan record in $r
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($r) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$xchan = $r;
|
|
|
|
$xchan_hash = $r['xchan_hash'];
|
|
|
|
$their_perms = EMPTY_STR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// failure case
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if (! $xchan_hash) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$result['message'] = t('Channel discovery failed.');
|
|
|
|
logger('follow: ' . $result['message']);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-09-11 02:31:08 +00:00
|
|
|
if (! check_channelallowed($xchan_hash)) {
|
|
|
|
$result['message'] = t('Channel is blocked on this site.');
|
|
|
|
logger('follow: ' . $result['message']);
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
}
|
2019-01-24 01:55:39 +00:00
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
|
2019-09-24 00:52:03 +00:00
|
|
|
if ($r['xchan_network'] === 'activitypub') {
|
2019-09-23 23:11:36 +00:00
|
|
|
if (! $ap_allowed) {
|
|
|
|
$result['message'] = t('Protocol not supported');
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
$singleton = true;
|
2019-01-24 01:55:39 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
// Now start processing the new connection
|
|
|
|
|
2019-01-24 01:55:39 +00:00
|
|
|
$aid = $channel['channel_account_id'];
|
|
|
|
$hash = $channel['channel_hash'];
|
|
|
|
$default_group = $channel['channel_default_group'];
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($hash === $xchan_hash) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$result['message'] = t('Cannot connect to yourself.');
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
$p = Permissions::connect_perms($uid);
|
|
|
|
|
2019-04-03 23:22:03 +00:00
|
|
|
// parent channels have unencumbered write permission
|
|
|
|
|
|
|
|
if ($sub_channel) {
|
|
|
|
$p['perms']['post_wall'] = 1;
|
|
|
|
$p['perms']['post_comments'] = 1;
|
|
|
|
$p['perms']['write_storage'] = 1;
|
|
|
|
$p['perms']['post_like'] = 1;
|
2019-04-06 23:36:23 +00:00
|
|
|
$p['perms']['delegate'] = 0;
|
2019-04-03 23:22:03 +00:00
|
|
|
$p['perms']['moderated'] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$my_perms = Permissions::serialise($p['perms']);
|
|
|
|
|
2019-01-24 01:55:39 +00:00
|
|
|
$profile_assign = get_pconfig($uid,'system','profile_assign','');
|
|
|
|
|
|
|
|
// See if we are already connected by virtue of having an abook record
|
|
|
|
|
|
|
|
$r = q("select abook_id, abook_xchan, abook_pending, abook_instance from abook
|
|
|
|
where abook_xchan = '%s' and abook_channel = %d limit 1",
|
|
|
|
dbesc($xchan_hash),
|
|
|
|
intval($uid)
|
|
|
|
);
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($r) {
|
2019-01-24 01:55:39 +00:00
|
|
|
|
|
|
|
$abook_instance = $r[0]['abook_instance'];
|
|
|
|
|
|
|
|
// If they are on a non-nomadic network, add them to this location
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if (($singleton) && strpos($abook_instance,z_root()) === false) {
|
|
|
|
if ($abook_instance) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$abook_instance .= ',';
|
|
|
|
}
|
|
|
|
$abook_instance .= z_root();
|
|
|
|
|
|
|
|
$x = q("update abook set abook_instance = '%s', abook_not_here = 0 where abook_id = %d",
|
|
|
|
dbesc($abook_instance),
|
|
|
|
intval($r[0]['abook_id'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if they have a pending connection, we just followed them so approve the connection request
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if (intval($r[0]['abook_pending'])) {
|
2019-01-24 01:55:39 +00:00
|
|
|
$x = q("update abook set abook_pending = 0 where abook_id = %d",
|
|
|
|
intval($r[0]['abook_id'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
// create a new abook record
|
|
|
|
|
|
|
|
$closeness = get_pconfig($uid,'system','new_abook_closeness',80);
|
|
|
|
|
|
|
|
$r = abook_store_lowlevel(
|
|
|
|
[
|
|
|
|
'abook_account' => intval($aid),
|
|
|
|
'abook_channel' => intval($uid),
|
|
|
|
'abook_closeness' => intval($closeness),
|
|
|
|
'abook_xchan' => $xchan_hash,
|
|
|
|
'abook_profile' => $profile_assign,
|
|
|
|
'abook_feed' => intval(($xchan['xchan_network'] === 'rss') ? 1 : 0),
|
|
|
|
'abook_created' => datetime_convert(),
|
|
|
|
'abook_updated' => datetime_convert(),
|
|
|
|
'abook_instance' => (($singleton) ? z_root() : '')
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if (! $r) {
|
2019-01-24 01:55:39 +00:00
|
|
|
logger('abook creation failed');
|
|
|
|
$result['message'] = t('error saving data');
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set suitable permissions to the connection
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($my_perms) {
|
2019-01-24 01:55:39 +00:00
|
|
|
set_abconfig($uid,$xchan_hash,'system','my_perms',$my_perms);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the entire record
|
|
|
|
|
|
|
|
$r = q("select abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash
|
|
|
|
where abook_xchan = '%s' and abook_channel = %d limit 1",
|
|
|
|
dbesc($xchan_hash),
|
|
|
|
intval($uid)
|
|
|
|
);
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($r) {
|
2019-11-04 05:38:19 +00:00
|
|
|
$result['abook'] = array_shift($r);
|
2020-06-15 07:29:56 +00:00
|
|
|
Run::Summon([ 'Notifier', 'permissions_create', $result['abook']['abook_id'] ]);
|
2019-01-24 01:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$arr = [ 'channel_id' => $uid, 'channel' => $channel, 'abook' => $result['abook'] ];
|
|
|
|
|
|
|
|
call_hooks('follow', $arr);
|
|
|
|
|
|
|
|
/** If there is a default group for this channel, add this connection to it */
|
|
|
|
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($default_group) {
|
2019-02-14 23:26:37 +00:00
|
|
|
$g = AccessList::rec_byhash($uid,$default_group);
|
2019-09-23 05:23:18 +00:00
|
|
|
if ($g) {
|
2019-02-14 23:26:37 +00:00
|
|
|
AccessList::member_add($uid,'',$xchan_hash,$g['id']);
|
2019-01-24 01:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result['success'] = true;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|