From 259f91caa9b13ed98adfaf4ac83fee56a7b1edbd Mon Sep 17 00:00:00 2001 From: Adam Magness Date: Wed, 15 Nov 2017 10:53:16 -0500 Subject: [PATCH] PortableContact created Create PortableContact and remove socgraph, update references, and calls --- include/cronjobs.php | 4 +- include/discover_poco.php | 23 +-- include/follow.php | 8 +- include/gprobe.php | 2 +- include/items.php | 5 +- include/ostatus.php | 3 +- include/pubsubpublish.php | 2 +- include/queue.php | 6 +- include/update_gcontact.php | 11 +- mod/allfriends.php | 1 - mod/contacts.php | 1 - mod/dirfind.php | 8 +- mod/hovercard.php | 1 - mod/item.php | 5 +- mod/match.php | 69 +++++---- mod/nogroup.php | 42 ++--- mod/poco.php | 4 +- mod/profiles.php | 2 - mod/settings.php | 8 +- mod/suggest.php | 1 - src/Model/GlobalContact.php | 13 +- src/Protocol/DFRN.php | 1 - src/Protocol/Diaspora.php | 1 - src/Protocol/PortableContact.php | 254 ++++++++++++++++++------------- src/Worker/OnePoll.php | 9 +- view/theme/vier/theme.php | 1 - 26 files changed, 266 insertions(+), 219 deletions(-) diff --git a/include/cronjobs.php b/include/cronjobs.php index 0b0aa7f330..2f6ade9c96 100644 --- a/include/cronjobs.php +++ b/include/cronjobs.php @@ -8,6 +8,7 @@ use Friendica\Core\Config; use Friendica\Database\DBM; use Friendica\Model\GlobalContact; use Friendica\Network\Probe; +use Friendica\Protocol\PortableContact; function cronjobs_run(&$argv, &$argc){ global $a; @@ -17,7 +18,6 @@ function cronjobs_run(&$argv, &$argc){ require_once 'mod/nodeinfo.php'; require_once 'include/photos.php'; require_once 'include/user.php'; - require_once 'include/socgraph.php'; // No parameter set? So return if ($argc <= 1) { @@ -226,7 +226,7 @@ function cron_repair_diaspora(App $a) { return; } - if (!poco_reachable($contact["url"])) { + if (!PortableContact::reachable($contact["url"])) { continue; } diff --git a/include/discover_poco.php b/include/discover_poco.php index 90d55075f8..3e374de943 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -55,7 +55,7 @@ function discover_poco_run(&$argv, &$argc) if ($mode == 8) { if ($argv[2] != "") { - poco_last_updated($argv[2], true); + PortableContact::lastUpdated($argv[2], true); } } elseif ($mode == 7) { if ($argc == 6) { @@ -65,7 +65,7 @@ function discover_poco_run(&$argv, &$argc) } PortableContact::load(intval($argv[2]), intval($argv[3]), intval($argv[4]), $url); } elseif ($mode == 6) { - poco_discover_single_server(intval($argv[2])); + PortableContact::discoverSingleServer(intval($argv[2])); } elseif ($mode == 5) { update_server(); } elseif ($mode == 4) { @@ -78,7 +78,7 @@ function discover_poco_run(&$argv, &$argc) return; } $result = "Checking server ".$server_url." - "; - $ret = poco_check_server($server_url); + $ret = PortableContact::checkServer($server_url); if ($ret) { $result .= "success"; } else { @@ -94,11 +94,12 @@ function discover_poco_run(&$argv, &$argc) gs_search_user($search); } elseif (($mode == 0) && ($search == "") && (Config::get('system', 'poco_discovery') > 0)) { // Query Friendica and Hubzilla servers for their users - poco_discover(); + PortableContact::discover(); // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server) - if (!Config::get('system', 'ostatus_disabled')) + if (!Config::get('system', 'ostatus_disabled')) { GlobalContact::gsDiscover(); + } } logger('end '.$search); @@ -120,7 +121,7 @@ function update_server() { $updated = 0; foreach ($r AS $server) { - if (!poco_do_update($server["created"], "", $server["last_failure"], $server["last_contact"])) { + if (!PortableContact::updateNeeded($server["created"], "", $server["last_failure"], $server["last_contact"])) { continue; } logger('Update server status for server '.$server["url"], LOGGER_DEBUG); @@ -172,7 +173,7 @@ function discover_users() { continue; } - $server_url = poco_detect_server($user["url"]); + $server_url = PortableContact::detectServer($user["url"]); $force_update = false; if ($user["server_url"] != "") { @@ -182,7 +183,7 @@ function discover_users() { $server_url = $user["server_url"]; } - if ((($server_url == "") && ($user["network"] == NETWORK_FEED)) || $force_update || poco_check_server($server_url, $user["network"])) { + if ((($server_url == "") && ($user["network"] == NETWORK_FEED)) || $force_update || PortableContact::checkServer($server_url, $user["network"])) { logger('Check profile '.$user["url"]); Worker::add(PRIORITY_LOW, "discover_poco", "check_profile", $user["url"]); @@ -227,13 +228,13 @@ function discover_directory($search) { continue; } // Update the contact - poco_last_updated($jj->url); + PortableContact::lastUpdated($jj->url); continue; } - $server_url = poco_detect_server($jj->url); + $server_url = PortableContact::detectServer($jj->url); if ($server_url != '') { - if (!poco_check_server($server_url)) { + if (!PortableContact::checkServer($server_url)) { logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG); continue; } diff --git a/include/follow.php b/include/follow.php index 7dced71567..0f52a5bd8e 100644 --- a/include/follow.php +++ b/include/follow.php @@ -1,5 +1,7 @@ 1) && (intval($argv[1]))) { @@ -34,7 +35,7 @@ function update_gcontact_run(&$argv, &$argc) { if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) { if ($r[0]["server_url"] != "") - poco_check_server($r[0]["server_url"], $r[0]["network"]); + PortableContact::checkServer($r[0]["server_url"], $r[0]["network"]); q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact_id)); diff --git a/mod/allfriends.php b/mod/allfriends.php index 360c7d3c9e..0a6989e4d0 100644 --- a/mod/allfriends.php +++ b/mod/allfriends.php @@ -7,7 +7,6 @@ use Friendica\Core\System; use Friendica\Database\DBM; use Friendica\Model\GlobalContact; -require_once 'include/socgraph.php'; require_once 'include/Contact.php'; require_once 'include/contact_selectors.php'; require_once 'mod/contacts.php'; diff --git a/mod/contacts.php b/mod/contacts.php index 8da40152e2..10a4f6b1b8 100644 --- a/mod/contacts.php +++ b/mod/contacts.php @@ -8,7 +8,6 @@ use Friendica\Model\GlobalContact; use Friendica\Network\Probe; require_once 'include/Contact.php'; -require_once 'include/socgraph.php'; require_once 'include/contact_selectors.php'; require_once 'mod/proxy.php'; require_once 'include/Photo.php'; diff --git a/mod/dirfind.php b/mod/dirfind.php index e4e611699d..84fe6f0ff4 100644 --- a/mod/dirfind.php +++ b/mod/dirfind.php @@ -1,14 +1,16 @@ items_page = $perpage; $j->page = $a->pager['page']; foreach ($results AS $result) { - if (poco_alternate_ostatus_url($result["url"])) { + if (PortableContact::alternateOStatusUrl($result["url"])) { continue; } diff --git a/mod/hovercard.php b/mod/hovercard.php index f21db31f88..a5a41e2635 100644 --- a/mod/hovercard.php +++ b/mod/hovercard.php @@ -12,7 +12,6 @@ use Friendica\App; use Friendica\Core\Config; use Friendica\Model\GlobalContact; -require_once "include/socgraph.php"; require_once "include/Contact.php"; function hovercard_init(App $a) { diff --git a/mod/item.php b/mod/item.php index 9a725bae44..400acfc971 100644 --- a/mod/item.php +++ b/mod/item.php @@ -1133,9 +1133,8 @@ function item_content(App $a) { * * @return boolean true if replaced, false if not replaced */ -function handle_tag(App $a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $network = "") { - require_once 'include/socgraph.php'; - +function handle_tag(App $a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $network = "") +{ $replaced = false; $r = null; $tag_type = '@'; diff --git a/mod/match.php b/mod/match.php index 2824e6b71d..7258fd9c70 100644 --- a/mod/match.php +++ b/mod/match.php @@ -1,14 +1,15 @@ cmd; - $r = q("SELECT `pub_keywords`, `prv_keywords` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1", + $r = q( + "SELECT `pub_keywords`, `prv_keywords` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1", intval(local_user()) ); if (! DBM::is_result($r)) { return; } - if(! $r[0]['pub_keywords'] && (! $r[0]['prv_keywords'])) { - notice( t('No keywords to match. Please add keywords to your default profile.') . EOL); + if (! $r[0]['pub_keywords'] && (! $r[0]['prv_keywords'])) { + notice(t('No keywords to match. Please add keywords to your default profile.') . EOL); return; } $params = array(); $tags = trim($r[0]['pub_keywords'] . ' ' . $r[0]['prv_keywords']); - if($tags) { + if ($tags) { $params['s'] = $tags; - if($a->pager['page'] != 1) + if ($a->pager['page'] != 1) { $params['p'] = $a->pager['page']; + } - if(strlen(Config::get('system','directory'))) + if (strlen(Config::get('system', 'directory'))) { $x = post_url(get_server().'/msearch', $params); - else + } else { $x = post_url(System::baseUrl() . '/msearch', $params); + } $j = json_decode($x); - if($j->total) { + if ($j->total) { $a->set_pager_total($j->total); $a->set_pager_itemspage($j->items_page); } - if(count($j->results)) { - + if (count($j->results)) { $id = 0; - foreach($j->results as $jj) { + foreach ($j->results as $jj) { $match_nurl = normalise_link($jj->url); - $match = q("SELECT `nurl` FROM `contact` WHERE `uid` = '%d' AND nurl='%s' LIMIT 1", + $match = q( + "SELECT `nurl` FROM `contact` WHERE `uid` = '%d' AND nurl='%s' LIMIT 1", intval(local_user()), - dbesc($match_nurl)); + dbesc($match_nurl) + ); if (!count($match)) { $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo); @@ -102,19 +108,18 @@ function match_content(App $a) { } } - $tpl = get_markup_template('viewcontact_template.tpl'); - - $o .= replace_macros($tpl,array( - '$title' => t('Profile Match'), - '$contacts' => $entries, - '$paginate' => paginate($a), - )); + $tpl = get_markup_template('viewcontact_template.tpl'); + $o .= replace_macros( + $tpl, + array( + '$title' => t('Profile Match'), + '$contacts' => $entries, + '$paginate' => paginate($a)) + ); + } else { + info(t('No matches') . EOL); } - else { - info( t('No matches') . EOL); - } - } return $o; diff --git a/mod/nogroup.php b/mod/nogroup.php index 0486663282..e212b30dc1 100644 --- a/mod/nogroup.php +++ b/mod/nogroup.php @@ -1,45 +1,44 @@ page,'aside')) { + if (! x($a->page, 'aside')) { $a->page['aside'] = ''; } - $a->page['aside'] .= group_side('contacts','group','extended',0,$contact_id); + $a->page['aside'] .= group_side('contacts', 'group', 'extended', 0, $contact_id); } - -function nogroup_content(App $a) { - +function nogroup_content(App $a) +{ if (! local_user()) { - notice( t('Permission denied.') . EOL); + notice(t('Permission denied.') . EOL); return ''; } - require_once('include/Contact.php'); + require_once 'include/Contact.php'; $r = contacts_not_grouped(local_user()); if (DBM::is_result($r)) { $a->set_pager_total($r[0]['total']); } - $r = contacts_not_grouped(local_user(),$a->pager['start'],$a->pager['itemspage']); + $r = contacts_not_grouped(local_user(), $a->pager['start'], $a->pager['itemspage']); if (DBM::is_result($r)) { foreach ($r as $rr) { - $contact_details = get_contact_details_by_url($rr['url'], local_user(), $rr); $contacts[] = array( @@ -64,12 +63,13 @@ function nogroup_content(App $a) { } $tpl = get_markup_template("nogroup-template.tpl"); - $o .= replace_macros($tpl, array( + $o .= replace_macros( + $tpl, + array( '$header' => t('Contacts who are not members of a group'), '$contacts' => $contacts, - '$paginate' => paginate($a), - )); + '$paginate' => paginate($a)) + ); return $o; - } diff --git a/mod/poco.php b/mod/poco.php index a32972f9ee..2e2791fc1e 100644 --- a/mod/poco.php +++ b/mod/poco.php @@ -7,6 +7,7 @@ use Friendica\App; use Friendica\Core\Cache; use Friendica\Core\Config; use Friendica\Database\DBM; +use Friendica\Protocol\PortableContact; function poco_init(App $a) { $system_mode = false; @@ -32,9 +33,8 @@ function poco_init(App $a) { $global = false; if ($a->argc > 1 && $a->argv[1] === '@server') { - require_once 'include/socgraph.php'; // List of all servers that this server knows - $ret = poco_serverlist(); + $ret = PortableContact::serverlist(); header('Content-type: application/json'); echo json_encode($ret); killme(); diff --git a/mod/profiles.php b/mod/profiles.php index f3e71491f7..4263897d0b 100644 --- a/mod/profiles.php +++ b/mod/profiles.php @@ -12,7 +12,6 @@ use Friendica\Model\GlobalContact; use Friendica\Network\Probe; require_once 'include/Contact.php'; -require_once 'include/socgraph.php'; function profiles_init(App $a) { @@ -755,7 +754,6 @@ function profiles_content(App $a) { return $o; } else { - // If we don't support multi profiles, don't display this list. if (!feature_enabled(local_user(), 'multi_profiles')) { $r = q("SELECT * FROM `profile` WHERE `uid` = %d AND `is-default`=1", diff --git a/mod/settings.php b/mod/settings.php index 296e8684ca..e693f77595 100644 --- a/mod/settings.php +++ b/mod/settings.php @@ -1,14 +1,16 @@ entries) { foreach ($j->entries as $entry) { - poco_check_server($entry->url); + PortableContact::checkServer($entry->url); $url = $entry->url . '/poco'; if (! in_array($url, $done)) { @@ -591,7 +590,7 @@ class GlobalContact */ public static function fixAlternateContactAddress(&$contact) { - if (($contact["network"] == NETWORK_OSTATUS) && poco_alternate_ostatus_url($contact["url"])) { + if (($contact["network"] == NETWORK_OSTATUS) && PortableContact::alternateOStatusUrl($contact["url"])) { $data = Probe::uri($contact["url"]); if ($contact["network"] == NETWORK_OSTATUS) { logger("Fix primary url from ".$contact["url"]." to ".$data["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG); diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 0788d36f4e..86f009f31a 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -24,7 +24,6 @@ use ostatus; require_once "include/Contact.php"; require_once "include/enotify.php"; require_once "include/threads.php"; -require_once "include/socgraph.php"; require_once "include/items.php"; require_once "include/tags.php"; require_once "include/files.php"; diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 758a23db88..0c0c3bb2fa 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -27,7 +27,6 @@ require_once 'include/items.php'; require_once 'include/bb2diaspora.php'; require_once 'include/Contact.php'; require_once 'include/Photo.php'; -require_once 'include/socgraph.php'; require_once 'include/group.php'; require_once 'include/datetime.php'; require_once 'include/queue_fn.php'; diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index f884b0f283..f4f20dc086 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -17,6 +17,10 @@ use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\GlobalContact; use Friendica\Network\Probe; +use dba; +use DOMDocument; +use DomXPath; +use Exception; require_once 'include/datetime.php'; require_once 'include/network.php'; @@ -210,21 +214,21 @@ class PortableContact ); } - function poco_reachable($profile, $server = "", $network = "", $force = false) { - + public static function reachable($profile, $server = "", $network = "", $force = false) + { if ($server == "") { - $server = poco_detect_server($profile); + $server = self::detectServer($profile); } if ($server == "") { return true; } - return poco_check_server($server, $network, $force); + return self::checkServer($server, $network, $force); } - function poco_detect_server($profile) { - + public static function detectServer($profile) + { // Try to detect the server path based upon some known standard paths $server_url = ""; @@ -283,8 +287,11 @@ class PortableContact return ""; } - $r = q("SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`", - dbesc(normalise_link($server_url))); + $r = q( + "SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`", + dbesc(normalise_link($server_url)) + ); + if (DBM::is_result($r)) { return $server_url; } @@ -298,14 +305,17 @@ class PortableContact return $server_url; } - function poco_alternate_ostatus_url($url) { + public static function alternateOStatusUrl($url) + { return(preg_match("=https?://.+/user/\d+=ism", $url, $matches)); } - function poco_last_updated($profile, $force = false) { - - $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", - dbesc(normalise_link($profile))); + public static function lastUpdated($profile, $force = false) + { + $gcontacts = q( + "SELECT * FROM `gcontact` WHERE `nurl` = '%s'", + dbesc(normalise_link($profile)) + ); if (!DBM::is_result($gcontacts)) { return false; @@ -318,7 +328,7 @@ class PortableContact } if ($force) { - $server_url = normalise_link(poco_detect_server($profile)); + $server_url = normalise_link(self::detectServer($profile)); } if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) { @@ -326,7 +336,7 @@ class PortableContact } if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) { - $server_url = normalise_link(poco_detect_server($profile)); + $server_url = normalise_link(self::detectServer($profile)); } if (!in_array($gcontacts[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""))) { @@ -335,10 +345,13 @@ class PortableContact } if ($server_url != "") { - if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) { + if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) { if ($force) { - q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", - dbesc(datetime_convert()), dbesc(normalise_link($profile))); + q( + "UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", + dbesc(datetime_convert()), + dbesc(normalise_link($profile)) + ); } logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG); @@ -348,8 +361,10 @@ class PortableContact } if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) { - $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''", - dbesc(normalise_link($server_url))); + $server = q( + "SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''", + dbesc(normalise_link($server_url)) + ); if ($server) { $contact['network'] = $server[0]["network"]; @@ -360,7 +375,6 @@ class PortableContact // noscrape is really fast so we don't cache the call. if (($server_url != "") && ($gcontacts[0]["nick"] != "")) { - // Use noscrape if possible $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($server_url))); @@ -368,7 +382,6 @@ class PortableContact $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]); if ($noscraperet["success"] && ($noscraperet["body"] != "")) { - $noscrape = json_decode($noscraperet["body"], true); if (is_array($noscrape)) { @@ -418,8 +431,11 @@ class PortableContact GlobalContact::update($contact); if (trim($noscrape["updated"]) != "") { - q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'", - dbesc(datetime_convert()), dbesc(normalise_link($profile))); + q( + "UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'", + dbesc(datetime_convert()), + dbesc(normalise_link($profile)) + ); logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG); @@ -431,7 +447,7 @@ class PortableContact } // If we only can poll the feed, then we only do this once a while - if (!$force && !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) { + if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) { logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG); GlobalContact::update($contact); @@ -442,7 +458,7 @@ class PortableContact // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711) // Then check the other link and delete this one - if (($data["network"] == NETWORK_OSTATUS) && poco_alternate_ostatus_url($profile) + if (($data["network"] == NETWORK_OSTATUS) && self::alternateOStatusUrl($profile) && (normalise_link($profile) == normalise_link($data["alias"])) && (normalise_link($profile) != normalise_link($data["url"])) ) { @@ -458,7 +474,7 @@ class PortableContact $gcontact = GlobalContact::sanitize($gcontact); GlobalContact::update($gcontact); - poco_last_updated($data["url"], $force); + self::lastUpdated($data["url"], $force); } catch (Exception $e) { logger($e->getMessage(), LOGGER_DEBUG); } @@ -468,8 +484,11 @@ class PortableContact } if (($data["poll"] == "") || (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) { - q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", - dbesc(datetime_convert()), dbesc(normalise_link($profile))); + q( + "UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", + dbesc(datetime_convert()), + dbesc(normalise_link($profile)) + ); logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG); return false; @@ -484,8 +503,11 @@ class PortableContact $feedret = z_fetch_url($data["poll"]); if (!$feedret["success"]) { - q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", - dbesc(datetime_convert()), dbesc(normalise_link($profile))); + q( + "UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", + dbesc(datetime_convert()), + dbesc(normalise_link($profile)) + ); logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG); return false; @@ -518,12 +540,18 @@ class PortableContact $last_updated = NULL_DATE; } } - q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'", - dbesc(DBM::date($last_updated)), dbesc(DBM::date()), dbesc(normalise_link($profile))); + q( + "UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'", + dbesc(DBM::date($last_updated)), + dbesc(DBM::date()), + dbesc(normalise_link($profile)) + ); if (($gcontacts[0]["generation"] == 0)) { - q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'", - dbesc(normalise_link($profile))); + q( + "UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'", + dbesc(normalise_link($profile)) + ); } logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG); @@ -531,7 +559,8 @@ class PortableContact return($last_updated); } - function poco_do_update($created, $updated, $last_failure, $last_contact) { + public static function updateNeeded($created, $updated, $last_failure, $last_contact) + { $now = strtotime(datetime_convert()); if ($updated > $last_contact) { @@ -575,7 +604,8 @@ class PortableContact return true; } - function poco_to_boolean($val) { + public static function toBoolean($val) + { if (($val == "true") || ($val == 1)) { return true; } elseif (($val == "false") || ($val == 0)) { @@ -591,7 +621,8 @@ class PortableContact * @param object $data POCO data * @return array Server data */ - function poco_detect_poco_data($data) { + public static function detectPocoData($data) + { $server = false; if (!isset($data->entry)) { @@ -627,7 +658,8 @@ class PortableContact * @param string $server_url address of the server * @return array Server data */ - function poco_fetch_nodeinfo($server_url) { + public static function fetchNodeinfo($server_url) + { $serverret = z_fetch_url($server_url."/.well-known/nodeinfo"); if (!$serverret["success"]) { return false; @@ -734,7 +766,8 @@ class PortableContact * @param string $body Front page of the server * @return array Server data */ - function poco_detect_server_type($body) { + public static function detectServerType($body) + { $server = false; $doc = new DOMDocument(); @@ -790,8 +823,8 @@ class PortableContact return $server; } - function poco_check_server($server_url, $network = "", $force = false) { - + public static function checkServer($server_url, $network = "", $force = false) + { // Unify the server address $server_url = trim($server_url, "/"); $server_url = str_replace("/index.php", "", $server_url); @@ -802,10 +835,12 @@ class PortableContact $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url))); if (DBM::is_result($servers)) { - if ($servers[0]["created"] <= NULL_DATE) { - q("UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'", - dbesc(datetime_convert()), dbesc(normalise_link($server_url))); + q( + "UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'", + dbesc(datetime_convert()), + dbesc(normalise_link($server_url)) + ); } $poco = $servers[0]["poco"]; $noscrape = $servers[0]["noscrape"]; @@ -822,7 +857,7 @@ class PortableContact $info = $servers[0]["info"]; $register_policy = $servers[0]["register_policy"]; - if (!$force && !poco_do_update($servers[0]["created"], "", $last_failure, $last_contact)) { + if (!$force && !self::updateNeeded($servers[0]["created"], "", $last_failure, $last_contact)) { logger("Use cached data for server ".$server_url, LOGGER_DEBUG); return ($last_contact >= $last_failure); } @@ -862,7 +897,7 @@ class PortableContact } // Maybe the page is unencrypted only? - $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0"); + $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0"); if (!$serverret["success"] || ($serverret["body"] == "") || (@sizeof($xmlobj) == 0) || !is_object($xmlobj)) { $server_url = str_replace("https://", "http://", $server_url); @@ -876,7 +911,7 @@ class PortableContact return false; } - $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0"); + $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0"); } if (!$serverret["success"] || ($serverret["body"] == "") || (sizeof($xmlobj) == 0) || !is_object($xmlobj)) { @@ -903,7 +938,7 @@ class PortableContact $data = json_decode($serverret["body"]); if (isset($data->totalResults)) { $poco = $server_url."/poco"; - $server = poco_detect_poco_data($data); + $server = self::detectPocoData($data); if ($server) { $platform = $server['platform']; $network = $server['network']; @@ -921,7 +956,7 @@ class PortableContact if (!$serverret["success"] || ($serverret["body"] == "")) { $failure = true; } else { - $server = poco_detect_server_type($serverret["body"]); + $server = self::detectServerType($serverret["body"]); if ($server) { $platform = $server['platform']; $network = $server['network']; @@ -929,11 +964,11 @@ class PortableContact $site_name = $server['site_name']; } - $lines = explode("\n",$serverret["header"]); + $lines = explode("\n", $serverret["header"]); if (count($lines)) { - foreach($lines as $line) { + foreach ($lines as $line) { $line = trim($line); - if (stristr($line,'X-Diaspora-Version:')) { + if (stristr($line, 'X-Diaspora-Version:')) { $platform = "Diaspora"; $version = trim(str_replace("X-Diaspora-Version:", "", $line)); $version = trim(str_replace("x-diaspora-version:", "", $version)); @@ -942,7 +977,7 @@ class PortableContact $version = $versionparts[0]; } - if (stristr($line,'Server: Mastodon')) { + if (stristr($line, 'Server: Mastodon')) { $platform = "Mastodon"; $network = NETWORK_OSTATUS; } @@ -1059,9 +1094,9 @@ class PortableContact $site_name = $data->site->name; - $data->site->closed = poco_to_boolean($data->site->closed); - $data->site->private = poco_to_boolean($data->site->private); - $data->site->inviteonly = poco_to_boolean($data->site->inviteonly); + $data->site->closed = self::toBoolean($data->site->closed); + $data->site->private = self::toBoolean($data->site->private); + $data->site->inviteonly = self::toBoolean($data->site->inviteonly); if (!$data->site->closed && !$data->site->private and $data->site->inviteonly) { $register_policy = REGISTER_APPROVE; @@ -1109,7 +1144,7 @@ class PortableContact // Query nodeinfo. Working for (at least) Diaspora and Friendica. if (!$failure) { - $server = poco_fetch_nodeinfo($server_url); + $server = self::fetchNodeinfo($server_url); if ($server) { $register_policy = $server['register_policy']; @@ -1195,7 +1230,8 @@ class PortableContact $platform = strip_tags($platform); if ($servers) { - q("UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s', + q( + "UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s', `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc($server_url), dbesc($version), @@ -1211,22 +1247,23 @@ class PortableContact dbesc(normalise_link($server_url)) ); } elseif (!$failure) { - q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`) + q( + "INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`) VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')", - dbesc($server_url), - dbesc(normalise_link($server_url)), - dbesc($version), - dbesc($site_name), - dbesc($info), - intval($register_policy), - dbesc($poco), - dbesc($noscrape), - dbesc($network), - dbesc($platform), - dbesc(datetime_convert()), - dbesc($last_contact), - dbesc($last_failure), - dbesc(datetime_convert()) + dbesc($server_url), + dbesc(normalise_link($server_url)), + dbesc($version), + dbesc($site_name), + dbesc($info), + intval($register_policy), + dbesc($poco), + dbesc($noscrape), + dbesc($network), + dbesc($platform), + dbesc(datetime_convert()), + dbesc($last_contact), + dbesc($last_failure), + dbesc(datetime_convert()) ); } logger("End discovery for server " . $server_url, LOGGER_DEBUG); @@ -1238,12 +1275,18 @@ class PortableContact * @brief Returns a list of all known servers * @return array List of server urls */ - function poco_serverlist() { - $r = q("SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver` + public static function serverlist() + { + $r = q( + "SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver` WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure` ORDER BY `last_contact` LIMIT 1000", - dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS)); + dbesc(NETWORK_DFRN), + dbesc(NETWORK_DIASPORA), + dbesc(NETWORK_OSTATUS) + ); + if (!DBM::is_result($r)) { return false; } @@ -1256,7 +1299,8 @@ class PortableContact * * @param string $poco URL to the POCO endpoint */ - function poco_fetch_serverlist($poco) { + public static function fetchServerlist($poco) + { $serverret = z_fetch_url($poco."/@server"); if (!$serverret["success"]) { return; @@ -1278,8 +1322,9 @@ class PortableContact } } - function poco_discover_federation() { - $last = Config::get('poco','last_federation_discovery'); + public static function discoverFederation() + { + $last = Config::get('poco', 'last_federation_discovery'); if ($last) { $next = $last + (24 * 60 * 60); @@ -1300,7 +1345,7 @@ class PortableContact } // Disvover Mastodon servers - if (!Config::get('system','ostatus_disabled')) { + if (!Config::get('system', 'ostatus_disabled')) { $serverdata = fetch_url("https://instances.mastodon.xyz/instances.json"); if ($serverdata) { @@ -1324,14 +1369,15 @@ class PortableContact // $servers = json_decode($result["body"]); // foreach($servers->data as $server) - // poco_check_server($server->instance_address); + // self::checkServer($server->instance_address); // } //} - Config::set('poco','last_federation_discovery', time()); + Config::set('poco', 'last_federation_discovery', time()); } - function poco_discover_single_server($id) { + public static function discoverSingleServer($id) + { $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id)); if (!DBM::is_result($r)) { return false; @@ -1340,7 +1386,7 @@ class PortableContact $server = $r[0]; // Discover new servers out there (Works from Friendica version 3.5.2) - poco_fetch_serverlist($server["poco"]); + self::fetchServerlist($server["poco"]); // Fetch all users from the other server $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation"; @@ -1351,11 +1397,10 @@ class PortableContact if ($retdata["success"]) { $data = json_decode($retdata["body"]); - poco_discover_server($data, 2); + self::discoverServer($data, 2); - if (Config::get('system','poco_discovery') > 1) { - - $timeframe = Config::get('system','poco_discovery_since'); + if (Config::get('system', 'poco_discovery') > 1) { + $timeframe = Config::get('system', 'poco_discovery_since'); if ($timeframe == 0) { $timeframe = 30; } @@ -1370,12 +1415,12 @@ class PortableContact $retdata = z_fetch_url($url); if ($retdata["success"]) { logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG); - $success = poco_discover_server(json_decode($retdata["body"])); + $success = self::discoverServer(json_decode($retdata["body"])); } - if (!$success && (Config::get('system','poco_discovery') > 2)) { + if (!$success && (Config::get('system', 'poco_discovery') > 2)) { logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG); - poco_discover_server_users($data, $server); + self::discoverServerUsers($data, $server); } } @@ -1384,7 +1429,7 @@ class PortableContact return true; } else { // If the server hadn't replied correctly, then force a sanity check - poco_check_server($server["url"], $server["network"], true); + self::checkServer($server["url"], $server["network"], true); // If we couldn't reach the server, we will try it some time later q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); @@ -1393,10 +1438,10 @@ class PortableContact } } - function poco_discover($complete = false) { - + public static function discover($complete = false) + { // Update the server list - poco_discover_federation(); + self::discoverFederation(); $no_of_queries = 5; @@ -1410,8 +1455,7 @@ class PortableContact $r = q("SELECT `id`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update)); if (DBM::is_result($r)) { foreach ($r as $server) { - - if (!poco_check_server($server["url"], $server["network"])) { + if (!self::checkServer($server["url"], $server["network"])) { // The server is not reachable? Okay, then we will try it later q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); continue; @@ -1427,8 +1471,8 @@ class PortableContact } } - function poco_discover_server_users($data, $server) { - + public static function discoverServerUsers($data, $server) + { if (!isset($data->entry)) { return; } @@ -1452,14 +1496,14 @@ class PortableContact $retdata = z_fetch_url($url); if ($retdata["success"]) { - poco_discover_server(json_decode($retdata["body"]), 3); + self::discoverServer(json_decode($retdata["body"]), 3); } } } } - function poco_discover_server($data, $default_generation = 0) { - + public static function discoverServer($data, $default_generation = 0) + { if (!isset($data->entry) || !count($data->entry)) { return false; } @@ -1524,11 +1568,11 @@ class PortableContact $gender = $entry->gender; } - if(isset($entry->generation) && ($entry->generation > 0)) { + if (isset($entry->generation) && ($entry->generation > 0)) { $generation = ++$entry->generation; } - if(isset($entry->contactType) && ($entry->contactType >= 0)) { + if (isset($entry->contactType) && ($entry->contactType >= 0)) { $contact_type = $entry->contactType; } diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index e4d58f66aa..11e7d30c3f 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -1,4 +1,7 @@ $last_updated, 'last-update' => $updated, 'success_update' => $updated); @@ -117,7 +120,7 @@ Class OnePoll // Update the contact entry if (($contact['network'] === NETWORK_OSTATUS) || ($contact['network'] === NETWORK_DIASPORA) || ($contact['network'] === NETWORK_DFRN)) { - if (!poco_reachable($contact['url'])) { + if (!PortableContact::reachable($contact['url'])) { logger("Skipping probably dead contact ".$contact['url']); return; } diff --git a/view/theme/vier/theme.php b/view/theme/vier/theme.php index 525bbe9d99..40d4d9c734 100644 --- a/view/theme/vier/theme.php +++ b/view/theme/vier/theme.php @@ -17,7 +17,6 @@ use Friendica\Database\DBM; use Friendica\Model\GlobalContact; require_once "include/plugin.php"; -require_once "include/socgraph.php"; require_once "mod/proxy.php"; function vier_init(App $a) {