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