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