]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
Cleanup /format pre-move
[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 function profile_photo_post(App $a) {
15
16         if (! local_user()) {
17                 notice ( t('Permission denied.') . EOL );
18                 return;
19         }
20
21         check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
22
23         if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
24
25                 // unless proven otherwise
26                 $is_default_profile = 1;
27
28                 if($_REQUEST['profile']) {
29                         $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
30                                 intval($_REQUEST['profile']),
31                                 intval(local_user())
32                         );
33                         if (dbm::is_result($r) && (! intval($r[0]['is-default'])))
34                                 $is_default_profile = 0;
35                 }
36
37
38
39                 // phase 2 - we have finished cropping
40
41                 if($a->argc != 2) {
42                         notice( t('Image uploaded but image cropping failed.') . EOL );
43                         return;
44                 }
45
46                 $image_id = $a->argv[1];
47
48                 if(substr($image_id,-2,1) == '-') {
49                         $scale = substr($image_id,-1,1);
50                         $image_id = substr($image_id,0,-2);
51                 }
52
53
54                 $srcX = $_POST['xstart'];
55                 $srcY = $_POST['ystart'];
56                 $srcW = $_POST['xfinal'] - $srcX;
57                 $srcH = $_POST['yfinal'] - $srcY;
58
59                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
60                         dbesc($image_id),
61                         dbesc(local_user()),
62                         intval($scale));
63
64                 if (dbm::is_result($r)) {
65
66                         $base_image = $r[0];
67
68                         $im = new Photo($base_image['data'], $base_image['type']);
69                         if($im->is_valid()) {
70                                 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
71
72                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, $is_default_profile);
73
74                                 if ($r === false) {
75                                         notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
76                                 }
77
78                                 $im->scaleImage(80);
79
80                                 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, $is_default_profile);
81
82                                 if ($r === false) {
83                                         notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
84                                 }
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
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(App::get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
104                                                 dbesc(App::get_baseurl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
105                                                 dbesc(App::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(App::get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
111                                                 dbesc(App::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 = App::get_baseurl() . '/profile/' . $a->user['nickname'];
128                                 if ($url && strlen(get_config('system','directory'))) {
129                                         proc_run(PRIORITY_LOW, "include/directory.php", $url);
130                                 }
131
132                                 require_once('include/profile_update.php');
133                                 profile_change();
134                         } else {
135                                 notice( t('Unable to process image') . EOL);
136                         }
137                 }
138
139                 goaway(App::get_baseurl() . '/profiles');
140                 return; // NOTREACHED
141         }
142
143         $src      = $_FILES['userfile']['tmp_name'];
144         $filename = basename($_FILES['userfile']['name']);
145         $filesize = intval($_FILES['userfile']['size']);
146         $filetype = $_FILES['userfile']['type'];
147         if ($filetype == "") {
148                 $filetype = guess_image_type($filename);
149         }
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 if(! function_exists('profile_photo_content')) {
175 function profile_photo_content(App $a) {
176
177         if (! local_user()) {
178                 notice( t('Permission denied.') . EOL );
179                 return;
180         }
181
182         $newuser = false;
183
184         if($a->argc == 2 && $a->argv[1] === 'new')
185                 $newuser = true;
186
187         if( $a->argv[1]=='use'){
188                 if ($a->argc<3){
189                         notice( t('Permission denied.') . EOL );
190                         return;
191                 };
192
193 //              check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
194
195                 $resource_id = $a->argv[2];
196                 //die(":".local_user());
197                 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
198                         intval(local_user()),
199                         dbesc($resource_id)
200                         );
201                 if (!dbm::is_result($r)){
202                         notice( t('Permission denied.') . EOL );
203                         return;
204                 }
205                 $havescale = false;
206                 foreach ($r as $rr) {
207                         if($rr['scale'] == 5)
208                                 $havescale = true;
209                 }
210
211                 // set an already uloaded photo as profile photo
212                 // if photo is in 'Profile Photos', change it in db
213                 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
214                         $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
215                                 intval(local_user()));
216
217                         $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
218                                 intval(local_user()),
219                                 dbesc($resource_id)
220                                 );
221
222                         $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
223                                 dbesc(datetime_convert()),
224                                 intval(local_user())
225                         );
226
227                         // Update global directory in background
228                         $url = $_SESSION['my_url'];
229                         if ($url && strlen(get_config('system','directory'))) {
230                                 proc_run(PRIORITY_LOW, "include/directory.php", $url);
231                         }
232
233                         goaway(App::get_baseurl() . '/profiles');
234                         return; // NOTREACHED
235                 }
236                 $ph = new Photo($r[0]['data'], $r[0]['type']);
237                 profile_photo_crop_ui_head($a, $ph);
238                 // go ahead as we have jus uploaded a new photo to crop
239         }
240
241         $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
242                 intval(local_user())
243         );
244
245
246         if(! x($a->config,'imagecrop')) {
247
248                 $tpl = get_markup_template('profile_photo.tpl');
249
250                 $o .= replace_macros($tpl,array(
251                         '$user' => $a->user['nickname'],
252                         '$lbl_upfile' => t('Upload File:'),
253                         '$lbl_profiles' => t('Select a profile:'),
254                         '$title' => t('Upload Profile Photo'),
255                         '$submit' => t('Upload'),
256                         '$profiles' => $profiles,
257                         '$form_security_token' => get_form_security_token("profile_photo"),
258                         '$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>')
259                 ));
260
261                 return $o;
262         }
263         else {
264                 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
265                 $resolution = $a->config['imagecrop_resolution'];
266                 $tpl = get_markup_template("cropbody.tpl");
267                 $o .= replace_macros($tpl,array(
268                         '$filename' => $filename,
269                         '$profile' => intval($_REQUEST['profile']),
270                         '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
271                         '$image_url' => App::get_baseurl() . '/photo/' . $filename,
272                         '$title' => t('Crop Image'),
273                         '$desc' => t('Please adjust the image cropping for optimum viewing.'),
274                         '$form_security_token' => get_form_security_token("profile_photo"),
275                         '$done' => t('Done Editing')
276                 ));
277                 return $o;
278         }
279
280         return; // NOTREACHED
281 }}
282
283
284 if(! function_exists('profile_photo_crop_ui_head')) {
285 function profile_photo_crop_ui_head(App $a, $ph) {
286         $max_length = get_config('system','max_image_length');
287         if (! $max_length) {
288                 $max_length = MAX_IMAGE_LENGTH;
289         }
290         if ($max_length > 0) {
291                 $ph->scaleImage($max_length);
292         }
293
294         $width = $ph->getWidth();
295         $height = $ph->getHeight();
296
297         if ($width < 175 || $height < 175) {
298                 $ph->scaleImageUp(200);
299                 $width = $ph->getWidth();
300                 $height = $ph->getHeight();
301         }
302
303         $hash = photo_new_resource();
304
305
306         $smallest = 0;
307
308         $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
309
310         if ($r) {
311                 info( t('Image uploaded successfully.') . EOL );
312         } else {
313                 notice( t('Image upload failed.') . EOL );
314         }
315
316         if ($width > 640 || $height > 640) {
317                 $ph->scaleImage(640);
318                 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
319
320                 if ($r === false) {
321                         notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
322                 } else {
323                         $smallest = 1;
324                 }
325         }
326
327         $a->config['imagecrop'] = $hash;
328         $a->config['imagecrop_resolution'] = $smallest;
329         $a->config['imagecrop_ext'] = $ph->getExt();
330         $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), array());
331         $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), array());
332         return;
333 }}
334