streams/Code/Lib/Connect.php

312 lines
9.7 KiB
PHP
Raw Normal View History

2021-12-03 03:01:39 +00:00
<?php
2022-02-16 04:08:28 +00:00
namespace Code\Lib;
2019-01-24 01:55:39 +00:00
2019-10-03 19:56:49 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Lib\ServiceClass;
use Code\Access\Permissions;
use Code\Daemon\Run;
use Code\Extend\Hook;
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +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
*/
public static function connect($channel, $url, $sub_channel = false)
{
$uid = $channel['channel_id'];
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
if (strpos($url, '@') === false && strpos($url, '/') === false) {
$url = $url . '@' . App::get_hostname();
}
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
$result = ['success' => false, 'message' => ''];
$my_perms = false;
$protocol = '';
2021-12-02 23:02:31 +00:00
$ap_allowed = get_config('system', 'activitypub', ACTIVITYPUB_ENABLED) && get_pconfig($uid, 'system', 'activitypub', ACTIVITYPUB_ENABLED);
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
if (substr($url, 0, 1) === '[') {
$x = strpos($url, ']');
if ($x) {
$protocol = substr($url, 1, $x - 1);
$url = substr($url, $x + 1);
}
}
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
if (!check_siteallowed($url)) {
$result['message'] = t('Channel is blocked on this site.');
return $result;
}
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
if (!$url) {
$result['message'] = t('Channel location missing.');
return $result;
}
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
// check service class limits
2019-01-24 01:55:39 +00:00
2021-12-03 03:01:39 +00:00
$r = q(
"select count(*) as total from abook where abook_channel = %d and abook_self = 0 ",
2021-12-02 23:02:31 +00:00
intval($uid)
);
if ($r) {
$total_channels = $r[0]['total'];
}
2019-01-24 01:55:39 +00:00
2022-01-25 01:26:12 +00:00
if (!ServiceClass::allows($uid, 'total_channels', $total_channels)) {
$result['message'] = ServiceClass::upgrade_message();
2021-12-02 23:02:31 +00:00
return $result;
}
2019-09-11 02:31:08 +00:00
2021-12-02 23:02:31 +00:00
$xchan_hash = '';
$sql_options = (($protocol) ? " and xchan_network = '" . dbesc($protocol) . "' " : '');
2019-01-24 01:55:39 +00:00
2021-12-03 03:01:39 +00:00
$r = q(
"select * from xchan where ( xchan_hash = '%s' or xchan_url = '%s' or xchan_addr = '%s') $sql_options ",
2021-12-02 23:02:31 +00:00
dbesc($url),
dbesc($url),
dbesc($url)
);
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
if ($r) {
// reset results to the best record or the first if we don't have the best
// note: this returns a single record and not an array of records
2021-12-02 23:02:31 +00:00
$r = Libzot::zot_record_preferred($r, 'xchan_network');
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
// ensure there's a valid hubloc for this xchan before proceeding - you cannot connect without it
2019-01-24 01:55:39 +00:00
if (in_array($r['xchan_network'], ['nomad', 'zot6', 'activitypub'])) {
2021-12-03 03:01:39 +00:00
$h = q(
"select * from hubloc where hubloc_hash = '%s'",
2021-12-02 23:02:31 +00:00
dbesc($r['xchan_hash'])
);
if (!$h) {
$r = null;
}
}
// we may have nulled out this record so check again
if ($r) {
// Check the site table to see if we should have a zot6 hubloc,
// If so, clear the xchan and start fresh
if ($r['xchan_network'] === 'activitypub') {
$m = parse_url($r['xchan_hash']);
unset($m['path']);
$h = unparse_url($m);
2021-12-03 03:01:39 +00:00
$s = q(
"select * from site where site_url = '%s'",
2021-12-02 23:02:31 +00:00
dbesc($h)
);
if (intval($s['site_type']) === SITE_TYPE_ZOT) {
logger('got zot - ignore activitypub entry');
$r = null;
}
}
}
}
$singleton = false;
if (!$r) {
// not in cache - try discovery
$wf = discover_by_webbie($url, $protocol, false);
if (!$wf) {
$result['message'] = t('Remote channel or protocol unavailable.');
return $result;
}
}
if ($wf) {
// something was discovered - find the record which was just created.
2021-12-03 03:01:39 +00:00
$r = q(
"select * from xchan where ( xchan_hash = '%s' or xchan_url = '%s' or xchan_addr = '%s' ) $sql_options",
2021-12-02 23:02:31 +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)
if ($r) {
$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
if ($r) {
$xchan = $r;
$xchan_hash = $r['xchan_hash'];
$their_perms = EMPTY_STR;
}
// failure case
if (!$xchan_hash) {
$result['message'] = t('Channel discovery failed.');
logger('follow: ' . $result['message']);
return $result;
}
if (!check_channelallowed($xchan_hash)) {
$result['message'] = t('Channel is blocked on this site.');
logger('follow: ' . $result['message']);
return $result;
}
if ($r['xchan_network'] === 'activitypub') {
if (!$ap_allowed) {
$result['message'] = t('Protocol not supported');
return $result;
}
$singleton = true;
}
// Now start processing the new connection
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
$aid = $channel['channel_account_id'];
$hash = $channel['channel_hash'];
$default_group = $channel['channel_default_group'];
if ($hash === $xchan_hash) {
$result['message'] = t('Cannot connect to yourself.');
return $result;
}
$p = Permissions::connect_perms($uid);
// parent channels have unencumbered write permission
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
if ($sub_channel) {
$p['perms']['post_wall'] = 1;
$p['perms']['post_comments'] = 1;
$p['perms']['write_storage'] = 1;
$p['perms']['post_like'] = 1;
$p['perms']['delegate'] = 0;
$p['perms']['moderated'] = 0;
}
$my_perms = Permissions::serialise($p['perms']);
2021-12-02 23:02:31 +00:00
$profile_assign = get_pconfig($uid, 'system', 'profile_assign', '');
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
// See if we are already connected by virtue of having an abook record
2019-01-24 01:55:39 +00:00
2021-12-03 03:01:39 +00:00
$r = q(
"select abook_id, abook_xchan, abook_pending, abook_instance from abook
2019-01-24 01:55:39 +00:00
where abook_xchan = '%s' and abook_channel = %d limit 1",
2021-12-02 23:02:31 +00:00
dbesc($xchan_hash),
intval($uid)
);
if ($r) {
$abook_instance = $r[0]['abook_instance'];
// If they are on a non-nomadic network, add them to this location
if (($singleton) && strpos($abook_instance, z_root()) === false) {
if ($abook_instance) {
$abook_instance .= ',';
}
$abook_instance .= z_root();
2021-12-03 03:01:39 +00:00
$x = q(
"update abook set abook_instance = '%s', abook_not_here = 0 where abook_id = %d",
2021-12-02 23:02:31 +00:00
dbesc($abook_instance),
intval($r[0]['abook_id'])
);
}
// if they have a pending connection, we just followed them so approve the connection request
if (intval($r[0]['abook_pending'])) {
2021-12-03 03:01:39 +00:00
$x = q(
"update abook set abook_pending = 0 where abook_id = %d",
2021-12-02 23:02:31 +00:00
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() : '')
]
);
}
if (!$r) {
logger('abook creation failed');
$result['message'] = t('error saving data');
return $result;
}
// Set suitable permissions to the connection
if ($my_perms) {
set_abconfig($uid, $xchan_hash, 'system', 'my_perms', $my_perms);
}
// fetch the entire record
2021-12-03 03:01:39 +00:00
$r = q(
"select abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash
2019-01-24 01:55:39 +00:00
where abook_xchan = '%s' and abook_channel = %d limit 1",
2021-12-02 23:02:31 +00:00
dbesc($xchan_hash),
intval($uid)
);
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
if ($r) {
$result['abook'] = array_shift($r);
Run::Summon(['Notifier', 'permissions_create', $result['abook']['abook_id']]);
}
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
$arr = ['channel_id' => $uid, 'channel' => $channel, 'abook' => $result['abook']];
2019-01-24 01:55:39 +00:00
Hook::call('follow', $arr);
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
/** If there is a default group for this channel, add this connection to it */
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
if ($default_group) {
$g = AccessList::rec_byhash($uid, $default_group);
if ($g) {
AccessList::member_add($uid, '', $xchan_hash, $g['id']);
}
}
2019-01-24 01:55:39 +00:00
2021-12-02 23:02:31 +00:00
$result['success'] = true;
return $result;
}
2021-12-03 03:01:39 +00:00
}