]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/Photos.php
Remove unused first parameter from BaseProfile::getTabsHTML
[friendica.git] / src / Module / Profile / Photos.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\App;
25 use Friendica\Content\Pager;
26 use Friendica\Content\Widget;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Session\Capability\IHandleUserSessions;
31 use Friendica\Database\Database;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Photo;
34 use Friendica\Model\Profile;
35 use Friendica\Model\User;
36 use Friendica\Module\Response;
37 use Friendica\Network\HTTPException;
38 use Friendica\Security\Security;
39 use Friendica\Util\Images;
40 use Friendica\Util\Profiler;
41 use Psr\Log\LoggerInterface;
42
43 class Photos extends \Friendica\Module\BaseProfile
44 {
45         /** @var IHandleUserSessions */
46         private $session;
47         /** @var App\Page */
48         private $page;
49         /** @var IManageConfigValues */
50         private $config;
51         /** @var App */
52         private $app;
53         /** @var Database */
54         private $database;
55
56         public function __construct(Database $database, App $app, IManageConfigValues $config, App\Page $page, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
57         {
58                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
59
60                 $this->session  = $session;
61                 $this->page     = $page;
62                 $this->config   = $config;
63                 $this->app      = $app;
64                 $this->database = $database;
65         }
66
67         protected function content(array $request = []): string
68         {
69                 parent::content($request);
70
71                 if ($this->config->get('system', 'block_public') && !$this->session->isAuthenticated()) {
72                         throw new HttpException\ForbiddenException($this->t('Public access denied.'));
73                 }
74
75                 $owner = Profile::load($this->app, $this->parameters['nickname'] ?? '');
76                 if (!$owner || $owner['account_removed'] || $owner['account_expired']) {
77                         throw new HTTPException\NotFoundException($this->t('User not found.'));
78                 }
79
80                 $owner_uid = $owner['uid'];
81                 $is_owner  = $this->session->getLocalUserId() && ($this->session->getLocalUserId() == $owner_uid);
82
83                 $remote_contact = false;
84                 if ($this->session->getRemoteContactID($owner_uid)) {
85                         $contact_id = $this->session->getRemoteContactID($owner_uid);
86
87                         $contact        = Contact::getContactForUser($contact_id, $owner_uid, ['blocked', 'pending']);
88                         $remote_contact = $contact && !$contact['blocked'] && !$contact['pending'];
89                 }
90
91                 if ($owner['hidewall'] && !$this->session->isAuthenticated()) {
92                         $this->baseUrl->redirect('profile/' . $owner['nickname'] . '/restricted');
93                 }
94
95                 $this->session->set('photo_return', $this->args->getCommand());
96
97                 $sql_extra = Security::getPermissionsSQLByUserId($owner_uid);
98
99                 $photo = $this->database->toArray($this->database->p(
100                         "SELECT COUNT(DISTINCT `resource-id`) AS `count`
101                         FROM `photo`
102                         WHERE `uid` = ?
103                           AND `photo-type` = ?
104                           $sql_extra",
105                         $owner['uid'],
106                         Photo::DEFAULT,
107                 ));
108                 $total = $photo[0]['count'];
109
110                 $pager = new Pager($this->l10n, $this->args->getQueryString(), 20);
111
112                 $photos = $this->database->toArray($this->database->p(
113                         "SELECT
114                                 `resource-id`,
115                                 ANY_VALUE(`id`) AS `id`,
116                                 ANY_VALUE(`filename`) AS `filename`,
117                                 ANY_VALUE(`type`) AS `type`,
118                                 ANY_VALUE(`album`) AS `album`,
119                                 max(`scale`) AS `scale`,
120                                 ANY_VALUE(`created`) AS `created`
121                         FROM `photo`
122                         WHERE `uid` = ?
123                           AND `photo-type` = ?
124                           $sql_extra
125                         GROUP BY `resource-id`
126                         ORDER BY `created` DESC
127                     LIMIT ? , ?",
128                         $owner['uid'],
129                         Photo::DEFAULT,
130                         $pager->getStart(),
131                         $pager->getItemsPerPage()
132                 ));
133
134                 $phototypes = Images::supportedTypes();
135
136                 $photos = array_map(function ($photo) use ($owner, $phototypes) {
137                         return [
138                                 'id'    => $photo['id'],
139                                 'link'  => 'photos/' . $owner['nickname'] . '/image/' . $photo['resource-id'],
140                                 'title' => $this->t('View Photo'),
141                                 'src'   => 'photo/' . $photo['resource-id'] . '-' . ((($photo['scale']) == 6) ? 4 : $photo['scale']) . '.' . $phototypes[$photo['type']],
142                                 'alt'   => $photo['filename'],
143                                 'album' => [
144                                         'link' => 'photos/' . $owner['nickname'] . '/album/' . bin2hex($photo['album']),
145                                         'name' => $photo['album'],
146                                         'alt'  => $this->t('View Album'),
147                                 ],
148                         ];
149                 }, $photos);
150
151                 $tpl = Renderer::getMarkupTemplate('photos_head.tpl');
152                 $this->page['htmlhead'] .= Renderer::replaceMacros($tpl, [
153                         '$ispublic' => $this->t('everybody')
154                 ]);
155
156                 if ($albums = Photo::getAlbums($owner['uid'])) {
157                         $albums = array_map(function ($album) use ($owner) {
158                                 return [
159                                         'text'      => $album['album'],
160                                         'total'     => $album['total'],
161                                         'url'       => 'photos/' . $owner['nickname'] . '/album/' . bin2hex($album['album']),
162                                         'urlencode' => urlencode($album['album']),
163                                         'bin2hex'   => bin2hex($album['album'])
164                                 ];
165                         }, $albums);
166
167                         $photo_albums_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('photo_albums.tpl'), [
168                                 '$nick'     => $owner['nickname'],
169                                 '$title'    => $this->t('Photo Albums'),
170                                 '$recent'   => $this->t('Recent Photos'),
171                                 '$albums'   => $albums,
172                                 '$upload'   => [$this->t('Upload New Photos'), 'photos/' . $owner['nickname'] . '/upload'],
173                                 '$can_post' => $this->session->getLocalUserId() && $owner['uid'] == $this->session->getLocalUserId(),
174                         ]);
175                 }
176
177                 if (!empty($photo_albums_widget)) {
178                         $this->page['aside'] .= $photo_albums_widget;
179                 }
180
181                 $o = self::getTabsHTML('photos', $is_owner, $owner['nickname'], Profile::getByUID($owner['uid'])['hide-friends'] ?? false);
182
183                 $tpl = Renderer::getMarkupTemplate('photos_recent.tpl');
184                 $o .= Renderer::replaceMacros($tpl, [
185                         '$title'    => $this->t('Recent Photos'),
186                         '$can_post' => $is_owner || $remote_contact && $owner['page-flags'] == User::PAGE_FLAGS_COMMUNITY,
187                         '$upload'   => [$this->t('Upload New Photos'), 'photos/' . $owner['nickname'] . '/upload'],
188                         '$photos'   => $photos,
189                         '$paginate' => $pager->renderFull($total),
190                 ]);
191
192                 return $o;
193         }
194 }