3 * @file src/Protocol/PortableContact.php
5 * @todo Move GNU Social URL schemata (http://server.tld/user/number) to http://server.tld/username
6 * @todo Fetch profile data from profile page for Redmatrix users
7 * @todo Detect if it is a forum
10 namespace Friendica\Protocol;
12 use Friendica\Core\Config;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBM;
15 use Friendica\Model\GContact;
16 use Friendica\Model\Profile;
17 use Friendica\Network\Probe;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Network;
25 require_once 'include/dba.php';
26 require_once 'include/html2bbcode.php';
31 * @brief Fetch POCO data
33 * @param integer $cid Contact ID
34 * @param integer $uid User ID
35 * @param integer $zcid Global Contact ID
36 * @param integer $url POCO address that should be polled
38 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
39 * and add the entries to the gcontact (Global Contact) table, or update existing entries
40 * if anything (name or photo) has changed.
41 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
43 * Once the global contact is stored add (if necessary) the contact linkage which associates
44 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
45 * pointing to the same global contact id.
48 public static function loadWorker($cid, $uid = 0, $zcid = 0, $url = null)
50 // Call the function "load" via the worker
51 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "load", (int)$cid, (int)$uid, (int)$zcid, $url);
55 * @brief Fetch POCO data from the worker
57 * @param integer $cid Contact ID
58 * @param integer $uid User ID
59 * @param integer $zcid Global Contact ID
60 * @param integer $url POCO address that should be polled
63 public static function load($cid, $uid, $zcid, $url)
69 $contact = dba::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]);
70 if (DBM::is_result($contact)) {
71 $url = $contact['poco'];
72 $uid = $contact['uid'];
84 $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation') ;
86 logger('load: ' . $url, LOGGER_DEBUG);
88 $s = Network::fetchUrl($url);
90 logger('load: returns ' . $s, LOGGER_DATA);
92 logger('load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
94 if (($a->get_curl_code() > 299) || (! $s)) {
100 logger('load: json: ' . print_r($j, true), LOGGER_DATA);
102 if (! isset($j->entry)) {
107 foreach ($j->entry as $entry) {
114 $updated = NULL_DATE;
122 $name = $entry->displayName;
124 if (isset($entry->urls)) {
125 foreach ($entry->urls as $url) {
126 if ($url->type == 'profile') {
127 $profile_url = $url->value;
130 if ($url->type == 'webfinger') {
131 $connect_url = str_replace('acct:', '', $url->value);
136 if (isset($entry->photos)) {
137 foreach ($entry->photos as $photo) {
138 if ($photo->type == 'profile') {
139 $profile_photo = $photo->value;
145 if (isset($entry->updated)) {
146 $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
149 if (isset($entry->network)) {
150 $network = $entry->network;
153 if (isset($entry->currentLocation)) {
154 $location = $entry->currentLocation;
157 if (isset($entry->aboutMe)) {
158 $about = html2bbcode($entry->aboutMe);
161 if (isset($entry->gender)) {
162 $gender = $entry->gender;
165 if (isset($entry->generation) && ($entry->generation > 0)) {
166 $generation = ++$entry->generation;
169 if (isset($entry->tags)) {
170 foreach ($entry->tags as $tag) {
171 $keywords = implode(", ", $tag);
175 if (isset($entry->contactType) && ($entry->contactType >= 0)) {
176 $contact_type = $entry->contactType;
179 $gcontact = ["url" => $profile_url,
181 "network" => $network,
182 "photo" => $profile_photo,
184 "location" => $location,
186 "keywords" => $keywords,
187 "connect" => $connect_url,
188 "updated" => $updated,
189 "contact-type" => $contact_type,
190 "generation" => $generation];
193 $gcontact = GContact::sanitize($gcontact);
194 $gcid = GContact::update($gcontact);
196 GContact::link($gcid, $uid, $cid, $zcid);
197 } catch (Exception $e) {
198 logger($e->getMessage(), LOGGER_DEBUG);
201 logger("load: loaded $total entries", LOGGER_DEBUG);
203 $condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
204 dba::delete('glink', $condition);
207 public static function reachable($profile, $server = "", $network = "", $force = false)
210 $server = self::detectServer($profile);
217 return self::checkServer($server, $network, $force);
220 public static function detectServer($profile)
222 // Try to detect the server path based upon some known standard paths
225 if ($server_url == "") {
226 $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
227 if ($friendica != $profile) {
228 $server_url = $friendica;
229 $network = NETWORK_DFRN;
233 if ($server_url == "") {
234 $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
235 if ($diaspora != $profile) {
236 $server_url = $diaspora;
237 $network = NETWORK_DIASPORA;
241 if ($server_url == "") {
242 $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
243 if ($red != $profile) {
245 $network = NETWORK_DIASPORA;
250 if ($server_url == "") {
251 $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
252 if ($mastodon != $profile) {
253 $server_url = $mastodon;
254 $network = NETWORK_OSTATUS;
258 // Numeric OStatus variant
259 if ($server_url == "") {
260 $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
261 if ($ostatus != $profile) {
262 $server_url = $ostatus;
263 $network = NETWORK_OSTATUS;
268 if ($server_url == "") {
269 $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
270 if ($base != $profile) {
272 $network = NETWORK_PHANTOM;
276 if ($server_url == "") {
281 "SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
282 dbesc(normalise_link($server_url))
285 if (DBM::is_result($r)) {
289 // Fetch the host-meta to check if this really is a server
290 $serverret = Network::curl($server_url."/.well-known/host-meta");
291 if (!$serverret["success"]) {
298 public static function alternateOStatusUrl($url)
300 return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
303 public static function lastUpdated($profile, $force = false)
306 "SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
307 dbesc(normalise_link($profile))
310 if (!DBM::is_result($gcontacts)) {
314 $contact = ["url" => $profile];
316 if ($gcontacts[0]["created"] <= NULL_DATE) {
317 $contact['created'] = DateTimeFormat::utcNow();
322 $server_url = normalise_link(self::detectServer($profile));
325 if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) {
326 $server_url = $gcontacts[0]["server_url"];
329 if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) {
330 $server_url = normalise_link(self::detectServer($profile));
333 if (!in_array($gcontacts[0]["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""])) {
334 logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
338 if ($server_url != "") {
339 if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
341 $fields = ['last_failure' => DateTimeFormat::utcNow()];
342 dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
345 logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
348 $contact['server_url'] = $server_url;
351 if (in_array($gcontacts[0]["network"], ["", NETWORK_FEED])) {
353 "SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
354 dbesc(normalise_link($server_url))
358 $contact['network'] = $server[0]["network"];
364 // noscrape is really fast so we don't cache the call.
365 if (($server_url != "") && ($gcontacts[0]["nick"] != "")) {
366 // Use noscrape if possible
367 $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($server_url)));
370 $noscraperet = Network::curl($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
372 if ($noscraperet["success"] && ($noscraperet["body"] != "")) {
373 $noscrape = json_decode($noscraperet["body"], true);
375 if (is_array($noscrape)) {
376 $contact["network"] = $server[0]["network"];
378 if (isset($noscrape["fn"])) {
379 $contact["name"] = $noscrape["fn"];
381 if (isset($noscrape["comm"])) {
382 $contact["community"] = $noscrape["comm"];
384 if (isset($noscrape["tags"])) {
385 $keywords = implode(" ", $noscrape["tags"]);
386 if ($keywords != "") {
387 $contact["keywords"] = $keywords;
391 $location = Profile::formatLocation($noscrape);
393 $contact["location"] = $location;
395 if (isset($noscrape["dfrn-notify"])) {
396 $contact["notify"] = $noscrape["dfrn-notify"];
398 // Remove all fields that are not present in the gcontact table
399 unset($noscrape["fn"]);
400 unset($noscrape["key"]);
401 unset($noscrape["homepage"]);
402 unset($noscrape["comm"]);
403 unset($noscrape["tags"]);
404 unset($noscrape["locality"]);
405 unset($noscrape["region"]);
406 unset($noscrape["country-name"]);
407 unset($noscrape["contacts"]);
408 unset($noscrape["dfrn-request"]);
409 unset($noscrape["dfrn-confirm"]);
410 unset($noscrape["dfrn-notify"]);
411 unset($noscrape["dfrn-poll"]);
413 // Set the date of the last contact
414 /// @todo By now the function "update_gcontact" doesn't work with this field
415 //$contact["last_contact"] = DateTimeFormat::utcNow();
417 $contact = array_merge($contact, $noscrape);
419 GContact::update($contact);
421 if (trim($noscrape["updated"]) != "") {
422 $fields = ['last_contact' => DateTimeFormat::utcNow()];
423 dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
425 logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
427 return $noscrape["updated"];
434 // If we only can poll the feed, then we only do this once a while
435 if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
436 logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
438 GContact::update($contact);
439 return $gcontacts[0]["updated"];
442 $data = Probe::uri($profile);
444 // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
445 // Then check the other link and delete this one
446 if (($data["network"] == NETWORK_OSTATUS) && self::alternateOStatusUrl($profile)
447 && (normalise_link($profile) == normalise_link($data["alias"]))
448 && (normalise_link($profile) != normalise_link($data["url"]))
450 // Delete the old entry
451 dba::delete('gcontact', ['nurl' => normalise_link($profile)]);
453 $gcontact = array_merge($gcontacts[0], $data);
455 $gcontact["server_url"] = $data["baseurl"];
458 $gcontact = GContact::sanitize($gcontact);
459 GContact::update($gcontact);
461 self::lastUpdated($data["url"], $force);
462 } catch (Exception $e) {
463 logger($e->getMessage(), LOGGER_DEBUG);
466 logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
470 if (($data["poll"] == "") || (in_array($data["network"], [NETWORK_FEED, NETWORK_PHANTOM]))) {
471 $fields = ['last_failure' => DateTimeFormat::utcNow()];
472 dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
474 logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
478 $contact = array_merge($contact, $data);
480 $contact["server_url"] = $data["baseurl"];
482 GContact::update($contact);
484 $feedret = Network::curl($data["poll"]);
486 if (!$feedret["success"]) {
487 $fields = ['last_failure' => DateTimeFormat::utcNow()];
488 dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
490 logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
494 $doc = new DOMDocument();
495 @$doc->loadXML($feedret["body"]);
497 $xpath = new DOMXPath($doc);
498 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
500 $entries = $xpath->query('/atom:feed/atom:entry');
504 foreach ($entries as $entry) {
505 $published = DateTimeFormat::utc($xpath->query('atom:published/text()', $entry)->item(0)->nodeValue);
506 $updated = DateTimeFormat::utc($xpath->query('atom:updated/text()' , $entry)->item(0)->nodeValue);
508 if ($last_updated < $published) {
509 $last_updated = $published;
512 if ($last_updated < $updated) {
513 $last_updated = $updated;
517 // Maybe there aren't any entries. Then check if it is a valid feed
518 if ($last_updated == "") {
519 if ($xpath->query('/atom:feed')->length > 0) {
520 $last_updated = NULL_DATE;
524 $fields = ['updated' => $last_updated, 'last_contact' => DateTimeFormat::utcNow()];
525 dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
527 if (($gcontacts[0]["generation"] == 0)) {
528 $fields = ['generation' => 9];
529 dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
532 logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
534 return $last_updated;
537 public static function updateNeeded($created, $updated, $last_failure, $last_contact)
539 $now = strtotime(DateTimeFormat::utcNow());
541 if ($updated > $last_contact) {
542 $contact_time = strtotime($updated);
544 $contact_time = strtotime($last_contact);
547 $failure_time = strtotime($last_failure);
548 $created_time = strtotime($created);
550 // If there is no "created" time then use the current time
551 if ($created_time <= 0) {
552 $created_time = $now;
555 // If the last contact was less than 24 hours then don't update
556 if (($now - $contact_time) < (60 * 60 * 24)) {
560 // If the last failure was less than 24 hours then don't update
561 if (($now - $failure_time) < (60 * 60 * 24)) {
565 // If the last contact was less than a week ago and the last failure is older than a week then don't update
566 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
569 // If the last contact time was more than a week ago and the contact was created more than a week ago, then only try once a week
570 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
574 // If the last contact time was more than a month ago and the contact was created more than a month ago, then only try once a month
575 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
582 private static function toBoolean($val)
584 if (($val == "true") || ($val == 1)) {
586 } elseif (($val == "false") || ($val == 0)) {
594 * @brief Detect server type (Hubzilla or Friendica) via the poco data
596 * @param object $data POCO data
597 * @return array Server data
599 private static function detectPocoData($data)
603 if (!isset($data->entry)) {
607 if (count($data->entry) == 0) {
611 if (!isset($data->entry[0]->urls)) {
615 if (count($data->entry[0]->urls) == 0) {
619 foreach ($data->entry[0]->urls as $url) {
620 if ($url->type == 'zot') {
622 $server["platform"] = 'Hubzilla';
623 $server["network"] = NETWORK_DIASPORA;
631 * @brief Detect server type by using the nodeinfo data
633 * @param string $server_url address of the server
634 * @return array Server data
636 private static function fetchNodeinfo($server_url)
638 $serverret = Network::curl($server_url."/.well-known/nodeinfo");
639 if (!$serverret["success"]) {
643 $nodeinfo = json_decode($serverret['body']);
645 if (!is_object($nodeinfo)) {
649 if (!is_array($nodeinfo->links)) {
656 foreach ($nodeinfo->links as $link) {
657 if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
658 $nodeinfo1_url = $link->href;
660 if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
661 $nodeinfo2_url = $link->href;
665 if ($nodeinfo1_url . $nodeinfo2_url == '') {
671 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
672 if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
673 $server = self::parseNodeinfo2($nodeinfo2_url);
676 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
677 if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
678 $server = self::parseNodeinfo1($nodeinfo1_url);
685 * @brief Parses Nodeinfo 1
687 * @param string $nodeinfo_url address of the nodeinfo path
688 * @return array Server data
690 private static function parseNodeinfo1($nodeinfo_url)
692 $serverret = Network::curl($nodeinfo_url);
693 if (!$serverret["success"]) {
697 $nodeinfo = json_decode($serverret['body']);
698 if (!is_object($nodeinfo)) {
704 $server['register_policy'] = REGISTER_CLOSED;
706 if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) {
707 $server['register_policy'] = REGISTER_OPEN;
710 if (is_object($nodeinfo->software)) {
711 if (isset($nodeinfo->software->name)) {
712 $server['platform'] = $nodeinfo->software->name;
715 if (isset($nodeinfo->software->version)) {
716 $server['version'] = $nodeinfo->software->version;
717 // Version numbers on Nodeinfo are presented with additional info, e.g.:
718 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
719 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
723 if (is_object($nodeinfo->metadata)) {
724 if (isset($nodeinfo->metadata->nodeName)) {
725 $server['site_name'] = $nodeinfo->metadata->nodeName;
729 if (!empty($nodeinfo->usage->users->total)) {
730 $server['registered-users'] = $nodeinfo->usage->users->total;
737 if (is_array($nodeinfo->protocols->inbound)) {
738 foreach ($nodeinfo->protocols->inbound as $inbound) {
739 if ($inbound == 'diaspora') {
742 if ($inbound == 'friendica') {
745 if ($inbound == 'gnusocial') {
752 $server['network'] = NETWORK_OSTATUS;
755 $server['network'] = NETWORK_DIASPORA;
758 $server['network'] = NETWORK_DFRN;
769 * @brief Parses Nodeinfo 2
771 * @param string $nodeinfo_url address of the nodeinfo path
772 * @return array Server data
774 private static function parseNodeinfo2($nodeinfo_url)
776 $serverret = Network::curl($nodeinfo_url);
777 if (!$serverret["success"]) {
781 $nodeinfo = json_decode($serverret['body']);
782 if (!is_object($nodeinfo)) {
788 $server['register_policy'] = REGISTER_CLOSED;
790 if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) {
791 $server['register_policy'] = REGISTER_OPEN;
794 if (is_object($nodeinfo->software)) {
795 if (isset($nodeinfo->software->name)) {
796 $server['platform'] = $nodeinfo->software->name;
799 if (isset($nodeinfo->software->version)) {
800 $server['version'] = $nodeinfo->software->version;
801 // Version numbers on Nodeinfo are presented with additional info, e.g.:
802 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
803 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
807 if (is_object($nodeinfo->metadata)) {
808 if (isset($nodeinfo->metadata->nodeName)) {
809 $server['site_name'] = $nodeinfo->metadata->nodeName;
813 if (!empty($nodeinfo->usage->users->total)) {
814 $server['registered-users'] = $nodeinfo->usage->users->total;
821 if (is_array($nodeinfo->protocols)) {
822 foreach ($nodeinfo->protocols as $protocol) {
823 if ($protocol == 'diaspora') {
826 if ($protocol == 'friendica') {
829 if ($protocol == 'gnusocial') {
836 $server['network'] = NETWORK_OSTATUS;
839 $server['network'] = NETWORK_DIASPORA;
842 $server['network'] = NETWORK_DFRN;
853 * @brief Detect server type (Hubzilla or Friendica) via the front page body
855 * @param string $body Front page of the server
856 * @return array Server data
858 private static function detectServerType($body)
862 $doc = new DOMDocument();
863 @$doc->loadHTML($body);
864 $xpath = new DOMXPath($doc);
866 $list = $xpath->query("//meta[@name]");
868 foreach ($list as $node) {
870 if ($node->attributes->length) {
871 foreach ($node->attributes as $attribute) {
872 $attr[$attribute->name] = $attribute->value;
875 if ($attr['name'] == 'generator') {
876 $version_part = explode(" ", $attr['content']);
877 if (count($version_part) == 2) {
878 if (in_array($version_part[0], ["Friendika", "Friendica"])) {
880 $server["platform"] = $version_part[0];
881 $server["version"] = $version_part[1];
882 $server["network"] = NETWORK_DFRN;
889 $list = $xpath->query("//meta[@property]");
891 foreach ($list as $node) {
893 if ($node->attributes->length) {
894 foreach ($node->attributes as $attribute) {
895 $attr[$attribute->name] = $attribute->value;
898 if ($attr['property'] == 'generator' && in_array($attr['content'], ["hubzilla", "BlaBlaNet"])) {
900 $server["platform"] = $attr['content'];
901 $server["version"] = "";
902 $server["network"] = NETWORK_DIASPORA;
911 $server["site_name"] = $xpath->evaluate("//head/title/text()")->item(0)->nodeValue;
915 public static function checkServer($server_url, $network = "", $force = false)
917 // Unify the server address
918 $server_url = trim($server_url, "/");
919 $server_url = str_replace("/index.php", "", $server_url);
921 if ($server_url == "") {
925 $gserver = dba::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]);
926 if (DBM::is_result($gserver)) {
927 if ($gserver["created"] <= NULL_DATE) {
928 $fields = ['created' => DateTimeFormat::utcNow()];
929 $condition = ['nurl' => normalise_link($server_url)];
930 dba::update('gserver', $fields, $condition);
932 $poco = $gserver["poco"];
933 $noscrape = $gserver["noscrape"];
935 if ($network == "") {
936 $network = $gserver["network"];
939 $last_contact = $gserver["last_contact"];
940 $last_failure = $gserver["last_failure"];
941 $version = $gserver["version"];
942 $platform = $gserver["platform"];
943 $site_name = $gserver["site_name"];
944 $info = $gserver["info"];
945 $register_policy = $gserver["register_policy"];
946 $registered_users = $gserver["registered-users"];
948 if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) {
949 logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
950 return ($last_contact >= $last_failure);
959 $register_policy = -1;
960 $registered_users = 0;
962 $last_contact = NULL_DATE;
963 $last_failure = NULL_DATE;
965 logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
968 $possible_failure = false;
969 $orig_last_failure = $last_failure;
970 $orig_last_contact = $last_contact;
972 // Mastodon uses the "@" for user profiles.
973 // But this can be misunderstood.
974 if (parse_url($server_url, PHP_URL_USER) != '') {
975 dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
979 // Check if the page is accessible via SSL.
980 $orig_server_url = $server_url;
981 $server_url = str_replace("http://", "https://", $server_url);
983 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
984 $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
986 // Quit if there is a timeout.
987 // But we want to make sure to only quit if we are mostly sure that this server url fits.
988 if (DBM::is_result($gserver) && ($orig_server_url == $server_url) &&
989 ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
990 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
991 dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
995 // Maybe the page is unencrypted only?
996 $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
997 if (!$serverret["success"] || ($serverret["body"] == "") || (@sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
998 $server_url = str_replace("https://", "http://", $server_url);
1000 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1001 $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
1003 // Quit if there is a timeout
1004 if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1005 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1006 dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
1010 $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1013 if (!$serverret["success"] || ($serverret["body"] == "") || (sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
1014 // Workaround for bad configured servers (known nginx problem)
1015 if (!in_array($serverret["debug"]["http_code"], ["403", "404"])) {
1018 $possible_failure = true;
1021 // If the server has no possible failure we reset the cached data
1022 if (!$possible_failure) {
1027 $register_policy = -1;
1031 // This will be too low, but better than no value at all.
1032 $registered_users = dba::count('gcontact', ['server_url' => normalise_link($server_url)]);
1037 $serverret = Network::curl($server_url."/poco");
1038 if ($serverret["success"]) {
1039 $data = json_decode($serverret["body"]);
1040 if (isset($data->totalResults)) {
1041 $registered_users = $data->totalResults;
1042 $poco = $server_url."/poco";
1043 $server = self::detectPocoData($data);
1045 $platform = $server['platform'];
1046 $network = $server['network'];
1051 // There are servers out there who don't return 404 on a failure
1052 // We have to be sure that don't misunderstand this
1053 if (is_null($data)) {
1062 // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1063 $serverret = Network::curl($server_url);
1065 if (!$serverret["success"] || ($serverret["body"] == "")) {
1068 $server = self::detectServerType($serverret["body"]);
1070 $platform = $server['platform'];
1071 $network = $server['network'];
1072 $version = $server['version'];
1073 $site_name = $server['site_name'];
1076 $lines = explode("\n", $serverret["header"]);
1077 if (count($lines)) {
1078 foreach ($lines as $line) {
1079 $line = trim($line);
1080 if (stristr($line, 'X-Diaspora-Version:')) {
1081 $platform = "Diaspora";
1082 $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1083 $version = trim(str_replace("x-diaspora-version:", "", $version));
1084 $network = NETWORK_DIASPORA;
1085 $versionparts = explode("-", $version);
1086 $version = $versionparts[0];
1089 if (stristr($line, 'Server: Mastodon')) {
1090 $platform = "Mastodon";
1091 $network = NETWORK_OSTATUS;
1098 if (!$failure && ($poco == "")) {
1099 // Test for Statusnet
1100 // Will also return data for Friendica and GNU Social - but it will be overwritten later
1101 // The "not implemented" is a special treatment for really, really old Friendica versions
1102 $serverret = Network::curl($server_url."/api/statusnet/version.json");
1103 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1104 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1105 $platform = "StatusNet";
1106 // Remove junk that some GNU Social servers return
1107 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1108 $version = trim($version, '"');
1109 $network = NETWORK_OSTATUS;
1112 // Test for GNU Social
1113 $serverret = Network::curl($server_url."/api/gnusocial/version.json");
1114 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1115 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1116 $platform = "GNU Social";
1117 // Remove junk that some GNU Social servers return
1118 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1119 $version = trim($version, '"');
1120 $network = NETWORK_OSTATUS;
1123 // Test for Mastodon
1124 $orig_version = $version;
1125 $serverret = Network::curl($server_url."/api/v1/instance");
1126 if ($serverret["success"] && ($serverret["body"] != '')) {
1127 $data = json_decode($serverret["body"]);
1129 if (isset($data->version)) {
1130 $platform = "Mastodon";
1131 $version = $data->version;
1132 $site_name = $data->title;
1133 $info = $data->description;
1134 $network = NETWORK_OSTATUS;
1136 if (!empty($data->stats->user_count)) {
1137 $registered_users = $data->stats->user_count;
1140 if (strstr($orig_version.$version, 'Pleroma')) {
1141 $platform = 'Pleroma';
1142 $version = trim(str_replace('Pleroma', '', $version));
1147 // Test for Hubzilla and Red
1148 $serverret = Network::curl($server_url."/siteinfo.json");
1149 if ($serverret["success"]) {
1150 $data = json_decode($serverret["body"]);
1151 if (isset($data->url)) {
1152 $platform = $data->platform;
1153 $version = $data->version;
1154 $network = NETWORK_DIASPORA;
1156 if (!empty($data->site_name)) {
1157 $site_name = $data->site_name;
1159 if (!empty($data->channels_total)) {
1160 $registered_users = $data->channels_total;
1162 switch ($data->register_policy) {
1163 case "REGISTER_OPEN":
1164 $register_policy = REGISTER_OPEN;
1166 case "REGISTER_APPROVE":
1167 $register_policy = REGISTER_APPROVE;
1169 case "REGISTER_CLOSED":
1171 $register_policy = REGISTER_CLOSED;
1175 // Test for Hubzilla, Redmatrix or Friendica
1176 $serverret = Network::curl($server_url."/api/statusnet/config.json");
1177 if ($serverret["success"]) {
1178 $data = json_decode($serverret["body"]);
1179 if (isset($data->site->server)) {
1180 if (isset($data->site->platform)) {
1181 $platform = $data->site->platform->PLATFORM_NAME;
1182 $version = $data->site->platform->STD_VERSION;
1183 $network = NETWORK_DIASPORA;
1185 if (isset($data->site->BlaBlaNet)) {
1186 $platform = $data->site->BlaBlaNet->PLATFORM_NAME;
1187 $version = $data->site->BlaBlaNet->STD_VERSION;
1188 $network = NETWORK_DIASPORA;
1190 if (isset($data->site->hubzilla)) {
1191 $platform = $data->site->hubzilla->PLATFORM_NAME;
1192 $version = $data->site->hubzilla->RED_VERSION;
1193 $network = NETWORK_DIASPORA;
1195 if (isset($data->site->redmatrix)) {
1196 if (isset($data->site->redmatrix->PLATFORM_NAME)) {
1197 $platform = $data->site->redmatrix->PLATFORM_NAME;
1198 } elseif (isset($data->site->redmatrix->RED_PLATFORM)) {
1199 $platform = $data->site->redmatrix->RED_PLATFORM;
1202 $version = $data->site->redmatrix->RED_VERSION;
1203 $network = NETWORK_DIASPORA;
1205 if (isset($data->site->friendica)) {
1206 $platform = $data->site->friendica->FRIENDICA_PLATFORM;
1207 $version = $data->site->friendica->FRIENDICA_VERSION;
1208 $network = NETWORK_DFRN;
1211 $site_name = $data->site->name;
1213 $data->site->closed = self::toBoolean($data->site->closed);
1214 $data->site->private = self::toBoolean($data->site->private);
1215 $data->site->inviteonly = self::toBoolean($data->site->inviteonly);
1217 if (!$data->site->closed && !$data->site->private and $data->site->inviteonly) {
1218 $register_policy = REGISTER_APPROVE;
1219 } elseif (!$data->site->closed && !$data->site->private) {
1220 $register_policy = REGISTER_OPEN;
1222 $register_policy = REGISTER_CLOSED;
1229 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1231 $serverret = Network::curl($server_url."/statistics.json");
1232 if ($serverret["success"]) {
1233 $data = json_decode($serverret["body"]);
1235 if (isset($data->version)) {
1236 $version = $data->version;
1237 // Version numbers on statistics.json are presented with additional info, e.g.:
1238 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1239 $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1242 if (!empty($data->name)) {
1243 $site_name = $data->name;
1246 if (!empty($data->network)) {
1247 $platform = $data->network;
1250 if ($platform == "Diaspora") {
1251 $network = NETWORK_DIASPORA;
1254 if ($data->registrations_open) {
1255 $register_policy = REGISTER_OPEN;
1257 $register_policy = REGISTER_CLOSED;
1262 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1264 $server = self::fetchNodeinfo($server_url);
1266 $register_policy = $server['register_policy'];
1268 if (isset($server['platform'])) {
1269 $platform = $server['platform'];
1272 if (isset($server['network'])) {
1273 $network = $server['network'];
1276 if (isset($server['version'])) {
1277 $version = $server['version'];
1280 if (isset($server['site_name'])) {
1281 $site_name = $server['site_name'];
1284 if (isset($server['registered-users'])) {
1285 $registered_users = $server['registered-users'];
1290 // Check for noscrape
1291 // Friendica servers could be detected as OStatus servers
1292 if (!$failure && in_array($network, [NETWORK_DFRN, NETWORK_OSTATUS])) {
1293 $serverret = Network::curl($server_url."/friendica/json");
1295 if (!$serverret["success"]) {
1296 $serverret = Network::curl($server_url."/friendika/json");
1299 if ($serverret["success"]) {
1300 $data = json_decode($serverret["body"]);
1302 if (isset($data->version)) {
1303 $network = NETWORK_DFRN;
1305 $noscrape = $data->no_scrape_url;
1306 $version = $data->version;
1307 $site_name = $data->site_name;
1308 $info = $data->info;
1309 $register_policy_str = $data->register_policy;
1310 $platform = $data->platform;
1312 switch ($register_policy_str) {
1313 case "REGISTER_CLOSED":
1314 $register_policy = REGISTER_CLOSED;
1316 case "REGISTER_APPROVE":
1317 $register_policy = REGISTER_APPROVE;
1319 case "REGISTER_OPEN":
1320 $register_policy = REGISTER_OPEN;
1327 // Every server has got at least an admin account
1328 if (!$failure && ($registered_users == 0)) {
1329 $registered_users = 1;
1332 if ($possible_failure && !$failure) {
1337 $last_contact = $orig_last_contact;
1338 $last_failure = DateTimeFormat::utcNow();
1340 $last_contact = DateTimeFormat::utcNow();
1341 $last_failure = $orig_last_failure;
1344 if (($last_contact <= $last_failure) && !$failure) {
1345 logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1346 } elseif (($last_contact >= $last_failure) && $failure) {
1347 logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1350 // Check again if the server exists
1351 $found = dba::exists('gserver', ['nurl' => normalise_link($server_url)]);
1353 $version = strip_tags($version);
1354 $site_name = strip_tags($site_name);
1355 $info = strip_tags($info);
1356 $platform = strip_tags($platform);
1358 $fields = ['url' => $server_url, 'version' => $version,
1359 'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
1360 'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
1361 'platform' => $platform, 'registered-users' => $registered_users,
1362 'last_contact' => $last_contact, 'last_failure' => $last_failure];
1365 dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
1366 } elseif (!$failure) {
1367 $fields['nurl'] = normalise_link($server_url);
1368 $fields['created'] = DateTimeFormat::utcNow();
1369 dba::insert('gserver', $fields);
1371 logger("End discovery for server " . $server_url, LOGGER_DEBUG);
1377 * @brief Returns a list of all known servers
1378 * @return array List of server urls
1380 public static function serverlist()
1383 "SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
1384 WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
1385 ORDER BY `last_contact`
1387 dbesc(NETWORK_DFRN),
1388 dbesc(NETWORK_DIASPORA),
1389 dbesc(NETWORK_OSTATUS)
1392 if (!DBM::is_result($r)) {
1400 * @brief Fetch server list from remote servers and adds them when they are new.
1402 * @param string $poco URL to the POCO endpoint
1404 private static function fetchServerlist($poco)
1406 $serverret = Network::curl($poco."/@server");
1407 if (!$serverret["success"]) {
1410 $serverlist = json_decode($serverret['body']);
1412 if (!is_array($serverlist)) {
1416 foreach ($serverlist as $server) {
1417 $server_url = str_replace("/index.php", "", $server->url);
1419 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1420 if (!DBM::is_result($r)) {
1421 logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1422 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url);
1427 private static function discoverFederation()
1429 $last = Config::get('poco', 'last_federation_discovery');
1432 $next = $last + (24 * 60 * 60);
1433 if ($next > time()) {
1438 // Discover Friendica, Hubzilla and Diaspora servers
1439 $serverdata = Network::fetchUrl("http://the-federation.info/pods.json");
1442 $servers = json_decode($serverdata);
1444 foreach ($servers->pods as $server) {
1445 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://".$server->host);
1449 // Disvover Mastodon servers
1450 if (!Config::get('system', 'ostatus_disabled')) {
1451 $accesstoken = Config::get('system', 'instances_social_key');
1452 if (!empty($accesstoken)) {
1453 $api = 'https://instances.social/api/1.0/instances/list?count=0';
1454 $header = ['Authorization: Bearer '.$accesstoken];
1455 $serverdata = Network::curl($api, false, $redirects, ['headers' => $header]);
1456 if ($serverdata['success']) {
1457 $servers = json_decode($serverdata['body']);
1458 foreach ($servers->instances as $server) {
1459 $url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
1460 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
1466 // Currently disabled, since the service isn't available anymore.
1467 // It is not removed since I hope that there will be a successor.
1468 // Discover GNU Social Servers.
1469 //if (!Config::get('system','ostatus_disabled')) {
1470 // $serverdata = "http://gstools.org/api/get_open_instances/";
1472 // $result = Network::curl($serverdata);
1473 // if ($result["success"]) {
1474 // $servers = json_decode($result["body"]);
1476 // foreach($servers->data as $server)
1477 // self::checkServer($server->instance_address);
1481 Config::set('poco', 'last_federation_discovery', time());
1484 public static function discoverSingleServer($id)
1486 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1487 if (!DBM::is_result($r)) {
1493 // Discover new servers out there (Works from Friendica version 3.5.2)
1494 self::fetchServerlist($server["poco"]);
1496 // Fetch all users from the other server
1497 $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1499 logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
1501 $retdata = Network::curl($url);
1502 if ($retdata["success"]) {
1503 $data = json_decode($retdata["body"]);
1505 self::discoverServer($data, 2);
1507 if (Config::get('system', 'poco_discovery') > 1) {
1508 $timeframe = Config::get('system', 'poco_discovery_since');
1509 if ($timeframe == 0) {
1513 $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
1515 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1516 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1520 $retdata = Network::curl($url);
1521 if ($retdata["success"]) {
1522 logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
1523 $success = self::discoverServer(json_decode($retdata["body"]));
1526 if (!$success && (Config::get('system', 'poco_discovery') > 2)) {
1527 logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
1528 self::discoverServerUsers($data, $server);
1532 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1533 dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1537 // If the server hadn't replied correctly, then force a sanity check
1538 self::checkServer($server["url"], $server["network"], true);
1540 // If we couldn't reach the server, we will try it some time later
1541 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1542 dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1548 public static function discover($complete = false)
1550 // Update the server list
1551 self::discoverFederation();
1555 $requery_days = intval(Config::get("system", "poco_requery_days"));
1557 if ($requery_days == 0) {
1560 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1562 $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));
1563 if (DBM::is_result($r)) {
1564 foreach ($r as $server) {
1565 if (!self::checkServer($server["url"], $server["network"])) {
1566 // The server is not reachable? Okay, then we will try it later
1567 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1568 dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1572 logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG);
1573 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server_directory", (int)$server['id']);
1575 if (!$complete && (--$no_of_queries == 0)) {
1582 private static function discoverServerUsers($data, $server)
1584 if (!isset($data->entry)) {
1588 foreach ($data->entry as $entry) {
1590 if (isset($entry->urls)) {
1591 foreach ($entry->urls as $url) {
1592 if ($url->type == 'profile') {
1593 $profile_url = $url->value;
1594 $urlparts = parse_url($profile_url);
1595 $username = end(explode("/", $urlparts["path"]));
1599 if ($username != "") {
1600 logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
1602 // Fetch all contacts from a given user from the other server
1603 $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1605 $retdata = Network::curl($url);
1606 if ($retdata["success"]) {
1607 self::discoverServer(json_decode($retdata["body"]), 3);
1613 private static function discoverServer($data, $default_generation = 0)
1615 if (!isset($data->entry) || !count($data->entry)) {
1621 foreach ($data->entry as $entry) {
1623 $profile_photo = '';
1627 $updated = NULL_DATE;
1633 $generation = $default_generation;
1635 $name = $entry->displayName;
1637 if (isset($entry->urls)) {
1638 foreach ($entry->urls as $url) {
1639 if ($url->type == 'profile') {
1640 $profile_url = $url->value;
1643 if ($url->type == 'webfinger') {
1644 $connect_url = str_replace('acct:' , '', $url->value);
1650 if (isset($entry->photos)) {
1651 foreach ($entry->photos as $photo) {
1652 if ($photo->type == 'profile') {
1653 $profile_photo = $photo->value;
1659 if (isset($entry->updated)) {
1660 $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
1663 if (isset($entry->network)) {
1664 $network = $entry->network;
1667 if (isset($entry->currentLocation)) {
1668 $location = $entry->currentLocation;
1671 if (isset($entry->aboutMe)) {
1672 $about = html2bbcode($entry->aboutMe);
1675 if (isset($entry->gender)) {
1676 $gender = $entry->gender;
1679 if (isset($entry->generation) && ($entry->generation > 0)) {
1680 $generation = ++$entry->generation;
1683 if (isset($entry->contactType) && ($entry->contactType >= 0)) {
1684 $contact_type = $entry->contactType;
1687 if (isset($entry->tags)) {
1688 foreach ($entry->tags as $tag) {
1689 $keywords = implode(", ", $tag);
1693 if ($generation > 0) {
1696 logger("Store profile ".$profile_url, LOGGER_DEBUG);
1698 $gcontact = ["url" => $profile_url,
1700 "network" => $network,
1701 "photo" => $profile_photo,
1703 "location" => $location,
1704 "gender" => $gender,
1705 "keywords" => $keywords,
1706 "connect" => $connect_url,
1707 "updated" => $updated,
1708 "contact-type" => $contact_type,
1709 "generation" => $generation];
1712 $gcontact = GContact::sanitize($gcontact);
1713 GContact::update($gcontact);
1714 } catch (Exception $e) {
1715 logger($e->getMessage(), LOGGER_DEBUG);
1718 logger("Done for profile ".$profile_url, LOGGER_DEBUG);