]> git.mxchange.org Git - friendica.git/blob - include/discover_poco.php
Do some caching
[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                 $searchmode = 1;
42         } elseif ($argc == 1) {
43                 $search = "";
44                 $searchmode = 0;
45         } else
46                 die("Unknown or missing parameter ".$argv[1]."\n");
47
48         $lockpath = get_lockpath();
49         if ($lockpath != '') {
50                 $pidfile = new pidfile($lockpath, 'discover_poco'.urlencode($search));
51                 if($pidfile->is_already_running()) {
52                         logger("discover_poco: Already running");
53                         if ($pidfile->running_time() > 19*60) {
54                                 $pidfile->kill();
55                                 logger("discover_poco: killed stale process");
56                                 // Calling a new instance
57                                 proc_run('php','include/discover_poco.php');
58                         }
59                         exit;
60                 }
61         }
62
63         $a->set_baseurl(get_config('system','url'));
64
65         load_hooks();
66
67         logger('start '.$search);
68
69         if (($search != "") and get_config('system','poco_local_search'))
70                 discover_directory($search);
71         elseif (($search == "") and get_config('system','poco_discovery') > 0)
72                 poco_discover();
73
74         logger('end '.$search);
75
76         return;
77 }
78
79 function discover_directory($search) {
80
81         $data = Cache::get("dirsearch:".$search);
82         if (!is_null($data)){
83                 // Only search for the same item every 24 hours
84                 if (time() < $data + (60 * 60 * 24)) {
85                         logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
86                         return;
87                 }
88         }
89
90         $x = fetch_url("http://dir.friendica.com/lsearch?p=1&n=500&search=".urlencode($search));
91         $j = json_decode($x);
92
93         if(count($j->results))
94                 foreach($j->results as $jj) {
95                         // Check if the contact already exists
96                         $exists = q("SELECT `id`, `last_contact`, `last_failure`  FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
97                         if ($exists) {
98                                 logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
99
100                                 if ($exists[0]["last_contact"] < $exists[0]["last_failure"])
101                                         continue;
102
103                                 // Update the contact
104                                 poco_last_updated($jj->url);
105                                 continue;
106                         }
107
108                         // Harcoded paths aren't so good. But in this case it is okay.
109                         // First: We only will get Friendica contacts (which always are using this url schema)
110                         // Second: There will be no further problems if we are doing a mistake
111                         $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $jj->url);
112                         if ($server_url != $jj->url)
113                                 if (!poco_check_server($server_url)) {
114                                         logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
115                                         continue;
116                                 }
117                                         logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
118
119                         logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
120                         $data = probe_url($jj->url);
121                         if ($data["network"] == NETWORK_DFRN) {
122                                 logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
123                                 poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
124                         }
125                 }
126         Cache::set("dirsearch:".$search, time());
127 }
128
129 if (array_search(__file__,get_included_files())===0){
130   discover_poco_run($_SERVER["argv"],$_SERVER["argc"]);
131   killme();
132 }