]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
Merge pull request #2166 from rabuzarus/1012_vier-forum
[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(count($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(count($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($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
103                                                 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
104                                                 dbesc($a->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($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
110                                                 dbesc($a->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 = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
127                                 if($url && strlen(get_config('system','directory')))
128                                         proc_run('php',"include/directory.php","$url");
129
130                                 require_once('include/profile_update.php');
131                                 profile_change();
132                         }
133                         else
134                                 notice( t('Unable to process image') . EOL);
135                 }
136
137                 goaway($a->get_baseurl() . '/profiles');
138                 return; // NOTREACHED
139         }
140
141         $src      = $_FILES['userfile']['tmp_name'];
142         $filename = basename($_FILES['userfile']['name']);
143         $filesize = intval($_FILES['userfile']['size']);
144         $filetype = $_FILES['userfile']['type'];
145     if ($filetype=="") $filetype=guess_image_type($filename);
146     
147         $maximagesize = get_config('system','maximagesize');
148
149         if(($maximagesize) && ($filesize > $maximagesize)) {
150                 notice( sprintf(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 Photo($imagedata, $filetype);
157
158         if(! $ph->is_valid()) {
159                 notice( t('Unable to process image.') . EOL );
160                 @unlink($src);
161                 return;
162         }
163
164         $ph->orient($src);
165         @unlink($src);
166         return profile_photo_crop_ui_head($a, $ph);
167         
168 }
169
170
171 if(! function_exists('profile_photo_content')) {
172 function profile_photo_content(&$a) {
173
174         if(! local_user()) {
175                 notice( t('Permission denied.') . EOL );
176                 return;
177         }
178         
179         $newuser = false;
180
181         if($a->argc == 2 && $a->argv[1] === 'new')
182                 $newuser = true;
183
184         if( $a->argv[1]=='use'){
185                 if ($a->argc<3){
186                         notice( t('Permission denied.') . EOL );
187                         return;
188                 };
189                 
190 //              check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
191         
192                 $resource_id = $a->argv[2];
193                 //die(":".local_user());
194                 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
195                         intval(local_user()),
196                         dbesc($resource_id)
197                         );
198                 if (!count($r)){
199                         notice( t('Permission denied.') . EOL );
200                         return;
201                 }
202                 $havescale = false;
203                 foreach($r as $rr) {
204                         if($rr['scale'] == 5)
205                                 $havescale = true;
206                 }
207
208                 // set an already uloaded photo as profile photo
209                 // if photo is in 'Profile Photos', change it in db
210                 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
211                         $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
212                                 intval(local_user()));
213
214                         $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
215                                 intval(local_user()),
216                                 dbesc($resource_id)
217                                 );
218
219                         $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
220                                 dbesc(datetime_convert()),
221                                 intval(local_user())
222                         );
223
224                         // Update global directory in background
225                         $url = $_SESSION['my_url'];
226                         if($url && strlen(get_config('system','directory')))
227                                 proc_run('php',"include/directory.php","$url");
228
229                         goaway($a->get_baseurl() . '/profiles');
230                         return; // NOTREACHED
231                 }
232                 $ph = new Photo($r[0]['data'], $r[0]['type']);
233                 profile_photo_crop_ui_head($a, $ph);
234                 // go ahead as we have jus uploaded a new photo to crop
235         }
236
237         $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
238                 intval(local_user())
239         );
240
241
242         if(! x($a->config,'imagecrop')) {
243         
244                 $tpl = get_markup_template('profile_photo.tpl');
245
246                 $o .= replace_macros($tpl,array(
247                         '$user' => $a->user['nickname'],
248                         '$lbl_upfile' => t('Upload File:'),
249                         '$lbl_profiles' => t('Select a profile:'),
250                         '$title' => t('Upload Profile Photo'),
251                         '$submit' => t('Upload'),
252                         '$profiles' => $profiles,
253                         '$form_security_token' => get_form_security_token("profile_photo"),
254                         '$select' => sprintf('%s %s', t('or'), ($newuser) ? '<a href="' . $a->get_baseurl() . '">' . t('skip this step') . '</a>' : '<a href="'. $a->get_baseurl() . '/photos/' . $a->user['nickname'] . '">' . t('select a photo from your photo albums') . '</a>')
255                 ));
256
257                 return $o;
258         }
259         else {
260                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
261                 $resolution = $a->config['imagecrop_resolution'];
262                 $tpl = get_markup_template("cropbody.tpl");
263                 $o .= replace_macros($tpl,array(
264                         '$filename' => $filename,
265                         '$profile' => intval($_REQUEST['profile']),
266                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
267                         '$image_url' => $a->get_baseurl() . '/photo/' . $filename,
268                         '$title' => t('Crop Image'),
269                         '$desc' => t('Please adjust the image cropping for optimum viewing.'),
270                         '$form_security_token' => get_form_security_token("profile_photo"),
271                         '$done' => t('Done Editing')
272                 ));
273                 return $o;
274         }
275
276         return; // NOTREACHED
277 }}
278
279
280 if(! function_exists('profile_photo_crop_ui_head')) {
281 function profile_photo_crop_ui_head(&$a, $ph){
282         $max_length = get_config('system','max_image_length');
283         if(! $max_length)
284                 $max_length = MAX_IMAGE_LENGTH;
285         if($max_length > 0)
286                 $ph->scaleImage($max_length);
287
288         $width = $ph->getWidth();
289         $height = $ph->getHeight();
290
291         if($width < 175 || $height < 175) {
292                 $ph->scaleImageUp(200);
293                 $width = $ph->getWidth();
294                 $height = $ph->getHeight();
295         }
296
297         $hash = photo_new_resource();
298         
299
300         $smallest = 0;
301
302         $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );   
303
304         if($r)
305                 info( t('Image uploaded successfully.') . EOL );
306         else
307                 notice( t('Image upload failed.') . EOL );
308
309         if($width > 640 || $height > 640) {
310                 $ph->scaleImage(640);
311                 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );   
312                 
313                 if($r === false)
314                         notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
315                 else
316                         $smallest = 1;
317         }
318
319         $a->config['imagecrop'] = $hash;
320         $a->config['imagecrop_resolution'] = $smallest;
321         $a->config['imagecrop_ext'] = $ph->getExt();
322         $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), array());
323         $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), array());
324         return;
325 }}
326