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