2 use Friendica\Core\Cache;
3 use Friendica\Core\Config;
4 use Friendica\Core\Worker;
5 use Friendica\Database\DBM;
6 use Friendica\Network\Probe;
8 require_once 'include/socgraph.php';
9 require_once 'include/datetime.php';
11 function discover_poco_run(&$argv, &$argc) {
14 This function can be called in these ways:
15 - dirsearch <search pattern>: Searches for "search pattern" in the directory. "search pattern" is url encoded.
16 - checkcontact: Updates gcontact entries
17 - suggestions: Discover other servers for their contacts.
18 - server <poco url>: Searches for the poco server list. "poco url" is base64 encoded.
19 - update_server: Frequently check the first 250 servers for vitality.
20 - update_server_directory: Discover the given server id for their contacts
21 - poco_load: Load POCO data from a given POCO address
22 - check_profile: Update remote profile data
25 if (($argc > 2) && ($argv[1] == "dirsearch")) {
26 $search = urldecode($argv[2]);
28 } elseif (($argc == 2) && ($argv[1] == "checkcontact")) {
30 } elseif (($argc == 2) && ($argv[1] == "suggestions")) {
32 } elseif (($argc == 3) && ($argv[1] == "server")) {
34 } elseif (($argc == 2) && ($argv[1] == "update_server")) {
36 } elseif (($argc == 3) && ($argv[1] == "update_server_directory")) {
38 } elseif (($argc > 5) && ($argv[1] == "poco_load")) {
40 } elseif (($argc == 3) && ($argv[1] == "check_profile")) {
42 } elseif ($argc == 1) {
46 die("Unknown or missing parameter ".$argv[1]."\n");
49 logger('start '.$search);
53 poco_last_updated($argv[2], true);
55 } elseif ($mode == 7) {
61 poco_load_worker(intval($argv[2]), intval($argv[3]), intval($argv[4]), $url);
62 } elseif ($mode == 6) {
63 poco_discover_single_server(intval($argv[2]));
64 } elseif ($mode == 5) {
66 } elseif ($mode == 4) {
67 $server_url = $argv[2];
68 if ($server_url == "") {
71 $server_url = filter_var($server_url, FILTER_SANITIZE_URL);
72 if (substr(normalise_link($server_url), 0, 7) != "http://") {
75 $result = "Checking server ".$server_url." - ";
76 $ret = poco_check_server($server_url);
82 logger($result, LOGGER_DEBUG);
83 } elseif ($mode == 3) {
85 } elseif (($mode == 2) && Config::get('system','poco_completion')) {
87 } elseif (($mode == 1) && ($search != "") && Config::get('system','poco_local_search')) {
88 discover_directory($search);
89 gs_search_user($search);
90 } elseif (($mode == 0) && ($search == "") && (Config::get('system','poco_discovery') > 0)) {
91 // Query Friendica and Hubzilla servers for their users
94 // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
95 if (!Config::get('system','ostatus_disabled'))
99 logger('end '.$search);
105 * @brief Updates the first 250 servers
108 function update_server() {
109 $r = q("SELECT `url`, `created`, `last_failure`, `last_contact` FROM `gserver` ORDER BY rand()");
111 if (!DBM::is_result($r)) {
117 foreach ($r AS $server) {
118 if (!poco_do_update($server["created"], "", $server["last_failure"], $server["last_contact"])) {
121 logger('Update server status for server '.$server["url"], LOGGER_DEBUG);
123 Worker::add(PRIORITY_LOW, "discover_poco", "server", $server["url"]);
125 if (++$updated > 250) {
131 function discover_users() {
132 logger("Discover users", LOGGER_DEBUG);
136 $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url`, `network` FROM `gcontact`
137 WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
138 `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
139 `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
140 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
141 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
148 foreach ($users AS $user) {
150 $urlparts = parse_url($user["url"]);
151 if (!isset($urlparts["scheme"])) {
152 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
153 dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
157 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
158 "identi.ca", "alpha.app.net"))) {
159 $networks = array("www.facebook.com" => NETWORK_FACEBOOK,
160 "facebook.com" => NETWORK_FACEBOOK,
161 "twitter.com" => NETWORK_TWITTER,
162 "identi.ca" => NETWORK_PUMPIO,
163 "alpha.app.net" => NETWORK_APPNET);
165 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
166 dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
170 $server_url = poco_detect_server($user["url"]);
171 $force_update = false;
173 if ($user["server_url"] != "") {
175 $force_update = (normalise_link($user["server_url"]) != normalise_link($server_url));
177 $server_url = $user["server_url"];
180 if ((($server_url == "") && ($user["network"] == NETWORK_FEED)) || $force_update || poco_check_server($server_url, $user["network"])) {
181 logger('Check profile '.$user["url"]);
182 Worker::add(PRIORITY_LOW, "discover_poco", "check_profile", $user["url"]);
184 if (++$checked > 100) {
188 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
189 dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
192 // Quit the loop after 3 minutes
193 if (time() > ($starttime + 180)) {
199 function discover_directory($search) {
201 $data = Cache::get("dirsearch:".$search);
202 if (!is_null($data)) {
203 // Only search for the same item every 24 hours
204 if (time() < $data + (60 * 60 * 24)) {
205 logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
210 $x = fetch_url(get_server()."/lsearch?p=1&n=500&search=".urlencode($search));
211 $j = json_decode($x);
213 if (count($j->results)) {
214 foreach ($j->results as $jj) {
215 // Check if the contact already exists
216 $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
217 if (DBM::is_result($exists)) {
218 logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
220 if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) &&
221 ($exists[0]["updated"] < $exists[0]["last_failure"])) {
224 // Update the contact
225 poco_last_updated($jj->url);
229 $server_url = poco_detect_server($jj->url);
230 if ($server_url != '') {
231 if (!poco_check_server($server_url)) {
232 logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
235 logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
238 $data = Probe::uri($jj->url);
239 if ($data["network"] == NETWORK_DFRN) {
240 logger("Profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
241 logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
243 if ($jj->tags != "") {
244 $data["keywords"] = $jj->tags;
247 $data["server_url"] = $data["baseurl"];
249 update_gcontact($data);
251 logger("Profile ".$jj->url." is not responding or no Friendica contact - but network ".$data["network"], LOGGER_DEBUG);
255 Cache::set("dirsearch:".$search, time(), CACHE_DAY);
259 * @brief Search for GNU Social user with gstools.org
261 * @param str $search User name
263 function gs_search_user($search) {
265 // Currently disabled, since the service isn't available anymore.
266 // It is not removed since I hope that there will be a successor.
271 $url = "http://gstools.org/api/users_search/".urlencode($search);
273 $result = z_fetch_url($url);
274 if (!$result["success"]) {
278 $contacts = json_decode($result["body"]);
280 if ($contacts->status == 'ERROR') {
284 /// @TODO AS is considered as a notation for constants (as they usually being written all upper-case)
285 /// @TODO find all those and convert to all lower-case which is a keyword then
286 foreach ($contacts->data AS $user) {
287 $contact = Probe::uri($user->site_address."/".$user->name);
288 if ($contact["network"] != NETWORK_PHANTOM) {
289 $contact["about"] = $user->description;
290 update_gcontact($contact);