]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/Contacts.php
Merge pull request #8879 from MrPetovan/task/8847-httpsig-quotes
[friendica.git] / src / Module / Profile / Contacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Profile;
23
24 use Friendica\Content\ContactSelector;
25 use Friendica\Content\Nav;
26 use Friendica\Content\Pager;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session;
30 use Friendica\Database\DBA;
31 use Friendica\DI;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Profile;
34 use Friendica\Module\BaseProfile;
35 use Friendica\Util\Proxy as ProxyUtils;
36
37 class Contacts extends BaseProfile
38 {
39         public static function content(array $parameters = [])
40         {
41                 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
42                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
43                 }
44
45                 $a = DI::app();
46
47                 //@TODO: Get value from router parameters
48                 $nickname = $a->argv[1];
49                 $type = ($a->argv[3] ?? '') ?: 'all';
50
51                 Nav::setSelected('home');
52
53                 $user = DBA::selectFirst('user', [], ['nickname' => $nickname, 'blocked' => false]);
54                 if (!DBA::isResult($user)) {
55                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
56                 }
57
58                 $a->profile_uid  = $user['uid'];
59
60                 Profile::load($a, $nickname);
61
62                 $is_owner = $a->profile['uid'] == local_user();
63
64                 $o = self::getTabsHTML($a, 'contacts', $is_owner, $nickname);
65
66                 if (!count($a->profile) || $a->profile['hide-friends']) {
67                         notice(DI::l10n()->t('Permission denied.') . EOL);
68                         return $o;
69                 }
70
71                 $condition = [
72                         'uid'     => $a->profile['uid'],
73                         'blocked' => false,
74                         'pending' => false,
75                         'hidden'  => false,
76                         'archive' => false,
77                         'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
78                 ];
79
80                 switch ($type) {
81                         case 'followers': $condition['rel'] = [1, 3]; break;
82                         case 'following': $condition['rel'] = [2, 3]; break;
83                         case 'mutuals': $condition['rel'] = 3; break;
84                 }
85
86                 $total = DBA::count('contact', $condition);
87
88                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
89
90                 $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
91
92                 $contacts_stmt = DBA::select('contact', [], $condition, $params);
93
94                 if (!DBA::isResult($contacts_stmt)) {
95                         info(DI::l10n()->t('No contacts.') . EOL);
96                         return $o;
97                 }
98
99                 $contacts = [];
100
101                 while ($contact = DBA::fetch($contacts_stmt)) {
102                         if ($contact['self']) {
103                                 continue;
104                         }
105
106                         $contact_details = array_merge($contact, Contact::getByURLForUser($contact['url'], $a->profile['uid']));
107
108                         $contacts[] = [
109                                 'id'           => $contact['id'],
110                                 'img_hover'    => DI::l10n()->t('Visit %s\'s profile [%s]', $contact_details['name'], $contact['url']),
111                                 'photo_menu'   => Contact::photoMenu($contact),
112                                 'thumb'        => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
113                                 'name'         => substr($contact_details['name'], 0, 20),
114                                 'username'     => $contact_details['name'],
115                                 'details'      => $contact_details['location'],
116                                 'tags'         => $contact_details['keywords'],
117                                 'about'        => $contact_details['about'],
118                                 'account_type' => Contact::getAccountType($contact_details),
119                                 'url'          => Contact::magicLink($contact['url']),
120                                 'sparkle'      => '',
121                                 'itemurl'      => $contact_details['addr'] ? : $contact['url'],
122                                 'network'      => ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']),
123                         ];
124                 }
125
126                 DBA::close($contacts_stmt);
127
128                 switch ($type) {
129                         case 'followers':    $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total); break;
130                         case 'following':    $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total); break;
131                         case 'mutuals':      $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total); break;
132
133                         case 'all': default: $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total); break;
134                 }
135
136                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
137                 $o .= Renderer::replaceMacros($tpl, [
138                         '$title'    => $title,
139                         '$nickname' => $nickname,
140                         '$type'     => $type,
141
142                         '$all_label' => DI::l10n()->t('All contacts'),
143                         '$followers_label' => DI::l10n()->t('Followers'),
144                         '$following_label' => DI::l10n()->t('Following'),
145                         '$mutuals_label' => DI::l10n()->t('Mutual friends'),
146
147                         '$contacts' => $contacts,
148                         '$paginate' => $pager->renderFull($total),
149                 ]);
150
151                 return $o;
152         }
153 }