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