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