]> git.mxchange.org Git - friendica.git/blob - src/Worker/SearchDirectory.php
4503f21ca6006968da6e5d37c4d113091745c9de
[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\Core\Search;
12 use Friendica\Database\DBA;
13 use Friendica\Model\GContact;
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                 $data = Cache::get('SearchDirectory:' . $search);
30                 if (!is_null($data)) {
31                         // Only search for the same item every 24 hours
32                         if (time() < $data + (60 * 60 * 24)) {
33                                 Logger::info('Already searched this in the last 24 hours', ['search' => $search]);
34                                 return;
35                         }
36                 }
37
38                 $x = Network::fetchUrl(Search::getGlobalDirectory() . '/lsearch?p=1&n=500&search=' . urlencode($search));
39                 $j = json_decode($x);
40
41                 if (!empty($j->results)) {
42                         foreach ($j->results as $jj) {
43                                 // Check if the contact already exists
44                                 $gcontact = DBA::selectFirst('gcontact', ['id', 'last_contact', 'last_failure', 'updated'], ['nurl' => Strings::normaliseLink($jj->url)]);
45                                 if (DBA::isResult($gcontact)) {
46                                         Logger::info('Profile already exists', ['profile' => $jj->url, 'search' => $search]);
47
48                                         if (($gcontact['last_contact'] < $gcontact['last_failure']) &&
49                                                 ($gcontact['updated'] < $gcontact['last_failure'])) {
50                                                 continue;
51                                         }
52
53                                         // Update the contact
54                                         GContact::updateFromProbe($jj->url);
55                                         continue;
56                                 }
57
58                                 $server_url = GContact::getBasepath($jj->url, true);
59                                 if ($server_url != '') {
60                                         if (!GServer::check($server_url)) {
61                                                 Logger::info("Friendica server doesn't answer.", ['server' => $server_url]);
62                                                 continue;
63                                         }
64                                         Logger::info('Friendica server seems to be okay.', ['server' => $server_url]);
65                                 }
66
67                                 $data = Probe::uri($jj->url);
68                                 if ($data['network'] == Protocol::DFRN) {
69                                         Logger::info('Add profile to local directory', ['profile' => $jj->url]);
70
71                                         if ($jj->tags != '') {
72                                                 $data['keywords'] = $jj->tags;
73                                         }
74
75                                         $data['server_url'] = $data['baseurl'];
76
77                                         GContact::update($data);
78                                 } else {
79                                         Logger::info('Profile is not responding or no Friendica contact', ['profile' => $jj->url, 'network' => $data['network']]);
80                                 }
81                         }
82                 }
83                 Cache::set('SearchDirectory:' . $search, time(), Cache::DAY);
84         }
85 }