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