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