]> git.mxchange.org Git - friendica.git/blob - include/discover_poco.php
550c9897be9fb3daee861cdc9561ad70997486f0
[friendica.git] / include / discover_poco.php
1 <?php
2
3 require_once("boot.php");
4 require_once("include/socgraph.php");
5
6
7 function discover_poco_run(&$argv, &$argc){
8         global $a, $db;
9
10         if(is_null($a)) {
11                 $a = new App;
12         }
13
14         if(is_null($db)) {
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);
19         };
20
21         require_once('include/session.php');
22         require_once('include/datetime.php');
23         require_once('include/pidfile.php');
24
25         load_config('config');
26         load_config('system');
27
28         // Don't check this stuff if the function is called by the poller
29         if (App::callstack() != "poller_run") {
30                 $maxsysload = intval(get_config('system','maxloadavg'));
31                 if($maxsysload < 1)
32                         $maxsysload = 50;
33
34                 $load = current_load();
35                 if($load) {
36                         if(intval($load) > $maxsysload) {
37                                 logger('system: load '.$load.' too high. discover_poco deferred to next scheduled run.');
38                                 return;
39                         }
40                 }
41         }
42
43         if(($argc > 2) && ($argv[1] == "dirsearch")) {
44                 $search = urldecode($argv[2]);
45                 $mode = 1;
46         } elseif(($argc == 2) && ($argv[1] == "checkcontact")) {
47                 $mode = 2;
48         } elseif(($argc == 2) && ($argv[1] == "suggestions")) {
49                 $mode = 3;
50         } elseif ($argc == 1) {
51                 $search = "";
52                 $mode = 0;
53         } else
54                 die("Unknown or missing parameter ".$argv[1]."\n");
55
56         // Don't check this stuff if the function is called by the poller
57         if (App::callstack() != "poller_run") {
58                 $lockpath = get_lockpath();
59                 if ($lockpath != '') {
60                         $pidfile = new pidfile($lockpath, 'discover_poco'.$mode.urlencode($search));
61                         if($pidfile->is_already_running()) {
62                                 logger("discover_poco: Already running");
63                                 if ($pidfile->running_time() > 19*60) {
64                                         $pidfile->kill();
65                                         logger("discover_poco: killed stale process");
66                                         // Calling a new instance
67                                         if ($mode == 0)
68                                                 proc_run('php','include/discover_poco.php');
69                                 }
70                                 exit;
71                         }
72                 }
73         }
74
75         $a->set_baseurl(get_config('system','url'));
76
77         load_hooks();
78
79         logger('start '.$search);
80
81         if ($mode==3)
82                 update_suggestions();
83         elseif (($mode == 2) AND get_config('system','poco_completion'))
84                 discover_users();
85         elseif (($mode == 1) AND ($search != "") and get_config('system','poco_local_search')) {
86                 discover_directory($search);
87                 gs_search_user($search);
88         } elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0)) {
89                 // Query Friendica and Hubzilla servers for their users
90                 poco_discover();
91
92                 // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
93                 if (!get_config('system','ostatus_disabled'))
94                         gs_discover();
95         }
96
97         logger('end '.$search);
98
99         return;
100 }
101
102 function discover_users() {
103         logger("Discover users", LOGGER_DEBUG);
104
105         $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url` FROM `gcontact`
106                         WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
107                                 `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
108                                 `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
109                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
110                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
111
112         if (!$users)
113                 return;
114
115         $checked = 0;
116
117         foreach ($users AS $user) {
118
119                 $urlparts = parse_url($user["url"]);
120                 if (!isset($urlparts["scheme"])) {
121                         q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
122                                 dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
123                         continue;
124                  }
125
126                 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
127                                                         "identi.ca", "alpha.app.net"))) {
128                         $networks = array("www.facebook.com" => NETWORK_FACEBOOK,
129                                         "facebook.com" => NETWORK_FACEBOOK,
130                                         "twitter.com" => NETWORK_TWITTER,
131                                         "identi.ca" => NETWORK_PUMPIO,
132                                         "alpha.app.net" => NETWORK_APPNET);
133
134                         q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
135                                 dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
136                         continue;
137                 }
138
139                 if ($user["server_url"] != "")
140                         $server_url = $user["server_url"];
141                 else
142                         $server_url = poco_detect_server($user["url"]);
143
144                 if (($server_url == "") OR poco_check_server($server_url, $gcontacts[0]["network"])) {
145                         logger('Check user '.$user["url"]);
146                         poco_last_updated($user["url"], true);
147
148                         if (++$checked > 100)
149                                 return;
150                 } else
151                         q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
152                                 dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
153         }
154 }
155
156 function discover_directory($search) {
157
158         $data = Cache::get("dirsearch:".$search);
159         if (!is_null($data)){
160                 // Only search for the same item every 24 hours
161                 if (time() < $data + (60 * 60 * 24)) {
162                         logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
163                         return;
164                 }
165         }
166
167         $x = fetch_url(get_server()."/lsearch?p=1&n=500&search=".urlencode($search));
168         $j = json_decode($x);
169
170         if(count($j->results))
171                 foreach($j->results as $jj) {
172                         // Check if the contact already exists
173                         $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
174                         if ($exists) {
175                                 logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
176
177                                 if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) AND
178                                         ($exists[0]["updated"] < $exists[0]["last_failure"]))
179                                         continue;
180
181                                 // Update the contact
182                                 poco_last_updated($jj->url);
183                                 continue;
184                         }
185
186                         // Harcoded paths aren't so good. But in this case it is okay.
187                         // First: We only will get Friendica contacts (which always are using this url schema)
188                         // Second: There will be no further problems if we are doing a mistake
189                         $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $jj->url);
190                         if ($server_url != $jj->url)
191                                 if (!poco_check_server($server_url)) {
192                                         logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
193                                         continue;
194                                 }
195                                         logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
196
197                         logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
198                         $data = probe_url($jj->url);
199                         if ($data["network"] == NETWORK_DFRN) {
200                                 logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
201                                 poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
202                         }
203                 }
204         Cache::set("dirsearch:".$search, time(), CACHE_DAY);
205 }
206
207 /**
208  * @brief Search for GNU Social user with gstools.org
209  *
210  * @param str $search User name
211  */
212 function gs_search_user($search) {
213
214         $a = get_app();
215
216         $url = "http://gstools.org/api/users_search/".urlencode($search);
217
218         $result = z_fetch_url($url);
219         if (!$result["success"])
220                 return false;
221
222         $contacts = json_decode($result["body"]);
223
224         if ($contacts->status == 'ERROR')
225                 return false;
226
227         foreach($contacts->data AS $user) {
228                 $contact = probe_url($user->site_address."/".$user->name);
229                 if ($contact["network"] != NETWORK_PHANTOM) {
230                         $contact["about"] = $user->description;
231                         update_gcontact($contact);
232                 }
233         }
234 }
235
236
237 if (array_search(__file__,get_included_files())===0){
238   discover_poco_run($_SERVER["argv"],$_SERVER["argc"]);
239   killme();
240 }