]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
use config var in settings form
[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::selectToArray([], ["resource-id" => $resource_id, "uid" => local_user()], ["order" => ["scale" => false]]);
196                 if (!DBA::isResult($r)) {
197                         notice(L10n::t('Permission denied.') . EOL);
198                         return;
199                 }
200
201                 $havescale = false;
202                 foreach ($r as $rr) {
203                         if ($rr['scale'] == 5) {
204                                 $havescale = true;
205                         }
206                 }
207
208                 // set an already uloaded photo as profile photo
209                 // if photo is in 'Profile Photos', change it in db
210                 if (($r[0]['album'] == L10n::t('Profile Photos')) && ($havescale)) {
211                         q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
212
213                         q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
214                                 DBA::escape($resource_id)
215                         );
216
217                         Contact::updateSelfFromUserID(local_user(), true);
218
219                         // Update global directory in background
220                         $url = $_SESSION['my_url'];
221                         if ($url && strlen(Config::get('system', 'directory'))) {
222                                 Worker::add(PRIORITY_LOW, "Directory", $url);
223                         }
224
225                         $a->internalRedirect('profile/' . $a->user['nickname']);
226                         return; // NOTREACHED
227                 }
228                 $ph = Photo::getImageForPhoto($r[0]);
229                 
230                 $imagecrop = profile_photo_crop_ui_head($a, $ph);
231                 // go ahead as we have jus uploaded a new photo to crop
232         }
233
234         $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
235                 intval(local_user())
236         );
237
238         if (empty($imagecrop)) {
239                 $tpl = Renderer::getMarkupTemplate('profile_photo.tpl');
240
241                 $o = Renderer::replaceMacros($tpl,
242                         [
243                         '$user' => $a->user['nickname'],
244                         '$lbl_upfile' => L10n::t('Upload File:'),
245                         '$lbl_profiles' => L10n::t('Select a profile:'),
246                         '$title' => L10n::t('Upload Profile Photo'),
247                         '$submit' => L10n::t('Upload'),
248                         '$profiles' => $profiles,
249                         '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
250                         '$select' => sprintf('%s %s', L10n::t('or'),
251                                 ($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>')
252                 ]);
253
254                 return $o;
255         } else {
256                 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
257                 $tpl = Renderer::getMarkupTemplate("cropbody.tpl");
258                 $o = Renderer::replaceMacros($tpl,
259                         [
260                         '$filename'  => $filename,
261                         '$profile'   => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
262                         '$resource'  => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
263                         '$image_url' => System::baseUrl() . '/photo/' . $filename,
264                         '$title'     => L10n::t('Crop Image'),
265                         '$desc'      => L10n::t('Please adjust the image cropping for optimum viewing.'),
266                         '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
267                         '$done'      => L10n::t('Done Editing')
268                 ]);
269                 return $o;
270         }
271 }
272
273 function profile_photo_crop_ui_head(App $a, Image $image)
274 {
275         $max_length = Config::get('system', 'max_image_length');
276         if (!$max_length) {
277                 $max_length = MAX_IMAGE_LENGTH;
278         }
279         if ($max_length > 0) {
280                 $image->scaleDown($max_length);
281         }
282
283         $width = $image->getWidth();
284         $height = $image->getHeight();
285
286         if ($width < 175 || $height < 175) {
287                 $image->scaleUp(300);
288                 $width = $image->getWidth();
289                 $height = $image->getHeight();
290         }
291
292         $hash = Photo::newResource();
293
294
295         $smallest = 0;
296         $filename = '';
297
298         $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
299
300         if ($r) {
301                 info(L10n::t('Image uploaded successfully.') . EOL);
302         } else {
303                 notice(L10n::t('Image upload failed.') . EOL);
304         }
305
306         if ($width > 640 || $height > 640) {
307                 $image->scaleDown(640);
308                 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
309
310                 if ($r === false) {
311                         notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
312                 } else {
313                         $smallest = 1;
314                 }
315         }
316
317         $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate("crophead.tpl"), []);
318
319         $imagecrop = [
320                 'hash'       => $hash,
321                 'resolution' => $smallest,
322                 'ext'        => $image->getExt(),
323         ];
324
325         return $imagecrop;
326 }