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