streams/include/follow.php

210 lines
5 KiB
PHP
Raw Normal View History

2012-06-03 06:12:20 +00:00
<?php
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
// $return['abook_id'] Address book ID 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
2012-11-01 04:42:20 +00:00
$ret = zot_finger($url,$channel,false);
if($ret['success']) {
$j = json_decode($ret['body']);
2012-11-01 04:42:20 +00:00
}
2012-11-01 08:54:51 +00:00
logger('follow: ' . $url . ' ' . print_r($j,true));
2012-11-01 04:42:20 +00:00
killme();
2012-06-03 06:12:20 +00:00
2012-11-01 08:54:51 +00:00
if(! ($j->success && $j->guid)) {
$result['message'] = t('Unable to communicate with requested channel.');
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-01 08:54:51 +00:00
$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'];
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)) {
$result['message'] .= upgrade_message();
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.
$xchan_hash = base64_urlencode(hash('whirlpool',$j->quid . $j->guid_sg, true));
// FIXME - verify the signature
2012-11-01 08:54:51 +00:00
$r = q("select * from xchan where xchan_hash = '%s' limit 1",
dbesc($xchan_hash)
);
if(! $r) {
$x = q("insert into xchan ( xchan_hash, xchan_guid, xchan_guid_sig, xchan_pubkey, xchan_photo_mimetype,
xchan_photo_l, xchan_addr, xchan_url, xchan_name, xchan_network, xchan_photo_date, xchan_name_date)
values ( '%s', '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ",
dbesc($xchan_hash),
dbesc($j->guid),
dbesc($j->guid_sig),
dbesc($j->key),
dbesc($j->photo_mimetype),
dbesc($j->photo),
dbesc($j->address),
dbesc($j->url),
dbesc($j->name),
dbesc('zot'),
dbesc($j->photo_updated),
dbesc($j->name_updated)
);
2012-06-03 06:12:20 +00:00
require_once("Photo.php");
$photos = import_profile_photo($j->photo,0,$contact_id);
$r = q("update xchan set xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s' = '%s', xchan_photo_mimetype = '%s'
where xchan_hash = '%s' limit 1",
dbesc($photos[0]),
dbesc($photos[1]),
dbesc($photos[2]),
dbesc($photos[3]),
dbesc($xchan_hash)
);
}
if($j->locations) {
foreach($j->locations as $location) {
$r = q("select * from hubloc where hubloc_hash = '%s' and hubloc_url = '%s' limit 1",
dbesc($xchan_hash),
dbesc($location['url'])
);
if($r)
continue;
// FIXME verify the signature
$r = q("insert into hubloc ( hubloc_guid, hubloc_guid_sig, hubloc_hash, hubloc_addr, hubloc_flags, hubloc_url, hubloc_url_sig, hubloc_host, hubloc_callback, hubloc_sitekey)
values ( '%s','%s','%s','%s', %d ,'%s','%s','%s','%s','%s')",
dbesc($j->guid),
dbesc($j->guid_sig),
dbesc($xchan_hash),
dbesc($location['address']),
intval((intval($location['primary'])) ? HUBLOC_FLAGS_PRIMARY : 0),
dbesc($location['url']),
dbesc($location['url_sig']),
dbesc($location['host']),
dbesc($location['callback']),
dbesc($location['sitekey'])
);
}
2012-06-03 06:12:20 +00:00
}
2012-06-03 06:12:20 +00:00
2012-06-25 05:23:17 +00:00
2012-11-01 08:54:51 +00:00
// Do we already have an abook entry?
// go directly to the abook edit page.
2012-06-25 05:23:17 +00:00
// Else create an entry
// Then send a ping/message to the other side
2012-06-25 05:23:17 +00:00
2012-11-01 08:54:51 +00:00
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
}