$ret['feed_atom'] = $url;
return $ret;
}
- if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
+ if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
$ret['feed_rss'] = $url;
return $ret;
}
}
}
- try {
- $dom = HTML5_Parser::parse($s);
- } catch (DOMException $e) {
- logger('scrape_feed: parse error: ' . $e);
- }
+ $basename = implode('/', array_slice(explode('/',$url),0,3)) . '/';
- if(! $dom) {
- logger('scrape_feed: failed to parse.');
- return $ret;
- }
+ $doc = new DOMDocument();
+ @$doc->loadHTML($s);
+ $xpath = new DomXPath($doc);
+ $base = $xpath->query("//base");
+ foreach ($base as $node) {
+ $attr = array();
- $head = $dom->getElementsByTagName('base');
- if($head) {
- foreach($head as $head0) {
- $basename = $head0->getAttribute('href');
- break;
- }
+ if ($node->attributes->length)
+ foreach ($node->attributes as $attribute)
+ $attr[$attribute->name] = $attribute->value;
+
+ if ($attr["href"] != "")
+ $basename = $attr["href"] ;
}
- if(! $basename)
- $basename = implode('/', array_slice(explode('/',$url),0,3)) . '/';
- $items = $dom->getElementsByTagName('link');
+ $list = $xpath->query("//link");
+ foreach ($list as $node) {
+ $attr = array();
- // get Atom/RSS link elements, take the first one of either.
+ if ($node->attributes->length)
+ foreach ($node->attributes as $attribute)
+ $attr[$attribute->name] = $attribute->value;
- if($items) {
- foreach($items as $item) {
- $x = $item->getAttribute('rel');
- if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
- if(! x($ret,'feed_atom'))
- $ret['feed_atom'] = $item->getAttribute('href');
- }
- if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
- if(! x($ret,'feed_rss'))
- $ret['feed_rss'] = $item->getAttribute('href');
- }
- }
+ if (($attr["rel"] == "alternate") AND ($attr["type"] == "application/atom+xml"))
+ $ret["feed_atom"] = $attr["href"];
+
+ if (($attr["rel"] == "alternate") AND ($attr["type"] == "application/rss+xml"))
+ $ret["feed_rss"] = $attr["href"];
}
- // Drupal and perhaps others only provide relative URL's. Turn them into absolute.
+ // Drupal and perhaps others only provide relative URLs. Turn them into absolute.
if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
$ret['feed_atom'] = $basename . $ret['feed_atom'];
}
if($mode == PROBE_NORMAL) {
+
if(strlen($zot)) {
$s = fetch_url($zot);
if($s) {
}
}
+
if(strlen($dfrn)) {
$ret = scrape_dfrn(($hcard) ? $hcard : $dfrn);
if(is_array($ret) && x($ret,'dfrn-request')) {
if($check_feed) {
$feedret = scrape_feed(($poll) ? $poll : $url);
+
logger('probe_url: scrape_feed ' . (($poll)? $poll : $url) . ' returns: ' . print_r($feedret,true), LOGGER_DATA);
if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
$poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), LOGGER_DATA);
// Don't try and parse an empty string
- $feed->set_raw_data(($xml) ? $xml : '<?xml version="1.0" encoding="utf-8" ?><xml></xml>');
+ $feed->set_raw_data(($xml) ? $xml : '<?xml version="1.0" encoding="utf-8" ?><xml></xml>');
$feed->init();
if($feed->error())
require_once("include/Scrape.php");
require_once("include/html2bbcode.php");
+/*
+ To-Do:
+ - Move GNU Social URL schemata (http://server.tld/user/number) to http://server.tld/username
+*/
+
/*
* poco_load
*
if ($profile_url == "")
return $gcid;
+ $urlparts = parse_url($profile_url);
+ if (!isset($urlparts["scheme"]))
+ return $gcid;
+
+ if (in_array($urlparts["host"], array("facebook.com", "twitter.com", "www.twitter-rss.com",
+ "identi.ca", "alpha.app.net")))
+ return $gcid;
+
$orig_updated = $updated;
// Don't store the statusnet connector as network
$nick = end(explode("/", $urlparts["path"]));
}
- if (($network == "") OR ($name == "") OR ($profile_photo == "") OR ($server_url == "")) {
+ if ((($network == "") OR ($name == "") OR ($profile_photo == "") OR ($server_url == ""))
+ AND poco_reachable($profile_url, $server_url, $network)) {
$data = probe_url($profile_url);
$network = $data["network"];
$name = $data["name"];
poco_check_server($server_url, $network);
// Only fetch last update manually if it wasn't provided and enabled in the system
- if (get_config('system','poco_completion') AND ($orig_updated == "0000-00-00 00:00:00") AND poco_do_update($created, $updated, $last_failure, $last_contact)) {
+ if (get_config('system','poco_completion') AND ($orig_updated == "0000-00-00 00:00:00")
+ AND poco_do_update($created, $updated, $last_failure, $last_contact)
+ AND poco_reachable($profile_url, $server_url, $network)) {
$last_updated = poco_last_updated($profile_url);
if ($last_updated) {
$updated = $last_updated;
return $gcid;
}
+function poco_reachable($profile, $server = "", $network = "") {
+
+ if ($server == "")
+ $server = poco_detect_server($profile);
+
+ if ($server == "")
+ return true;
+
+ return poco_check_server($server, $network);
+}
+
+function poco_detect_server($profile) {
+
+ // Try to detect the server path based upon some known standard paths
+ $server_url = "";
+
+ if ($server_url == "") {
+ $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
+ if ($friendica != $profile) {
+ $server_url = $friendica;
+ $network = NETWORK_DFRN;
+ }
+ }
+
+ if ($server_url == "") {
+ $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
+ if ($diaspora != $profile) {
+ $server_url = $diaspora;
+ $network = NETWORK_DIASPORA;
+ }
+ }
+
+ if ($server_url == "") {
+ $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
+ if ($red != $profile) {
+ $server_url = $red;
+ $network = NETWORK_DIASPORA;
+ }
+ }
+
+ return $server_url;
+}
+
function poco_last_updated($profile) {
$gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
// If we only can poll the feed, then we only do this once a while
if (!poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"]))
-/ return $gcontacts[0]["updated"];
+ return $gcontacts[0]["updated"];
$data = probe_url($profile);
return ($val);
}
-function poco_check_server($server_url, $network = "") {
+function poco_check_server($server_url, $network = "", $force = false) {
if ($server_url == "")
return false;
$info = $servers[0]["info"];
$register_policy = $servers[0]["register_policy"];
- if (!poco_do_update($servers[0]["created"], "", $last_failure, $last_contact))
+ if (!$force AND !poco_do_update($servers[0]["created"], "", $last_failure, $last_contact)) {
+ logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
return ($last_contact >= $last_failure);
+ }
} else {
$poco = "";
$noscrape = "";
$last_contact = "0000-00-00 00:00:00";
$last_failure = "0000-00-00 00:00:00";
}
+ logger("Server ".$server_url." is unknown. Start discovery.", LOGGER_DEBUG);
$failure = false;
$orig_last_failure = $last_failure;
}
}
+ // Check again if the server exists
+ $servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
+
if ($servers)
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'",
);
else
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')",
+ 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($last_failure),
dbesc(datetime_convert())
);
+
+ logger("End discovery for server ".$server_url, LOGGER_DEBUG);
+
return !$failure;
}
function poco_discover($complete = false) {
- $last_update = date("c", time() - (60 * 60 * 24));
+ $no_of_queries = 5;
+
+ $last_update = date("c", time() - (60 * 60 * 6)); // 24
- $r = q("SELECT `poco`, `nurl` FROM `gserver` WHERE `last_contact` > `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
+ $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `last_contact` > `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
if ($r)
foreach ($r AS $server) {
+
+ if (!poco_check_server($server["url"], $server["network"]))
+ continue;
+
// Fetch all users from the other server
$url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation";
$retdata = z_fetch_url($url);
if ($retdata["success"]) {
$data = json_decode($retdata["body"]);
+
poco_discover_server($data, 2);
if (get_config('system','poco_discovery') > 1) {
// Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
$url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,generation";
+ $success = false;
+
$retdata = z_fetch_url($url);
if ($retdata["success"]) {
logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
- poco_discover_server(json_decode($retdata["body"]));
- } elseif (get_config('system','poco_discovery') > 2) {
+ $success = poco_discover_server(json_decode($retdata["body"]));
+ }
+
+ if (!$success AND (get_config('system','poco_discovery') > 2)) {
logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
- poco_discover_server_users($data);
+ poco_discover_server_users($data, $server);
}
}
q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
- if (!$complete)
+ if (!$complete AND (--$no_of_queries == 0))
break;
- }
+ } else // If the server hadn't replied correctly, then force a sanity check
+ poco_check_server($server["url"], $server["network"], true);
}
}
-function poco_discover_server_users($data) {
+function poco_discover_server_users($data, $server) {
foreach ($data->entry AS $entry) {
$username = "";
if (isset($entry->urls)) {
function poco_discover_server($data, $default_generation = 0) {
if (!isset($data->entry) OR !count($data->entry))
- return;
+ return false;
+
+ $success = false;
foreach ($data->entry AS $entry) {
$profile_url = '';
}
}
}
+
if(isset($entry->photos)) {
foreach($entry->photos as $photo) {
if($photo->type == 'profile') {
$keywords = implode(", ", $tag);
if ($generation > 0) {
+ $success = true;
+
logger("Store profile ".$profile_url, LOGGER_DEBUG);
- poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation);
+ poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, 0, 0, 0);
logger("Done for profile ".$profile_url, LOGGER_DEBUG);
}
}
+ return $success;
}
?>