3 require_once("boot.php");
4 require_once("include/socgraph.php");
7 function discover_poco_run(&$argv, &$argc){
15 @include(".htconfig.php");
16 require_once("include/dba.php");
17 $db = new dba($db_host, $db_user, $db_pass, $db_data);
18 unset($db_host, $db_user, $db_pass, $db_data);
23 require_once('include/session.php');
24 require_once('include/datetime.php');
26 load_config('config');
27 load_config('system');
29 // Don't check this stuff if the function is called by the poller
30 if (App::callstack() != "poller_run")
31 if (App::maxload_reached())
34 if(($argc > 2) && ($argv[1] == "dirsearch")) {
35 $search = urldecode($argv[2]);
37 } elseif(($argc == 2) && ($argv[1] == "checkcontact")) {
39 } elseif(($argc == 2) && ($argv[1] == "suggestions")) {
41 } elseif ($argc == 1) {
45 die("Unknown or missing parameter ".$argv[1]."\n");
47 // Don't check this stuff if the function is called by the poller
48 if (App::callstack() != "poller_run")
49 if (App::is_already_running('discover_poco'.$mode.urlencode($search), 'include/discover_poco.php', 1140))
52 $a->set_baseurl(get_config('system','url'));
56 logger('start '.$search);
60 elseif (($mode == 2) AND get_config('system','poco_completion'))
62 elseif (($mode == 1) AND ($search != "") and get_config('system','poco_local_search')) {
63 discover_directory($search);
64 gs_search_user($search);
65 } elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0)) {
66 // Query Friendica and Hubzilla servers for their users
69 // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
70 if (!get_config('system','ostatus_disabled'))
74 logger('end '.$search);
79 function discover_users() {
80 logger("Discover users", LOGGER_DEBUG);
82 $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url` FROM `gcontact`
83 WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
84 `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
85 `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
86 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
87 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
94 foreach ($users AS $user) {
96 $urlparts = parse_url($user["url"]);
97 if (!isset($urlparts["scheme"])) {
98 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
99 dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
103 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
104 "identi.ca", "alpha.app.net"))) {
105 $networks = array("www.facebook.com" => NETWORK_FACEBOOK,
106 "facebook.com" => NETWORK_FACEBOOK,
107 "twitter.com" => NETWORK_TWITTER,
108 "identi.ca" => NETWORK_PUMPIO,
109 "alpha.app.net" => NETWORK_APPNET);
111 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
112 dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
116 if ($user["server_url"] != "")
117 $server_url = $user["server_url"];
119 $server_url = poco_detect_server($user["url"]);
121 if (($server_url == "") OR poco_check_server($server_url, $gcontacts[0]["network"])) {
122 logger('Check user '.$user["url"]);
123 poco_last_updated($user["url"], true);
125 if (++$checked > 100)
128 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
129 dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
133 function discover_directory($search) {
135 $data = Cache::get("dirsearch:".$search);
136 if (!is_null($data)){
137 // Only search for the same item every 24 hours
138 if (time() < $data + (60 * 60 * 24)) {
139 logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
144 $x = fetch_url(get_server()."/lsearch?p=1&n=500&search=".urlencode($search));
145 $j = json_decode($x);
147 if(count($j->results))
148 foreach($j->results as $jj) {
149 // Check if the contact already exists
150 $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
152 logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
154 if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) AND
155 ($exists[0]["updated"] < $exists[0]["last_failure"]))
158 // Update the contact
159 poco_last_updated($jj->url);
163 // Harcoded paths aren't so good. But in this case it is okay.
164 // First: We only will get Friendica contacts (which always are using this url schema)
165 // Second: There will be no further problems if we are doing a mistake
166 $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $jj->url);
167 if ($server_url != $jj->url)
168 if (!poco_check_server($server_url)) {
169 logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
172 logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
174 logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
175 $data = probe_url($jj->url);
176 if ($data["network"] == NETWORK_DFRN) {
177 logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
178 poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
181 Cache::set("dirsearch:".$search, time(), CACHE_DAY);
185 * @brief Search for GNU Social user with gstools.org
187 * @param str $search User name
189 function gs_search_user($search) {
193 $url = "http://gstools.org/api/users_search/".urlencode($search);
195 $result = z_fetch_url($url);
196 if (!$result["success"])
199 $contacts = json_decode($result["body"]);
201 if ($contacts->status == 'ERROR')
204 foreach($contacts->data AS $user) {
205 $contact = probe_url($user->site_address."/".$user->name);
206 if ($contact["network"] != NETWORK_PHANTOM) {
207 $contact["about"] = $user->description;
208 update_gcontact($contact);
214 if (array_search(__file__,get_included_files())===0){
215 discover_poco_run($_SERVER["argv"],$_SERVER["argc"]);