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