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