]> git.mxchange.org Git - friendica.git/blob - mod/match.php
Fix mods/README.md format
[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\Util\Network;
16 use Friendica\Util\Proxy as ProxyUtils;
17 use Friendica\Util\Strings;
18
19 require_once 'include/text.php';
20
21 /**
22  * @brief Controller for /match.
23  *
24  * It takes keywords from your profile and queries the directory server for
25  * matching keywords from other profiles.
26  *
27  * @param App $a App
28  *
29  * @return void|string
30  */
31 function match_content(App $a)
32 {
33         $o = '';
34         if (! local_user()) {
35                 return;
36         }
37
38         $a->page['aside'] .= Widget::findPeople();
39         $a->page['aside'] .= Widget::follow();
40
41         $_SESSION['return_path'] = $a->cmd;
42
43         $r = q(
44                 "SELECT `pub_keywords`, `prv_keywords` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
45                 intval(local_user())
46         );
47         if (! DBA::isResult($r)) {
48                 return;
49         }
50         if (! $r[0]['pub_keywords'] && (! $r[0]['prv_keywords'])) {
51                 notice(L10n::t('No keywords to match. Please add keywords to your default profile.') . EOL);
52                 return;
53         }
54
55         $params = [];
56         $tags = trim($r[0]['pub_keywords'] . ' ' . $r[0]['prv_keywords']);
57
58         if ($tags) {
59                 $pager = new Pager($a->query_string);
60
61                 $params['s'] = $tags;
62                 if ($pager->getPage() != 1) {
63                         $params['p'] = $pager->getPage();
64                 }
65
66                 if (strlen(Config::get('system', 'directory'))) {
67                         $x = Network::post(get_server().'/msearch', $params)->getBody();
68                 } else {
69                         $x = Network::post(System::baseUrl() . '/msearch', $params)->getBody();
70                 }
71
72                 $j = json_decode($x);
73
74                 if (count($j->results)) {
75                         $pager->setItemsPerPage($j->items_page);
76
77                         $id = 0;
78
79                         foreach ($j->results as $jj) {
80                                 $match_nurl = Strings::normaliseLink($jj->url);
81                                 $match = q(
82                                         "SELECT `nurl` FROM `contact` WHERE `uid` = '%d' AND nurl='%s' LIMIT 1",
83                                         intval(local_user()),
84                                         DBA::escape($match_nurl)
85                                 );
86
87                                 if (!count($match)) {
88                                         $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
89                                         $connlnk = System::baseUrl() . '/follow/?url=' . $jj->url;
90                                         $photo_menu = [
91                                                 'profile' => [L10n::t("View Profile"), Contact::magicLink($jj->url)],
92                                                 'follow' => [L10n::t("Connect/Follow"), $connlnk]
93                                         ];
94
95                                         $contact_details = Contact::getDetailsByURL($jj->url, local_user());
96
97                                         $entry = [
98                                                 'url' => Contact::magicLink($jj->url),
99                                                 'itemurl' => defaults($contact_details, 'addr', $jj->url),
100                                                 'name' => $jj->name,
101                                                 'details'       => defaults($contact_details, 'location', ''),
102                                                 'tags'          => defaults($contact_details, 'keywords', ''),
103                                                 'about'         => defaults($contact_details, 'about', ''),
104                                                 'account_type'  => Contact::getAccountType($contact_details),
105                                                 'thumb' => ProxyUtils::proxifyUrl($jj->photo, false, ProxyUtils::SIZE_THUMB),
106                                                 'inttxt' => ' ' . L10n::t('is interested in:'),
107                                                 'conntxt' => L10n::t('Connect'),
108                                                 'connlnk' => $connlnk,
109                                                 'img_hover' => $jj->tags,
110                                                 'photo_menu' => $photo_menu,
111                                                 'id' => ++$id,
112                                         ];
113                                         $entries[] = $entry;
114                                 }
115                         }
116
117                         $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
118
119                         $o .= Renderer::replaceMacros($tpl, [
120                                 '$title'    => L10n::t('Profile Match'),
121                                 '$contacts' => $entries,
122                                 '$paginate' => $pager->renderFull($j->total)
123                         ]);
124                 } else {
125                         info(L10n::t('No matches') . EOL);
126                 }
127         }
128
129         return $o;
130 }