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