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