From ea3b2755ad08f963796cbe9a975653c76d40eb66 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Sun, 17 Jun 2007 06:18:26 +0200 Subject: [PATCH] [svn r12622] Added code to handle the user's upload directory split when the new admin settings 'split users upload directory' is enabled (the DLTT translation is clear about the fact that admins unsure shouldn't use it). --- main/auth/profile.php | 52 +++++++++++++------- main/inc/lib/usermanager.lib.php | 81 ++++++++++++++++++++++++++++++-- 2 files changed, 112 insertions(+), 21 deletions(-) diff --git a/main/auth/profile.php b/main/auth/profile.php index fc794b7054..90792d671b 100644 --- a/main/auth/profile.php +++ b/main/auth/profile.php @@ -1,5 +1,5 @@ addRule('phone', get_lang('EmailWrong'), 'email');*/ // PICTURE if (is_profile_editable() && api_get_setting('profile', 'picture') == 'true') { - $form->addElement('file', 'picture', (get_user_image($_user['user_id']) != '' ? get_lang('UpdateImage') : get_lang('AddImage'))); + $form->addElement('file', 'picture', ($user_image != '' ? get_lang('UpdateImage') : get_lang('AddImage'))); $form->add_progress_bar(); if( strlen($user_data['picture_uri']) > 0) { @@ -276,6 +275,7 @@ function is_profile_editable() */ /** + * Deprecated function. Use UserManager::get_user_picture_path_by_id($user_id,'none') instead * Get a user's display picture. If the user doesn't have a picture, this * function will return an empty string. * @@ -284,6 +284,9 @@ function is_profile_editable() */ function get_user_image($user_id) { + $path = UserManager::get_user_picture_path_by_id($user_id,'none'); + return $path['file']; + /* $table_user = Database :: get_main_table(TABLE_MAIN_USER); $sql = "SELECT picture_uri FROM $table_user WHERE user_id = '$user_id'"; $result = api_sql_query($sql, __FILE__, __LINE__); @@ -294,6 +297,7 @@ function get_user_image($user_id) $image = ''; return $image; + */ } /** @@ -309,15 +313,20 @@ function upload_user_image($user_id) * Moved inside a function and refactored by Thomas Corthals - 2005-11-04 */ - $image_repository = api_get_path(SYS_CODE_PATH).'upload/users/'; - $existing_image = get_user_image($user_id); + $image_path = UserManager::get_user_picture_path_by_id($user_id,'system',true); + $image_repository = $image_path['dir']; + $existing_image = $image_path['file']; $file_extension = explode('.', $_FILES['picture']['name']); $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]); if (!file_exists($image_repository)) + { + //error_log('Making path '.$image_repository,0); mkpath($image_repository); - + }else{ + //error_log('Path '.$image_repository.' exists',0); + } if ($existing_image != '') { if (KEEP_THE_NAME_WHEN_CHANGE_IMAGE) @@ -367,8 +376,9 @@ function upload_user_image($user_id) */ function remove_user_image($user_id) { - $image_repository = api_get_path(SYS_CODE_PATH).'upload/users/'; - $image = get_user_image($user_id); + $image_path = UserManager::get_user_picture_path_by_id($user_id,'system'); + $image_repository = $image_path['dir']; + $image = $image_path['file']; if ($image != '') { @@ -409,7 +419,8 @@ function build_production_list($user_id, $force = false) if (empty($productions)) return false; - $production_dir = api_get_path(WEB_CODE_PATH)."upload/users/$user_id/"; + $production_path = UserManager::get_user_picture_path_by_id($user_id,'web',true); + $production_dir = $production_path['dir']; $del_image = api_get_path(WEB_CODE_PATH).'img/delete.gif'; $del_text = get_lang('Delete'); @@ -434,7 +445,8 @@ function build_production_list($user_id, $force = false) */ function get_user_productions($user_id) { - $production_repository = api_get_path(SYS_CODE_PATH)."upload/users/$user_id/"; + $production_path = UserManager::get_user_picture_path_by_id($user_id,'system',true); + $production_repository = $production_path['dir'].$user_id.'/'; $productions = array(); if (is_dir($production_repository)) @@ -461,7 +473,8 @@ function get_user_productions($user_id) */ function upload_user_production($user_id) { - $production_repository = api_get_path(SYS_CODE_PATH)."upload/users/$user_id/"; + $image_path = UserManager::get_user_picture_path_by_id($user_id,'system',true); + $production_repository = $image_path['dir'].$user_id.'/'; if (!file_exists($production_repository)) mkpath($production_repository); @@ -483,7 +496,8 @@ function upload_user_production($user_id) */ function remove_user_production($user_id, $production) { - unlink(api_get_path(SYS_CODE_PATH)."upload/users/$user_id/$production"); + $production_path = UserManager::get_user_picture_path_by_id($user_id,'system',true); + unlink($production_path['dir'].$user_id.'/'.$production); } /* @@ -592,8 +606,10 @@ elseif ($update_success) Display :: display_normal_message(get_lang('ProfileReg')); } // USER PICTURE -$image = get_user_image($_user['user_id']); -$image_file = ($image != '' ? api_get_path(WEB_CODE_PATH)."upload/users/$image" : api_get_path(WEB_CODE_PATH).'img/unknown.jpg'); +$image_path = UserManager::get_user_picture_path_by_id($_user['user_id'],'web'); +$image_dir = $image_path['dir']; +$image = $image_path['file']; +$image_file = ($image != '' ? $image_dir.$image : api_get_path(WEB_CODE_PATH).'img/unknown.jpg'); $image_size = @getimagesize($image_file); $img_attributes = 'src="'.$image_file.'?rand='.time().'" ' diff --git a/main/inc/lib/usermanager.lib.php b/main/inc/lib/usermanager.lib.php index da0d062b48..feb5051eed 100644 --- a/main/inc/lib/usermanager.lib.php +++ b/main/inc/lib/usermanager.lib.php @@ -280,7 +280,14 @@ class UserManager $user_table = Database :: get_main_table(TABLE_MAIN_USER); $sql = "SELECT * FROM $user_table WHERE username='".$username."'"; $res = api_sql_query($sql,__FILE__,__LINE__); - $user = mysql_fetch_array($res,MYSQL_ASSOC); + if(Database::num_rows($res)>0) + { + $user = Database::fetch_array($res); + } + else + { + $user = false; + } return $user; } @@ -295,7 +302,14 @@ class UserManager $user_table = Database :: get_main_table(TABLE_MAIN_USER); $sql = "SELECT * FROM $user_table WHERE user_id=".$user_id; $res = api_sql_query($sql,__FILE__,__LINE__); - $user = mysql_fetch_array($res,MYSQL_ASSOC); + if(Database::num_rows($res)>0) + { + $user = Database::fetch_array($res); + } + else + { + $user = false; + } return $user; } @@ -314,6 +328,67 @@ class UserManager } echo ""; } - + /** + * Get user picture URL or path from user ID (returns an array). + * The return format is a complete path, enabling recovery of the directory + * with dirname() or the file with basename(). This also works for the + * functions dealing with the user's productions, as they are located in + * the same directory. + * @param integer User ID + * @param string Type of path to return (can be 'none','system','rel','web') + * @param bool Whether we want to have the directory name returned 'as if' there was a file or not (in the case we want to know which directory to create - otherwise no file means no split subdir) + * @return array Array of 2 elements: 'dir' and 'file' which contain the dir and file as the name implies + */ + function get_user_picture_path_by_id($id,$type='none',$preview=false) + { + if(empty($id) or empty($type)) + { + //$error = 'Insufficient parameters'; + return array('dir'=>'','file'=>''); + } + $user_id = intval($id); + $user_table = Database :: get_main_table(TABLE_MAIN_USER); + $sql = "SELECT picture_uri FROM $user_table WHERE user_id=".$user_id; + $res = api_sql_query($sql,__FILE__,__LINE__); + if(Database::num_rows($res)>0) + { + $user = Database::fetch_array($res); + } + else + { + $user = false; + return array('dir'=>'','file'=>''); + } + $path = trim($user['picture_uri']); + $dir = ''; + $first = ''; + if(api_get_setting('split_users_upload_directory') === 'true') + { + if(!empty($path)) + { + $first = substr($path,0,1).'/'; + } + elseif($preview==true) + { + $first = substr(''.$user_id,0,1).'/'; + } + } + switch($type) + { + case 'system': //return the complete path to the file, from root + $dir = api_get_path(SYS_CODE_PATH).'upload/users/'.$first; + break; + case 'rel': //return the relative path to the file, from the Dokeos base dir + $dir = api_get_path(REL_CODE_PATH).'upload/users/'.$first; + break; + case 'web': //return the complete web URL to the file + $dir = api_get_path(WEB_CODE_PATH).'upload/users/'.$first; + break; + case 'none': //return only the picture_uri (as is, without subdir) + default: + break; + } + return array('dir'=>$dir,'file'=>$path); + } } ?> \ No newline at end of file