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