Esempio n. 1
0
function poco_last_updated($profile, $force = false)
{
    $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
    if ($gcontacts[0]["created"] == "0000-00-00 00:00:00") {
        q("UPDATE `gcontact` SET `created` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($profile)));
    }
    if ($gcontacts[0]["server_url"] != "") {
        $server_url = $gcontacts[0]["server_url"];
    } else {
        $server_url = poco_detect_server($profile);
    }
    if ($server_url != "") {
        if (!poco_check_server($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)));
            }
            return false;
        }
        q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'", dbesc($server_url), dbesc(normalise_link($profile)));
    }
    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)));
        if ($server) {
            q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'", dbesc($server[0]["network"]), dbesc(normalise_link($profile)));
        } else {
            return;
        }
    }
    // noscrape is really fast so we don't cache the call.
    if ($gcontacts[0]["server_url"] != "" and $gcontacts[0]["nick"] != "") {
        //  Use noscrape if possible
        $server = q("SELECT `noscrape` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($gcontacts[0]["server_url"])));
        if ($server) {
            $noscraperet = z_fetch_url($server[0]["noscrape"] . "/" . $gcontacts[0]["nick"]);
            if ($noscraperet["success"] and $noscraperet["body"] != "") {
                $noscrape = json_decode($noscraperet["body"], true);
                if ($noscrape["fn"] != "" and $noscrape["fn"] != $gcontacts[0]["name"]) {
                    q("UPDATE `gcontact` SET `name` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["fn"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["photo"] != "" and $noscrape["photo"] != $gcontacts[0]["photo"]) {
                    q("UPDATE `gcontact` SET `photo` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["photo"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["updated"] != "" and $noscrape["updated"] != $gcontacts[0]["updated"]) {
                    q("UPDATE `gcontact` SET `updated` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["updated"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["gender"] != "" and $noscrape["gender"] != $gcontacts[0]["gender"]) {
                    q("UPDATE `gcontact` SET `gender` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["gender"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["pdesc"] != "" and $noscrape["pdesc"] != $gcontacts[0]["about"]) {
                    q("UPDATE `gcontact` SET `about` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["pdesc"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["about"] != "" and $noscrape["about"] != $gcontacts[0]["about"]) {
                    q("UPDATE `gcontact` SET `about` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["about"]), dbesc(normalise_link($profile)));
                }
                if (isset($noscrape["comm"]) and $noscrape["comm"] != $gcontacts[0]["community"]) {
                    q("UPDATE `gcontact` SET `community` = %d WHERE `nurl` = '%s'", intval($noscrape["comm"]), dbesc(normalise_link($profile)));
                }
                if (isset($noscrape["tags"])) {
                    $keywords = implode(" ", $noscrape["tags"]);
                } else {
                    $keywords = "";
                }
                if ($keywords != "" and $keywords != $gcontacts[0]["keywords"]) {
                    q("UPDATE `gcontact` SET `keywords` = '%s' WHERE `nurl` = '%s'", dbesc($keywords), dbesc(normalise_link($profile)));
                }
                $location = $noscrape["locality"];
                if ($noscrape["region"] != "") {
                    if ($location != "") {
                        $location .= ", ";
                    }
                    $location .= $noscrape["region"];
                }
                if ($noscrape["country-name"] != "") {
                    if ($location != "") {
                        $location .= ", ";
                    }
                    $location .= $noscrape["country-name"];
                }
                if ($location != "" and $location != $gcontacts[0]["location"]) {
                    q("UPDATE `gcontact` SET `location` = '%s' WHERE `nurl` = '%s'", dbesc($location), dbesc(normalise_link($profile)));
                }
                // If we got data from noscrape then mark the contact as reachable
                if (is_array($noscrape) and count($noscrape)) {
                    q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($profile)));
                }
                return $noscrape["updated"];
            }
        }
    }
    // If we only can poll the feed, then we only do this once a while
    if (!$force and !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
        return $gcontacts[0]["updated"];
    }
    $data = probe_url($profile);
    // 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 and poco_alternate_ostatus_url($profile) and normalise_link($profile) == normalise_link($data["alias"]) and normalise_link($profile) != normalise_link($data["url"])) {
        // Delete the old entry
        q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
        q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
        poco_check($data["url"], $data["name"], $data["network"], $data["photo"], $gcontacts[0]["about"], $gcontacts[0]["location"], $gcontacts[0]["gender"], $gcontacts[0]["keywords"], $data["addr"], $gcontacts[0]["updated"], $gcontacts[0]["generation"]);
        poco_last_updated($data["url"], $force);
        return false;
    }
    if ($data["poll"] == "" or $data["network"] == NETWORK_FEED) {
        q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($profile)));
        return false;
    }
    if ($data["name"] != "" and $data["name"] != $gcontacts[0]["name"]) {
        q("UPDATE `gcontact` SET `name` = '%s' WHERE `nurl` = '%s'", dbesc($data["name"]), dbesc(normalise_link($profile)));
    }
    if ($data["nick"] != "" and $data["nick"] != $gcontacts[0]["nick"]) {
        q("UPDATE `gcontact` SET `nick` = '%s' WHERE `nurl` = '%s'", dbesc($data["nick"]), dbesc(normalise_link($profile)));
    }
    if ($data["addr"] != "" and $data["addr"] != $gcontacts[0]["connect"]) {
        q("UPDATE `gcontact` SET `connect` = '%s' WHERE `nurl` = '%s'", dbesc($data["addr"]), dbesc(normalise_link($profile)));
    }
    if ($data["photo"] != "" and $data["photo"] != $gcontacts[0]["photo"]) {
        q("UPDATE `gcontact` SET `photo` = '%s' WHERE `nurl` = '%s'", dbesc($data["photo"]), dbesc(normalise_link($profile)));
    }
    if ($data["baseurl"] != "" and $data["baseurl"] != $gcontacts[0]["server_url"]) {
        q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'", dbesc($data["baseurl"]), dbesc(normalise_link($profile)));
    }
    $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)));
        return false;
    }
    $doc = new DOMDocument();
    @$doc->loadXML($feedret["body"]);
    $xpath = new DomXPath($doc);
    $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
    $entries = $xpath->query('/atom:feed/atom:entry');
    $last_updated = "";
    foreach ($entries as $entry) {
        $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
        $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
        if ($last_updated < $published) {
            $last_updated = $published;
        }
        if ($last_updated < $updated) {
            $last_updated = $updated;
        }
    }
    // Maybe there aren't any entries. Then check if it is a valid feed
    if ($last_updated == "") {
        if ($xpath->query('/atom:feed')->length > 0) {
            $last_updated = "0000-00-00 00:00:00";
        }
    }
    q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc($last_updated), dbesc(datetime_convert()), dbesc(normalise_link($profile)));
    if ($gcontacts[0]["generation"] == 0) {
        q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
    }
    return $last_updated;
}
Esempio n. 2
0
function discover_users()
{
    logger("Discover users", LOGGER_DEBUG);
    $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url` FROM `gcontact`\n\t\t\tWHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND\n\t\t\t\t`last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND\n\t\t\t\t`network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()", dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
    if (!$users) {
        return;
    }
    $checked = 0;
    foreach ($users as $user) {
        $urlparts = parse_url($user["url"]);
        if (!isset($urlparts["scheme"])) {
            q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'", dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
            continue;
        }
        if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com", "identi.ca", "alpha.app.net"))) {
            $networks = array("www.facebook.com" => NETWORK_FACEBOOK, "facebook.com" => NETWORK_FACEBOOK, "twitter.com" => NETWORK_TWITTER, "identi.ca" => NETWORK_PUMPIO, "alpha.app.net" => NETWORK_APPNET);
            q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'", dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
            continue;
        }
        if ($user["server_url"] != "") {
            $server_url = $user["server_url"];
        } else {
            $server_url = poco_detect_server($user["url"]);
        }
        if (poco_check_server($server_url, $gcontacts[0]["network"])) {
            logger('Check user ' . $user["url"]);
            poco_last_updated($user["url"], true);
            if (++$checked > 100) {
                return;
            }
        } else {
            q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
        }
    }
}