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