streams/include/follow.php

277 lines
6.8 KiB
PHP
Raw Normal View History

2013-02-26 01:09:40 +00:00
<?php /** @file */
2012-06-03 06:12:20 +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.
2012-11-01 04:42:20 +00:00
require_once('include/zot.php');
2012-06-03 06:22:02 +00:00
function new_contact($uid,$url,$channel,$interactive = false, $confirm = false) {
2012-06-03 06:12:20 +00:00
$result = array('success' => false,'message' => '');
$a = get_app();
2014-02-18 00:00:17 +00:00
$is_red = false;
$is_http = ((strpos($url,'://') !== false) ? true : false);
2012-06-03 06:12:20 +00:00
if($is_http && substr($url,-1,1) === '/')
$url = substr($url,0,-1);
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;
}
// check service class limits
$r = q("select count(*) as total from abook where abook_channel = %d and not (abook_flags & %d) ",
intval($uid),
intval(ABOOK_FLAG_SELF)
);
if($r)
$total_channels = $r[0]['total'];
if(! service_class_allows($uid,'total_channels',$total_channels)) {
$result['message'] = upgrade_message();
return $result;
}
$arr = array('url' => $url, 'channel' => array());
2012-06-03 06:12:20 +00:00
call_hooks('follow', $arr);
2012-11-01 09:00:25 +00:00
if($arr['channel']['success'])
2012-11-01 08:54:51 +00:00
$ret = $arr['channel'];
2014-09-02 03:41:05 +00:00
elseif(! $is_http)
$ret = zot_finger($url,$channel);
2012-11-01 04:42:20 +00:00
2014-09-01 01:50:30 +00:00
if($ret && $ret['success']) {
2014-02-18 00:00:17 +00:00
$is_red = true;
$j = json_decode($ret['body'],true);
}
2014-02-18 00:00:17 +00:00
if($is_red && $j) {
2012-06-03 06:12:20 +00:00
2014-02-18 00:00:17 +00:00
$my_perms = PERMS_W_STREAM|PERMS_W_MAIL;
$role = get_pconfig($uid,'system','permissions_role');
if($role) {
$x = get_role_perms($role);
if($x['perms_follow'])
$my_perms = $x['perms_follow'];
}
2014-02-18 00:00:17 +00:00
logger('follow: ' . $url . ' ' . print_r($j,true), LOGGER_DEBUG);
2012-06-03 06:12:20 +00:00
2014-02-18 00:00:17 +00:00
if(! ($j['success'] && $j['guid'])) {
$result['message'] = t('Response from remote channel was incomplete.');
logger('mod_follow: ' . $result['message']);
return $result;
}
2014-02-18 00:00:17 +00:00
// Premium channel, set confirm before callback to avoid recursion
if(array_key_exists('connect_url',$j) && ($interactive) && (! $confirm))
2014-02-18 00:00:17 +00:00
goaway(zid($j['connect_url']));
2012-06-25 05:23:17 +00:00
2014-02-18 00:00:17 +00:00
// do we have an xchan and hubloc?
// If not, create them.
2012-11-01 08:54:51 +00:00
2014-02-18 00:00:17 +00:00
$x = import_xchan($j);
2012-06-03 06:12:20 +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;
2014-02-18 00:00:17 +00:00
$xchan_hash = $x['hash'];
2012-11-02 05:23:13 +00:00
2014-02-18 00:00:17 +00:00
$their_perms = 0;
2012-11-02 05:23:13 +00:00
2014-02-18 00:00:17 +00:00
$global_perms = get_perms();
2012-11-02 05:23:13 +00:00
2014-02-18 00:00:17 +00:00
if( array_key_exists('permissions',$j) && array_key_exists('data',$j['permissions'])) {
$permissions = crypto_unencapsulate(array(
'data' => $j['permissions']['data'],
'key' => $j['permissions']['key'],
'iv' => $j['permissions']['iv']),
$channel['channel_prvkey']);
if($permissions)
$permissions = json_decode($permissions,true);
logger('decrypted permissions: ' . print_r($permissions,true), LOGGER_DATA);
}
else
$permissions = $j['permissions'];
2012-11-02 23:25:59 +00:00
2014-02-18 00:00:17 +00:00
foreach($permissions as $k => $v) {
if($v) {
$their_perms = $their_perms | intval($global_perms[$k][1]);
}
2012-11-02 07:19:04 +00:00
}
}
2014-02-18 00:00:17 +00:00
else {
if(! ($is_http)) {
if(! intval(get_config('system','diaspora_enabled'))) {
$result['message'] = t('Protocol disabled.');
return $result;
}
}
2014-02-18 00:00:17 +00:00
$my_perms = 0;
$their_perms = 0;
$xchan_hash = '';
2014-09-01 01:50:30 +00:00
$r = q("select * from xchan where xchan_hash = '%s' or xchan_url = '%s' limit 1",
dbesc($url),
dbesc($url)
);
2014-02-18 00:00:17 +00:00
if(! $r) {
// attempt network auto-discovery
if(strpos($url,'@') && (! $is_http)) {
$r = discover_by_webbie($url);
}
elseif($is_http) {
2014-09-01 01:50:30 +00:00
$r = discover_by_url($url);
}
if($r) {
$r = q("select * from xchan where xchan_hash = '%s' or xchan_url = '%s' limit 1",
dbesc($url),
dbesc($url)
);
}
}
if($r) {
2014-09-01 01:50:30 +00:00
$xchan_hash = $r[0]['xchan_hash'];
$their_perms = 0;
$my_perms = PERMS_W_STREAM|PERMS_W_MAIL;
$role = get_pconfig($uid,'system','permissions_role');
if($role) {
$x = get_role_perms($role);
if($x['perms_follow'])
$my_perms = $x['perms_follow'];
}
}
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
if((local_user()) && $uid == local_user()) {
$aid = get_account_id();
$hash = get_observer_hash();
$ch = $a->get_channel();
$default_group = $ch['channel_default_group'];
}
else {
$r = q("select * from channel where channel_id = %d limit 1",
intval($uid)
);
if(! $r) {
2012-12-26 06:57:16 +00:00
$result['message'] = t('local account not found.');
return $result;
}
$aid = $r[0]['channel_account_id'];
$hash = $r[0]['channel_hash'];
$default_group = $r[0]['channel_default_group'];
}
if($is_http) {
2014-09-01 11:17:10 +00:00
if(! intval(get_config('system','feed_contacts'))) {
$result['message'] = t('Protocol disabled.');
return $result;
}
2014-09-02 03:59:53 +00:00
$r = q("select count(*) as total from abook where abook_account = %d and ( abook_flags & ABOOK_FLAG_FEED )",
intval($aid)
);
if($r)
$total_feeds = $r[0]['total'];
if(! service_class_allows($uid,'total_feeds',$total_feeds)) {
$result['message'] = upgrade_message();
return $result;
}
}
if($hash == $xchan_hash) {
2012-12-26 06:57:16 +00:00
$result['message'] = t('Cannot connect to yourself.');
return $result;
}
2012-06-25 05:23:17 +00:00
$r = q("select abook_xchan from abook where abook_xchan = '%s' and abook_channel = %d limit 1",
dbesc($xchan_hash),
intval($uid)
);
if($r) {
$x = q("update abook set abook_their_perms = %d where abook_id = %d limit 1",
intval($their_perms),
intval($r[0]['abook_id'])
);
}
else {
2014-09-01 05:15:00 +00:00
$r = q("insert into abook ( abook_account, abook_channel, abook_xchan, abook_flags, abook_their_perms, abook_my_perms, abook_created, abook_updated )
values( %d, %d, '%s', %d, %d, %d, '%s', '%s' ) ",
2012-12-26 06:57:16 +00:00
intval($aid),
intval($uid),
dbesc($xchan_hash),
2014-09-01 05:15:00 +00:00
intval(($is_http) ? ABOOK_FLAG_FEED : 0),
2014-09-04 23:51:24 +00:00
intval(($is_http) ? $their_perms|PERMS_R_STREAM|PERMS_A_REPUBLISH : $their_perms),
2014-02-18 00:00:17 +00:00
intval($my_perms),
dbesc(datetime_convert()),
dbesc(datetime_convert())
);
}
if(! $r)
logger('mod_follow: abook creation failed');
$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)
);
2013-05-20 03:38:53 +00:00
if($r) {
$result['abook'] = $r[0];
2014-02-18 00:00:17 +00:00
if($is_red)
proc_run('php', 'include/notifier.php', 'permission_update', $result['abook']['abook_id']);
2013-05-20 03:38:53 +00:00
}
2013-11-22 01:49:14 +00:00
$arr = array('channel_id' => $uid, 'abook' => $result['abook']);
call_hooks('follow', $arr);
/** If there is a default group for this channel, add this member to it */
2012-06-03 06:12:20 +00:00
if($default_group) {
2012-06-03 06:12:20 +00:00
require_once('include/group.php');
$g = group_rec_byhash($uid,$default_group);
if($g)
group_add_member($uid,'',$xchan_hash,$g['id']);
2012-06-03 06:12:20 +00:00
}
$result['success'] = true;
return $result;
}