]> git.mxchange.org Git - friendica.git/blob - mod/viewcontacts.php
Merge pull request #6053 from zeroadam/CoreRenderer
[friendica.git] / mod / viewcontacts.php
1 <?php
2 /**
3  * @file mod/viewcontacts.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Pager;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Profile;
18 use Friendica\Util\Proxy as ProxyUtils;
19
20 function viewcontacts_init(App $a)
21 {
22         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
23                 System::httpExit(403, ["title" => L10n::t('Access denied.')]);
24         }
25
26         if ($a->argc < 2) {
27                 System::httpExit(403, ["title" => L10n::t('Access denied.')]);
28         }
29
30         Nav::setSelected('home');
31
32         $nick = $a->argv[1];
33         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
34                 DBA::escape($nick)
35         );
36
37         if (!DBA::isResult($r)) {
38                 System::httpExit(404, ["title" => L10n::t('Page not found.')]);
39         }
40
41         $a->data['user'] = $r[0];
42         $a->profile_uid = $r[0]['uid'];
43         $is_owner = (local_user() && (local_user() == $a->profile_uid));
44
45         Profile::load($a, $a->argv[1]);
46 }
47
48 function viewcontacts_content(App $a)
49 {
50         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
51                 notice(L10n::t('Public access denied.') . EOL);
52                 return;
53         }
54
55         $is_owner = $a->profile['profile_uid'] == local_user();
56
57         $o = "";
58
59         // tabs
60         $o .= Profile::getTabs($a, $is_owner, $a->data['user']['nickname']);
61
62         if (!count($a->profile) || $a->profile['hide-friends']) {
63                 notice(L10n::t('Permission denied.') . EOL);
64                 return $o;
65         }
66
67         $total = 0;
68         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
69                 WHERE `uid` = %d AND NOT `blocked` AND NOT `pending`
70                         AND NOT `hidden` AND NOT `archive`
71                         AND `network` IN ('%s', '%s', '%s')",
72                 intval($a->profile['uid']),
73                 DBA::escape(Protocol::DFRN),
74                 DBA::escape(Protocol::DIASPORA),
75                 DBA::escape(Protocol::OSTATUS)
76         );
77         if (DBA::isResult($r)) {
78                 $total = $r[0]['total'];
79         }
80         $pager = new Pager($a->query_string);
81
82         $r = q("SELECT * FROM `contact`
83                 WHERE `uid` = %d AND NOT `blocked` AND NOT `pending`
84                         AND NOT `hidden` AND NOT `archive`
85                         AND `network` IN ('%s', '%s', '%s')
86                 ORDER BY `name` ASC LIMIT %d, %d",
87                 intval($a->profile['uid']),
88                 DBA::escape(Protocol::DFRN),
89                 DBA::escape(Protocol::DIASPORA),
90                 DBA::escape(Protocol::OSTATUS),
91                 $pager->getStart(),
92                 $pager->getItemsPerPage()
93         );
94         if (!DBA::isResult($r)) {
95                 info(L10n::t('No contacts.').EOL);
96                 return $o;
97         }
98
99         $contacts = [];
100
101         foreach ($r as $rr) {
102                 /// @TODO This triggers an E_NOTICE if 'self' is not there
103                 if ($rr['self']) {
104                         continue;
105                 }
106
107                 $contact_details = Contact::getDetailsByURL($rr['url'], $a->profile['uid'], $rr);
108
109                 $contacts[] = [
110                         'id' => $rr['id'],
111                         'img_hover' => L10n::t('Visit %s\'s profile [%s]', $contact_details['name'], $rr['url']),
112                         'photo_menu' => Contact::photoMenu($rr),
113                         'thumb' => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
114                         'name' => htmlentities(substr($contact_details['name'], 0, 20)),
115                         'username' => htmlentities($contact_details['name']),
116                         'details'       => $contact_details['location'],
117                         'tags'          => $contact_details['keywords'],
118                         'about'         => $contact_details['about'],
119                         'account_type'  => Contact::getAccountType($contact_details),
120                         'url' => Contact::magicLink($rr['url']),
121                         'sparkle' => '',
122                         'itemurl' => (($contact_details['addr'] != "") ? $contact_details['addr'] : $rr['url']),
123                         'network' => ContactSelector::networkToName($rr['network'], $rr['url']),
124                 ];
125         }
126
127
128         $tpl = Renderer::getMarkupTemplate("viewcontact_template.tpl");
129         $o .= Renderer::replaceMacros($tpl, [
130                 '$title' => L10n::t('Contacts'),
131                 '$contacts' => $contacts,
132                 '$paginate' => $pager->renderFull($total),
133         ]);
134
135         return $o;
136 }