streams/include/hubloc.php

311 lines
11 KiB
PHP
Raw Normal View History

<?php
/**
* @file include/hubloc.php
* @brief Hubloc related functions.
*/
2021-04-13 03:49:58 +00:00
use Zotlabs\Daemon\Run;
/**
* @brief Create an array for hubloc table and insert record.
*
* Creates an assoziative array which will be inserted into the hubloc table.
*
* @param array $arr An assoziative array with hubloc values
* @return boolean|PDOStatement
*/
2017-01-30 23:01:22 +00:00
function hubloc_store_lowlevel($arr) {
$store = [
'hubloc_guid' => ((array_key_exists('hubloc_guid',$arr)) ? $arr['hubloc_guid'] : ''),
'hubloc_guid_sig' => ((array_key_exists('hubloc_guid_sig',$arr)) ? $arr['hubloc_guid_sig'] : ''),
2018-05-24 04:48:19 +00:00
'hubloc_id_url' => ((array_key_exists('hubloc_id_url',$arr)) ? $arr['hubloc_id_url'] : ''),
2017-01-30 23:01:22 +00:00
'hubloc_hash' => ((array_key_exists('hubloc_hash',$arr)) ? $arr['hubloc_hash'] : ''),
'hubloc_addr' => ((array_key_exists('hubloc_addr',$arr)) ? $arr['hubloc_addr'] : ''),
'hubloc_network' => ((array_key_exists('hubloc_network',$arr)) ? $arr['hubloc_network'] : ''),
'hubloc_flags' => ((array_key_exists('hubloc_flags',$arr)) ? $arr['hubloc_flags'] : 0),
'hubloc_status' => ((array_key_exists('hubloc_status',$arr)) ? $arr['hubloc_status'] : 0),
'hubloc_url' => ((array_key_exists('hubloc_url',$arr)) ? $arr['hubloc_url'] : ''),
'hubloc_url_sig' => ((array_key_exists('hubloc_url_sig',$arr)) ? $arr['hubloc_url_sig'] : ''),
'hubloc_site_id' => ((array_key_exists('hubloc_site_id',$arr)) ? $arr['hubloc_site_id'] : ''),
2017-01-30 23:01:22 +00:00
'hubloc_host' => ((array_key_exists('hubloc_host',$arr)) ? $arr['hubloc_host'] : ''),
'hubloc_callback' => ((array_key_exists('hubloc_callback',$arr)) ? $arr['hubloc_callback'] : ''),
'hubloc_connect' => ((array_key_exists('hubloc_connect',$arr)) ? $arr['hubloc_connect'] : ''),
'hubloc_sitekey' => ((array_key_exists('hubloc_sitekey',$arr)) ? $arr['hubloc_sitekey'] : ''),
'hubloc_updated' => ((array_key_exists('hubloc_updated',$arr)) ? $arr['hubloc_updated'] : NULL_DATE),
'hubloc_connected' => ((array_key_exists('hubloc_connected',$arr)) ? $arr['hubloc_connected'] : NULL_DATE),
'hubloc_primary' => ((array_key_exists('hubloc_primary',$arr)) ? $arr['hubloc_primary'] : 0),
'hubloc_orphancheck' => ((array_key_exists('hubloc_orphancheck',$arr)) ? $arr['hubloc_orphancheck'] : 0),
'hubloc_error' => ((array_key_exists('hubloc_error',$arr)) ? $arr['hubloc_error'] : 0),
'hubloc_deleted' => ((array_key_exists('hubloc_deleted',$arr)) ? $arr['hubloc_deleted'] : 0)
];
return create_table_from_array('hubloc', $store);
2017-01-30 23:01:22 +00:00
}
function site_store_lowlevel($arr) {
$store = [
'site_url' => ((array_key_exists('site_url',$arr)) ? $arr['site_url'] : ''),
'site_access' => ((array_key_exists('site_access',$arr)) ? $arr['site_access'] : 0),
'site_flags' => ((array_key_exists('site_flags',$arr)) ? $arr['site_flags'] : 0),
'site_update' => ((array_key_exists('site_update',$arr)) ? $arr['site_update'] : NULL_DATE),
'site_pull' => ((array_key_exists('site_pull',$arr)) ? $arr['site_pull'] : NULL_DATE),
'site_sync' => ((array_key_exists('site_sync',$arr)) ? $arr['site_sync'] : NULL_DATE),
'site_directory' => ((array_key_exists('site_directory',$arr)) ? $arr['site_directory'] : ''),
'site_register' => ((array_key_exists('site_register',$arr)) ? $arr['site_register'] : 0),
'site_sellpage' => ((array_key_exists('site_sellpage',$arr)) ? $arr['site_sellpage'] : ''),
'site_location' => ((array_key_exists('site_location',$arr)) ? $arr['site_location'] : ''),
'site_realm' => ((array_key_exists('site_realm',$arr)) ? $arr['site_realm'] : ''),
'site_valid' => ((array_key_exists('site_valid',$arr)) ? $arr['site_valid'] : 0),
'site_dead' => ((array_key_exists('site_dead',$arr)) ? $arr['site_dead'] : 0),
'site_type' => ((array_key_exists('site_type',$arr)) ? $arr['site_type'] : 0),
'site_project' => ((array_key_exists('site_project',$arr)) ? $arr['site_project'] : ''),
'site_version' => ((array_key_exists('site_version',$arr)) ? $arr['site_version'] : ''),
'site_crypto' => ((array_key_exists('site_crypto',$arr)) ? $arr['site_crypto'] : '')
];
return create_table_from_array('site', $store);
}
2017-01-30 23:01:22 +00:00
function prune_hub_reinstalls() {
$r = q("select site_url from site where site_type = %d",
intval(SITE_TYPE_ZOT)
);
if($r) {
foreach($r as $rr) {
$x = q("select count(*) as t, hubloc_sitekey, max(hubloc_connected) as c from hubloc where hubloc_url = '%s' group by hubloc_sitekey order by c",
dbesc($rr['site_url'])
);
// see if this url has more than one sitekey, indicating it has been re-installed.
if(count($x) > 1) {
$d1 = datetime_convert('UTC', 'UTC', $x[0]['c']);
$d2 = datetime_convert('UTC', 'UTC', 'now - 3 days');
// allow some slop period, say 3 days - just in case this is a glitch or transient occurrence
2014-03-20 02:56:21 +00:00
// Then remove any hublocs pointing to the oldest entry.
if(($d1 < $d2) && ($x[0]['hubloc_sitekey'])) {
logger('prune_hub_reinstalls: removing dead hublocs at ' . $rr['site_url']);
$y = q("delete from hubloc where hubloc_sitekey = '%s'",
dbesc($x[0]['hubloc_sitekey'])
);
}
}
}
}
}
/**
* @brief Remove obsolete hublocs.
*
* 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.
*/
function remove_obsolete_hublocs() {
logger('remove_obsolete_hublocs', LOGGER_DEBUG);
// 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;
// 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_deleted = 1 where hubloc_id = %d",
intval($rr['hubloc_id'])
);
$x = q("select channel_id from channel where channel_hash = '%s' limit 1",
dbesc($rr['hubloc_hash'])
);
if($x) {
2021-04-13 03:49:58 +00:00
Run::Summon( [ 'Notifier', 'refresh_all', $x[0]['channel_id'] ] );
if($interval)
@time_sleep_until(microtime(true) + (float) $interval);
}
}
}
/**
* @brief Change primary hubloc.
*
* This actually changes other structures to match the given (presumably current)
* hubloc primary selection.
*
* @param array $hubloc
* @return boolean
*/
2014-10-13 22:27:03 +00:00
function hubloc_change_primary($hubloc) {
2014-10-14 00:59:03 +00:00
if(! is_array($hubloc)) {
logger('no hubloc');
2014-10-13 22:27:03 +00:00
return false;
2014-10-14 00:59:03 +00:00
}
2018-10-31 04:28:51 +00:00
logger('setting primary: ' . $hubloc['hubloc_url'] . ((intval($hubloc['hubloc_primary'])) ? ' true' : ' false'));
2014-10-13 22:27:03 +00:00
2018-10-31 04:28:51 +00:00
// See if this is a local hubloc and if so update the primary for the corresponding channel record.
2014-10-13 22:27:03 +00:00
2018-10-31 04:28:51 +00:00
if($hubloc['hubloc_url'] === z_root()) {
$r = q("select channel_id from channel where channel_hash = '%s' limit 1",
dbesc($hubloc['hubloc_hash'])
);
if($r) {
q("update channel set channel_primary = %d where channel_id = %d",
intval($hubloc['hubloc_primary']),
2016-03-09 23:56:51 +00:00
intval($r[0]['channel_id'])
);
}
2014-10-13 22:27:03 +00:00
}
2018-10-31 04:28:51 +00:00
// we only need to proceed further if this particular hubloc is now primary
if(! (intval($hubloc['hubloc_primary']))) {
logger('not primary: ' . $hubloc['hubloc_url']);
return false;
}
2014-10-13 22:27:03 +00:00
// do we even have an xchan for this hubloc and if so is it already set as primary?
$r = q("select * from xchan where xchan_hash = '%s' limit 1",
dbesc($hubloc['hubloc_hash'])
);
2014-10-14 00:59:03 +00:00
if(! $r) {
logger('xchan not found');
2014-10-13 22:27:03 +00:00
return false;
2014-10-14 00:59:03 +00:00
}
if($r[0]['xchan_addr'] === $hubloc['hubloc_addr']) {
logger('xchan already changed');
2014-10-13 22:27:03 +00:00
return false;
2014-10-14 00:59:03 +00:00
}
2014-10-13 22:27:03 +00:00
$url = $hubloc['hubloc_url'];
$lwebbie = substr($hubloc['hubloc_addr'], 0, strpos($hubloc['hubloc_addr'], '@'));
2014-10-13 22:27:03 +00:00
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
$r = q("update xchan set xchan_addr = '%s', xchan_url = '%s', xchan_follow = '%s', xchan_connurl = '%s' where xchan_hash = '%s'",
2014-10-13 22:27:03 +00:00
dbesc($hubloc['hubloc_addr']),
dbesc($url . '/channel/' . $lwebbie),
dbesc($url . '/follow?f=&url=%s'),
dbesc($url . '/poco/' . $lwebbie),
dbesc($hubloc['hubloc_hash'])
);
2014-10-14 00:59:03 +00:00
if(! $r)
logger('xchan_update failed.');
2014-10-13 22:27:03 +00:00
logger('primary hubloc changed.' . print_r($hubloc, true), LOGGER_DEBUG);
2014-10-13 22:27:03 +00:00
return true;
}
/**
* @brief Mark a hubloc as down.
*
* We use the post url to distinguish between http and https hublocs.
* The https might be alive, and the http dead.
* Also set site_dead for the corresponding entry in the site table
*
* @param string $posturl Hubloc callback url which to disable
*/
function hubloc_mark_as_down($posturl) {
$r = q("update hubloc set hubloc_status = ( hubloc_status | %d ) where hubloc_callback = '%s'",
intval(HUBLOC_OFFLINE),
dbesc($posturl)
);
// extract the baseurl and set site.site_dead to match
$m = parse_url($posturl);
$h = $m['scheme'] . '://' . $m['host'];
$r = q("update site set site_dead = 1 where site_url = '%s'",
dbesc($h)
);
}
2014-10-24 02:33:47 +00:00
/**
* @brief return comma separated string of non-dead clone locations (net addresses) for a given netid
*
* @param string $netid network identity (typically xchan_hash or hubloc_hash)
* @return string
*/
function locations_by_netid($netid) {
$locs = q("select hubloc_addr as location from hubloc left join site on hubloc_url = site_url where hubloc_hash = '%s' and hubloc_deleted = 0 and site_dead = 0",
dbesc($netid)
);
return array_elm_to_str($locs,'location',', ','trim_and_unpunify');
}
function ping_site($url) {
$ret = array('success' => false);
2018-06-01 02:42:13 +00:00
$r = Zotlabs\Lib\Zotfinger::exec($url);
if(! $r['data']) {
$ret['message'] = 'no answer from ' . $url;
return $ret;
}
2018-06-01 02:42:13 +00:00
$ret['success'] = true;
2015-09-15 02:16:25 +00:00
return $ret;
}
2019-04-16 04:55:59 +00:00
function get_hubloc_addrs_by_hash($hash) {
return q("select hubloc_addr from hubloc where hubloc_hash = '%s' and hubloc_deleted = 0",
dbesc($hash)
);
}