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