]> git.mxchange.org Git - friendica.git/blob - mod/match.php
Move Config::get() to DI::config()->get()
[friendica.git] / mod / match.php
1 <?php
2 /**
3  * @file mod/match.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Widget;
8 use Friendica\Core\Config;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\Search;
11 use Friendica\Database\DBA;
12 use Friendica\DI;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Profile;
15 use Friendica\Util\Network;
16 use Friendica\Util\Proxy as ProxyUtils;
17
18 /**
19  * Controller for /match.
20  *
21  * It takes keywords from your profile and queries the directory server for
22  * matching keywords from other profiles.
23  *
24  * @param App $a App
25  *
26  * @return string
27  * @throws ImagickException
28  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
29  * @throws Exception
30  */
31 function match_content(App $a)
32 {
33         if (!local_user()) {
34                 return '';
35         }
36
37         DI::page()['aside'] .= Widget::findPeople();
38         DI::page()['aside'] .= Widget::follow();
39
40         $_SESSION['return_path'] = DI::args()->getCommand();
41
42         $profile = Profile::getByUID(local_user());
43
44         if (!DBA::isResult($profile)) {
45                 return '';
46         }
47         if (!$profile['pub_keywords'] && (!$profile['prv_keywords'])) {
48                 notice(DI::l10n()->t('No keywords to match. Please add keywords to your default profile.') . EOL);
49                 return '';
50         }
51
52         $params = [];
53         $tags = trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']);
54
55         $params['s'] = $tags;
56         $params['n'] = 100;
57
58         if (strlen(DI::config()->get('system', 'directory'))) {
59                 $host = Search::getGlobalDirectory();
60         } else {
61                 $host = DI::baseUrl();
62         }
63
64         $msearch_json = Network::post($host . '/msearch', $params)->getBody();
65
66         $msearch = json_decode($msearch_json);
67
68         $start = $_GET['start'] ?? 0;
69         $entries = [];
70         $paginate = '';
71
72         if (!empty($msearch->results)) {
73                 for ($i = $start;count($entries) < 10 && $i < $msearch->total; $i++) {
74                         $profile = $msearch->results[$i];
75
76                         // Already known contact
77                         if (!$profile || Contact::getIdForURL($profile->url, local_user(), true)) {
78                                 continue;
79                         }
80
81                         // Workaround for wrong directory photo URL
82                         $profile->photo = str_replace('http:///photo/', Search::getGlobalDirectory() . '/photo/', $profile->photo);
83
84                         $connlnk = DI::baseUrl() . '/follow/?url=' . $profile->url;
85                         $photo_menu = [
86                                 'profile' => [DI::l10n()->t("View Profile"), Contact::magicLink($profile->url)],
87                                 'follow' => [DI::l10n()->t("Connect/Follow"), $connlnk]
88                         ];
89
90                         $contact_details = Contact::getDetailsByURL($profile->url, 0);
91
92                         $entry = [
93                                 'url'          => Contact::magicLink($profile->url),
94                                 'itemurl'      => $contact_details['addr'] ?? $profile->url,
95                                 'name'         => $profile->name,
96                                 'details'      => $contact_details['location'] ?? '',
97                                 'tags'         => $contact_details['keywords'] ?? '',
98                                 'about'        => $contact_details['about'] ?? '',
99                                 'account_type' => Contact::getAccountType($contact_details),
100                                 'thumb'        => ProxyUtils::proxifyUrl($profile->photo, false, ProxyUtils::SIZE_THUMB),
101                                 'conntxt'      => DI::l10n()->t('Connect'),
102                                 'connlnk'      => $connlnk,
103                                 'img_hover'    => $profile->tags,
104                                 'photo_menu'   => $photo_menu,
105                                 'id'           => $i,
106                         ];
107                         $entries[] = $entry;
108                 }
109
110                 $data = [
111                         'class' => 'pager',
112                         'first' => [
113                                 'url'   => 'match',
114                                 'text'  => DI::l10n()->t('first'),
115                                 'class' => 'previous' . ($start == 0 ? 'disabled' : '')
116                         ],
117                         'next'  => [
118                                 'url'   => 'match?start=' . $i,
119                                 'text'  => DI::l10n()->t('next'),
120                                 'class' =>  'next' . ($i >= $msearch->total ? ' disabled' : '')
121                         ]
122                 ];
123
124                 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
125                 $paginate = Renderer::replaceMacros($tpl, ['pager' => $data]);
126         }
127
128         if (empty($entries)) {
129                 info(DI::l10n()->t('No matches') . EOL);
130         }
131
132         $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
133         $o = Renderer::replaceMacros($tpl, [
134                 '$title'    => DI::l10n()->t('Profile Match'),
135                 '$contacts' => $entries,
136                 '$paginate' => $paginate
137         ]);
138
139         return $o;
140 }