streams/include/follow.php

227 lines
5.4 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
2012-11-01 04:58:17 +00:00
function new_contact($uid,$url,$channel,$interactive = false) {
2012-06-03 06:12:20 +00:00
$result = array('success' => false,'message' => '');
$a = get_app();
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;
}
$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'];
2012-06-03 06:12:20 +00:00
else
$ret = zot_finger($url,$channel);
2012-11-01 04:42:20 +00:00
if($ret['success']) {
$j = json_decode($ret['body'],true);
}
else {
$result['message'] = t('Channel discovery failed. Website may be down or misconfigured.');
logger('mod_follow: ' . $result['message']);
return $result;
2012-11-01 04:42:20 +00:00
}
2012-11-01 08:54:51 +00:00
logger('follow: ' . $url . ' ' . print_r($j,true));
2012-06-03 06:12:20 +00:00
if(! $j) {
$result['message'] = t('Response from remote channel was not understood.');
logger('mod_follow: ' . $result['message']);
return $result;
}
if(! ($j['success'] && $j['guid'])) {
$result['message'] = t('Response from remote channel was incomplete.');
logger('mod_follow: ' . $result['message']);
2012-06-03 06:12:20 +00:00
return $result;
}
2012-06-25 05:23:17 +00:00
2012-11-01 08:54:51 +00:00
// check service class limits
2012-06-25 05:23:17 +00:00
2012-11-30 07:06:03 +00:00
$r = q("select count(*) as total from abook where abook_channel = %d and not (abook_flags & %d) ",
2012-11-01 08:54:51 +00:00
intval($uid),
intval(ABOOK_FLAG_SELF)
);
if($r)
$total_channels = $r[0]['total'];
2012-06-25 05:23:17 +00:00
2012-11-01 08:54:51 +00:00
if(! service_class_allows($uid,'total_channels',$total_channels)) {
2012-12-26 06:57:16 +00:00
$result['message'] = upgrade_message();
2012-11-01 08:54:51 +00:00
return $result;
2012-06-03 06:12:20 +00:00
}
2012-11-01 08:54:51 +00:00
// do we have an xchan and hubloc?
// If not, create them.
$x = import_xchan($j);
2012-06-03 06:12:20 +00:00
if(! $x['success'])
return $x;
$xchan_hash = $x['hash'];
2012-11-02 05:23:13 +00:00
$their_perms = 0;
$global_perms = get_perms();
if( array_key_exists('permissions',$j) && array_key_exists('data',$j['permissions'])) {
2012-11-02 23:25:59 +00:00
$permissions = aes_unencapsulate(array(
'data' => $j['permissions']['data'],
'key' => $j['permissions']['key'],
'iv' => $j['permissions']['iv']),
2012-11-02 23:25:59 +00:00
$channel['channel_prvkey']);
if($permissions)
$permissions = json_decode($permissions,true);
2012-11-02 23:25:59 +00:00
logger('decrypted permissions: ' . print_r($permissions,true), LOGGER_DATA);
}
else
$permissions = $j['permissions'];
2012-11-02 23:25:59 +00:00
foreach($permissions as $k => $v) {
2012-11-02 07:19:04 +00:00
if($v) {
2012-11-02 05:23:13 +00:00
$their_perms = $their_perms | intval($global_perms[$k][1]);
2012-11-02 07:19:04 +00:00
}
}
2012-06-03 06:12:20 +00:00
if((local_user()) && $uid == local_user()) {
$aid = get_account_id();
$hash = $a->observer['xchan_hash'];
}
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'];
}
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 {
$r = q("insert into abook ( abook_account, abook_channel, abook_xchan, abook_their_perms, abook_created, abook_updated )
values( %d, %d, '%s', %d, '%s', '%s' ) ",
2012-12-26 06:57:16 +00:00
intval($aid),
intval($uid),
dbesc($xchan_hash),
intval($their_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)
);
if($r)
$result['abook'] = $r[0];
// Then send a ping/message to the other side
2012-06-25 05:23:17 +00:00
2012-11-01 09:00:25 +00:00
/*
2012-06-03 06:12:20 +00:00
2012-11-01 08:54:51 +00:00
$r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
2012-08-31 01:47:07 +00:00
`writable`, `hidden`, `blocked`, `readonly`, `pending` )
VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0) ",
2012-06-03 06:12:20 +00:00
intval($uid),
dbesc(datetime_convert()),
dbesc($ret['url']),
dbesc(normalise_link($ret['url'])),
dbesc($ret['addr']),
dbesc($ret['alias']),
dbesc($ret['batch']),
dbesc($ret['notify']),
dbesc($ret['poll']),
dbesc($ret['poco']),
dbesc($ret['name']),
dbesc($ret['nick']),
dbesc($ret['photo']),
dbesc($ret['network']),
dbesc($ret['pubkey']),
intval($new_relation),
intval($ret['priority']),
intval($writeable),
2012-08-31 01:47:07 +00:00
intval($hidden)
2012-06-03 06:12:20 +00:00
);
}
$r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
dbesc($ret['url']),
intval($uid)
);
if(! count($r)) {
$result['message'] .= t('Unable to retrieve contact information.') . EOL;
return $result;
}
$contact = $r[0];
$contact_id = $r[0]['id'];
$g = q("select def_gid from user where uid = %d limit 1",
intval($uid)
);
if($g && intval($g[0]['def_gid'])) {
require_once('include/group.php');
group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
}
2012-11-01 09:00:25 +00:00
*/
2012-06-03 06:12:20 +00:00
$result['success'] = true;
return $result;
2012-11-01 09:00:25 +00:00
2012-06-03 06:12:20 +00:00
}