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