3 use \Friendica\Core\Config;
5 require_once("include/socgraph.php");
6 require_once('include/datetime.php');
8 function discover_poco_run(&$argv, &$argc){
10 if (($argc > 2) && ($argv[1] == "dirsearch")) {
11 $search = urldecode($argv[2]);
13 } elseif(($argc == 2) && ($argv[1] == "checkcontact")) {
15 } elseif(($argc == 2) && ($argv[1] == "suggestions")) {
17 } elseif ($argc == 1) {
21 die("Unknown or missing parameter ".$argv[1]."\n");
23 logger('start '.$search);
27 elseif (($mode == 2) AND get_config('system','poco_completion'))
29 elseif (($mode == 1) AND ($search != "") and get_config('system','poco_local_search')) {
30 discover_directory($search);
31 gs_search_user($search);
32 } elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0)) {
33 // Query Friendica and Hubzilla servers for their users
36 // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
37 if (!get_config('system','ostatus_disabled'))
41 logger('end '.$search);
46 function discover_users() {
47 logger("Discover users", LOGGER_DEBUG);
49 $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url` FROM `gcontact`
50 WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
51 `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
52 `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
53 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
54 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
61 foreach ($users AS $user) {
63 $urlparts = parse_url($user["url"]);
64 if (!isset($urlparts["scheme"])) {
65 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
66 dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
70 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
71 "identi.ca", "alpha.app.net"))) {
72 $networks = array("www.facebook.com" => NETWORK_FACEBOOK,
73 "facebook.com" => NETWORK_FACEBOOK,
74 "twitter.com" => NETWORK_TWITTER,
75 "identi.ca" => NETWORK_PUMPIO,
76 "alpha.app.net" => NETWORK_APPNET);
78 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
79 dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
83 if ($user["server_url"] != "")
84 $server_url = $user["server_url"];
86 $server_url = poco_detect_server($user["url"]);
88 if (($server_url == "") OR poco_check_server($server_url, $gcontacts[0]["network"])) {
89 logger('Check user '.$user["url"]);
90 poco_last_updated($user["url"], true);
95 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
96 dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
100 function discover_directory($search) {
102 $data = Cache::get("dirsearch:".$search);
103 if (!is_null($data)){
104 // Only search for the same item every 24 hours
105 if (time() < $data + (60 * 60 * 24)) {
106 logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
111 $x = fetch_url(get_server()."/lsearch?p=1&n=500&search=".urlencode($search));
112 $j = json_decode($x);
114 if(count($j->results))
115 foreach($j->results as $jj) {
116 // Check if the contact already exists
117 $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
119 logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
121 if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) AND
122 ($exists[0]["updated"] < $exists[0]["last_failure"]))
125 // Update the contact
126 poco_last_updated($jj->url);
130 // Harcoded paths aren't so good. But in this case it is okay.
131 // First: We only will get Friendica contacts (which always are using this url schema)
132 // Second: There will be no further problems if we are doing a mistake
133 $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $jj->url);
134 if ($server_url != $jj->url)
135 if (!poco_check_server($server_url)) {
136 logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
139 logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
141 logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
142 $data = probe_url($jj->url);
143 if ($data["network"] == NETWORK_DFRN) {
144 logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
145 poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
148 Cache::set("dirsearch:".$search, time(), CACHE_DAY);
152 * @brief Search for GNU Social user with gstools.org
154 * @param str $search User name
156 function gs_search_user($search) {
160 $url = "http://gstools.org/api/users_search/".urlencode($search);
162 $result = z_fetch_url($url);
163 if (!$result["success"])
166 $contacts = json_decode($result["body"]);
168 if ($contacts->status == 'ERROR')
171 foreach($contacts->data AS $user) {
172 $contact = probe_url($user->site_address."/".$user->name);
173 if ($contact["network"] != NETWORK_PHANTOM) {
174 $contact["about"] = $user->description;
175 update_gcontact($contact);