3 * @file mod/profile_photo.php
6 use Friendica\Core\Config;
7 use Friendica\Core\L10n;
8 use Friendica\Core\System;
9 use Friendica\Core\Worker;
10 use Friendica\Database\DBM;
11 use Friendica\Model\Photo;
12 use Friendica\Model\Profile;
13 use Friendica\Object\Image;
15 function profile_photo_init(App $a)
21 Profile::load($a, $a->user['nickname']);
24 function profile_photo_post(App $a) {
27 notice(L10n::t('Permission denied.') . EOL );
31 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
33 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
35 // unless proven otherwise
36 $is_default_profile = 1;
38 if($_REQUEST['profile']) {
39 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
40 intval($_REQUEST['profile']),
43 if (DBM::is_result($r) && (! intval($r[0]['is-default'])))
44 $is_default_profile = 0;
49 // phase 2 - we have finished cropping
52 notice(L10n::t('Image uploaded but image cropping failed.') . EOL );
56 $image_id = $a->argv[1];
58 if(substr($image_id,-2,1) == '-') {
59 $scale = substr($image_id,-1,1);
60 $image_id = substr($image_id,0,-2);
64 $srcX = $_POST['xstart'];
65 $srcY = $_POST['ystart'];
66 $srcW = $_POST['xfinal'] - $srcX;
67 $srcH = $_POST['yfinal'] - $srcY;
69 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
74 if (DBM::is_result($r)) {
78 $Image = new Image($base_image['data'], $base_image['type']);
79 if ($Image->isValid()) {
80 $Image->crop(175,$srcX,$srcY,$srcW,$srcH);
82 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'],$base_image['filename'], L10n::t('Profile Photos'), 4, $is_default_profile);
85 notice(L10n::t('Image size reduction [%s] failed.', "175") . EOL);
88 $Image->scaleDown(80);
90 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'],$base_image['filename'], L10n::t('Profile Photos'), 5, $is_default_profile);
93 notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
96 $Image->scaleDown(48);
98 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'],$base_image['filename'], L10n::t('Profile Photos'), 6, $is_default_profile);
101 notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
104 // If setting for the default profile, unset the profile photo flag from any other photos I own
106 if($is_default_profile) {
107 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
108 dbesc($base_image['resource-id']),
112 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `self` AND `uid` = %d",
113 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
114 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
115 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-6.' . $Image->getExt()),
119 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
120 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
121 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
122 intval($_REQUEST['profile']),
127 // we'll set the updated profile-photo timestamp even if it isn't the default profile,
128 // so that browsers will do a cache update unconditionally
130 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
131 dbesc(datetime_convert()),
135 info(L10n::t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
136 // Update global directory in background
137 $url = System::baseUrl() . '/profile/' . $a->user['nickname'];
138 if ($url && strlen(Config::get('system','directory'))) {
139 Worker::add(PRIORITY_LOW, "Directory", $url);
142 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
144 notice(L10n::t('Unable to process image') . EOL);
148 goaway(System::baseUrl() . '/profiles');
149 return; // NOTREACHED
152 $src = $_FILES['userfile']['tmp_name'];
153 $filename = basename($_FILES['userfile']['name']);
154 $filesize = intval($_FILES['userfile']['size']);
155 $filetype = $_FILES['userfile']['type'];
156 if ($filetype == "") {
157 $filetype = Image::guessType($filename);
160 $maximagesize = Config::get('system', 'maximagesize');
162 if (($maximagesize) && ($filesize > $maximagesize)) {
163 notice(L10n::t('Image exceeds size limit of %s', formatBytes($maximagesize)) . EOL);
168 $imagedata = @file_get_contents($src);
169 $ph = new Image($imagedata, $filetype);
171 if (! $ph->isValid()) {
172 notice(L10n::t('Unable to process image.') . EOL);
179 return profile_photo_crop_ui_head($a, $ph);
183 function profile_photo_content(App $a) {
185 if (! local_user()) {
186 notice(L10n::t('Permission denied.') . EOL );
192 if($a->argc == 2 && $a->argv[1] === 'new')
195 if( $a->argv[1]=='use'){
197 notice(L10n::t('Permission denied.') . EOL );
201 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
203 $resource_id = $a->argv[2];
204 //die(":".local_user());
205 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
206 intval(local_user()),
209 if (!DBM::is_result($r)){
210 notice(L10n::t('Permission denied.') . EOL );
214 foreach ($r as $rr) {
215 if($rr['scale'] == 5)
219 // set an already uloaded photo as profile photo
220 // if photo is in 'Profile Photos', change it in db
221 if (($r[0]['album']== L10n::t('Profile Photos')) && ($havescale)){
222 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
223 intval(local_user()));
225 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
226 intval(local_user()),
230 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
231 dbesc(datetime_convert()),
235 // Update global directory in background
236 $url = $_SESSION['my_url'];
237 if ($url && strlen(Config::get('system','directory'))) {
238 Worker::add(PRIORITY_LOW, "Directory", $url);
241 goaway(System::baseUrl() . '/profiles');
242 return; // NOTREACHED
244 $ph = new Image($r[0]['data'], $r[0]['type']);
245 profile_photo_crop_ui_head($a, $ph);
246 // go ahead as we have jus uploaded a new photo to crop
249 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
254 if(! x($a->config,'imagecrop')) {
256 $tpl = get_markup_template('profile_photo.tpl');
258 $o .= replace_macros($tpl,[
259 '$user' => $a->user['nickname'],
260 '$lbl_upfile' => L10n::t('Upload File:'),
261 '$lbl_profiles' => L10n::t('Select a profile:'),
262 '$title' => L10n::t('Upload Profile Photo'),
263 '$submit' => L10n::t('Upload'),
264 '$profiles' => $profiles,
265 '$form_security_token' => get_form_security_token("profile_photo"),
266 '$select' => sprintf('%s %s', L10n::t('or'), ($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>')
272 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
273 $resolution = $a->config['imagecrop_resolution'];
274 $tpl = get_markup_template("cropbody.tpl");
275 $o .= replace_macros($tpl,[
276 '$filename' => $filename,
277 '$profile' => intval($_REQUEST['profile']),
278 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
279 '$image_url' => System::baseUrl() . '/photo/' . $filename,
280 '$title' => L10n::t('Crop Image'),
281 '$desc' => L10n::t('Please adjust the image cropping for optimum viewing.'),
282 '$form_security_token' => get_form_security_token("profile_photo"),
283 '$done' => L10n::t('Done Editing')
288 return; // NOTREACHED
292 function profile_photo_crop_ui_head(App $a, Image $Image) {
293 $max_length = Config::get('system','max_image_length');
295 $max_length = MAX_IMAGE_LENGTH;
297 if ($max_length > 0) {
298 $Image->scaleDown($max_length);
301 $width = $Image->getWidth();
302 $height = $Image->getHeight();
304 if ($width < 175 || $height < 175) {
305 $Image->scaleUp(200);
306 $width = $Image->getWidth();
307 $height = $Image->getHeight();
310 $hash = photo_new_resource();
315 $r = Photo::store($Image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
318 info(L10n::t('Image uploaded successfully.') . EOL);
320 notice(L10n::t('Image upload failed.') . EOL);
323 if ($width > 640 || $height > 640) {
324 $Image->scaleDown(640);
325 $r = Photo::store($Image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
328 notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
334 $a->config['imagecrop'] = $hash;
335 $a->config['imagecrop_resolution'] = $smallest;
336 $a->config['imagecrop_ext'] = $Image->getExt();
337 $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), []);
338 $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), []);