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