4 use Friendica\Core\System;
5 use Friendica\Core\Worker;
7 require_once("include/Photo.php");
9 function profile_photo_init(App $a) {
15 profile_load($a,$a->user['nickname']);
18 function profile_photo_post(App $a) {
21 notice ( t('Permission denied.') . EOL );
25 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
27 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
29 // unless proven otherwise
30 $is_default_profile = 1;
32 if($_REQUEST['profile']) {
33 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
34 intval($_REQUEST['profile']),
37 if (dbm::is_result($r) && (! intval($r[0]['is-default'])))
38 $is_default_profile = 0;
43 // phase 2 - we have finished cropping
46 notice( t('Image uploaded but image cropping failed.') . EOL );
50 $image_id = $a->argv[1];
52 if(substr($image_id,-2,1) == '-') {
53 $scale = substr($image_id,-1,1);
54 $image_id = substr($image_id,0,-2);
58 $srcX = $_POST['xstart'];
59 $srcY = $_POST['ystart'];
60 $srcW = $_POST['xfinal'] - $srcX;
61 $srcH = $_POST['yfinal'] - $srcY;
63 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
68 if (dbm::is_result($r)) {
72 $im = new Photo($base_image['data'], $base_image['type']);
74 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
76 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, $is_default_profile);
79 notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
84 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, $is_default_profile);
87 notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
92 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, $is_default_profile);
95 notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
98 // If setting for the default profile, unset the profile photo flag from any other photos I own
100 if($is_default_profile) {
101 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
102 dbesc($base_image['resource-id']),
106 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `self` AND `uid` = %d",
107 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
108 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
109 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-6.' . $im->getExt()),
113 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
114 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
115 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
116 intval($_REQUEST['profile']),
121 // we'll set the updated profile-photo timestamp even if it isn't the default profile,
122 // so that browsers will do a cache update unconditionally
124 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
125 dbesc(datetime_convert()),
129 info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
130 // Update global directory in background
131 $url = System::baseUrl() . '/profile/' . $a->user['nickname'];
132 if ($url && strlen(get_config('system','directory'))) {
133 Worker::add(PRIORITY_LOW, "directory", $url);
136 Worker::add(PRIORITY_LOW, 'profile_update', local_user());
138 notice( t('Unable to process image') . EOL);
142 goaway(System::baseUrl() . '/profiles');
143 return; // NOTREACHED
146 $src = $_FILES['userfile']['tmp_name'];
147 $filename = basename($_FILES['userfile']['name']);
148 $filesize = intval($_FILES['userfile']['size']);
149 $filetype = $_FILES['userfile']['type'];
150 if ($filetype == "") {
151 $filetype = guess_image_type($filename);
154 $maximagesize = get_config('system','maximagesize');
156 if (($maximagesize) && ($filesize > $maximagesize)) {
157 notice( sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize)) . EOL);
162 $imagedata = @file_get_contents($src);
163 $ph = new Photo($imagedata, $filetype);
165 if (! $ph->is_valid()) {
166 notice( t('Unable to process image.') . EOL );
173 return profile_photo_crop_ui_head($a, $ph);
177 if(! function_exists('profile_photo_content')) {
178 function profile_photo_content(App $a) {
180 if (! local_user()) {
181 notice( t('Permission denied.') . EOL );
187 if($a->argc == 2 && $a->argv[1] === 'new')
190 if( $a->argv[1]=='use'){
192 notice( t('Permission denied.') . EOL );
196 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
198 $resource_id = $a->argv[2];
199 //die(":".local_user());
200 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
201 intval(local_user()),
204 if (!dbm::is_result($r)){
205 notice( t('Permission denied.') . EOL );
209 foreach ($r as $rr) {
210 if($rr['scale'] == 5)
214 // set an already uloaded photo as profile photo
215 // if photo is in 'Profile Photos', change it in db
216 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
217 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
218 intval(local_user()));
220 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
221 intval(local_user()),
225 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
226 dbesc(datetime_convert()),
230 // Update global directory in background
231 $url = $_SESSION['my_url'];
232 if ($url && strlen(get_config('system','directory'))) {
233 Worker::add(PRIORITY_LOW, "directory", $url);
236 goaway(System::baseUrl() . '/profiles');
237 return; // NOTREACHED
239 $ph = new Photo($r[0]['data'], $r[0]['type']);
240 profile_photo_crop_ui_head($a, $ph);
241 // go ahead as we have jus uploaded a new photo to crop
244 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
249 if(! x($a->config,'imagecrop')) {
251 $tpl = get_markup_template('profile_photo.tpl');
253 $o .= replace_macros($tpl,array(
254 '$user' => $a->user['nickname'],
255 '$lbl_upfile' => t('Upload File:'),
256 '$lbl_profiles' => t('Select a profile:'),
257 '$title' => t('Upload Profile Photo'),
258 '$submit' => t('Upload'),
259 '$profiles' => $profiles,
260 '$form_security_token' => get_form_security_token("profile_photo"),
261 '$select' => sprintf('%s %s', t('or'), ($newuser) ? '<a href="' . System::baseUrl() . '">' . t('skip this step') . '</a>' : '<a href="'. System::baseUrl() . '/photos/' . $a->user['nickname'] . '">' . t('select a photo from your photo albums') . '</a>')
267 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
268 $resolution = $a->config['imagecrop_resolution'];
269 $tpl = get_markup_template("cropbody.tpl");
270 $o .= replace_macros($tpl,array(
271 '$filename' => $filename,
272 '$profile' => intval($_REQUEST['profile']),
273 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
274 '$image_url' => System::baseUrl() . '/photo/' . $filename,
275 '$title' => t('Crop Image'),
276 '$desc' => t('Please adjust the image cropping for optimum viewing.'),
277 '$form_security_token' => get_form_security_token("profile_photo"),
278 '$done' => t('Done Editing')
283 return; // NOTREACHED
287 if(! function_exists('profile_photo_crop_ui_head')) {
288 function profile_photo_crop_ui_head(App $a, $ph) {
289 $max_length = get_config('system','max_image_length');
291 $max_length = MAX_IMAGE_LENGTH;
293 if ($max_length > 0) {
294 $ph->scaleImage($max_length);
297 $width = $ph->getWidth();
298 $height = $ph->getHeight();
300 if ($width < 175 || $height < 175) {
301 $ph->scaleImageUp(200);
302 $width = $ph->getWidth();
303 $height = $ph->getHeight();
306 $hash = photo_new_resource();
311 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
314 info( t('Image uploaded successfully.') . EOL );
316 notice( t('Image upload failed.') . EOL );
319 if ($width > 640 || $height > 640) {
320 $ph->scaleImage(640);
321 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
324 notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
330 $a->config['imagecrop'] = $hash;
331 $a->config['imagecrop_resolution'] = $smallest;
332 $a->config['imagecrop_ext'] = $ph->getExt();
333 $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), array());
334 $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), array());