3 require_once("include/Photo.php");
5 if(! function_exists('profile_photo_init')) {
6 function profile_photo_init(&$a) {
12 profile_load($a,$a->user['nickname']);
16 if(! function_exists('profile_photo_post')) {
17 function profile_photo_post(&$a) {
20 notice ( t('Permission denied.') . EOL );
24 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
26 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
28 // unless proven otherwise
29 $is_default_profile = 1;
31 if($_REQUEST['profile']) {
32 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
33 intval($_REQUEST['profile']),
36 if(count($r) && (! intval($r[0]['is-default'])))
37 $is_default_profile = 0;
42 // phase 2 - we have finished cropping
45 notice( t('Image uploaded but image cropping failed.') . EOL );
49 $image_id = $a->argv[1];
51 if(substr($image_id,-2,1) == '-') {
52 $scale = substr($image_id,-1,1);
53 $image_id = substr($image_id,0,-2);
57 $srcX = $_POST['xstart'];
58 $srcY = $_POST['ystart'];
59 $srcW = $_POST['xfinal'] - $srcX;
60 $srcH = $_POST['yfinal'] - $srcY;
62 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
71 $im = new Photo($base_image['data'], $base_image['type']);
73 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
75 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, $is_default_profile);
78 notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
82 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, $is_default_profile);
85 notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
89 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, $is_default_profile);
92 notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
94 // If setting for the default profile, unset the profile photo flag from any other photos I own
96 if($is_default_profile) {
97 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
98 dbesc($base_image['resource-id']),
102 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `self` AND `uid` = %d",
103 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
104 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
105 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-6.' . $im->getExt()),
109 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
110 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
111 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
112 intval($_REQUEST['profile']),
117 // we'll set the updated profile-photo timestamp even if it isn't the default profile,
118 // so that browsers will do a cache update unconditionally
120 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
121 dbesc(datetime_convert()),
125 info( 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 $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
128 if($url && strlen(get_config('system','directory')))
129 proc_run('php',"include/directory.php","$url");
131 require_once('include/profile_update.php');
135 notice( t('Unable to process image') . EOL);
138 goaway($a->get_baseurl() . '/profiles');
139 return; // NOTREACHED
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=="") $filetype=guess_image_type($filename);
148 $maximagesize = get_config('system','maximagesize');
150 if(($maximagesize) && ($filesize > $maximagesize)) {
151 notice( sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize)) . EOL);
156 $imagedata = @file_get_contents($src);
157 $ph = new Photo($imagedata, $filetype);
159 if(! $ph->is_valid()) {
160 notice( t('Unable to process image.') . EOL );
167 return profile_photo_crop_ui_head($a, $ph);
172 if(! function_exists('profile_photo_content')) {
173 function profile_photo_content(&$a) {
176 notice( t('Permission denied.') . EOL );
182 if($a->argc == 2 && $a->argv[1] === 'new')
185 if( $a->argv[1]=='use'){
187 notice( t('Permission denied.') . EOL );
191 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
193 $resource_id = $a->argv[2];
194 //die(":".local_user());
195 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
196 intval(local_user()),
200 notice( t('Permission denied.') . EOL );
205 if($rr['scale'] == 5)
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']== t('Profile Photos')) && ($havescale)){
212 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
213 intval(local_user()));
215 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
216 intval(local_user()),
220 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
221 dbesc(datetime_convert()),
225 // Update global directory in background
226 $url = $_SESSION['my_url'];
227 if($url && strlen(get_config('system','directory')))
228 proc_run('php',"include/directory.php","$url");
230 goaway($a->get_baseurl() . '/profiles');
231 return; // NOTREACHED
233 $ph = new Photo($r[0]['data'], $r[0]['type']);
234 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",
243 if(! x($a->config,'imagecrop')) {
245 $tpl = get_markup_template('profile_photo.tpl');
247 $o .= replace_macros($tpl,array(
248 '$user' => $a->user['nickname'],
249 '$lbl_upfile' => t('Upload File:'),
250 '$lbl_profiles' => t('Select a profile:'),
251 '$title' => t('Upload Profile Photo'),
252 '$submit' => t('Upload'),
253 '$profiles' => $profiles,
254 '$form_security_token' => get_form_security_token("profile_photo"),
255 '$select' => sprintf('%s %s', t('or'), ($newuser) ? '<a href="' . $a->get_baseurl() . '">' . t('skip this step') . '</a>' : '<a href="'. $a->get_baseurl() . '/photos/' . $a->user['nickname'] . '">' . t('select a photo from your photo albums') . '</a>')
261 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
262 $resolution = $a->config['imagecrop_resolution'];
263 $tpl = get_markup_template("cropbody.tpl");
264 $o .= replace_macros($tpl,array(
265 '$filename' => $filename,
266 '$profile' => intval($_REQUEST['profile']),
267 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
268 '$image_url' => $a->get_baseurl() . '/photo/' . $filename,
269 '$title' => t('Crop Image'),
270 '$desc' => t('Please adjust the image cropping for optimum viewing.'),
271 '$form_security_token' => get_form_security_token("profile_photo"),
272 '$done' => t('Done Editing')
277 return; // NOTREACHED
281 if(! function_exists('profile_photo_crop_ui_head')) {
282 function profile_photo_crop_ui_head(&$a, $ph){
283 $max_length = get_config('system','max_image_length');
285 $max_length = MAX_IMAGE_LENGTH;
287 $ph->scaleImage($max_length);
289 $width = $ph->getWidth();
290 $height = $ph->getHeight();
292 if($width < 175 || $height < 175) {
293 $ph->scaleImageUp(200);
294 $width = $ph->getWidth();
295 $height = $ph->getHeight();
298 $hash = photo_new_resource();
303 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
306 info( t('Image uploaded successfully.') . EOL );
308 notice( t('Image upload failed.') . EOL );
310 if($width > 640 || $height > 640) {
311 $ph->scaleImage(640);
312 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
315 notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
320 $a->config['imagecrop'] = $hash;
321 $a->config['imagecrop_resolution'] = $smallest;
322 $a->config['imagecrop_ext'] = $ph->getExt();
323 $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), array());
324 $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), array());