]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
Move Composer autoload require out of boot.php
[friendica.git] / mod / profile_photo.php
1 <?php
2 /**
3  * @file mod/profile_photo.php
4  */
5
6 use Friendica\App;
7 use Friendica\BaseModule;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Core\System;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Photo;
16 use Friendica\Model\Profile;
17 use Friendica\Object\Image;
18 use Friendica\Util\Security;
19 use Friendica\Util\Strings;
20
21 function profile_photo_init(App $a)
22 {
23         if (!local_user()) {
24                 return;
25         }
26
27         Profile::load($a, $a->user['nickname']);
28 }
29
30 function profile_photo_post(App $a)
31 {
32         if (!local_user()) {
33                 notice(L10n::t('Permission denied.') . EOL);
34                 return;
35         }
36
37         BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
38
39         if (!empty($_POST['cropfinal']) && $_POST['cropfinal'] == 1) {
40
41                 // unless proven otherwise
42                 $is_default_profile = 1;
43
44                 if ($_REQUEST['profile']) {
45                         $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1", intval($_REQUEST['profile']),
46                                 intval(local_user())
47                         );
48
49                         if (DBA::isResult($r) && (!intval($r[0]['is-default']))) {
50                                 $is_default_profile = 0;
51                         }
52                 }
53
54
55
56                 // phase 2 - we have finished cropping
57
58                 if ($a->argc != 2) {
59                         notice(L10n::t('Image uploaded but image cropping failed.') . EOL);
60                         return;
61                 }
62
63                 $image_id = $a->argv[1];
64
65                 if (substr($image_id, -2, 1) == '-') {
66                         $scale = substr($image_id, -1, 1);
67                         $image_id = substr($image_id, 0, -2);
68                 }
69
70
71                 $srcX = $_POST['xstart'];
72                 $srcY = $_POST['ystart'];
73                 $srcW = $_POST['xfinal'] - $srcX;
74                 $srcH = $_POST['yfinal'] - $srcY;
75
76                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1", DBA::escape($image_id),
77                         DBA::escape(local_user()), intval($scale));
78
79                 $path = 'profile/' . $a->user['nickname'];
80                 if (DBA::isResult($r)) {
81                         $base_image = $r[0];
82
83                         $Image = new Image($base_image['data'], $base_image['type']);
84                         if ($Image->isValid()) {
85                                 $Image->crop(300, $srcX, $srcY, $srcW, $srcH);
86
87                                 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
88                                                 L10n::t('Profile Photos'), 4, $is_default_profile);
89
90                                 if ($r === false) {
91                                         notice(L10n::t('Image size reduction [%s] failed.', "300") . EOL);
92                                 }
93
94                                 $Image->scaleDown(80);
95
96                                 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
97                                                 L10n::t('Profile Photos'), 5, $is_default_profile);
98
99                                 if ($r === false) {
100                                         notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
101                                 }
102
103                                 $Image->scaleDown(48);
104
105                                 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
106                                                 L10n::t('Profile Photos'), 6, $is_default_profile);
107
108                                 if ($r === false) {
109                                         notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
110                                 }
111
112                                 // If setting for the default profile, unset the profile photo flag from any other photos I own
113
114                                 if ($is_default_profile) {
115                                         $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
116                                                 DBA::escape($base_image['resource-id']), intval(local_user())
117                                         );
118                                 } else {
119                                         $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
120                                                 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
121                                                 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
122                                                 intval($_REQUEST['profile']), intval(local_user())
123                                         );
124                                 }
125
126                                 Contact::updateSelfFromUserID(local_user(), true);
127
128                                 info(L10n::t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
129                                 // Update global directory in background
130                                 if ($path && strlen(Config::get('system', 'directory'))) {
131                                         Worker::add(PRIORITY_LOW, "Directory", $a->getBaseURL() . '/' . $path);
132                                 }
133
134                                 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
135                         } else {
136                                 notice(L10n::t('Unable to process image') . EOL);
137                         }
138                 }
139
140                 $a->internalRedirect($path);
141                 return; // NOTREACHED
142         }
143
144         $src = $_FILES['userfile']['tmp_name'];
145         $filename = basename($_FILES['userfile']['name']);
146         $filesize = intval($_FILES['userfile']['size']);
147         $filetype = $_FILES['userfile']['type'];
148         if ($filetype == "") {
149                 $filetype = Image::guessType($filename);
150         }
151
152         $maximagesize = Config::get('system', 'maximagesize');
153
154         if (($maximagesize) && ($filesize > $maximagesize)) {
155                 notice(L10n::t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)) . EOL);
156                 @unlink($src);
157                 return;
158         }
159
160         $imagedata = @file_get_contents($src);
161         $ph = new Image($imagedata, $filetype);
162
163         if (!$ph->isValid()) {
164                 notice(L10n::t('Unable to process image.') . EOL);
165                 @unlink($src);
166                 return;
167         }
168
169         $ph->orient($src);
170         @unlink($src);
171
172         $imagecrop = profile_photo_crop_ui_head($a, $ph);
173         $a->internalRedirect('profile_photo/use/' . $imagecrop['hash']);
174 }
175
176 function profile_photo_content(App $a)
177 {
178
179         if (!local_user()) {
180                 notice(L10n::t('Permission denied.') . EOL);
181                 return;
182         }
183
184         $newuser = false;
185
186         if ($a->argc == 2 && $a->argv[1] === 'new') {
187                 $newuser = true;
188         }
189
190         $imagecrop = [];
191
192         if (isset($a->argv[1]) && $a->argv[1] == 'use' && $a->argc >= 3) {
193                 // BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
194
195                 $resource_id = $a->argv[2];
196                 //die(":".local_user());
197                 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC", intval(local_user()),
198                         DBA::escape($resource_id)
199                 );
200
201                 if (!DBA::isResult($r)) {
202                         notice(L10n::t('Permission denied.') . EOL);
203                         return;
204                 }
205
206                 $havescale = false;
207                 foreach ($r as $rr) {
208                         if ($rr['scale'] == 5) {
209                                 $havescale = true;
210                         }
211                 }
212
213                 // set an already uloaded photo as profile photo
214                 // if photo is in 'Profile Photos', change it in db
215                 if (($r[0]['album'] == L10n::t('Profile Photos')) && ($havescale)) {
216                         $r = q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
217
218                         $r = q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
219                                 DBA::escape($resource_id)
220                         );
221
222                         Contact::updateSelfFromUserID(local_user(), true);
223
224                         // Update global directory in background
225                         $url = $_SESSION['my_url'];
226                         if ($url && strlen(Config::get('system', 'directory'))) {
227                                 Worker::add(PRIORITY_LOW, "Directory", $url);
228                         }
229
230                         $a->internalRedirect('profile/' . $a->user['nickname']);
231                         return; // NOTREACHED
232                 }
233                 $ph = new Image($r[0]['data'], $r[0]['type']);
234                 $imagecrop = profile_photo_crop_ui_head($a, $ph);
235                 // go ahead as we have jus uploaded a new photo to crop
236         }
237
238         $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
239                 intval(local_user())
240         );
241
242         if (empty($imagecrop)) {
243                 $tpl = Renderer::getMarkupTemplate('profile_photo.tpl');
244
245                 $o = Renderer::replaceMacros($tpl,
246                         [
247                         '$user' => $a->user['nickname'],
248                         '$lbl_upfile' => L10n::t('Upload File:'),
249                         '$lbl_profiles' => L10n::t('Select a profile:'),
250                         '$title' => L10n::t('Upload Profile Photo'),
251                         '$submit' => L10n::t('Upload'),
252                         '$profiles' => $profiles,
253                         '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
254                         '$select' => sprintf('%s %s', L10n::t('or'),
255                                 ($newuser) ? '<a href="' . System::baseUrl() . '">' . L10n::t('skip this step') . '</a>' : '<a href="' . System::baseUrl() . '/photos/' . $a->user['nickname'] . '">' . L10n::t('select a photo from your photo albums') . '</a>')
256                 ]);
257
258                 return $o;
259         } else {
260                 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
261                 $tpl = Renderer::getMarkupTemplate("cropbody.tpl");
262                 $o = Renderer::replaceMacros($tpl,
263                         [
264                         '$filename'  => $filename,
265                         '$profile'   => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
266                         '$resource'  => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
267                         '$image_url' => System::baseUrl() . '/photo/' . $filename,
268                         '$title'     => L10n::t('Crop Image'),
269                         '$desc'      => L10n::t('Please adjust the image cropping for optimum viewing.'),
270                         '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
271                         '$done'      => L10n::t('Done Editing')
272                 ]);
273                 return $o;
274         }
275
276         return; // NOTREACHED
277 }
278
279 function profile_photo_crop_ui_head(App $a, Image $image)
280 {
281         $max_length = Config::get('system', 'max_image_length');
282         if (!$max_length) {
283                 $max_length = MAX_IMAGE_LENGTH;
284         }
285         if ($max_length > 0) {
286                 $image->scaleDown($max_length);
287         }
288
289         $width = $image->getWidth();
290         $height = $image->getHeight();
291
292         if ($width < 175 || $height < 175) {
293                 $image->scaleUp(300);
294                 $width = $image->getWidth();
295                 $height = $image->getHeight();
296         }
297
298         $hash = Photo::newResource();
299
300
301         $smallest = 0;
302         $filename = '';
303
304         $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
305
306         if ($r) {
307                 info(L10n::t('Image uploaded successfully.') . EOL);
308         } else {
309                 notice(L10n::t('Image upload failed.') . EOL);
310         }
311
312         if ($width > 640 || $height > 640) {
313                 $image->scaleDown(640);
314                 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
315
316                 if ($r === false) {
317                         notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
318                 } else {
319                         $smallest = 1;
320                 }
321         }
322
323         $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate("crophead.tpl"), []);
324
325         $imagecrop = [
326                 'hash'       => $hash,
327                 'resolution' => $smallest,
328                 'ext'        => $image->getExt(),
329         ];
330
331         return $imagecrop;
332 }