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