]> git.mxchange.org Git - friendica.git/blob - src/Worker/SearchDirectory.php
d489acb7fb6d6aa027879e98edb8762db31400bd
[friendica.git] / src / Worker / SearchDirectory.php
1 <?php
2 /**
3  * @file src/Worker/SearchDirectory.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\Core\Cache;
8 use Friendica\Core\Config;
9 use Friendica\Core\Logger;
10 use Friendica\Core\Protocol;
11 use Friendica\Database\DBA;
12 use Friendica\Model\GContact;
13 use Friendica\Model\GServer;
14 use Friendica\Network\Probe;
15 use Friendica\Util\Network;
16 use Friendica\Util\Strings;
17
18 class SearchDirectory
19 {
20         // <search pattern>: Searches for "search pattern" in the directory.
21         public static function execute($search)
22         {
23                 if (!Config::get('system', 'poco_local_search')) {
24                         Logger::info('Local search is not enabled');
25                         return;
26                 }
27
28                 $data = Cache::get('SearchDirectory:' . $search);
29                 if (!is_null($data)) {
30                         // Only search for the same item every 24 hours
31                         if (time() < $data + (60 * 60 * 24)) {
32                                 Logger::info('Already searched this in the last 24 hours', ['search' => $search]);
33                                 return;
34                         }
35                 }
36
37                 $x = Network::fetchUrl(get_server() . '/lsearch?p=1&n=500&search=' . urlencode($search));
38                 $j = json_decode($x);
39
40                 if (!empty($j->results)) {
41                         foreach ($j->results as $jj) {
42                                 // Check if the contact already exists
43                                 $gcontact = DBA::selectFirst('gcontact', ['id', 'last_contact', 'last_failure', 'updated'], ['nurl' => Strings::normaliseLink($jj->url)]);
44                                 if (DBA::isResult($gcontact)) {
45                                         Logger::info('Profile already exists', ['profile' => $jj->url, 'search' => $search]);
46
47                                         if (($gcontact['last_contact'] < $gcontact['last_failure']) &&
48                                                 ($gcontact['updated'] < $gcontact['last_failure'])) {
49                                                 continue;
50                                         }
51
52                                         // Update the contact
53                                         GContact::updateFromProbe($jj->url);
54                                         continue;
55                                 }
56
57                                 $server_url = GContact::getBasepath($jj->url, true);
58                                 if ($server_url != '') {
59                                         if (!GServer::check($server_url)) {
60                                                 Logger::info("Friendica server doesn't answer.", ['server' => $server_url]);
61                                                 continue;
62                                         }
63                                         Logger::info('Friendica server seems to be okay.', ['server' => $server_url]);
64                                 }
65
66                                 $data = Probe::uri($jj->url);
67                                 if ($data['network'] == Protocol::DFRN) {
68                                         Logger::info('Add profile to local directory', ['profile' => $jj->url]);
69
70                                         if ($jj->tags != '') {
71                                                 $data['keywords'] = $jj->tags;
72                                         }
73
74                                         $data['server_url'] = $data['baseurl'];
75
76                                         GContact::update($data);
77                                 } else {
78                                         Logger::info('Profile is not responding or no Friendica contact', ['profile' => $jj->url, 'network' => $data['network']]);
79                                 }
80                         }
81                 }
82                 Cache::set('SearchDirectory:' . $search, time(), Cache::DAY);
83         }
84 }