]> git.mxchange.org Git - friendica.git/blob - include/discover_poco.php
Merge pull request #2112 from rabuzarus/2811_group_side
[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         $maxsysload = intval(get_config('system','maxloadavg'));
29         if($maxsysload < 1)
30                 $maxsysload = 50;
31         if(function_exists('sys_getloadavg')) {
32                 $load = sys_getloadavg();
33                 if(intval($load[0]) > $maxsysload) {
34                         logger('system: load ' . $load[0] . ' too high. discover_poco deferred to next scheduled run.');
35                         return;
36                 }
37         }
38
39         if(($argc > 2) && ($argv[1] == "dirsearch")) {
40                 $search = urldecode($argv[2]);
41                 $mode = 1;
42         } elseif(($argc == 2) && ($argv[1] == "checkcontact")) {
43                 $mode = 2;
44         } elseif(($argc == 2) && ($argv[1] == "suggestions")) {
45                 $mode = 3;
46         } elseif ($argc == 1) {
47                 $search = "";
48                 $mode = 0;
49         } else
50                 die("Unknown or missing parameter ".$argv[1]."\n");
51
52         $lockpath = get_lockpath();
53         if ($lockpath != '') {
54                 $pidfile = new pidfile($lockpath, 'discover_poco'.$mode.urlencode($search));
55                 if($pidfile->is_already_running()) {
56                         logger("discover_poco: Already running");
57                         if ($pidfile->running_time() > 19*60) {
58                                 $pidfile->kill();
59                                 logger("discover_poco: killed stale process");
60                                 // Calling a new instance
61                                 if ($mode == 0)
62                                         proc_run('php','include/discover_poco.php');
63                         }
64                         exit;
65                 }
66         }
67
68         $a->set_baseurl(get_config('system','url'));
69
70         load_hooks();
71
72         logger('start '.$search);
73
74         if ($mode==3)
75                 update_suggestions();
76         elseif (($mode == 2) AND get_config('system','poco_completion'))
77                 discover_users();
78         elseif (($mode == 1) AND ($search != "") and get_config('system','poco_local_search'))
79                 discover_directory($search);
80         elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0))
81                 poco_discover();
82
83         logger('end '.$search);
84
85         return;
86 }
87
88 function discover_users() {
89         logger("Discover users", LOGGER_DEBUG);
90
91         $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url` FROM `gcontact`
92                         WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
93                                 `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
94                                 `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
95                         dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
96                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
97
98         if (!$users)
99                 return;
100
101         $checked = 0;
102
103         foreach ($users AS $user) {
104
105                 $urlparts = parse_url($user["url"]);
106                 if (!isset($urlparts["scheme"])) {
107                         q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
108                                 dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
109                         continue;
110                  }
111
112                 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
113                                                         "identi.ca", "alpha.app.net"))) {
114                         $networks = array("www.facebook.com" => NETWORK_FACEBOOK,
115                                         "facebook.com" => NETWORK_FACEBOOK,
116                                         "twitter.com" => NETWORK_TWITTER,
117                                         "identi.ca" => NETWORK_PUMPIO,
118                                         "alpha.app.net" => NETWORK_APPNET);
119
120                         q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
121                                 dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
122                         continue;
123                 }
124
125                 if ($user["server_url"] != "")
126                         $server_url = $user["server_url"];
127                 else
128                         $server_url = poco_detect_server($user["url"]);
129
130                 if (poco_check_server($server_url, $gcontacts[0]["network"])) {
131                         logger('Check user '.$user["url"]);
132                         poco_last_updated($user["url"], true);
133
134                         if (++$checked > 100)
135                                 return;
136                 } else
137                         q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
138                                 dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
139         }
140 }
141
142 function discover_directory($search) {
143
144         $data = Cache::get("dirsearch:".$search);
145         if (!is_null($data)){
146                 // Only search for the same item every 24 hours
147                 if (time() < $data + (60 * 60 * 24)) {
148                         logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
149                         return;
150                 }
151         }
152
153         $x = fetch_url(get_server()."/lsearch?p=1&n=500&search=".urlencode($search));
154         $j = json_decode($x);
155
156         if(count($j->results))
157                 foreach($j->results as $jj) {
158                         // Check if the contact already exists
159                         $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
160                         if ($exists) {
161                                 logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
162
163                                 if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) AND
164                                         ($exists[0]["updated"] < $exists[0]["last_failure"]))
165                                         continue;
166
167                                 // Update the contact
168                                 poco_last_updated($jj->url);
169                                 continue;
170                         }
171
172                         // Harcoded paths aren't so good. But in this case it is okay.
173                         // First: We only will get Friendica contacts (which always are using this url schema)
174                         // Second: There will be no further problems if we are doing a mistake
175                         $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $jj->url);
176                         if ($server_url != $jj->url)
177                                 if (!poco_check_server($server_url)) {
178                                         logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
179                                         continue;
180                                 }
181                                         logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
182
183                         logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
184                         $data = probe_url($jj->url);
185                         if ($data["network"] == NETWORK_DFRN) {
186                                 logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
187                                 poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
188                         }
189                 }
190         Cache::set("dirsearch:".$search, time(), CACHE_DAY);
191 }
192
193 if (array_search(__file__,get_included_files())===0){
194   discover_poco_run($_SERVER["argv"],$_SERVER["argc"]);
195   killme();
196 }