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