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