4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6 use Friendica\Core\Worker;
8 require_once("include/Photo.php");
10 function profile_photo_init(App $a) {
16 profile_load($a,$a->user['nickname']);
19 function profile_photo_post(App $a) {
22 notice ( t('Permission denied.') . EOL );
26 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
28 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
30 // unless proven otherwise
31 $is_default_profile = 1;
33 if($_REQUEST['profile']) {
34 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
35 intval($_REQUEST['profile']),
38 if (dbm::is_result($r) && (! intval($r[0]['is-default'])))
39 $is_default_profile = 0;
44 // phase 2 - we have finished cropping
47 notice( t('Image uploaded but image cropping failed.') . EOL );
51 $image_id = $a->argv[1];
53 if(substr($image_id,-2,1) == '-') {
54 $scale = substr($image_id,-1,1);
55 $image_id = substr($image_id,0,-2);
59 $srcX = $_POST['xstart'];
60 $srcY = $_POST['ystart'];
61 $srcW = $_POST['xfinal'] - $srcX;
62 $srcH = $_POST['yfinal'] - $srcY;
64 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
69 if (dbm::is_result($r)) {
73 $im = new Photo($base_image['data'], $base_image['type']);
75 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
77 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, $is_default_profile);
80 notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
85 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, $is_default_profile);
88 notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
93 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, $is_default_profile);
96 notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
99 // If setting for the default profile, unset the profile photo flag from any other photos I own
101 if($is_default_profile) {
102 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
103 dbesc($base_image['resource-id']),
107 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `self` AND `uid` = %d",
108 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
109 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
110 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-6.' . $im->getExt()),
114 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
115 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
116 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
117 intval($_REQUEST['profile']),
122 // we'll set the updated profile-photo timestamp even if it isn't the default profile,
123 // so that browsers will do a cache update unconditionally
125 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
126 dbesc(datetime_convert()),
130 info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
131 // Update global directory in background
132 $url = System::baseUrl() . '/profile/' . $a->user['nickname'];
133 if ($url && strlen(Config::get('system','directory'))) {
134 Worker::add(PRIORITY_LOW, "directory", $url);
137 Worker::add(PRIORITY_LOW, 'profile_update', local_user());
139 notice( t('Unable to process image') . EOL);
143 goaway(System::baseUrl() . '/profiles');
144 return; // NOTREACHED
147 $src = $_FILES['userfile']['tmp_name'];
148 $filename = basename($_FILES['userfile']['name']);
149 $filesize = intval($_FILES['userfile']['size']);
150 $filetype = $_FILES['userfile']['type'];
151 if ($filetype == "") {
152 $filetype = guess_image_type($filename);
155 $maximagesize = Config::get('system','maximagesize');
157 if (($maximagesize) && ($filesize > $maximagesize)) {
158 notice( sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize)) . EOL);
163 $imagedata = @file_get_contents($src);
164 $ph = new Photo($imagedata, $filetype);
166 if (! $ph->is_valid()) {
167 notice( t('Unable to process image.') . EOL );
174 return profile_photo_crop_ui_head($a, $ph);
178 if(! function_exists('profile_photo_content')) {
179 function profile_photo_content(App $a) {
181 if (! local_user()) {
182 notice( t('Permission denied.') . EOL );
188 if($a->argc == 2 && $a->argv[1] === 'new')
191 if( $a->argv[1]=='use'){
193 notice( t('Permission denied.') . EOL );
197 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
199 $resource_id = $a->argv[2];
200 //die(":".local_user());
201 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
202 intval(local_user()),
205 if (!dbm::is_result($r)){
206 notice( t('Permission denied.') . EOL );
210 foreach ($r as $rr) {
211 if($rr['scale'] == 5)
215 // set an already uloaded photo as profile photo
216 // if photo is in 'Profile Photos', change it in db
217 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
218 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
219 intval(local_user()));
221 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
222 intval(local_user()),
226 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
227 dbesc(datetime_convert()),
231 // Update global directory in background
232 $url = $_SESSION['my_url'];
233 if ($url && strlen(Config::get('system','directory'))) {
234 Worker::add(PRIORITY_LOW, "directory", $url);
237 goaway(System::baseUrl() . '/profiles');
238 return; // NOTREACHED
240 $ph = new Photo($r[0]['data'], $r[0]['type']);
241 profile_photo_crop_ui_head($a, $ph);
242 // go ahead as we have jus uploaded a new photo to crop
245 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
250 if(! x($a->config,'imagecrop')) {
252 $tpl = get_markup_template('profile_photo.tpl');
254 $o .= replace_macros($tpl,array(
255 '$user' => $a->user['nickname'],
256 '$lbl_upfile' => t('Upload File:'),
257 '$lbl_profiles' => t('Select a profile:'),
258 '$title' => t('Upload Profile Photo'),
259 '$submit' => t('Upload'),
260 '$profiles' => $profiles,
261 '$form_security_token' => get_form_security_token("profile_photo"),
262 '$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>')
268 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
269 $resolution = $a->config['imagecrop_resolution'];
270 $tpl = get_markup_template("cropbody.tpl");
271 $o .= replace_macros($tpl,array(
272 '$filename' => $filename,
273 '$profile' => intval($_REQUEST['profile']),
274 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
275 '$image_url' => System::baseUrl() . '/photo/' . $filename,
276 '$title' => t('Crop Image'),
277 '$desc' => t('Please adjust the image cropping for optimum viewing.'),
278 '$form_security_token' => get_form_security_token("profile_photo"),
279 '$done' => t('Done Editing')
284 return; // NOTREACHED
288 if(! function_exists('profile_photo_crop_ui_head')) {
289 function profile_photo_crop_ui_head(App $a, $ph) {
290 $max_length = Config::get('system','max_image_length');
292 $max_length = MAX_IMAGE_LENGTH;
294 if ($max_length > 0) {
295 $ph->scaleImage($max_length);
298 $width = $ph->getWidth();
299 $height = $ph->getHeight();
301 if ($width < 175 || $height < 175) {
302 $ph->scaleImageUp(200);
303 $width = $ph->getWidth();
304 $height = $ph->getHeight();
307 $hash = photo_new_resource();
312 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
315 info( t('Image uploaded successfully.') . EOL );
317 notice( t('Image upload failed.') . EOL );
320 if ($width > 640 || $height > 640) {
321 $ph->scaleImage(640);
322 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
325 notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
331 $a->config['imagecrop'] = $hash;
332 $a->config['imagecrop_resolution'] = $smallest;
333 $a->config['imagecrop_ext'] = $ph->getExt();
334 $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), array());
335 $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), array());