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