4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6 use Friendica\Core\Worker;
7 use Friendica\Database\DBM;
9 require_once("include/Photo.php");
11 function profile_photo_init(App $a) {
17 profile_load($a,$a->user['nickname']);
20 function profile_photo_post(App $a) {
23 notice ( t('Permission denied.') . EOL );
27 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
29 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
31 // unless proven otherwise
32 $is_default_profile = 1;
34 if($_REQUEST['profile']) {
35 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
36 intval($_REQUEST['profile']),
39 if (DBM::is_result($r) && (! intval($r[0]['is-default'])))
40 $is_default_profile = 0;
45 // phase 2 - we have finished cropping
48 notice( t('Image uploaded but image cropping failed.') . EOL );
52 $image_id = $a->argv[1];
54 if(substr($image_id,-2,1) == '-') {
55 $scale = substr($image_id,-1,1);
56 $image_id = substr($image_id,0,-2);
60 $srcX = $_POST['xstart'];
61 $srcY = $_POST['ystart'];
62 $srcW = $_POST['xfinal'] - $srcX;
63 $srcH = $_POST['yfinal'] - $srcY;
65 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
70 if (DBM::is_result($r)) {
74 $im = new Photo($base_image['data'], $base_image['type']);
76 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
78 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, $is_default_profile);
81 notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
86 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, $is_default_profile);
89 notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
94 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, $is_default_profile);
97 notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
100 // If setting for the default profile, unset the profile photo flag from any other photos I own
102 if($is_default_profile) {
103 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
104 dbesc($base_image['resource-id']),
108 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `self` AND `uid` = %d",
109 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
110 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
111 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-6.' . $im->getExt()),
115 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
116 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $im->getExt()),
117 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $im->getExt()),
118 intval($_REQUEST['profile']),
123 // we'll set the updated profile-photo timestamp even if it isn't the default profile,
124 // so that browsers will do a cache update unconditionally
126 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
127 dbesc(datetime_convert()),
131 info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
132 // Update global directory in background
133 $url = System::baseUrl() . '/profile/' . $a->user['nickname'];
134 if ($url && strlen(Config::get('system','directory'))) {
135 Worker::add(PRIORITY_LOW, "directory", $url);
138 Worker::add(PRIORITY_LOW, 'profile_update', local_user());
140 notice( t('Unable to process image') . EOL);
144 goaway(System::baseUrl() . '/profiles');
145 return; // NOTREACHED
148 $src = $_FILES['userfile']['tmp_name'];
149 $filename = basename($_FILES['userfile']['name']);
150 $filesize = intval($_FILES['userfile']['size']);
151 $filetype = $_FILES['userfile']['type'];
152 if ($filetype == "") {
153 $filetype = guess_image_type($filename);
156 $maximagesize = Config::get('system','maximagesize');
158 if (($maximagesize) && ($filesize > $maximagesize)) {
159 notice( sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize)) . EOL);
164 $imagedata = @file_get_contents($src);
165 $ph = new Photo($imagedata, $filetype);
167 if (! $ph->is_valid()) {
168 notice( t('Unable to process image.') . EOL );
175 return profile_photo_crop_ui_head($a, $ph);
179 if(! function_exists('profile_photo_content')) {
180 function profile_photo_content(App $a) {
182 if (! local_user()) {
183 notice( t('Permission denied.') . EOL );
189 if($a->argc == 2 && $a->argv[1] === 'new')
192 if( $a->argv[1]=='use'){
194 notice( t('Permission denied.') . EOL );
198 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
200 $resource_id = $a->argv[2];
201 //die(":".local_user());
202 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
203 intval(local_user()),
206 if (!DBM::is_result($r)){
207 notice( t('Permission denied.') . EOL );
211 foreach ($r as $rr) {
212 if($rr['scale'] == 5)
216 // set an already uloaded photo as profile photo
217 // if photo is in 'Profile Photos', change it in db
218 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
219 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
220 intval(local_user()));
222 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
223 intval(local_user()),
227 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
228 dbesc(datetime_convert()),
232 // Update global directory in background
233 $url = $_SESSION['my_url'];
234 if ($url && strlen(Config::get('system','directory'))) {
235 Worker::add(PRIORITY_LOW, "directory", $url);
238 goaway(System::baseUrl() . '/profiles');
239 return; // NOTREACHED
241 $ph = new Photo($r[0]['data'], $r[0]['type']);
242 profile_photo_crop_ui_head($a, $ph);
243 // go ahead as we have jus uploaded a new photo to crop
246 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
251 if(! x($a->config,'imagecrop')) {
253 $tpl = get_markup_template('profile_photo.tpl');
255 $o .= replace_macros($tpl,array(
256 '$user' => $a->user['nickname'],
257 '$lbl_upfile' => t('Upload File:'),
258 '$lbl_profiles' => t('Select a profile:'),
259 '$title' => t('Upload Profile Photo'),
260 '$submit' => t('Upload'),
261 '$profiles' => $profiles,
262 '$form_security_token' => get_form_security_token("profile_photo"),
263 '$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>')
269 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
270 $resolution = $a->config['imagecrop_resolution'];
271 $tpl = get_markup_template("cropbody.tpl");
272 $o .= replace_macros($tpl,array(
273 '$filename' => $filename,
274 '$profile' => intval($_REQUEST['profile']),
275 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
276 '$image_url' => System::baseUrl() . '/photo/' . $filename,
277 '$title' => t('Crop Image'),
278 '$desc' => t('Please adjust the image cropping for optimum viewing.'),
279 '$form_security_token' => get_form_security_token("profile_photo"),
280 '$done' => t('Done Editing')
285 return; // NOTREACHED
289 if(! function_exists('profile_photo_crop_ui_head')) {
290 function profile_photo_crop_ui_head(App $a, $ph) {
291 $max_length = Config::get('system','max_image_length');
293 $max_length = MAX_IMAGE_LENGTH;
295 if ($max_length > 0) {
296 $ph->scaleImage($max_length);
299 $width = $ph->getWidth();
300 $height = $ph->getHeight();
302 if ($width < 175 || $height < 175) {
303 $ph->scaleImageUp(200);
304 $width = $ph->getWidth();
305 $height = $ph->getHeight();
308 $hash = photo_new_resource();
313 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
316 info( t('Image uploaded successfully.') . EOL );
318 notice( t('Image upload failed.') . EOL );
321 if ($width > 640 || $height > 640) {
322 $ph->scaleImage(640);
323 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
326 notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
332 $a->config['imagecrop'] = $hash;
333 $a->config['imagecrop_resolution'] = $smallest;
334 $a->config['imagecrop_ext'] = $ph->getExt();
335 $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), array());
336 $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), array());