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