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);
21 require_once('include/session.php');
22 require_once('include/datetime.php');
23 require_once('include/pidfile.php');
25 load_config('config');
26 load_config('system');
28 $maxsysload = intval(get_config('system','maxloadavg'));
32 $load = current_load();
34 if(intval($load) > $maxsysload) {
35 logger('system: load ' . $load . ' too high. discover_poco deferred to next scheduled run.');
40 if(($argc > 2) && ($argv[1] == "dirsearch")) {
41 $search = urldecode($argv[2]);
43 } elseif(($argc == 2) && ($argv[1] == "checkcontact")) {
45 } elseif(($argc == 2) && ($argv[1] == "suggestions")) {
47 } elseif ($argc == 1) {
51 die("Unknown or missing parameter ".$argv[1]."\n");
53 $lockpath = get_lockpath();
54 if ($lockpath != '') {
55 $pidfile = new pidfile($lockpath, 'discover_poco'.$mode.urlencode($search));
56 if($pidfile->is_already_running()) {
57 logger("discover_poco: Already running");
58 if ($pidfile->running_time() > 19*60) {
60 logger("discover_poco: killed stale process");
61 // Calling a new instance
63 proc_run('php','include/discover_poco.php');
69 $a->set_baseurl(get_config('system','url'));
73 logger('start '.$search);
77 elseif (($mode == 2) AND get_config('system','poco_completion'))
79 elseif (($mode == 1) AND ($search != "") and get_config('system','poco_local_search')) {
80 discover_directory($search);
81 gs_search_user($search);
82 } elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0)) {
83 // Query Friendica and Hubzilla servers for their users
86 // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
87 if (!get_config('system','ostatus_disabled'))
91 logger('end '.$search);
96 function discover_users() {
97 logger("Discover users", LOGGER_DEBUG);
99 $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url` FROM `gcontact`
100 WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
101 `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
102 `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
103 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
104 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
111 foreach ($users AS $user) {
113 $urlparts = parse_url($user["url"]);
114 if (!isset($urlparts["scheme"])) {
115 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
116 dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
120 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
121 "identi.ca", "alpha.app.net"))) {
122 $networks = array("www.facebook.com" => NETWORK_FACEBOOK,
123 "facebook.com" => NETWORK_FACEBOOK,
124 "twitter.com" => NETWORK_TWITTER,
125 "identi.ca" => NETWORK_PUMPIO,
126 "alpha.app.net" => NETWORK_APPNET);
128 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
129 dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
133 if ($user["server_url"] != "")
134 $server_url = $user["server_url"];
136 $server_url = poco_detect_server($user["url"]);
138 if (($server_url == "") OR poco_check_server($server_url, $gcontacts[0]["network"])) {
139 logger('Check user '.$user["url"]);
140 poco_last_updated($user["url"], true);
142 if (++$checked > 100)
145 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
146 dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
150 function discover_directory($search) {
152 $data = Cache::get("dirsearch:".$search);
153 if (!is_null($data)){
154 // Only search for the same item every 24 hours
155 if (time() < $data + (60 * 60 * 24)) {
156 logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
161 $x = fetch_url(get_server()."/lsearch?p=1&n=500&search=".urlencode($search));
162 $j = json_decode($x);
164 if(count($j->results))
165 foreach($j->results as $jj) {
166 // Check if the contact already exists
167 $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
169 logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
171 if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) AND
172 ($exists[0]["updated"] < $exists[0]["last_failure"]))
175 // Update the contact
176 poco_last_updated($jj->url);
180 // Harcoded paths aren't so good. But in this case it is okay.
181 // First: We only will get Friendica contacts (which always are using this url schema)
182 // Second: There will be no further problems if we are doing a mistake
183 $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $jj->url);
184 if ($server_url != $jj->url)
185 if (!poco_check_server($server_url)) {
186 logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
189 logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
191 logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
192 $data = probe_url($jj->url);
193 if ($data["network"] == NETWORK_DFRN) {
194 logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
195 poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
198 Cache::set("dirsearch:".$search, time(), CACHE_DAY);
202 * @brief Search for GNU Social user with gstools.org
204 * @param str $search User name
206 function gs_search_user($search) {
210 $url = "http://gstools.org/api/users_search/".urlencode($search);
212 $result = z_fetch_url($url);
213 if (!$result["success"])
216 $contacts = json_decode($result["body"]);
218 if ($contacts->status == 'ERROR')
221 foreach($contacts->data AS $user) {
222 $contact = probe_url($user->site_address."/".$user->name);
223 if ($contact["network"] != NETWORK_PHANTOM) {
224 $contact["about"] = $user->description;
225 update_gcontact($contact);
231 if (array_search(__file__,get_included_files())===0){
232 discover_poco_run($_SERVER["argv"],$_SERVER["argc"]);