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