]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Profile/Photo/Index.php
ee551f88bce17850c22b9c05685fb61eaa432345
[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 == 0) {
59                         $maximagesize = INF;
60                 }
61
62                 if ($maximagesize && $filesize > $maximagesize) {
63                         DI::sysmsg()->addNotice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));
64                         @unlink($src);
65                         return;
66                 }
67
68                 $imagedata = @file_get_contents($src);
69                 $Image = new Image($imagedata, $filetype);
70
71                 if (!$Image->isValid()) {
72                         DI::sysmsg()->addNotice(DI::l10n()->t('Unable to process image.'));
73                         @unlink($src);
74                         return;
75                 }
76
77                 $Image->orient($src);
78                 @unlink($src);
79
80                 $max_length = DI::config()->get('system', 'max_image_length', 0);
81                 if ($max_length > 0) {
82                         $Image->scaleDown($max_length);
83                 }
84
85                 $width = $Image->getWidth();
86                 $height = $Image->getHeight();
87
88                 if ($width < 175 || $height < 175) {
89                         $Image->scaleUp(300);
90                         $width = $Image->getWidth();
91                         $height = $Image->getHeight();
92                 }
93
94                 $resource_id = Photo::newResource();
95
96                 $filename = '';
97
98                 if (!Photo::store($Image, DI::userSession()->getLocalUserId(), 0, $resource_id, $filename, DI::l10n()->t(Photo::PROFILE_PHOTOS), 0, Photo::USER_AVATAR)) {
99                         DI::sysmsg()->addNotice(DI::l10n()->t('Image upload failed.'));
100                 }
101
102                 if ($width > 640 || $height > 640) {
103                         $Image->scaleDown(640);
104                         if (!Photo::store($Image, DI::userSession()->getLocalUserId(), 0, $resource_id, $filename, DI::l10n()->t(Photo::PROFILE_PHOTOS), 1, Photo::USER_AVATAR)) {
105                                 DI::sysmsg()->addNotice(DI::l10n()->t('Image size reduction [%s] failed.', '640'));
106                         }
107                 }
108
109                 DI::baseUrl()->redirect('settings/profile/photo/crop/' . $resource_id);
110         }
111
112         protected function content(array $request = []): string
113         {
114                 if (!DI::userSession()->isAuthenticated()) {
115                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
116                 }
117
118                 parent::content();
119
120                 $args = DI::args();
121
122                 $newuser = $args->get($args->getArgc() - 1) === 'new';
123
124                 $contact = Contact::selectFirst(['avatar'], ['uid' => DI::userSession()->getLocalUserId(), 'self' => true]);
125
126                 $tpl = Renderer::getMarkupTemplate('settings/profile/photo/index.tpl');
127                 $o = Renderer::replaceMacros($tpl, [
128                         '$title'           => DI::l10n()->t('Profile Picture Settings'),
129                         '$current_picture' => DI::l10n()->t('Current Profile Picture'),
130                         '$upload_picture'  => DI::l10n()->t('Upload Profile Picture'),
131                         '$lbl_upfile'      => DI::l10n()->t('Upload Picture:'),
132                         '$submit'          => DI::l10n()->t('Upload'),
133                         '$avatar'          => $contact['avatar'],
134                         '$form_security_token' => self::getFormSecurityToken('settings_profile_photo'),
135                         '$select'          => sprintf('%s %s',
136                                 DI::l10n()->t('or'),
137                                 ($newuser) ?
138                                         '<a href="' . DI::baseUrl() . '">' . DI::l10n()->t('skip this step') . '</a>'
139                                         : '<a href="' . DI::baseUrl() . '/profile/' . DI::app()->getLoggedInUserNickname() . '/photos">'
140                                                 . DI::l10n()->t('select a photo from your photo albums') . '</a>'
141                         ),
142                 ]);
143
144                 return $o;
145         }
146 }