]> git.mxchange.org Git - friendica.git/blob - mod/match.php
Merge pull request #8465 from AlfredSK/AlfredSK-dont_mention_multiprofiles
[friendica.git] / mod / match.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Content\Widget;
24 use Friendica\Core\Renderer;
25 use Friendica\Core\Search;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Profile;
30 use Friendica\Util\Network;
31 use Friendica\Util\Proxy as ProxyUtils;
32
33 /**
34  * Controller for /match.
35  *
36  * It takes keywords from your profile and queries the directory server for
37  * matching keywords from other profiles.
38  *
39  * @param App $a App
40  *
41  * @return string
42  * @throws ImagickException
43  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
44  * @throws Exception
45  */
46 function match_content(App $a)
47 {
48         if (!local_user()) {
49                 return '';
50         }
51
52         DI::page()['aside'] .= Widget::findPeople();
53         DI::page()['aside'] .= Widget::follow();
54
55         $_SESSION['return_path'] = DI::args()->getCommand();
56
57         $profile = Profile::getByUID(local_user());
58
59         if (!DBA::isResult($profile)) {
60                 return '';
61         }
62         if (!$profile['pub_keywords'] && (!$profile['prv_keywords'])) {
63                 notice(DI::l10n()->t('No keywords to match. Please add keywords to your profile.') . EOL);
64                 return '';
65         }
66
67         $params = [];
68         $tags = trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']);
69
70         $params['s'] = $tags;
71         $params['n'] = 100;
72
73         if (strlen(DI::config()->get('system', 'directory'))) {
74                 $host = Search::getGlobalDirectory();
75         } else {
76                 $host = DI::baseUrl();
77         }
78
79         $msearch_json = Network::post($host . '/msearch', $params)->getBody();
80
81         $msearch = json_decode($msearch_json);
82
83         $start = $_GET['start'] ?? 0;
84         $entries = [];
85         $paginate = '';
86
87         if (!empty($msearch->results)) {
88                 for ($i = $start;count($entries) < 10 && $i < $msearch->total; $i++) {
89                         $profile = $msearch->results[$i];
90
91                         // Already known contact
92                         if (!$profile || Contact::getIdForURL($profile->url, local_user(), true)) {
93                                 continue;
94                         }
95
96                         // Workaround for wrong directory photo URL
97                         $profile->photo = str_replace('http:///photo/', Search::getGlobalDirectory() . '/photo/', $profile->photo);
98
99                         $connlnk = DI::baseUrl() . '/follow/?url=' . $profile->url;
100                         $photo_menu = [
101                                 'profile' => [DI::l10n()->t("View Profile"), Contact::magicLink($profile->url)],
102                                 'follow' => [DI::l10n()->t("Connect/Follow"), $connlnk]
103                         ];
104
105                         $contact_details = Contact::getDetailsByURL($profile->url, 0);
106
107                         $entry = [
108                                 'url'          => Contact::magicLink($profile->url),
109                                 'itemurl'      => $contact_details['addr'] ?? $profile->url,
110                                 'name'         => $profile->name,
111                                 'details'      => $contact_details['location'] ?? '',
112                                 'tags'         => $contact_details['keywords'] ?? '',
113                                 'about'        => $contact_details['about'] ?? '',
114                                 'account_type' => Contact::getAccountType($contact_details),
115                                 'thumb'        => ProxyUtils::proxifyUrl($profile->photo, false, ProxyUtils::SIZE_THUMB),
116                                 'conntxt'      => DI::l10n()->t('Connect'),
117                                 'connlnk'      => $connlnk,
118                                 'img_hover'    => $profile->tags,
119                                 'photo_menu'   => $photo_menu,
120                                 'id'           => $i,
121                         ];
122                         $entries[] = $entry;
123                 }
124
125                 $data = [
126                         'class' => 'pager',
127                         'first' => [
128                                 'url'   => 'match',
129                                 'text'  => DI::l10n()->t('first'),
130                                 'class' => 'previous' . ($start == 0 ? 'disabled' : '')
131                         ],
132                         'next'  => [
133                                 'url'   => 'match?start=' . $i,
134                                 'text'  => DI::l10n()->t('next'),
135                                 'class' =>  'next' . ($i >= $msearch->total ? ' disabled' : '')
136                         ]
137                 ];
138
139                 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
140                 $paginate = Renderer::replaceMacros($tpl, ['pager' => $data]);
141         }
142
143         if (empty($entries)) {
144                 info(DI::l10n()->t('No matches') . EOL);
145         }
146
147         $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
148         $o = Renderer::replaceMacros($tpl, [
149                 '$title'    => DI::l10n()->t('Profile Match'),
150                 '$contacts' => $entries,
151                 '$paginate' => $paginate
152         ]);
153
154         return $o;
155 }