aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorPaolo Tacconi <p.tacconi@giunti.it>2014-09-15 10:01:25 +0200
committerPaolo Tacconi <p.tacconi@giunti.it>2014-09-15 10:01:25 +0200
commit4f793069bf4aafcfe77d50e47690173104ef23c7 (patch)
tree7fe1bfcb18672300bf14967338690ea4fa51e640 /include
parented7f3001c1c1deec5076ae12114e5c42865b6251 (diff)
parent58aad83e5cb0edaf1ccc543147030e782c5e7ba9 (diff)
downloadvolse-hubzilla-4f793069bf4aafcfe77d50e47690173104ef23c7.tar.gz
volse-hubzilla-4f793069bf4aafcfe77d50e47690173104ef23c7.tar.bz2
volse-hubzilla-4f793069bf4aafcfe77d50e47690173104ef23c7.zip
Merge branch 'master' of https://github.com/friendica/red
Diffstat (limited to 'include')
-rw-r--r--include/ItemObject.php1
-rw-r--r--include/bb2diaspora.php7
-rwxr-xr-xinclude/diaspora.php17
-rw-r--r--include/hubloc.php73
-rwxr-xr-xinclude/items.php22
-rw-r--r--include/notifier.php29
-rw-r--r--include/poller.php3
-rw-r--r--include/zot.php425
8 files changed, 398 insertions, 179 deletions
diff --git a/include/ItemObject.php b/include/ItemObject.php
index 451e369b0..96abe4df6 100644
--- a/include/ItemObject.php
+++ b/include/ItemObject.php
@@ -250,6 +250,7 @@ class Item extends BaseObject {
'osparkle' => $osparkle,
'sparkle' => $sparkle,
'title' => $item['title'],
+ 'title_tosource' => get_pconfig($conv->get_profile_owner(),'system','title_tosource'),
'ago' => relative_date($item['created']),
'app' => $item['app'],
'str_app' => sprintf( t(' from %s'), $item['app']),
diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php
index 9c884926a..846725639 100644
--- a/include/bb2diaspora.php
+++ b/include/bb2diaspora.php
@@ -266,6 +266,10 @@ function bb2dmention_callback($match) {
function bb2diaspora_itemwallwall(&$item) {
if(($item['mid'] == $item['parent_mid']) && ($item['author_xchan'] != $item['owner_xchan']) && (is_array($item['author']))) {
+ logger('bb2diaspora_itemwallwall: author: ' . print_r($item['author'],true), LOGGER_DEBUG);
+ }
+
+ if(($item['mid'] == $item['parent_mid']) && ($item['author_xchan'] != $item['owner_xchan']) && (is_array($item['author'])) && $item['author']['url'] && $item['author']['name'] && $item['author']['photo']['src']) {
logger('bb2diaspora_itemwallwall: wall to wall post',LOGGER_DEBUG);
// post will come across with the owner's identity. Throw a preamble onto the post to indicate the true author.
$item['body'] = "\n\n"
@@ -292,7 +296,8 @@ function bb2diaspora_itembody($item) {
logger('bb2diaspora_itembody: cached ');
$newitem = $item;
$newitem['body'] = $meta['body'];
- bb2diaspora_itemwallwall($newitem);
+// this won't work - the post is now in markdown
+// bb2diaspora_itemwallwall($newitem);
return $newitem['body'];
}
}
diff --git a/include/diaspora.php b/include/diaspora.php
index 8a3eaf873..ea3c78bfe 100755
--- a/include/diaspora.php
+++ b/include/diaspora.php
@@ -142,6 +142,7 @@ function diaspora_process_outbound($arr) {
'cmd' => $cmd,
'expire' => $expire,
'mail' => $mail,
+ 'location' => $location,
'fsuggest' => $fsuggest,
'normal_mode' => $normal_mode,
'packet_type' => $packet_type,
@@ -150,6 +151,10 @@ function diaspora_process_outbound($arr) {
*/
+ if($arr['location'])
+ return;
+
+
$target_item = $arr['target_item'];
if($target_item && array_key_exists('item_flags',$target_item) && ($target_item['item_flags'] & ITEM_OBSCURED)) {
@@ -161,6 +166,7 @@ function diaspora_process_outbound($arr) {
}
+
if($arr['env_recips']) {
$hashes = array();
@@ -1966,19 +1972,16 @@ function diaspora_retraction($importer,$xml) {
if($type === 'Person') {
require_once('include/Contact.php');
- contact_remove($contact['id']);
+ contact_remove($importer['channel_id'],$contact['abook_id']);
}
elseif($type === 'Post') {
- $r = q("select * from item where guid = '%s' and uid = %d and not file like '%%[%%' limit 1",
+ $r = q("select * from item where mid = '%s' and uid = %d limit 1",
dbesc('guid'),
intval($importer['channel_id'])
);
if(count($r)) {
- if(link_compare($r[0]['author-link'],$contact['url'])) {
- q("update item set `deleted` = 1, `changed` = '%s' where `id` = %d",
- dbesc(datetime_convert()),
- intval($r[0]['id'])
- );
+ if(link_compare($r[0]['author_xchan'],$contact['xchan_hash'])) {
+ drop_item($r[0]['id'],false);
}
}
}
diff --git a/include/hubloc.php b/include/hubloc.php
index 566875ce9..cdc9de4af 100644
--- a/include/hubloc.php
+++ b/include/hubloc.php
@@ -42,4 +42,75 @@ function prune_hub_reinstalls() {
}
}
}
-} \ No newline at end of file
+}
+
+function remove_obsolete_hublocs() {
+
+ logger('remove_obsolete_hublocs',LOGGER_DEBUG);
+
+ // Get rid of any hublocs which are ours but aren't valid anymore -
+ // e.g. they point to a different and perhaps transient URL that we aren't using.
+
+ // I need to stress that this shouldn't happen. fix_system_urls() fixes hublocs
+ // when it discovers the URL has changed. So it's unclear how we could end up
+ // with URLs pointing to the old site name. But it happens. This may be an artifact
+ // of an old bug or maybe a regression in some newer code. In any event, they
+ // mess up communications and we have to take action if we find any.
+
+ // First make sure we have any hublocs (at all) with this URL and sitekey.
+ // We don't want to perform this operation while somebody is in the process
+ // of renaming their hub or installing certs.
+
+ $r = q("select hubloc_id from hubloc where hubloc_url = '%s' and hubloc_sitekey = '%s'",
+ dbesc(z_root()),
+ dbesc(get_config('system','pubkey'))
+ );
+ if((! $r) || (! count($r)))
+ return;
+
+ $channels = array();
+
+ // Good. We have at least one *valid* hubloc.
+
+ // Do we have any invalid ones?
+
+ $r = q("select hubloc_id from hubloc where hubloc_sitekey = '%s' and hubloc_url != '%s'",
+ dbesc(get_config('system','pubkey')),
+ dbesc(z_root())
+ );
+ $p = q("select hubloc_id from hubloc where hubloc_sitekey != '%s' and hubloc_url = '%s'",
+ dbesc(get_config('system','pubkey')),
+ dbesc(z_root())
+ );
+ if(is_array($r) && is_array($p))
+ $r = array_merge($r,$p);
+
+ if(! $r)
+ return;
+
+ // We've got invalid hublocs. Get rid of them.
+
+ logger('remove_obsolete_hublocs: removing ' . count($r) . ' hublocs.');
+
+ $interval = ((get_config('system','delivery_interval') !== false)
+ ? intval(get_config('system','delivery_interval')) : 2 );
+
+ foreach($r as $rr) {
+ q("update hubloc set hubloc_flags = (hubloc_flags | %d) where hubloc_id = %d limit 1",
+ intval(HUBLOC_FLAGS_DELETED),
+ intval($rr['hubloc_id'])
+ );
+
+ $x = q("select channel_id from channel where channel_hash = '%s' limit 1",
+ dbesc($rr['hubloc_hash'])
+ );
+ if($x) {
+// proc_run('php','include/notifier.php','location',$x[0]['channel_id']);
+// if($interval)
+// @time_sleep_until(microtime(true) + (float) $interval);
+ }
+ }
+}
+
+
+ \ No newline at end of file
diff --git a/include/items.php b/include/items.php
index 166303cac..beec65d8a 100755
--- a/include/items.php
+++ b/include/items.php
@@ -68,13 +68,31 @@ function collect_recipients($item,&$private_envelope) {
$private_envelope = false;
if(array_key_exists('public_policy',$item) && $item['public_policy'] !== 'self') {
- $r = q("select abook_xchan from abook where abook_channel = %d and not (abook_flags & %d) ",
+ $r = q("select abook_xchan, xchan_network from abook left join xchan on abook_xchan = xchan_hash where abook_channel = %d and not (abook_flags & %d) ",
intval($item['uid']),
intval(ABOOK_FLAG_SELF|ABOOK_FLAG_PENDING|ABOOK_FLAG_ARCHIVED)
);
if($r) {
+
+ // filter out restrictive public_policy settings from remote networks
+ // which don't have this concept and will treat them as public.
+
+ $policy = substr($item['public_policy'],0,3);
foreach($r as $rr) {
- $recipients[] = $rr['abook_xchan'];
+ switch($policy) {
+ case 'net':
+ case 'aut':
+ case 'sit':
+ case 'any':
+ case 'con':
+ if($rr['xchan_network'] != 'zot')
+ break;
+ case 'pub':
+ case '':
+ default:
+ $recipients[] = $rr['abook_xchan'];
+ break;
+ }
}
}
}
diff --git a/include/notifier.php b/include/notifier.php
index 88bb9a0cb..ae781bcf8 100644
--- a/include/notifier.php
+++ b/include/notifier.php
@@ -57,6 +57,7 @@ require_once('include/html2plain.php');
* purge_all channel_id
* expire channel_id
* relay item_id (item was relayed to owner, we will deliver it as owner)
+ * location channel_id
*
*/
@@ -144,6 +145,7 @@ function notifier_run($argv, $argc){
$mail = false;
$fsuggest = false;
$top_level = false;
+ $location = false;
$recipients = array();
$url_recipients = array();
$normal_mode = true;
@@ -230,6 +232,30 @@ function notifier_run($argv, $argc){
$private = false;
$packet_type = 'refresh';
}
+ elseif($cmd === 'location') {
+ logger('notifier: location: ' . $item_id);
+ $s = q("select * from channel where channel_id = %d limit 1",
+ intval($item_id)
+ );
+ if($s)
+ $channel = $s[0];
+ $uid = $item_id;
+ $recipients = array();
+ $r = q("select abook_xchan from abook where abook_channel = %d",
+ intval($item_id)
+ );
+ if($r) {
+ foreach($r as $rr) {
+ $recipients[] = $rr['abook_xchan'];
+ }
+ }
+
+ $encoded_item = array('locations' => zot_encode_locations($channel),'type' => 'location', 'encoding' => 'zot');
+ $target_item = array('aid' => $channel['channel_account_id'],'uid' => $channel['channel_id']);
+ $private = false;
+ $packet_type = 'location';
+ $location = true;
+ }
elseif($cmd === 'purge_all') {
logger('notifier: purge_all: ' . $item_id);
$s = q("select * from channel where channel_id = %d limit 1",
@@ -432,6 +458,8 @@ function notifier_run($argv, $argc){
$sql_extra = (($private) ? "" : " or hubloc_url = '" . dbesc(z_root()) . "' ");
+ logger('notifier: hub choice: ' . intval($relay_to_owner) . ' ' . intval($private) . ' ' . $cmd, LOGGER_DEBUG);
+
if($relay_to_owner && (! $private) && ($cmd !== 'relay')) {
// If sending a followup to the post owner, only send it to one channel clone - to avoid race conditions.
@@ -514,6 +542,7 @@ function notifier_run($argv, $argc){
'cmd' => $cmd,
'expire' => $expire,
'mail' => $mail,
+ 'location' => $location,
'fsuggest' => $fsuggest,
'normal_mode' => $normal_mode,
'packet_type' => $packet_type,
diff --git a/include/poller.php b/include/poller.php
index c90e48d6b..f689059b9 100644
--- a/include/poller.php
+++ b/include/poller.php
@@ -171,6 +171,9 @@ function poller_run($argv, $argc){
proc_run('php','include/expire.php');
proc_run('php','include/cli_suggest.php');
+ require_once('include/hubloc.php');
+ remove_obsolete_hublocs();
+
/**
* End Cron Daily
*/
diff --git a/include/zot.php b/include/zot.php
index 644d20ec2..f6732037a 100644
--- a/include/zot.php
+++ b/include/zot.php
@@ -661,6 +661,8 @@ function import_xchan($arr,$ud_flags = UPDATE_FLAGS_UPDATED, $ud_arr = null) {
}
$xchan_hash = make_xchan_hash($arr['guid'],$arr['guid_sig']);
+ $arr['hash'] = $xchan_hash;
+
$import_photos = false;
if(! rsa_verify($arr['guid'],base64url_decode($arr['guid_sig']),$arr['key'])) {
@@ -843,174 +845,16 @@ function import_xchan($arr,$ud_flags = UPDATE_FLAGS_UPDATED, $ud_arr = null) {
// what we are missing for true hub independence is for any changes in the primary hub to
// get reflected not only in the hublocs, but also to update the URLs and addr in the appropriate xchan
- if($arr['locations']) {
-
- $xisting = q("select hubloc_id, hubloc_url, hubloc_sitekey from hubloc where hubloc_hash = '%s'",
- dbesc($xchan_hash)
- );
-
- // See if a primary is specified
-
- $has_primary = false;
- foreach($arr['locations'] as $location) {
- if($location['primary']) {
- $has_primary = true;
- break;
- }
- }
-
- foreach($arr['locations'] as $location) {
- if(! rsa_verify($location['url'],base64url_decode($location['url_sig']),$arr['key'])) {
- logger('import_xchan: Unable to verify site signature for ' . $location['url']);
- $ret['message'] .= sprintf( t('Unable to verify site signature for %s'), $location['url']) . EOL;
- continue;
- }
-
- // Ensure that they have one primary hub
-
- if(! $has_primary)
- $location['primary'] = true;
-
-
- for($x = 0; $x < count($xisting); $x ++) {
- if(($xisting[$x]['hubloc_url'] === $location['url']) && ($xisting[$x]['hubloc_sitekey'] === $location['sitekey'])) {
- $xisting[$x]['updated'] = true;
- }
- }
-
- if(! $location['sitekey']) {
- logger('import_xchan: empty hubloc sitekey. ' . print_r($location,true));
- continue;
- }
-
- // Catch some malformed entries from the past which still exist
-
- if(strpos($location['address'],'/') !== false)
- $location['address'] = substr($location['address'],0,strpos($location['address'],'/'));
-
- // match as many fields as possible in case anything at all changed.
-
- $r = q("select * from hubloc where hubloc_hash = '%s' and hubloc_guid = '%s' and hubloc_guid_sig = '%s' and hubloc_url = '%s' and hubloc_url_sig = '%s' and hubloc_host = '%s' and hubloc_addr = '%s' and hubloc_callback = '%s' and hubloc_sitekey = '%s' ",
- dbesc($xchan_hash),
- dbesc($arr['guid']),
- dbesc($arr['guid_sig']),
- dbesc($location['url']),
- dbesc($location['url_sig']),
- dbesc($location['host']),
- dbesc($location['address']),
- dbesc($location['callback']),
- dbesc($location['sitekey'])
- );
- if($r) {
- logger('import_xchan: hub exists: ' . $location['url']);
- // update connection timestamp if this is the site we're talking to
- if($location['url'] == $arr['site']['url']) {
- q("update hubloc set hubloc_connected = '%s', hubloc_updated = '%s' where hubloc_id = %d limit 1",
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- intval($r[0]['hubloc_id'])
- );
- }
- if($r[0]['hubloc_status'] & HUBLOC_OFFLINE) {
- q("update hubloc set hubloc_status = (hubloc_status ^ %d) where hubloc_id = %d limit 1",
- intval(HUBLOC_OFFLINE),
- intval($r[0]['hubloc_id'])
- );
- if($r[0]['hubloc_flags'] & HUBLOC_FLAGS_ORPHANCHECK) {
- q("update hubloc set hubloc_flags = (hubloc_flags ^ %d) where hubloc_id = %d limit 1",
- intval(HUBLOC_FLAGS_ORPHANCHECK),
- intval($r[0]['hubloc_id'])
- );
- }
- q("update xchan set xchan_flags = (xchan_flags ^ %d) where (xchan_flags & %d) and xchan_hash = '%s' limit 1",
- intval(XCHAN_FLAGS_ORPHAN),
- intval(XCHAN_FLAGS_ORPHAN),
- dbesc($xchan_hash)
- );
-
- }
-
- // Remove pure duplicates
- if(count($r) > 1) {
- for($h = 1; $h < count($r); $h ++) {
- q("delete from hubloc where hubloc_id = %d limit 1",
- intval($r[$h]['hubloc_id'])
- );
- }
- }
-
- if((($r[0]['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY) && (! $location['primary']))
- || ((! ($r[0]['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY)) && ($location['primary']))) {
- $r = q("update hubloc set hubloc_flags = (hubloc_flags ^ %d), hubloc_updated = '%s' where hubloc_id = %d limit 1",
- intval(HUBLOC_FLAGS_PRIMARY),
- dbesc(datetime_convert()),
- intval($r[0]['hubloc_id'])
- );
- $what = 'primary_hub ';
- $changed = true;
- }
- if((($r[0]['hubloc_flags'] & HUBLOC_FLAGS_DELETED) && (! $location['deleted']))
- || ((! ($r[0]['hubloc_flags'] & HUBLOC_FLAGS_DELETED)) && ($location['deleted']))) {
- $r = q("update hubloc set hubloc_flags = (hubloc_flags ^ %d), hubloc_updated = '%s' where hubloc_id = %d limit 1",
- intval(HUBLOC_FLAGS_DELETED),
- dbesc(datetime_convert()),
- intval($r[0]['hubloc_id'])
- );
- $what = 'delete_hub ';
- $changed = true;
- }
- continue;
- }
- // new hub claiming to be primary. Make it so.
-
- if(intval($location['primary'])) {
- $r = q("update hubloc set hubloc_flags = (hubloc_flags ^ %d), hubloc_updated = '%s' where hubloc_hash = '%s' and (hubloc_flags & %d )",
- intval(HUBLOC_FLAGS_PRIMARY),
- dbesc(datetime_convert()),
- dbesc($xchan_hash),
- intval(HUBLOC_FLAGS_PRIMARY)
- );
- }
- logger('import_xchan: new hub: ' . $location['url']);
- $r = q("insert into hubloc ( hubloc_guid, hubloc_guid_sig, hubloc_hash, hubloc_addr, hubloc_network, hubloc_flags, hubloc_url, hubloc_url_sig, hubloc_host, hubloc_callback, hubloc_sitekey, hubloc_updated, hubloc_connected)
- values ( '%s','%s','%s','%s', '%s', %d ,'%s','%s','%s','%s','%s','%s','%s')",
- dbesc($arr['guid']),
- dbesc($arr['guid_sig']),
- dbesc($xchan_hash),
- dbesc($location['address']),
- dbesc('zot'),
- intval((intval($location['primary'])) ? HUBLOC_FLAGS_PRIMARY : 0),
- dbesc($location['url']),
- dbesc($location['url_sig']),
- dbesc($location['host']),
- dbesc($location['callback']),
- dbesc($location['sitekey']),
- dbesc(datetime_convert()),
- dbesc(datetime_convert())
- );
- $what .= 'newhub ';
- $changed = true;
-
- }
-
- // get rid of any hubs we have for this channel which weren't reported.
- // This was needed at one time to resolve complicated cross-site inconsistencies, but can cause sync conflict.
- // currently disabled.
-
-// if($xisting) {
-// foreach($xisting as $x) {
-// if(! array_key_exists('updated',$x)) {
-// logger('import_xchan: removing unreferenced hub location ' . $x['hubloc_url']);
-// $r = q("delete from hubloc where hubloc_id = %d limit 1",
-// intval($x['hubloc_id'])
-// );
-// $what .= 'removed_hub';
-// $changed = true;
-// }
-// }
-// }
+ $s = sync_locations($arr,$arr);
+ if($s) {
+ if($s['change_message'])
+ $what .= $s['change_message'];
+ if($s['changed'])
+ $changed = $s['changed'];
+ if($s['message'])
+ $ret['message'] .= $s['message'];
}
// Which entries in the update table are we interested in updating?
@@ -1189,7 +1033,8 @@ function zot_fetch($arr) {
* we will verify the sender and url in each returned message structure and also verify
* that all the messages returned match the site url that we are currently processing.
*
- * The message types handled here are 'activity' (e.g. posts), 'mail' , 'profile', and 'channel_sync'
+ * The message types handled here are 'activity' (e.g. posts), 'mail' , 'profile', 'location',
+ * and 'channel_sync'
*
* @returns array => array ( [0] => string $channel_hash, [1] => string $delivery_status, [2] => string $address )
* suitable for logging remotely, enumerating the processing results of each message/recipient combination.
@@ -1276,6 +1121,11 @@ function zot_import($arr, $sender_url) {
$deliveries = allowed_public_recips($i);
+ if($i['message'] && array_key_exists('type',$i['message']) && $i['message']['type'] === 'location') {
+ $sys = get_sys_channel();
+ $deliveries = array(array('hash' => $sys['xchan_hash']));
+ }
+
// if the scope is anything but 'public' we're going to store it as private regardless
// of the private flag on the post.
@@ -1348,7 +1198,7 @@ function zot_import($arr, $sender_url) {
}
elseif($i['message']['type'] === 'channel_sync') {
-// $arr = get_channelsync_elements($i['message']);
+ // $arr = get_channelsync_elements($i['message']);
$arr = $i['message'];
@@ -1357,6 +1207,15 @@ function zot_import($arr, $sender_url) {
$result = process_channel_sync_delivery($i['notify']['sender'],$arr,$deliveries);
}
+ elseif($i['message']['type'] === 'location') {
+ $arr = $i['message'];
+
+ logger('Location message received: ' . print_r($arr,true), LOGGER_DATA);
+ logger('Location message recipients: ' . print_r($deliveries,true), LOGGER_DATA);
+
+ $result = process_location_delivery($i['notify']['sender'],$arr,$deliveries);
+ }
+
}
if($result){
$return = array_merge($return,$result);
@@ -1800,6 +1659,236 @@ function process_profile_delivery($sender,$arr,$deliveries) {
import_directory_profile($sender['hash'],$arr,$r[0]['xchan_addr'], UPDATE_FLAGS_UPDATED, 0);
}
+function process_location_delivery($sender,$arr,$deliveries) {
+
+ // deliveries is irrelevant
+ logger('process_location_delivery', LOGGER_DEBUG);
+
+ $r = q("select xchan_pubkey from xchan where xchan_hash = '%s' limit 1",
+ dbesc($sender['hash'])
+ );
+ if($r)
+ $sender['key'] = $r[0]['xchan_pubkey'];
+
+ $x = sync_locations($sender,$arr,true);
+ logger('process_location_delivery: results: ' . print_r($x,true), LOGGER_DATA);
+}
+
+// We need to merge this code with that in the import_xchan function so as to make it
+// easier to maintain changes.
+
+function sync_locations($sender,$arr,$absolute = false) {
+
+ $ret = array();
+
+ if($arr['locations']) {
+
+ $xisting = q("select hubloc_id, hubloc_url, hubloc_sitekey from hubloc where hubloc_hash = '%s'",
+ dbesc($sender['hash'])
+ );
+
+ // See if a primary is specified
+
+ $has_primary = false;
+ foreach($arr['locations'] as $location) {
+ if($location['primary']) {
+ $has_primary = true;
+ break;
+ }
+ }
+
+ foreach($arr['locations'] as $location) {
+ if(! rsa_verify($location['url'],base64url_decode($location['url_sig']),$sender['key'])) {
+ logger('sync_locations: Unable to verify site signature for ' . $location['url']);
+ $ret['message'] .= sprintf( t('Unable to verify site signature for %s'), $location['url']) . EOL;
+ continue;
+ }
+
+ // Ensure that they have one primary hub
+
+ if(! $has_primary)
+ $location['primary'] = true;
+
+ for($x = 0; $x < count($xisting); $x ++) {
+ if(($xisting[$x]['hubloc_url'] === $location['url'])
+ && ($xisting[$x]['hubloc_sitekey'] === $location['sitekey'])) {
+ $xisting[$x]['updated'] = true;
+ }
+ }
+
+ if(! $location['sitekey']) {
+ logger('sync_locations: empty hubloc sitekey. ' . print_r($location,true));
+ continue;
+ }
+
+ // Catch some malformed entries from the past which still exist
+
+ if(strpos($location['address'],'/') !== false)
+ $location['address'] = substr($location['address'],0,strpos($location['address'],'/'));
+
+ // match as many fields as possible in case anything at all changed.
+
+ $r = q("select * from hubloc where hubloc_hash = '%s' and hubloc_guid = '%s' and hubloc_guid_sig = '%s' and hubloc_url = '%s' and hubloc_url_sig = '%s' and hubloc_host = '%s' and hubloc_addr = '%s' and hubloc_callback = '%s' and hubloc_sitekey = '%s' ",
+ dbesc($sender['hash']),
+ dbesc($sender['guid']),
+ dbesc($sender['guid_sig']),
+ dbesc($location['url']),
+ dbesc($location['url_sig']),
+ dbesc($location['host']),
+ dbesc($location['address']),
+ dbesc($location['callback']),
+ dbesc($location['sitekey'])
+ );
+ if($r) {
+ logger('sync_locations: hub exists: ' . $location['url'], LOGGER_DEBUG);
+
+ // update connection timestamp if this is the site we're talking to
+ // This only happens when called from import_xchan
+
+ if(array_key_exists('site',$arr) && $location['url'] == $arr['site']['url']) {
+ q("update hubloc set hubloc_connected = '%s', hubloc_updated = '%s' where hubloc_id = %d limit 1",
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ intval($r[0]['hubloc_id'])
+ );
+ }
+
+ // if it's marked offline/dead, bring it back
+ // Should we do this? It's basically saying that the channel knows better than
+ // the directory server if the site is alive.
+
+ if($r[0]['hubloc_status'] & HUBLOC_OFFLINE) {
+ q("update hubloc set hubloc_status = (hubloc_status ^ %d) where hubloc_id = %d limit 1",
+ intval(HUBLOC_OFFLINE),
+ intval($r[0]['hubloc_id'])
+ );
+ if($r[0]['hubloc_flags'] & HUBLOC_FLAGS_ORPHANCHECK) {
+ q("update hubloc set hubloc_flags = (hubloc_flags ^ %d) where hubloc_id = %d limit 1",
+ intval(HUBLOC_FLAGS_ORPHANCHECK),
+ intval($r[0]['hubloc_id'])
+ );
+ }
+ q("update xchan set xchan_flags = (xchan_flags ^ %d) where (xchan_flags & %d) and xchan_hash = '%s' limit 1",
+ intval(XCHAN_FLAGS_ORPHAN),
+ intval(XCHAN_FLAGS_ORPHAN),
+ dbesc($sender['hash'])
+ );
+ }
+
+ // Remove pure duplicates
+ if(count($r) > 1) {
+ for($h = 1; $h < count($r); $h ++) {
+ q("delete from hubloc where hubloc_id = %d limit 1",
+ intval($r[$h]['hubloc_id'])
+ );
+ }
+ }
+
+ if((($r[0]['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY) && (! $location['primary']))
+ || ((! ($r[0]['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY)) && ($location['primary']))) {
+ $r = q("update hubloc set hubloc_flags = (hubloc_flags ^ %d), hubloc_updated = '%s' where hubloc_id = %d limit 1",
+ intval(HUBLOC_FLAGS_PRIMARY),
+ dbesc(datetime_convert()),
+ intval($r[0]['hubloc_id'])
+ );
+ $what .= 'primary_hub ';
+ $changed = true;
+ }
+ if((($r[0]['hubloc_flags'] & HUBLOC_FLAGS_DELETED) && (! $location['deleted']))
+ || ((! ($r[0]['hubloc_flags'] & HUBLOC_FLAGS_DELETED)) && ($location['deleted']))) {
+ $r = q("update hubloc set hubloc_flags = (hubloc_flags ^ %d), hubloc_updated = '%s' where hubloc_id = %d limit 1",
+ intval(HUBLOC_FLAGS_DELETED),
+ dbesc(datetime_convert()),
+ intval($r[0]['hubloc_id'])
+ );
+ $what .= 'delete_hub ';
+ $changed = true;
+ }
+ continue;
+ }
+
+ // new hub claiming to be primary. Make it so.
+
+ if(intval($location['primary'])) {
+ $r = q("update hubloc set hubloc_flags = (hubloc_flags ^ %d), hubloc_updated = '%s' where hubloc_hash = '%s' and (hubloc_flags & %d )",
+ intval(HUBLOC_FLAGS_PRIMARY),
+ dbesc(datetime_convert()),
+ dbesc($sender['hash']),
+ intval(HUBLOC_FLAGS_PRIMARY)
+ );
+ }
+ logger('sync_locations: new hub: ' . $location['url']);
+ $r = q("insert into hubloc ( hubloc_guid, hubloc_guid_sig, hubloc_hash, hubloc_addr, hubloc_network, hubloc_flags, hubloc_url, hubloc_url_sig, hubloc_host, hubloc_callback, hubloc_sitekey, hubloc_updated, hubloc_connected)
+ values ( '%s','%s','%s','%s', '%s', %d ,'%s','%s','%s','%s','%s','%s','%s')",
+ dbesc($sender['guid']),
+ dbesc($sender['guid_sig']),
+ dbesc($sender['hash']),
+ dbesc($location['address']),
+ dbesc('zot'),
+ intval((intval($location['primary'])) ? HUBLOC_FLAGS_PRIMARY : 0),
+ dbesc($location['url']),
+ dbesc($location['url_sig']),
+ dbesc($location['host']),
+ dbesc($location['callback']),
+ dbesc($location['sitekey']),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert())
+ );
+ $what .= 'newhub ';
+ $changed = true;
+
+ }
+
+ // get rid of any hubs we have for this channel which weren't reported.
+
+ if($absolute && $xisting) {
+ foreach($xisting as $x) {
+ if(! array_key_exists('updated',$x)) {
+ logger('sync_locations: removing unreferenced hub location ' . $x['hubloc_url']);
+ $r = q("delete from hubloc where hubloc_id = %d limit 1",
+ intval($x['hubloc_id'])
+ );
+ $what .= 'removed_hub';
+ $changed = true;
+ }
+ }
+ }
+ }
+
+ $ret['change_message'] = $what;
+ $ret['changed'] = $changed;
+
+ return $ret;
+
+}
+
+
+function zot_encode_locations($channel) {
+ $ret = array();
+
+ $x = zot_get_hublocs($channel['channel_hash']);
+ if($x && count($x)) {
+ foreach($x as $hub) {
+ if(! ($hub['hubloc_flags'] & HUBLOC_FLAGS_UNVERIFIED)) {
+ $ret[] = array(
+ 'host' => $hub['hubloc_host'],
+ 'address' => $hub['hubloc_addr'],
+ 'primary' => (($hub['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY) ? true : false),
+ 'url' => $hub['hubloc_url'],
+ 'url_sig' => $hub['hubloc_url_sig'],
+ 'callback' => $hub['hubloc_callback'],
+ 'sitekey' => $hub['hubloc_sitekey'],
+ 'deleted' => (($hub['hubloc_flags'] & HUBLOC_FLAGS_DELETED) ? true : false)
+ );
+ }
+ }
+ }
+ return $ret;
+}
+
+
+
+
/*
* @function import_directory_profile