]> git.mxchange.org Git - friendica.git/blob - mod/match.php
Merge pull request #12025 from annando/no-boot-src-module
[friendica.git] / mod / match.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Core\Session;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Profile;
31 use Friendica\Module\Contact as ModuleContact;
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 (!Session::getLocalUser()) {
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(Session::getLocalUser());
58
59         if (!DBA::isResult($profile)) {
60                 return '';
61         }
62         if (!$profile['pub_keywords'] && (!$profile['prv_keywords'])) {
63                 DI::sysmsg()->addNotice(DI::l10n()->t('No keywords to match. Please add keywords to your profile.'));
64                 return '';
65         }
66
67         $params = [];
68         $tags = trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']);
69
70         if (DI::mode()->isMobile()) {
71                 $limit = DI::pConfig()->get(Session::getLocalUser(), 'system', 'itemspage_mobile_network',
72                         DI::config()->get('system', 'itemspage_network_mobile'));
73         } else {
74                 $limit = DI::pConfig()->get(Session::getLocalUser(), 'system', 'itemspage_network',
75                         DI::config()->get('system', 'itemspage_network'));
76         }
77
78         $params['s'] = $tags;
79         $params['n'] = 100;
80
81         $entries = [];
82         foreach ([Search::getGlobalDirectory(), DI::baseUrl()] as $server) {
83                 if (empty($server)) {
84                         continue;
85                 }
86
87                 $msearch = json_decode(DI::httpClient()->post($server . '/msearch', $params)->getBody());
88                 if (!empty($msearch)) {
89                         $entries = match_get_contacts($msearch, $entries, $limit);
90                 }
91         }
92
93         if (empty($entries)) {
94                 DI::sysmsg()->addInfo(DI::l10n()->t('No matches'));
95         }
96
97         $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
98         $o = Renderer::replaceMacros($tpl, [
99                 '$title'    => DI::l10n()->t('Profile Match'),
100                 '$contacts' => array_slice($entries, 0, $limit),
101         ]);
102
103         return $o;
104 }
105
106 function match_get_contacts($msearch, $entries, $limit)
107 {
108         if (empty($msearch->results)) {
109                 return $entries;
110         }
111
112         foreach ($msearch->results as $profile) {
113                 if (!$profile) {
114                         continue;
115                 }
116
117                 // Already known contact
118                 $contact = Contact::getByURL($profile->url, null, ['rel'], Session::getLocalUser());
119                 if (!empty($contact) && in_array($contact['rel'], [Contact::FRIEND, Contact::SHARING])) {
120                         continue;
121                 }
122
123                 $contact = Contact::getByURLForUser($profile->url, Session::getLocalUser());
124                 if (!empty($contact)) {
125                         $entries[$contact['id']] = ModuleContact::getContactTemplateVars($contact);
126                 }
127
128                 if (count($entries) == $limit) {
129                         break;
130                 }
131         }
132         return $entries;
133 }