]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Profile/Photo/Index.php
Removed redundant maximagesize = INF statements
[friendica.git] / src / Module / Settings / Profile / Photo / Index.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\Settings\Profile\Photo;
23
24 use Friendica\Core\Renderer;
25 use Friendica\DI;
26 use Friendica\Model\Contact;
27 use Friendica\Model\Photo;
28 use Friendica\Module\BaseSettings;
29 use Friendica\Network\HTTPException;
30 use Friendica\Object\Image;
31 use Friendica\Util\Images;
32 use Friendica\Util\Strings;
33
34 class Index extends BaseSettings
35 {
36         protected function post(array $request = [])
37         {
38                 if (!DI::userSession()->isAuthenticated()) {
39                         return;
40                 }
41
42                 self::checkFormSecurityTokenRedirectOnError('/settings/profile/photo', 'settings_profile_photo');
43
44                 if (empty($_FILES['userfile'])) {
45                         DI::sysmsg()->addNotice(DI::l10n()->t('Missing uploaded image.'));
46                         return;
47                 }
48
49                 $src = $_FILES['userfile']['tmp_name'];
50                 $filename = basename($_FILES['userfile']['name']);
51                 $filesize = intval($_FILES['userfile']['size']);
52                 $filetype = $_FILES['userfile']['type'];
53
54                 $filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
55
56                 $maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize', 0));
57
58                 if ($maximagesize && $filesize > $maximagesize) {
59                         DI::sysmsg()->addNotice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));
60                         @unlink($src);
61                         return;
62                 }
63
64                 $imagedata = @file_get_contents($src);
65                 $Image = new Image($imagedata, $filetype);
66
67                 if (!$Image->isValid()) {
68                         DI::sysmsg()->addNotice(DI::l10n()->t('Unable to process image.'));
69                         @unlink($src);
70                         return;
71                 }
72
73                 $Image->orient($src);
74                 @unlink($src);
75
76                 $max_length = DI::config()->get('system', 'max_image_length', 0);
77                 if ($max_length > 0) {
78                         $Image->scaleDown($max_length);
79                 }
80
81                 $width = $Image->getWidth();
82                 $height = $Image->getHeight();
83
84                 if ($width < 175 || $height < 175) {
85                         $Image->scaleUp(300);
86                         $width = $Image->getWidth();
87                         $height = $Image->getHeight();
88                 }
89
90                 $resource_id = Photo::newResource();
91
92                 $filename = '';
93
94                 if (!Photo::store($Image, DI::userSession()->getLocalUserId(), 0, $resource_id, $filename, DI::l10n()->t(Photo::PROFILE_PHOTOS), 0, Photo::USER_AVATAR)) {
95                         DI::sysmsg()->addNotice(DI::l10n()->t('Image upload failed.'));
96                 }
97
98                 if ($width > 640 || $height > 640) {
99                         $Image->scaleDown(640);
100                         if (!Photo::store($Image, DI::userSession()->getLocalUserId(), 0, $resource_id, $filename, DI::l10n()->t(Photo::PROFILE_PHOTOS), 1, Photo::USER_AVATAR)) {
101                                 DI::sysmsg()->addNotice(DI::l10n()->t('Image size reduction [%s] failed.', '640'));
102                         }
103                 }
104
105                 DI::baseUrl()->redirect('settings/profile/photo/crop/' . $resource_id);
106         }
107
108         protected function content(array $request = []): string
109         {
110                 if (!DI::userSession()->isAuthenticated()) {
111                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
112                 }
113
114                 parent::content();
115
116                 $args = DI::args();
117
118                 $newuser = $args->get($args->getArgc() - 1) === 'new';
119
120                 $contact = Contact::selectFirst(['avatar'], ['uid' => DI::userSession()->getLocalUserId(), 'self' => true]);
121
122                 $tpl = Renderer::getMarkupTemplate('settings/profile/photo/index.tpl');
123                 $o = Renderer::replaceMacros($tpl, [
124                         '$title'           => DI::l10n()->t('Profile Picture Settings'),
125                         '$current_picture' => DI::l10n()->t('Current Profile Picture'),
126                         '$upload_picture'  => DI::l10n()->t('Upload Profile Picture'),
127                         '$lbl_upfile'      => DI::l10n()->t('Upload Picture:'),
128                         '$submit'          => DI::l10n()->t('Upload'),
129                         '$avatar'          => $contact['avatar'],
130                         '$form_security_token' => self::getFormSecurityToken('settings_profile_photo'),
131                         '$select'          => sprintf('%s %s',
132                                 DI::l10n()->t('or'),
133                                 ($newuser) ?
134                                         '<a href="' . DI::baseUrl() . '">' . DI::l10n()->t('skip this step') . '</a>'
135                                         : '<a href="' . DI::baseUrl() . '/profile/' . DI::app()->getLoggedInUserNickname() . '/photos">'
136                                                 . DI::l10n()->t('select a photo from your photo albums') . '</a>'
137                         ),
138                 ]);
139
140                 return $o;
141         }
142 }