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