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