2013-02-26 01:09:40 +00:00
|
|
|
<?php /** @file */
|
2012-06-03 06:12:20 +00:00
|
|
|
|
|
|
|
|
2018-06-01 02:42:13 +00:00
|
|
|
use Zotlabs\Lib\Libzot;
|
2018-07-03 23:40:54 +00:00
|
|
|
use Zotlabs\Lib\Group;
|
2018-06-01 02:42:13 +00:00
|
|
|
|
2012-06-03 06:22:02 +00:00
|
|
|
//
|
2012-11-01 04:42:20 +00:00
|
|
|
// Takes a $uid and the channel associated with the uid, and a url/handle and adds a new channel
|
2012-06-03 06:22:02 +00:00
|
|
|
|
|
|
|
// Returns an array
|
|
|
|
// $return['success'] boolean true if successful
|
2013-01-01 01:18:11 +00:00
|
|
|
// $return['abook'] Address book entry joined with xchan if successful
|
2012-06-03 06:22:02 +00:00
|
|
|
// $return['message'] error text if success is false.
|
|
|
|
|
2018-06-01 02:42:13 +00:00
|
|
|
|
2012-06-03 06:22:02 +00:00
|
|
|
|
2013-08-22 05:10:08 +00:00
|
|
|
function new_contact($uid,$url,$channel,$interactive = false, $confirm = false) {
|
2012-06-03 06:12:20 +00:00
|
|
|
|
2017-02-10 01:29:24 +00:00
|
|
|
$result = [ 'success' => false, 'message' => '' ];
|
2014-10-28 02:21:41 +00:00
|
|
|
|
2017-02-10 01:29:24 +00:00
|
|
|
$my_perms = false;
|
|
|
|
$is_zot = false;
|
2017-08-10 19:49:20 +00:00
|
|
|
$protocol = '';
|
|
|
|
|
|
|
|
|
|
|
|
if(substr($url,0,1) === '[') {
|
|
|
|
$x = strpos($url,']');
|
|
|
|
if($x) {
|
|
|
|
$protocol = substr($url,1,$x-1);
|
|
|
|
$url = substr($url,$x+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:29:24 +00:00
|
|
|
$is_http = ((strpos($url,'://') !== false) ? true : false);
|
2012-06-03 06:12:20 +00:00
|
|
|
|
2018-05-29 02:42:40 +00:00
|
|
|
$url = rtrim($url,'/');
|
2014-09-24 03:55:22 +00:00
|
|
|
|
2012-06-03 06:12:20 +00:00
|
|
|
if(! allowed_url($url)) {
|
2012-11-01 04:42:20 +00:00
|
|
|
$result['message'] = t('Channel is blocked on this site.');
|
2012-06-03 06:12:20 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(! $url) {
|
2012-11-01 04:42:20 +00:00
|
|
|
$result['message'] = t('Channel location missing.');
|
2012-06-03 06:12:20 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-09-01 02:30:35 +00:00
|
|
|
|
|
|
|
// check service class limits
|
|
|
|
|
2015-06-15 04:08:00 +00:00
|
|
|
$r = q("select count(*) as total from abook where abook_channel = %d and abook_self = 0 ",
|
|
|
|
intval($uid)
|
2014-09-01 02:30:35 +00:00
|
|
|
);
|
|
|
|
if($r)
|
|
|
|
$total_channels = $r[0]['total'];
|
|
|
|
|
|
|
|
if(! service_class_allows($uid,'total_channels',$total_channels)) {
|
|
|
|
$result['message'] = upgrade_message();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-29 02:42:40 +00:00
|
|
|
$arr = array('url' => $url, 'protocol' => $protocol, 'channel' => array());
|
2012-06-03 06:12:20 +00:00
|
|
|
|
2017-04-20 00:52:15 +00:00
|
|
|
call_hooks('follow_init', $arr);
|
2012-06-03 06:12:20 +00:00
|
|
|
|
2012-11-01 09:00:25 +00:00
|
|
|
if($arr['channel']['success'])
|
2012-11-01 08:54:51 +00:00
|
|
|
$ret = $arr['channel'];
|
2018-05-29 02:42:40 +00:00
|
|
|
else {
|
|
|
|
$href = \Zotlabs\Lib\Webfinger::zot_url(punify($url));
|
|
|
|
if($href) {
|
|
|
|
$zf = \Zotlabs\Lib\Zotfinger::exec($href,$channel);
|
|
|
|
}
|
|
|
|
if(is_array($zf) && array_path_exists('signature/signer',$zf) && $zf['signature']['signer'] === $href
|
|
|
|
&& intval($zf['signature']['header_valid']) && array_path_exists('data/permissions',$zf)) {
|
2018-06-01 02:42:13 +00:00
|
|
|
$x = Libzot::import_xchan($zf['data']);
|
2018-05-29 02:42:40 +00:00
|
|
|
$j = $zf['data'];
|
|
|
|
$is_zot = true;
|
|
|
|
}
|
2013-01-29 10:04:07 +00:00
|
|
|
}
|
2012-10-31 23:55:19 +00:00
|
|
|
|
2014-09-18 00:59:46 +00:00
|
|
|
|
2018-05-29 02:42:40 +00:00
|
|
|
$p = \Zotlabs\Access\Permissions::connect_perms($uid);
|
|
|
|
$my_perms = \Zotlabs\Access\Permissions::serialise($p['perms']);
|
2013-01-29 10:04:07 +00:00
|
|
|
|
2018-05-29 02:42:40 +00:00
|
|
|
if($x) {
|
2013-08-22 05:10:08 +00:00
|
|
|
|
2014-02-18 00:00:17 +00:00
|
|
|
// Premium channel, set confirm before callback to avoid recursion
|
2013-08-22 05:10:08 +00:00
|
|
|
|
2018-05-29 02:42:40 +00:00
|
|
|
// if(array_key_exists('connect_url',$j) && (! $confirm)) {
|
|
|
|
// if($interactive) {
|
|
|
|
// goaway(zid($j['connect_url']));
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// $result['message'] = t('Premium channel - please visit:') . ' ' . zid($j['connect_url']);
|
|
|
|
// logger('mod_follow: ' . $result['message']);
|
|
|
|
// return $result;
|
|
|
|
// }
|
|
|
|
// }
|
2018-03-02 09:01:30 +00:00
|
|
|
|
2014-04-07 09:55:00 +00:00
|
|
|
|
|
|
|
if(array_key_exists('deleted',$j) && intval($j['deleted'])) {
|
|
|
|
$result['message'] = t('Channel was deleted and no longer exists.');
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-02-18 00:00:17 +00:00
|
|
|
if(! $x['success'])
|
|
|
|
return $x;
|
2012-11-02 08:44:27 +00:00
|
|
|
|
2014-02-18 00:00:17 +00:00
|
|
|
$xchan_hash = $x['hash'];
|
2012-11-07 02:13:47 +00:00
|
|
|
|
2018-05-29 02:42:40 +00:00
|
|
|
$permissions = $j['permissions'];
|
|
|
|
|
|
|
|
set_abconfig($channel['channel_uid'],$xchan_hash,'system','their_perms',$j['permissions']);
|
2012-11-02 23:25:59 +00:00
|
|
|
|
2012-11-01 23:36:35 +00:00
|
|
|
}
|
2014-02-18 00:00:17 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
$xchan_hash = '';
|
2017-08-10 19:49:20 +00:00
|
|
|
$sql_options = (($protocol) ? " and xchan_network = '" . dbesc($protocol) . "' " : '');
|
|
|
|
|
2014-02-18 00:00:17 +00:00
|
|
|
|
2017-08-10 19:49:20 +00:00
|
|
|
$r = q("select * from xchan where xchan_hash = '%s' or xchan_url = '%s' $sql_options limit 1",
|
2014-09-01 01:50:30 +00:00
|
|
|
dbesc($url),
|
2014-08-25 00:52:26 +00:00
|
|
|
dbesc($url)
|
|
|
|
);
|
2014-02-18 00:00:17 +00:00
|
|
|
|
2014-08-25 00:52:26 +00:00
|
|
|
if(! $r) {
|
2017-08-12 07:59:05 +00:00
|
|
|
|
2014-08-25 00:52:26 +00:00
|
|
|
// attempt network auto-discovery
|
2016-03-21 04:41:19 +00:00
|
|
|
|
2018-04-19 02:41:09 +00:00
|
|
|
$wf = discover_by_webbie($url,$protocol);
|
2016-03-21 04:41:19 +00:00
|
|
|
|
2018-04-19 02:41:09 +00:00
|
|
|
if((! $wf) && ($is_http)) {
|
2016-03-21 04:41:19 +00:00
|
|
|
|
|
|
|
// try RSS discovery
|
|
|
|
|
2017-08-12 07:59:05 +00:00
|
|
|
$feeds = get_config('system','feed_contacts');
|
|
|
|
|
2018-05-10 20:48:18 +00:00
|
|
|
if(($feeds) && ($protocol === '' || $protocol === 'feed' || $protocol === 'rss')) {
|
2015-10-18 22:49:52 +00:00
|
|
|
$d = discover_by_url($url);
|
2016-03-21 04:41:19 +00:00
|
|
|
}
|
2015-10-18 22:49:52 +00:00
|
|
|
else {
|
2017-08-12 07:59:05 +00:00
|
|
|
$result['message'] = t('Remote channel or protocol unavailable.');
|
2015-08-03 23:52:51 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2015-10-18 22:49:52 +00:00
|
|
|
}
|
2016-03-21 04:41:19 +00:00
|
|
|
|
2018-04-19 02:41:09 +00:00
|
|
|
if($wf || $d) {
|
2014-09-01 01:50:30 +00:00
|
|
|
$r = q("select * from xchan where xchan_hash = '%s' or xchan_url = '%s' limit 1",
|
2018-04-19 02:41:09 +00:00
|
|
|
dbesc(($wf) ? $wf : $url),
|
2014-09-01 01:50:30 +00:00
|
|
|
dbesc($url)
|
|
|
|
);
|
|
|
|
}
|
2014-08-25 00:52:26 +00:00
|
|
|
}
|
2016-03-21 04:41:19 +00:00
|
|
|
|
|
|
|
// if discovery was a success we should have an xchan record in $r
|
|
|
|
|
2014-08-25 00:52:26 +00:00
|
|
|
if($r) {
|
2015-12-10 02:30:30 +00:00
|
|
|
$xchan = $r[0];
|
2014-09-01 01:50:30 +00:00
|
|
|
$xchan_hash = $r[0]['xchan_hash'];
|
2018-06-08 02:44:09 +00:00
|
|
|
$their_perms = EMPTY_STR;
|
2014-08-25 00:52:26 +00:00
|
|
|
}
|
2014-02-18 00:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(! $xchan_hash) {
|
|
|
|
$result['message'] = t('Channel discovery failed.');
|
|
|
|
logger('follow: ' . $result['message']);
|
|
|
|
return $result;
|
|
|
|
}
|
2012-06-03 06:12:20 +00:00
|
|
|
|
2017-02-10 01:29:24 +00:00
|
|
|
$allowed = (($is_zot || $r[0]['xchan_network'] === 'rss') ? 1 : 0);
|
2016-03-17 01:00:13 +00:00
|
|
|
|
|
|
|
$x = array('channel_id' => $uid, 'follow_address' => $url, 'xchan' => $r[0], 'allowed' => $allowed, 'singleton' => 0);
|
2015-10-18 22:49:52 +00:00
|
|
|
|
|
|
|
call_hooks('follow_allow',$x);
|
|
|
|
|
|
|
|
if(! $x['allowed']) {
|
|
|
|
$result['message'] = t('Protocol disabled.');
|
|
|
|
return $result;
|
|
|
|
}
|
2016-07-14 06:05:19 +00:00
|
|
|
|
2015-12-10 02:30:30 +00:00
|
|
|
$singleton = intval($x['singleton']);
|
2015-10-18 22:49:52 +00:00
|
|
|
|
2016-03-21 04:41:19 +00:00
|
|
|
$aid = $channel['channel_account_id'];
|
2018-07-16 01:31:39 +00:00
|
|
|
$hash = $channel['channel_hash'];
|
2016-03-21 04:41:19 +00:00
|
|
|
$default_group = $channel['channel_default_group'];
|
2015-12-28 05:08:28 +00:00
|
|
|
|
2017-07-31 02:04:17 +00:00
|
|
|
if($hash == $xchan_hash) {
|
|
|
|
$result['message'] = t('Cannot connect to yourself.');
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-03-21 04:41:19 +00:00
|
|
|
if($xchan['xchan_network'] === 'rss') {
|
2015-01-25 21:30:40 +00:00
|
|
|
|
2016-03-21 04:41:19 +00:00
|
|
|
// check service class feed limits
|
2014-09-01 11:17:10 +00:00
|
|
|
|
2015-06-15 04:08:00 +00:00
|
|
|
$r = q("select count(*) as total from abook where abook_account = %d and abook_feed = 1 ",
|
|
|
|
intval($aid)
|
2014-09-01 02:30:35 +00:00
|
|
|
);
|
|
|
|
if($r)
|
|
|
|
$total_feeds = $r[0]['total'];
|
|
|
|
|
|
|
|
if(! service_class_allows($uid,'total_feeds',$total_feeds)) {
|
|
|
|
$result['message'] = upgrade_message();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-07-31 02:04:17 +00:00
|
|
|
// Always set these "remote" permissions for feeds since we cannot interact with them
|
|
|
|
// to negotiate a suitable permission response
|
|
|
|
|
2018-06-08 02:44:09 +00:00
|
|
|
$p = get_abconfig($uid,$xchan_hash,'system','their_perms',EMPTY_STR);
|
|
|
|
if($p)
|
|
|
|
$p .= ',';
|
|
|
|
$p .= 'view_stream,republish';
|
|
|
|
set_abconfig($uid,$xchan_hash,'system','their_perms',$p);
|
2017-07-31 02:04:17 +00:00
|
|
|
|
2012-12-26 02:48:51 +00:00
|
|
|
}
|
2012-06-25 05:23:17 +00:00
|
|
|
|
2017-11-24 22:55:39 +00:00
|
|
|
$profile_assign = get_pconfig($uid,'system','profile_assign','');
|
|
|
|
|
2017-07-31 02:04:17 +00:00
|
|
|
|
2017-02-10 01:29:24 +00:00
|
|
|
$r = q("select abook_id, abook_xchan, abook_pending, abook_instance from abook
|
|
|
|
where abook_xchan = '%s' and abook_channel = %d limit 1",
|
2012-11-07 02:13:47 +00:00
|
|
|
dbesc($xchan_hash),
|
2012-12-26 02:48:51 +00:00
|
|
|
intval($uid)
|
2012-11-07 02:13:47 +00:00
|
|
|
);
|
2015-12-28 05:08:28 +00:00
|
|
|
|
2012-12-26 02:48:51 +00:00
|
|
|
if($r) {
|
2017-02-10 01:29:24 +00:00
|
|
|
|
2015-12-10 02:30:30 +00:00
|
|
|
$abook_instance = $r[0]['abook_instance'];
|
|
|
|
|
|
|
|
if(($singleton) && strpos($abook_instance,z_root()) === false) {
|
|
|
|
if($abook_instance)
|
|
|
|
$abook_instance .= ',';
|
|
|
|
$abook_instance .= z_root();
|
|
|
|
|
2017-05-25 20:37:13 +00:00
|
|
|
$x = q("update abook set abook_instance = '%s', abook_not_here = 0 where abook_id = %d",
|
|
|
|
dbesc($abook_instance),
|
|
|
|
intval($r[0]['abook_id'])
|
|
|
|
);
|
|
|
|
}
|
2016-11-03 00:27:18 +00:00
|
|
|
|
|
|
|
if(intval($r[0]['abook_pending'])) {
|
|
|
|
$x = q("update abook set abook_pending = 0 where abook_id = %d",
|
|
|
|
intval($r[0]['abook_id'])
|
|
|
|
);
|
|
|
|
}
|
2012-12-26 02:48:51 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-03-08 21:11:38 +00:00
|
|
|
$closeness = get_pconfig($uid,'system','new_abook_closeness');
|
|
|
|
if($closeness === false)
|
|
|
|
$closeness = 80;
|
|
|
|
|
2017-02-10 01:29:24 +00:00
|
|
|
$r = abook_store_lowlevel(
|
|
|
|
[
|
|
|
|
'abook_account' => intval($aid),
|
|
|
|
'abook_channel' => intval($uid),
|
|
|
|
'abook_closeness' => intval($closeness),
|
|
|
|
'abook_xchan' => $xchan_hash,
|
2017-11-24 22:55:39 +00:00
|
|
|
'abook_profile' => $profile_assign,
|
2017-07-31 02:04:17 +00:00
|
|
|
'abook_feed' => intval(($xchan['xchan_network'] === 'rss') ? 1 : 0),
|
2017-02-10 01:29:24 +00:00
|
|
|
'abook_created' => datetime_convert(),
|
|
|
|
'abook_updated' => datetime_convert(),
|
|
|
|
'abook_instance' => (($singleton) ? z_root() : '')
|
|
|
|
]
|
2012-12-26 02:48:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-11-07 02:13:47 +00:00
|
|
|
if(! $r)
|
|
|
|
logger('mod_follow: abook creation failed');
|
2012-11-01 23:36:35 +00:00
|
|
|
|
2017-02-10 01:29:24 +00:00
|
|
|
if($my_perms) {
|
2018-05-29 02:42:40 +00:00
|
|
|
set_abconfig($uid,$xchan_hash,'system','my_perms',$my_perms);
|
2016-07-14 06:05:19 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 23:47:51 +00:00
|
|
|
$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)
|
|
|
|
);
|
2015-12-28 05:08:28 +00:00
|
|
|
|
2013-05-20 03:38:53 +00:00
|
|
|
if($r) {
|
2012-12-31 23:47:51 +00:00
|
|
|
$result['abook'] = $r[0];
|
2018-06-28 01:43:39 +00:00
|
|
|
Zotlabs\Daemon\Master::Summon(array('Notifier', 'permissions_create', $result['abook']['abook_id']));
|
2013-05-20 03:38:53 +00:00
|
|
|
}
|
2012-12-31 23:47:51 +00:00
|
|
|
|
2016-03-21 04:41:19 +00:00
|
|
|
$arr = array('channel_id' => $uid, 'channel' => $channel, 'abook' => $result['abook']);
|
2013-11-22 01:49:14 +00:00
|
|
|
|
|
|
|
call_hooks('follow', $arr);
|
|
|
|
|
2016-06-15 12:28:16 +00:00
|
|
|
/** If there is a default group for this channel, add this connection to it */
|
2012-06-03 06:12:20 +00:00
|
|
|
|
2013-03-28 02:35:34 +00:00
|
|
|
if($default_group) {
|
2018-07-03 23:40:54 +00:00
|
|
|
|
|
|
|
$g = Group::rec_byhash($uid,$default_group);
|
2013-03-28 02:35:34 +00:00
|
|
|
if($g)
|
2018-07-03 23:40:54 +00:00
|
|
|
Group_member_add($uid,'',$xchan_hash,$g['id']);
|
2012-06-03 06:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$result['success'] = true;
|
|
|
|
return $result;
|
|
|
|
}
|