]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
Merge pull request #2323 from annando/1601-dfrn-import
[friendica.git] / mod / profile_photo.php
1 <?php
2
3 require_once("include/Photo.php");
4
5 if(! function_exists('profile_photo_init')) {
6 function profile_photo_init(&$a) {
7
8         if(! local_user()) {
9                 return;
10         }
11
12         profile_load($a,$a->user['nickname']);
13 }
14 }
15
16 if(! function_exists('profile_photo_post')) {
17 function profile_photo_post(&$a) {
18
19         if(! local_user()) {
20                 notice ( t('Permission denied.') . EOL );
21                 return;
22         }
23
24         check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
25
26         if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
27
28                 // unless proven otherwise
29                 $is_default_profile = 1;
30
31                 if($_REQUEST['profile']) {
32                         $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
33                                 intval($_REQUEST['profile']),
34                                 intval(local_user())
35                         );
36                         if(count($r) && (! intval($r[0]['is-default'])))
37                                 $is_default_profile = 0;
38                 }
39
40
41
42                 // phase 2 - we have finished cropping
43
44                 if($a->argc != 2) {
45                         notice( t('Image uploaded but image cropping failed.') . EOL );
46                         return;
47                 }
48
49                 $image_id = $a->argv[1];
50
51                 if(substr($image_id,-2,1) == '-') {
52                         $scale = substr($image_id,-1,1);
53                         $image_id = substr($image_id,0,-2);
54                 }
55
56
57                 $srcX = $_POST['xstart'];
58                 $srcY = $_POST['ystart'];
59                 $srcW = $_POST['xfinal'] - $srcX;
60                 $srcH = $_POST['yfinal'] - $srcY;
61
62                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
63                         dbesc($image_id),
64                         dbesc(local_user()),
65                         intval($scale));
66
67                 if(count($r)) {
68
69                         $base_image = $r[0];
70
71                         $im = new Photo($base_image['data'], $base_image['type']);
72                         if($im->is_valid()) {
73                                 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
74
75                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, $is_default_profile);
76
77                                 if($r === false)
78                                         notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
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                                 $im->scaleImage(48);
88
89                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, $is_default_profile);
90
91                                 if($r === false)
92                                         notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
93
94                                 // If setting for the default profile, unset the profile photo flag from any other photos I own
95
96                                 if($is_default_profile) {
97                                         $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
98                                                 dbesc($base_image['resource-id']),
99                                                 intval(local_user())
100                                         );
101
102                                         $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s'  WHERE `self` AND `uid` = %d",
103                                                 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
104                                                 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
105                                                 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-6.' . $im->getExt()),
106                                                 intval(local_user())
107                                         );
108                                 } else {
109                                         $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
110                                                 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
111                                                 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
112                                                 intval($_REQUEST['profile']),
113                                                 intval(local_user())
114                                         );
115                                 }
116
117                                 // we'll set the updated profile-photo timestamp even if it isn't the default profile,
118                                 // so that browsers will do a cache update unconditionally
119
120                                 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
121                                         dbesc(datetime_convert()),
122                                         intval(local_user())
123                                 );
124
125                                 info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
126                                 // Update global directory in background
127                                 $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
128                                 if($url && strlen(get_config('system','directory')))
129                                         proc_run('php',"include/directory.php","$url");
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($a->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 (!count($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('php',"include/directory.php","$url");
229
230                         goaway($a->get_baseurl() . '/profiles');
231                         return; // NOTREACHED
232                 }
233                 $ph = new Photo($r[0]['data'], $r[0]['type']);
234                 profile_photo_crop_ui_head($a, $ph);
235                 // go ahead as we have jus uploaded a new photo to crop
236         }
237
238         $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
239                 intval(local_user())
240         );
241
242
243         if(! x($a->config,'imagecrop')) {
244
245                 $tpl = get_markup_template('profile_photo.tpl');
246
247                 $o .= replace_macros($tpl,array(
248                         '$user' => $a->user['nickname'],
249                         '$lbl_upfile' => t('Upload File:'),
250                         '$lbl_profiles' => t('Select a profile:'),
251                         '$title' => t('Upload Profile Photo'),
252                         '$submit' => t('Upload'),
253                         '$profiles' => $profiles,
254                         '$form_security_token' => get_form_security_token("profile_photo"),
255                         '$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>')
256                 ));
257
258                 return $o;
259         }
260         else {
261                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
262                 $resolution = $a->config['imagecrop_resolution'];
263                 $tpl = get_markup_template("cropbody.tpl");
264                 $o .= replace_macros($tpl,array(
265                         '$filename' => $filename,
266                         '$profile' => intval($_REQUEST['profile']),
267                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
268                         '$image_url' => $a->get_baseurl() . '/photo/' . $filename,
269                         '$title' => t('Crop Image'),
270                         '$desc' => t('Please adjust the image cropping for optimum viewing.'),
271                         '$form_security_token' => get_form_security_token("profile_photo"),
272                         '$done' => t('Done Editing')
273                 ));
274                 return $o;
275         }
276
277         return; // NOTREACHED
278 }}
279
280
281 if(! function_exists('profile_photo_crop_ui_head')) {
282 function profile_photo_crop_ui_head(&$a, $ph){
283         $max_length = get_config('system','max_image_length');
284         if(! $max_length)
285                 $max_length = MAX_IMAGE_LENGTH;
286         if($max_length > 0)
287                 $ph->scaleImage($max_length);
288
289         $width = $ph->getWidth();
290         $height = $ph->getHeight();
291
292         if($width < 175 || $height < 175) {
293                 $ph->scaleImageUp(200);
294                 $width = $ph->getWidth();
295                 $height = $ph->getHeight();
296         }
297
298         $hash = photo_new_resource();
299
300
301         $smallest = 0;
302
303         $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
304
305         if($r)
306                 info( t('Image uploaded successfully.') . EOL );
307         else
308                 notice( t('Image upload failed.') . EOL );
309
310         if($width > 640 || $height > 640) {
311                 $ph->scaleImage(640);
312                 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
313
314                 if($r === false)
315                         notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
316                 else
317                         $smallest = 1;
318         }
319
320         $a->config['imagecrop'] = $hash;
321         $a->config['imagecrop_resolution'] = $smallest;
322         $a->config['imagecrop_ext'] = $ph->getExt();
323         $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), array());
324         $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), array());
325         return;
326 }}