3 require_once("Photo.php");
5 function profile_photo_init(&$a) {
11 profile_load($a,$a->user['nickname']);
16 function profile_photo_post(&$a) {
19 notice ( t('Permission denied.') . EOL );
23 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
25 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
27 // unless proven otherwise
28 $is_default_profile = 1;
30 if($_REQUEST['profile']) {
31 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
32 intval($_REQUEST['profile']),
35 if(count($r) && (! intval($r[0]['is-default'])))
36 $is_default_profile = 0;
41 // phase 2 - we have finished cropping
44 notice( t('Image uploaded but image cropping failed.') . EOL );
48 $image_id = $a->argv[1];
50 if(substr($image_id,-2,1) == '-') {
51 $scale = substr($image_id,-1,1);
52 $image_id = substr($image_id,0,-2);
56 $srcX = $_POST['xstart'];
57 $srcY = $_POST['ystart'];
58 $srcW = $_POST['xfinal'] - $srcX;
59 $srcH = $_POST['yfinal'] - $srcY;
61 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
70 $im = new Photo($base_image['data'], $base_image['type']);
72 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
74 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, $is_default_profile);
77 notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
81 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, $is_default_profile);
84 notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
88 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, $is_default_profile);
91 notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
93 // If setting for the default profile, unset the profile photo flag from any other photos I own
95 if($is_default_profile) {
96 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
97 dbesc($base_image['resource-id']),
102 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d limit 1",
103 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-4'),
104 dbesc($a->get_baseurl() . '/photo/' . $base_image['resource-id'] . '-5'),
105 intval($_REQUEST['profile']),
110 // we'll set the updated profile-photo timestamp even if it isn't the default profile,
111 // so that browsers will do a cache update unconditionally
113 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
114 dbesc(datetime_convert()),
118 info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
119 // Update global directory in background
120 $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
121 if($url && strlen(get_config('system','directory_submit_url')))
122 proc_run('php',"include/directory.php","$url");
124 require_once('include/profile_update.php');
128 notice( t('Unable to process image') . EOL);
131 goaway($a->get_baseurl() . '/profiles');
132 return; // NOTREACHED
135 $src = $_FILES['userfile']['tmp_name'];
136 $filename = basename($_FILES['userfile']['name']);
137 $filesize = intval($_FILES['userfile']['size']);
138 $filetype = $_FILES['userfile']['type'];
139 if ($filetype=="") $filetype=guess_image_type($filename);
141 $maximagesize = get_config('system','maximagesize');
143 if(($maximagesize) && ($filesize > $maximagesize)) {
144 notice( sprintf(t('Image exceeds size limit of %d'), $maximagesize) . EOL);
149 $imagedata = @file_get_contents($src);
150 $ph = new Photo($imagedata, $filetype);
152 if(! $ph->is_valid()) {
153 notice( t('Unable to process image.') . EOL );
160 return profile_photo_crop_ui_head($a, $ph);
165 if(! function_exists('profile_photo_content')) {
166 function profile_photo_content(&$a) {
169 notice( t('Permission denied.') . EOL );
175 if($a->argc == 2 && $a->argv[1] === 'new')
178 if( $a->argv[1]=='use'){
180 notice( t('Permission denied.') . EOL );
184 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
186 $resource_id = $a->argv[2];
187 //die(":".local_user());
188 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
189 intval(local_user()),
193 notice( t('Permission denied.') . EOL );
198 if($rr['scale'] == 5)
202 // set an already uloaded photo as profile photo
203 // if photo is in 'Profile Photos', change it in db
204 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
205 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
206 intval(local_user()));
208 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
209 intval(local_user()),
213 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
214 dbesc(datetime_convert()),
218 // Update global directory in background
219 $url = $_SESSION['my_url'];
220 if($url && strlen(get_config('system','directory_submit_url')))
221 proc_run('php',"include/directory.php","$url");
223 goaway($a->get_baseurl() . '/profiles');
224 return; // NOTREACHED
226 $ph = new Photo($r[0]['data'], $r[0]['type']);
227 profile_photo_crop_ui_head($a, $ph);
228 // go ahead as we have jus uploaded a new photo to crop
231 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
236 if(! x($a->config,'imagecrop')) {
238 $tpl = get_markup_template('profile_photo.tpl');
240 $o .= replace_macros($tpl,array(
241 '$user' => $a->user['nickname'],
242 '$lbl_upfile' => t('Upload File:'),
243 '$lbl_profiles' => t('Select a profile:'),
244 '$title' => t('Upload Profile Photo'),
245 '$submit' => t('Upload'),
246 '$profiles' => $profiles,
247 '$form_security_token' => get_form_security_token("profile_photo"),
248 '$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>')
254 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
255 $resolution = $a->config['imagecrop_resolution'];
256 $tpl = get_markup_template("cropbody.tpl");
257 $o .= replace_macros($tpl,array(
258 '$filename' => $filename,
259 '$profile' => intval($_REQUEST['profile']),
260 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
261 '$image_url' => $a->get_baseurl() . '/photo/' . $filename,
262 '$title' => t('Crop Image'),
263 '$desc' => t('Please adjust the image cropping for optimum viewing.'),
264 '$form_security_token' => get_form_security_token("profile_photo"),
265 '$done' => t('Done Editing')
270 return; // NOTREACHED
274 if(! function_exists('profile_photo_crop_ui_head')) {
275 function profile_photo_crop_ui_head(&$a, $ph){
276 $max_length = get_config('system','max_image_length');
278 $max_length = MAX_IMAGE_LENGTH;
280 $ph->scaleImage($max_length);
282 $width = $ph->getWidth();
283 $height = $ph->getHeight();
285 if($width < 175 || $height < 175) {
286 $ph->scaleImageUp(200);
287 $width = $ph->getWidth();
288 $height = $ph->getHeight();
291 $hash = photo_new_resource();
296 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
299 info( t('Image uploaded successfully.') . EOL );
301 notice( t('Image upload failed.') . EOL );
303 if($width > 640 || $height > 640) {
304 $ph->scaleImage(640);
305 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
308 notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
313 $a->config['imagecrop'] = $hash;
314 $a->config['imagecrop_resolution'] = $smallest;
315 $a->config['imagecrop_ext'] = $ph->getExt();
316 $a->page['htmlhead'] .= get_markup_template("crophead.tpl");
317 $a->page['end'] .= get_markup_template("cropend.tpl");