]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
Merge pull request #6977 from annando/fix-reshare
[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\Strings;
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                 $base_image = Photo::selectFirst([], ['resource-id' => $image_id, 'uid' => local_user(), 'scale' => $scale]);
76
77                 $path = 'profile/' . $a->user['nickname'];
78                 if (DBA::isResult($base_image)) {
79
80                         $Image = Photo::getImageForPhoto($base_image);
81                         if ($Image->isValid()) {
82                                 $Image->crop(300, $srcX, $srcY, $srcW, $srcH);
83
84                                 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
85                                                 L10n::t('Profile Photos'), 4, $is_default_profile);
86
87                                 if ($r === false) {
88                                         notice(L10n::t('Image size reduction [%s] failed.', "300") . EOL);
89                                 }
90
91                                 $Image->scaleDown(80);
92
93                                 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
94                                                 L10n::t('Profile Photos'), 5, $is_default_profile);
95
96                                 if ($r === false) {
97                                         notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
98                                 }
99
100                                 $Image->scaleDown(48);
101
102                                 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
103                                                 L10n::t('Profile Photos'), 6, $is_default_profile);
104
105                                 if ($r === false) {
106                                         notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
107                                 }
108
109                                 // If setting for the default profile, unset the profile photo flag from any other photos I own
110
111                                 if ($is_default_profile) {
112                                         q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
113                                                 DBA::escape($base_image['resource-id']), intval(local_user())
114                                         );
115                                 } else {
116                                         q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
117                                                 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
118                                                 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
119                                                 intval($_REQUEST['profile']), intval(local_user())
120                                         );
121                                 }
122
123                                 Contact::updateSelfFromUserID(local_user(), true);
124
125                                 info(L10n::t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
126                                 // Update global directory in background
127                                 if ($path && strlen(Config::get('system', 'directory'))) {
128                                         Worker::add(PRIORITY_LOW, "Directory", $a->getBaseURL() . '/' . $path);
129                                 }
130
131                                 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
132                         } else {
133                                 notice(L10n::t('Unable to process image') . EOL);
134                         }
135                 }
136
137                 $a->internalRedirect($path);
138                 return; // NOTREACHED
139         }
140
141         $src = $_FILES['userfile']['tmp_name'];
142         $filename = basename($_FILES['userfile']['name']);
143         $filesize = intval($_FILES['userfile']['size']);
144         $filetype = $_FILES['userfile']['type'];
145         if ($filetype == "") {
146                 $filetype = Image::guessType($filename);
147         }
148
149         $maximagesize = Config::get('system', 'maximagesize');
150
151         if (($maximagesize) && ($filesize > $maximagesize)) {
152                 notice(L10n::t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)) . EOL);
153                 @unlink($src);
154                 return;
155         }
156
157         $imagedata = @file_get_contents($src);
158         $ph = new Image($imagedata, $filetype);
159
160         if (!$ph->isValid()) {
161                 notice(L10n::t('Unable to process image.') . EOL);
162                 @unlink($src);
163                 return;
164         }
165
166         $ph->orient($src);
167         @unlink($src);
168
169         $imagecrop = profile_photo_crop_ui_head($a, $ph);
170         $a->internalRedirect('profile_photo/use/' . $imagecrop['hash']);
171 }
172
173 function profile_photo_content(App $a)
174 {
175
176         if (!local_user()) {
177                 notice(L10n::t('Permission denied.') . EOL);
178                 return;
179         }
180
181         $newuser = false;
182
183         if ($a->argc == 2 && $a->argv[1] === 'new') {
184                 $newuser = true;
185         }
186
187         $imagecrop = [];
188
189         if (isset($a->argv[1]) && $a->argv[1] == 'use' && $a->argc >= 3) {
190                 // BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
191
192                 $resource_id = $a->argv[2];
193                 //die(":".local_user());
194
195                 $r = Photo::select([], ["resource-id" => $resource_id, "uid" => local_user()], ["order" => ["scale"=>false]]);
196
197                 /*
198                 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC", intval(local_user()),
199                         DBA::escape($resource_id)
200                 );
201                 */
202
203                 if (!DBA::isResult($r)) {
204                         notice(L10n::t('Permission denied.') . EOL);
205                         return;
206                 }
207
208                 $havescale = false;
209                 foreach ($r as $rr) {
210                         if ($rr['scale'] == 5) {
211                                 $havescale = true;
212                         }
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'] == L10n::t('Profile Photos')) && ($havescale)) {
218                         q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
219
220                         q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
221                                 DBA::escape($resource_id)
222                         );
223
224                         Contact::updateSelfFromUserID(local_user(), true);
225
226                         // Update global directory in background
227                         $url = $_SESSION['my_url'];
228                         if ($url && strlen(Config::get('system', 'directory'))) {
229                                 Worker::add(PRIORITY_LOW, "Directory", $url);
230                         }
231
232                         $a->internalRedirect('profile/' . $a->user['nickname']);
233                         return; // NOTREACHED
234                 }
235                 $ph = Photo::getImageForPhoto($r[0]);
236                 
237                 $imagecrop = profile_photo_crop_ui_head($a, $ph);
238                 // go ahead as we have jus uploaded a new photo to crop
239         }
240
241         $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
242                 intval(local_user())
243         );
244
245         if (empty($imagecrop)) {
246                 $tpl = Renderer::getMarkupTemplate('profile_photo.tpl');
247
248                 $o = Renderer::replaceMacros($tpl,
249                         [
250                         '$user' => $a->user['nickname'],
251                         '$lbl_upfile' => L10n::t('Upload File:'),
252                         '$lbl_profiles' => L10n::t('Select a profile:'),
253                         '$title' => L10n::t('Upload Profile Photo'),
254                         '$submit' => L10n::t('Upload'),
255                         '$profiles' => $profiles,
256                         '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
257                         '$select' => sprintf('%s %s', L10n::t('or'),
258                                 ($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>')
259                 ]);
260
261                 return $o;
262         } else {
263                 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
264                 $tpl = Renderer::getMarkupTemplate("cropbody.tpl");
265                 $o = Renderer::replaceMacros($tpl,
266                         [
267                         '$filename'  => $filename,
268                         '$profile'   => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
269                         '$resource'  => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
270                         '$image_url' => System::baseUrl() . '/photo/' . $filename,
271                         '$title'     => L10n::t('Crop Image'),
272                         '$desc'      => L10n::t('Please adjust the image cropping for optimum viewing.'),
273                         '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
274                         '$done'      => L10n::t('Done Editing')
275                 ]);
276                 return $o;
277         }
278 }
279
280 function profile_photo_crop_ui_head(App $a, Image $image)
281 {
282         $max_length = Config::get('system', 'max_image_length');
283         if (!$max_length) {
284                 $max_length = MAX_IMAGE_LENGTH;
285         }
286         if ($max_length > 0) {
287                 $image->scaleDown($max_length);
288         }
289
290         $width = $image->getWidth();
291         $height = $image->getHeight();
292
293         if ($width < 175 || $height < 175) {
294                 $image->scaleUp(300);
295                 $width = $image->getWidth();
296                 $height = $image->getHeight();
297         }
298
299         $hash = Photo::newResource();
300
301
302         $smallest = 0;
303         $filename = '';
304
305         $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
306
307         if ($r) {
308                 info(L10n::t('Image uploaded successfully.') . EOL);
309         } else {
310                 notice(L10n::t('Image upload failed.') . EOL);
311         }
312
313         if ($width > 640 || $height > 640) {
314                 $image->scaleDown(640);
315                 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
316
317                 if ($r === false) {
318                         notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
319                 } else {
320                         $smallest = 1;
321                 }
322         }
323
324         $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate("crophead.tpl"), []);
325
326         $imagecrop = [
327                 'hash'       => $hash,
328                 'resolution' => $smallest,
329                 'ext'        => $image->getExt(),
330         ];
331
332         return $imagecrop;
333 }