]> git.mxchange.org Git - friendica.git/blob - src/Module/Media/Photo/Browser.php
spelling: one
[friendica.git] / src / Module / Media / Photo / Browser.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Media\Photo;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Core\System;
30 use Friendica\Model\Photo;
31 use Friendica\Module\Response;
32 use Friendica\Network\HTTPException\UnauthorizedException;
33 use Friendica\Util\Images;
34 use Friendica\Util\Profiler;
35 use Friendica\Util\Strings;
36 use Psr\Log\LoggerInterface;
37
38 /**
39  * Browser for Photos
40  */
41 class Browser extends BaseModule
42 {
43         /** @var IHandleUserSessions */
44         protected $session;
45         /** @var App */
46         protected $app;
47
48         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, App $app, array $server, array $parameters = [])
49         {
50                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
51
52                 $this->session = $session;
53                 $this->app     = $app;
54         }
55
56         protected function content(array $request = []): string
57         {
58                 if (!$this->session->getLocalUserId()) {
59                         throw new UnauthorizedException($this->t('You need to be logged in to access this page.'));
60                 }
61
62                 // Needed to match the correct template in a module that uses a different theme than the user/site/default
63                 $theme = Strings::sanitizeFilePathItem($request['theme'] ?? '');
64                 if ($theme && is_file("view/theme/$theme/config.php")) {
65                         $this->app->setCurrentTheme($theme);
66                 }
67
68                 $album = $this->parameters['album'] ?? null;
69
70                 $photos = Photo::getBrowsablePhotosForUser($this->session->getLocalUserId(), $album);
71                 $albums = $album ? false : Photo::getBrowsableAlbumsForUser($this->session->getLocalUserId());
72
73                 $path = [
74                         '' => $this->t('Photos'),
75                 ];
76                 if (!empty($album)) {
77                         $path[$album] = $album;
78                 }
79
80                 $photosArray = array_map([$this, 'map_files'], $photos);
81
82                 $tpl    = Renderer::getMarkupTemplate('media/browser.tpl');
83                 $output = Renderer::replaceMacros($tpl, [
84                         '$type'     => 'photo',
85                         '$path'     => $path,
86                         '$folders'  => $albums,
87                         '$files'    => $photosArray,
88                         '$cancel'   => $this->t('Cancel'),
89                         '$nickname' => $this->app->getLoggedInUserNickname(),
90                         '$upload'   => $this->t('Upload'),
91                 ]);
92
93                 if (empty($request['mode'])) {
94                         System::httpExit($output);
95                 }
96
97                 return $output;
98         }
99
100         protected function map_files(array $record): array
101         {
102                 $types      = Images::supportedTypes();
103                 $ext        = $types[$record['type']];
104                 $filename_e = $record['filename'];
105
106                 // Take the largest picture that is smaller or equal 640 pixels
107                 $photo = Photo::selectFirst(
108                         ['scale'],
109                         [
110                                 "`resource-id` = ? AND `height` <= ? AND `width` <= ?",
111                                 $record['resource-id'],
112                                 640,
113                                 640
114                         ],
115                         ['order' => ['scale']]);
116                 $scale = $photo['scale'] ?? $record['loq'];
117
118                 return [
119                         sprintf('%s/photos/%s/image/%s', $this->baseUrl, $this->app->getLoggedInUserNickname(), $record['resource-id']),
120                         $filename_e,
121                         sprintf('%s/photo/%s-%s.%s', $this->baseUrl, $record['resource-id'], $scale, $ext),
122                         $record['desc'],
123                 ];
124         }
125 }