X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FNetwork%2FProbe.php;h=5f665814b4d3cda1c48e924e2c47e6c80dd04b0e;hb=f60da34357b93a0dfa18bd49856f56eb9bdfd160;hp=e3130a4d753991e322b9df579fd1d6550fa4f835;hpb=1eb7c19c1e2018e183baa503a873d3a538a31758;p=friendica.git diff --git a/src/Network/Probe.php b/src/Network/Probe.php index e3130a4d75..5f665814b4 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -20,13 +20,12 @@ use Friendica\Protocol\Feed; use Friendica\Util\Crypto; use Friendica\Util\Network; use Friendica\Util\XML; - +use Friendica\Util\DateTimeFormat; use dba; use DOMXPath; use DOMDocument; require_once 'include/dba.php'; -require_once 'include/network.php'; /** * @brief This class contain functions for probing URL @@ -91,6 +90,9 @@ class Probe /** * @brief Probes for webfinger path via "host-meta" * + * We have to check if the servers in the future still will offer this. + * It seems as if it was dropped from the standard. + * * @param string $host The host part of an url * * @return array with template and type of the webfinger template for JSON or XML @@ -108,21 +110,21 @@ class Probe logger("Probing for ".$host, LOGGER_DEBUG); - $ret = Network::zFetchURL($ssl_url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']); + $ret = Network::curl($ssl_url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']); if ($ret['success']) { $xml = $ret['body']; - $xrd = parse_xml_string($xml, false); + $xrd = XML::parseString($xml, false); $host_url = 'https://'.$host; } if (!is_object($xrd)) { - $ret = Network::zFetchURL($url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']); + $ret = Network::curl($url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']); if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { logger("Probing timeout for ".$url, LOGGER_DEBUG); return false; } $xml = $ret['body']; - $xrd = parse_xml_string($xml, false); + $xrd = XML::parseString($xml, false); $host_url = 'http://'.$host; } if (!is_object($xrd)) { @@ -210,7 +212,7 @@ class Probe /** * @brief Check an URI for LRDD data * - * this is a replacement for the "lrdd" function in include/network.php. + * this is a replacement for the "lrdd" function. * It isn't used in this class and has some redundancies in the code. * When time comes we can check the existing calls for "lrdd" if we can rework them. * @@ -332,7 +334,7 @@ class Probe } if (x($data, "photo")) { - $data["baseurl"] = matching_url(normalise_link($data["baseurl"]), normalise_link($data["photo"])); + $data["baseurl"] = Network::getUrlMatch(normalise_link($data["baseurl"]), normalise_link($data["photo"])); } else { $data["photo"] = System::baseUrl().'/images/person-175.jpg'; } @@ -355,11 +357,11 @@ class Probe } } - if (self::$baseurl != "") { + if (!empty(self::$baseurl)) { $data["baseurl"] = self::$baseurl; } - if (!isset($data["network"])) { + if (empty($data["network"])) { $data["network"] = NETWORK_PHANTOM; } @@ -393,6 +395,12 @@ class Probe 'network' => $data['network'], 'server_url' => $data['baseurl']]; + // This doesn't cover the case when a community isn't a community anymore + if (!empty($data['community']) && $data['community']) { + $fields['community'] = $data['community']; + $fields['contact-type'] = ACCOUNT_TYPE_COMMUNITY; + } + $fieldnames = []; foreach ($fields as $key => $val) { @@ -409,6 +417,17 @@ class Probe $old_fields = dba::selectFirst('gcontact', $fieldnames, $condition); + // When the gcontact doesn't exist, the value "true" will trigger an insert. + // In difference to the public contacts we want to have every contact + // in the world in our global contacts. + if (!$old_fields) { + $old_fields = true; + + // These values have to be set only on insert + $fields['photo'] = $data['photo']; + $fields['created'] = DateTimeFormat::utcNow(); + } + dba::update('gcontact', $fields, $condition, $old_fields); $fields = ['name' => $data['name'], @@ -426,7 +445,10 @@ class Probe 'confirm' => $data['confirm'], 'poco' => $data['poco'], 'network' => $data['network'], - 'success_update' => DBM::date()]; + 'pubkey' => $data['pubkey'], + 'priority' => $data['priority'], + 'writable' => true, + 'rel' => CONTACT_IS_SHARING]; $fieldnames = []; @@ -440,8 +462,16 @@ class Probe $condition = ['nurl' => normalise_link($data["url"]), 'self' => false, 'uid' => 0]; + // "$old_fields" will return a "false" when the contact doesn't exist. + // This won't trigger an insert. This is intended, since we only need + // public contacts for everyone we store items from. + // We don't need to store every contact on the planet. $old_fields = dba::selectFirst('contact', $fieldnames, $condition); + $fields['name-date'] = DateTimeFormat::utcNow(); + $fields['uri-date'] = DateTimeFormat::utcNow(); + $fields['success_update'] = DateTimeFormat::utcNow(); + dba::update('contact', $fields, $condition, $old_fields); } } @@ -650,7 +680,7 @@ class Probe $result = self::ostatus($webfinger); } if ((!$result && ($network == "")) || ($network == NETWORK_PUMPIO)) { - $result = self::pumpio($webfinger); + $result = self::pumpio($webfinger, $addr); } if ((!$result && ($network == "")) || ($network == NETWORK_FEED)) { $result = self::feed($uri); @@ -674,7 +704,6 @@ class Probe $result["baseurl"] = substr($result["url"], 0, $pos).$host; } } - return $result; } @@ -693,7 +722,7 @@ class Probe $xrd_timeout = Config::get('system', 'xrd_timeout', 20); $redirects = 0; - $ret = Network::zFetchURL($url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => $type]); + $ret = Network::curl($url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => $type]); if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { return false; } @@ -709,7 +738,7 @@ class Probe } // If it is not JSON, maybe it is XML - $xrd = parse_xml_string($data, false); + $xrd = XML::parseString($data, false); if (!is_object($xrd)) { logger("No webfinger data retrievable for ".$url, LOGGER_DEBUG); return false; @@ -760,7 +789,7 @@ class Probe */ private static function pollNoscrape($noscrape_url, $data) { - $ret = Network::zFetchURL($noscrape_url); + $ret = Network::curl($noscrape_url); if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { return false; } @@ -960,6 +989,10 @@ class Probe } } + if (substr($webfinger["subject"], 0, 5) == "acct:") { + $data["addr"] = substr($webfinger["subject"], 5); + } + if (!isset($data["network"]) || ($hcard_url == "")) { return false; } @@ -994,7 +1027,7 @@ class Probe */ private static function pollHcard($hcard_url, $data, $dfrn = false) { - $ret = Network::zFetchURL($hcard_url); + $ret = Network::curl($hcard_url); if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { return false; } @@ -1233,7 +1266,7 @@ class Probe $pubkey = substr($pubkey, 5); } } elseif (normalise_link($pubkey) == 'http://') { - $ret = Network::zFetchURL($pubkey); + $ret = Network::curl($pubkey); if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { return false; } @@ -1265,7 +1298,7 @@ class Probe } // Fetch all additional data from the feed - $ret = Network::zFetchURL($data["poll"]); + $ret = Network::curl($data["poll"]); if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { return false; } @@ -1326,14 +1359,33 @@ class Probe $data = []; - // This is ugly - but pump.io doesn't seem to know a better way for it - $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue); - $pos = strpos($data["name"], chr(10)); - if ($pos) { - $data["name"] = trim(substr($data["name"], 0, $pos)); + $data["name"] = $xpath->query("//span[contains(@class, 'p-name')]")->item(0)->nodeValue; + + if ($data["name"] == '') { + // This is ugly - but pump.io doesn't seem to know a better way for it + $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue); + $pos = strpos($data["name"], chr(10)); + if ($pos) { + $data["name"] = trim(substr($data["name"], 0, $pos)); + } + } + + $data["location"] = $xpath->query("//p[contains(@class, 'p-locality')]")->item(0)->nodeValue; + + if ($data["location"] == '') { + $data["location"] = $xpath->query("//p[contains(@class, 'location')]")->item(0)->nodeValue; } - $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0); + $data["about"] = $xpath->query("//p[contains(@class, 'p-note')]")->item(0)->nodeValue; + + if ($data["about"] == '') { + $data["about"] = $xpath->query("//p[contains(@class, 'summary')]")->item(0)->nodeValue; + } + + $avatar = $xpath->query("//img[contains(@class, 'u-photo')]")->item(0); + if (!$avatar) { + $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0); + } if ($avatar) { foreach ($avatar->attributes as $attribute) { if ($attribute->name == "src") { @@ -1342,9 +1394,6 @@ class Probe } } - $data["location"] = $xpath->query("//p[@class='location']")->item(0)->nodeValue; - $data["about"] = $xpath->query("//p[@class='summary']")->item(0)->nodeValue; - return $data; } @@ -1355,7 +1404,7 @@ class Probe * * @return array pump.io data */ - private static function pumpio($webfinger) + private static function pumpio($webfinger, $addr) { $data = []; foreach ($webfinger["links"] as $link) { @@ -1393,6 +1442,13 @@ class Probe $data = array_merge($data, $profile_data); + if (($addr != '') && ($data['name'] != '')) { + $name = trim(str_replace($addr, '', $data['name'])); + if ($name != '') { + $data['name'] = $name; + } + } + return $data; } @@ -1449,7 +1505,7 @@ class Probe */ private static function feed($url, $probe = true) { - $ret = Network::zFetchURL($url); + $ret = Network::curl($url); if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { return false; } @@ -1510,33 +1566,35 @@ class Probe */ private static function mail($uri, $uid) { - if (!Network::validateEmail($uri)) { + if (!Network::isEmailDomainValid($uri)) { return false; } - if ($uid != 0) { - $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid)); - - $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid)); + if ($uid == 0) { + return false; + } - if (DBM::is_result($x) && DBM::is_result($r)) { - $mailbox = Email::constructMailboxName($r[0]); - $password = ''; - openssl_private_decrypt(hex2bin($r[0]['pass']), $password, $x[0]['prvkey']); - $mbox = Email::connect($mailbox, $r[0]['user'], $password); - if (!mbox) { - return false; - } - } + $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid)); - $msgs = Email::poll($mbox, $uri); - logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG); + $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid)); - if (!count($msgs)) { + if (DBM::is_result($x) && DBM::is_result($r)) { + $mailbox = Email::constructMailboxName($r[0]); + $password = ''; + openssl_private_decrypt(hex2bin($r[0]['pass']), $password, $x[0]['prvkey']); + $mbox = Email::connect($mailbox, $r[0]['user'], $password); + if (!$mbox) { return false; } } + $msgs = Email::poll($mbox, $uri); + logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG); + + if (!count($msgs)) { + return false; + } + $phost = substr($uri, strpos($uri, '@') + 1); $data = []; @@ -1544,7 +1602,7 @@ class Probe $data["network"] = NETWORK_MAIL; $data["name"] = substr($uri, 0, strpos($uri, '@')); $data["nick"] = $data["name"]; - $data["photo"] = Network::avatarImg($uri); + $data["photo"] = Network::lookupAvatarByEmail($uri); $data["url"] = 'mailto:'.$uri; $data["notify"] = 'smtp '.random_string(); $data["poll"] = 'email '.random_string();