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