3 * @file mod/profile_photo.php
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 use Friendica\Util\Strings;
21 function profile_photo_init(App $a)
27 Profile::load($a, $a->user['nickname']);
30 function profile_photo_post(App $a)
33 notice(L10n::t('Permission denied.') . EOL);
37 BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
39 if (!empty($_POST['cropfinal']) && $_POST['cropfinal'] == 1) {
41 // unless proven otherwise
42 $is_default_profile = 1;
44 if ($_REQUEST['profile']) {
45 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1", intval($_REQUEST['profile']),
49 if (DBA::isResult($r) && (!intval($r[0]['is-default']))) {
50 $is_default_profile = 0;
56 // phase 2 - we have finished cropping
59 notice(L10n::t('Image uploaded but image cropping failed.') . EOL);
63 $image_id = $a->argv[1];
65 if (substr($image_id, -2, 1) == '-') {
66 $scale = substr($image_id, -1, 1);
67 $image_id = substr($image_id, 0, -2);
71 $srcX = $_POST['xstart'];
72 $srcY = $_POST['ystart'];
73 $srcW = $_POST['xfinal'] - $srcX;
74 $srcH = $_POST['yfinal'] - $srcY;
76 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1", DBA::escape($image_id),
77 DBA::escape(local_user()), intval($scale));
79 $path = 'profile/' . $a->user['nickname'];
80 if (DBA::isResult($r)) {
83 $Image = new Image($base_image['data'], $base_image['type']);
84 if ($Image->isValid()) {
85 $Image->crop(300, $srcX, $srcY, $srcW, $srcH);
87 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
88 L10n::t('Profile Photos'), 4, $is_default_profile);
91 notice(L10n::t('Image size reduction [%s] failed.', "300") . EOL);
94 $Image->scaleDown(80);
96 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
97 L10n::t('Profile Photos'), 5, $is_default_profile);
100 notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
103 $Image->scaleDown(48);
105 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
106 L10n::t('Profile Photos'), 6, $is_default_profile);
109 notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
112 // If setting for the default profile, unset the profile photo flag from any other photos I own
114 if ($is_default_profile) {
115 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
116 DBA::escape($base_image['resource-id']), intval(local_user())
119 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
120 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
121 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
122 intval($_REQUEST['profile']), intval(local_user())
126 Contact::updateSelfFromUserID(local_user(), true);
128 info(L10n::t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
129 // Update global directory in background
130 if ($path && strlen(Config::get('system', 'directory'))) {
131 Worker::add(PRIORITY_LOW, "Directory", $a->getBaseURL() . '/' . $path);
134 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
136 notice(L10n::t('Unable to process image') . EOL);
140 $a->internalRedirect($path);
141 return; // NOTREACHED
144 $src = $_FILES['userfile']['tmp_name'];
145 $filename = basename($_FILES['userfile']['name']);
146 $filesize = intval($_FILES['userfile']['size']);
147 $filetype = $_FILES['userfile']['type'];
148 if ($filetype == "") {
149 $filetype = Image::guessType($filename);
152 $maximagesize = Config::get('system', 'maximagesize');
154 if (($maximagesize) && ($filesize > $maximagesize)) {
155 notice(L10n::t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)) . EOL);
160 $imagedata = @file_get_contents($src);
161 $ph = new Image($imagedata, $filetype);
163 if (!$ph->isValid()) {
164 notice(L10n::t('Unable to process image.') . EOL);
172 $imagecrop = profile_photo_crop_ui_head($a, $ph);
173 $a->internalRedirect('profile_photo/use/' . $imagecrop['hash']);
176 function profile_photo_content(App $a)
180 notice(L10n::t('Permission denied.') . EOL);
186 if ($a->argc == 2 && $a->argv[1] === 'new') {
192 if (isset($a->argv[1]) && $a->argv[1] == 'use' && $a->argc >= 3) {
193 // BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
195 $resource_id = $a->argv[2];
196 //die(":".local_user());
197 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC", intval(local_user()),
198 DBA::escape($resource_id)
201 if (!DBA::isResult($r)) {
202 notice(L10n::t('Permission denied.') . EOL);
207 foreach ($r as $rr) {
208 if ($rr['scale'] == 5) {
213 // set an already uloaded photo as profile photo
214 // if photo is in 'Profile Photos', change it in db
215 if (($r[0]['album'] == L10n::t('Profile Photos')) && ($havescale)) {
216 $r = q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
218 $r = q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
219 DBA::escape($resource_id)
222 Contact::updateSelfFromUserID(local_user(), true);
224 // Update global directory in background
225 $url = $_SESSION['my_url'];
226 if ($url && strlen(Config::get('system', 'directory'))) {
227 Worker::add(PRIORITY_LOW, "Directory", $url);
230 $a->internalRedirect('profile/' . $a->user['nickname']);
231 return; // NOTREACHED
233 $ph = new Image($r[0]['data'], $r[0]['type']);
234 $imagecrop = profile_photo_crop_ui_head($a, $ph);
235 // go ahead as we have jus uploaded a new photo to crop
238 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
242 if (empty($imagecrop)) {
243 $tpl = Renderer::getMarkupTemplate('profile_photo.tpl');
245 $o = Renderer::replaceMacros($tpl,
247 '$user' => $a->user['nickname'],
248 '$lbl_upfile' => L10n::t('Upload File:'),
249 '$lbl_profiles' => L10n::t('Select a profile:'),
250 '$title' => L10n::t('Upload Profile Photo'),
251 '$submit' => L10n::t('Upload'),
252 '$profiles' => $profiles,
253 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
254 '$select' => sprintf('%s %s', L10n::t('or'),
255 ($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>')
260 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
261 $tpl = Renderer::getMarkupTemplate("cropbody.tpl");
262 $o = Renderer::replaceMacros($tpl,
264 '$filename' => $filename,
265 '$profile' => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
266 '$resource' => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
267 '$image_url' => System::baseUrl() . '/photo/' . $filename,
268 '$title' => L10n::t('Crop Image'),
269 '$desc' => L10n::t('Please adjust the image cropping for optimum viewing.'),
270 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
271 '$done' => L10n::t('Done Editing')
276 return; // NOTREACHED
279 function profile_photo_crop_ui_head(App $a, Image $image)
281 $max_length = Config::get('system', 'max_image_length');
283 $max_length = MAX_IMAGE_LENGTH;
285 if ($max_length > 0) {
286 $image->scaleDown($max_length);
289 $width = $image->getWidth();
290 $height = $image->getHeight();
292 if ($width < 175 || $height < 175) {
293 $image->scaleUp(300);
294 $width = $image->getWidth();
295 $height = $image->getHeight();
298 $hash = Photo::newResource();
304 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
307 info(L10n::t('Image uploaded successfully.') . EOL);
309 notice(L10n::t('Image upload failed.') . EOL);
312 if ($width > 640 || $height > 640) {
313 $image->scaleDown(640);
314 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
317 notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
323 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate("crophead.tpl"), []);
327 'resolution' => $smallest,
328 'ext' => $image->getExt(),