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