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