Selaa lähdekoodia

Feature #306 - Code rework about uploading user photos in administration section and in profile tool.

Ivan Tcholakov 15 vuotta sitten
vanhempi
commit
3eea1f1a8d
4 muutettua tiedostoa jossa 125 lisäystä ja 167 poistoa
  1. 1 31
      main/admin/user_add.php
  2. 4 33
      main/admin/user_edit.php
  3. 2 99
      main/auth/profile.php
  4. 118 4
      main/inc/lib/usermanager.lib.php

+ 1 - 31
main/admin/user_add.php

@@ -345,38 +345,8 @@ if( $form->validate()) {
 		$user_id = UserManager::create_user($firstname, $lastname, $status, $email, $username, $password, $official_code, $language, $phone, $picture_uri, $auth_source, $expiration_date, $active, $hr_dept_id);
 
 		if (!empty($picture['name'])) {
-
-			// picture path
-			if (api_get_setting('split_users_upload_directory') === 'true') {
-				$picture_path = api_get_path(SYS_CODE_PATH).'upload/users/'.substr((string)$user_id, 0, 1).'/'.$user_id.'/';
-			} else {
-				$picture_path = api_get_path(SYS_CODE_PATH).'upload/users/'.$user_id.'/';
-			}
-
-			$picture_uri = $user_id.'_'.uniqid('').'_'.replace_dangerous_char($picture['name']);
-
+			$picture_uri = UserManager::update_user_picture($user_id, $_FILES['picture']['name'], $_FILES['picture']['tmp_name']);
 			UserManager::update_user($user_id, $firstname, $lastname, $username, $password, $auth_source, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active, null, $hr_dept_id, null, $language);
-
-			$perm = api_get_setting('permissions_for_new_directories');
-			$perm = octdec(!empty($perm) ? $perm : '0770');
-			if (!file_exists($picture_path)) {
-				@mkdir($picture_path, $perm, true);
-			}
-
-			$picture_info = @getimagesize($_FILES['picture']['tmp_name']);
-			$type=$picture_info[2];
-			$small_picture = UserManager::resize_picture($_FILES['picture']['tmp_name'], 22);
-			$medium_picture = UserManager::resize_picture($_FILES['picture']['tmp_name'], 85);
-			$normal_picture = UserManager::resize_picture($_FILES['picture']['tmp_name'], 200);
-			$big_picture = new image($_FILES['picture']['tmp_name']); // This is the original picture.
-
-			$picture_types = array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG');
-			if (in_array($type, array_keys($picture_types))) {
-				$small_picture->send_image($picture_types[$type], $picture_path.'small_'.$picture_uri);
-				$medium_picture->send_image($picture_types[$type], $picture_path.'medium_'.$picture_uri);
-				$normal_picture->send_image($picture_types[$type], $picture_path.$picture_uri);
-				$big_picture->send_image($picture_types[$type], $picture_path.'big_'.$picture_uri);
-			}
 		}
 
 		$extras = array();

+ 4 - 33
main/admin/user_edit.php

@@ -310,40 +310,11 @@ if ( $form->validate()) {
 	$picture = $picture_element->getValue();
 
 	$picture_uri = $user_data['picture_uri'];
-
-	//get the picture directory
-	$picture_path_info = UserManager::get_user_picture_path_by_id($user_id, 'system', true);
-	$picture_path = $picture_path_info['dir'];
-
-	if ($user['delete_picture'] || !empty($picture['name'])) {
-		@unlink($picture_path.'small_'.$picture_uri);
-		@unlink($picture_path.'medium_'.$picture_uri);
-		@unlink($picture_path.'big_'.$picture_uri);
-		@unlink($picture_path.$picture_uri);
-		$picture_uri = '';
-	}
-
-	if (!empty($picture['name'])) {
-		$picture_uri = $user_id.'_'.uniqid('').'_'.replace_dangerous_char($picture['name']);
-		$perm = api_get_setting('permissions_for_new_directories');
-		$perm = octdec(!empty($perm) ? $perm : '0770');
-		if (!file_exists($picture_path)) {
-			@mkdir($picture_path, $perm, true);
-		}
-		$picture_info = @getimagesize($_FILES['picture']['tmp_name']);
-		$type = $picture_info[2];
-		$small_picture = UserManager::resize_picture($_FILES['picture']['tmp_name'], 22);
-		$medium_picture = UserManager::resize_picture($_FILES['picture']['tmp_name'], 85);
-		$normal_picture = UserManager::resize_picture($_FILES['picture']['tmp_name'], 200);
-		$big_picture = new image($_FILES['picture']['tmp_name']); // This is the original picture.
-
-		$picture_types = array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG');
-		if (in_array($type, array_keys($picture_types))) {
-			$small_picture->send_image($picture_types[$type], $picture_path.'small_'.$picture_uri);
-			$medium_picture->send_image($picture_types[$type], $picture_path.'medium_'.$picture_uri);
-			$normal_picture->send_image($picture_types[$type], $picture_path.$picture_uri);
-			$big_picture->send_image($picture_types[$type], $picture_path.'big_'.$picture_uri);
+	if ($user['delete_picture']) {
+		$picture_uri = UserManager::delete_user_picture($user_id);
 		}
+	elseif (!empty($picture['name'])) {
+		$picture_uri = UserManager::update_user_picture($user_id, $_FILES['picture']['name'], $_FILES['picture']['tmp_name']);
 	}
 
 	$lastname = $user['lastname'];

+ 2 - 99
main/auth/profile.php

@@ -434,103 +434,6 @@ function is_profile_editable() {
 	return $GLOBALS['profileIsEditable'];
 }
 
-/*
------------------------------------------------------------
-	USER IMAGE FUNCTIONS
------------------------------------------------------------
-*/
-
-/**
- * Upload a submitted user image.
- *
- * @param	$user_id User id
- * @return	The filename of the new picture or FALSE if the upload has failed
- */
-function upload_user_image($user_id) {
-
-	/* Originally added by Miguel (miguel@cesga.es) - 2003-11-04
-	 * Code Refactoring by Hugues Peeters (hugues.peeters@claroline.net) - 2003-11-24
-	 * Moved inside a function and refactored by Thomas Corthals - 2005-11-04
-	 */
-
-	$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[count($file_extension) - 1]);
-
-	if (!file_exists($image_repository)) {
-		mkpath($image_repository);
-	}
-
-	if ($existing_image != '') {
-		if (KEEP_THE_NAME_WHEN_CHANGE_IMAGE) {
-			$picture_filename = $existing_image;
-			$old_picture_filename = 'saved_'.date('Y_m_d_H_i_s').'_'.uniqid('').'_'.$existing_image;
-		} else {
-			$old_picture_filename = $existing_image;
-			$picture_filename = (PREFIX_IMAGE_FILENAME_WITH_UID ? 'u'.$user_id.'_' : '').uniqid('').'.'.$file_extension;
-		}
-
-		if (KEEP_THE_OLD_IMAGE_AFTER_CHANGE) {
-			@rename($image_repository.$existing_image, $image_repository.$old_picture_filename);
-		} else {
-			@unlink($image_repository.$existing_image);
-		}
-	} else {
-		$picture_filename = (PREFIX_IMAGE_FILENAME_WITH_UID ? $user_id.'_' : '').uniqid('').'.'.$file_extension;
-	}
-
-	// get the picture and resize only if the picture is bigger width
-	$picture_infos = @getimagesize($_FILES['picture']['tmp_name']);
-	$type = $picture_infos[2];
-	$small_temp = UserManager::resize_picture($_FILES['picture']['tmp_name'], 22); //small picture
-	$medium_temp = UserManager::resize_picture($_FILES['picture']['tmp_name'], 85); //medium picture
-	$temp = UserManager::resize_picture($_FILES['picture']['tmp_name'], 200); // normal picture
-	$big_temp = new image($_FILES['picture']['tmp_name']); // original picture
-
-    switch (!empty($type)) {
-	    case 2 :
-	    	$small_temp->send_image('JPG', $image_repository.'small_'.$picture_filename);
-	    	$medium_temp->send_image('JPG', $image_repository.'medium_'.$picture_filename);
-	    	$temp->send_image('JPG', $image_repository.$picture_filename);
-	    	$big_temp->send_image('JPG', $image_repository.'big_'.$picture_filename);
-            break;
-	    case 3 :
-	    	$small_temp->send_image('PNG', $image_repository.'small_'.$picture_filename);
-	    	$medium_temp->send_image('PNG', $image_repository.'medium_'.$picture_filename);
-	    	$temp->send_image('PNG', $image_repository.$picture_filename);
-	    	$big_temp->send_image('PNG', $image_repository.'big_'.$picture_filename);
-            break;
-	    case 1 :
-	    	$small_temp->send_image('GIF', $image_repository.'small_'.$picture_filename);
-	    	$medium_temp->send_image('GIF', $image_repository.'medium_'.$picture_filename);
-	    	$temp->send_image('GIF', $image_repository.$picture_filename);
-	    	$big_temp->send_image('GIF', $image_repository.'big_'.$picture_filename);
-            break;
-    }
-    return $picture_filename;
-}
-
-/**
- * Remove an existing user image.
- *
- * @param	$user_id	User id
- */
-function remove_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 (!empty($image)) {
-		if (KEEP_THE_OLD_IMAGE_AFTER_CHANGE) {
-			@rename($image_repository.$image, $image_repository.'deleted_'.date('Y_m_d_H_i_s').'_'.$image);
-		} else {
-			@unlink($image_repository.$image);
-		}
-	}
-}
-
 /*
 -----------------------------------------------------------
 	PRODUCTIONS FUNCTIONS
@@ -637,14 +540,14 @@ if (!empty($_SESSION['production_uploaded'])) {
 
 	// upload picture if a new one is provided
 	if ($_FILES['picture']['size']) {
-		if ($new_picture = upload_user_image($_user['user_id'])) {
+		if ($new_picture = UserManager::update_user_picture($_user['user_id'], $_FILES['picture']['name'], $_FILES['picture']['tmp_name'])) {
 			$user_data['picture_uri'] = $new_picture;
 			$_SESSION['image_uploaded'] = 'success';
 		}
 	}
 	// remove existing picture if asked
 	elseif (!empty($user_data['remove_picture'])) {
-		remove_user_image($_user['user_id']);
+		UserManager::delete_user_picture($_user['user_id']);
 		$user_data['picture_uri'] = '';
 	}
 

+ 118 - 4
main/inc/lib/usermanager.lib.php

@@ -635,6 +635,120 @@ class UserManager {
 		return array('dir' => $dir, 'file' => $picture_filename);
 	}
 
+	/**
+	 * Creates new user pfotos in various sizes of a user, or deletes user pfotos.
+	 * Note: This method relies on configuration setting from dokeos/main/inc/conf/profile.conf.php
+	 * @param int $user_id			The user internal identitfication number.
+	 * @param string $file			The common file name for the newly created pfotos. It will be checked and modified for compatibility with the file system.
+	 * If full name is provided, path component is ignored.
+	 * If an empty name is provided, then old user photos are deleted only, @see UserManager::delete_user_picture() as the prefered way for deletion.
+	 * @param string $source_file	The full system name of the image from which user photos will be created.
+	 * @return string/bool			Returns the resulting common file name of created images which usually should be stored in database.
+	 * When deletion is recuested returns empty string. In case of internal error or negative validation returns FALSE.
+	 */
+	public static function update_user_picture($user_id, $file = null, $source_file = null) {
+
+		// Validation 1.
+		if (empty($user_id)) {
+			return false;
+		}
+		$delete = empty($file);
+		if (empty($source_file)) {
+			$source_file = $file;
+		}
+
+		// Configuration options about user photos.
+		require_once api_get_path(CONFIGURATION_PATH).'profile.conf.php';
+
+		// User-reserved directory where photos have to be placed.
+		$path_info = self::get_user_picture_path_by_id($user_id, 'system', true);
+		$path = $path_info['dir'];
+		// If this directory does not exist - we create it.
+		if (!file_exists($path)) {
+			$perm = api_get_setting('permissions_for_new_directories');
+			$perm = octdec(!empty($perm) ? $perm : '0770');
+			@mkdir($path, $perm, true);
+		}
+
+		// The old photos (if any).
+		$old_file = $path_info['file'];
+
+		// Let us delete them.
+		if (!empty($old_file)) {
+			if (KEEP_THE_OLD_IMAGE_AFTER_CHANGE) {
+				$prefix = 'saved_'.date('Y_m_d_H_i_s').'_'.uniqid('').'_';
+				@rename($path.'small_'.$old_file, $path.$prefix.'small_'.$old_file);
+				@rename($path.'medium_'.$old_file, $path.$prefix.'medium_'.$old_file);
+				@rename($path.'big_'.$old_file, $path.$prefix.'big_'.$old_file);
+				@rename($path.$old_file, $path.$prefix.$old_file);
+			} else {
+				@unlink($path.'small_'.$old_file);
+				@unlink($path.'medium_'.$old_file);
+				@unlink($path.'big_'.$old_file);
+				@unlink($path.$old_file);
+			}
+		}
+
+		// Exit if only deletion has been requested. Return an empty picture name.
+		if ($delete) {
+			return '';
+		}
+
+		// Validation 2.
+		$allowed_types = array('jpg', 'jpeg', 'png', 'gif');
+		$file = str_replace('\\', '/', $file);
+		$filename = (($pos = strrpos($file, '/')) !== false) ? substr($file, $pos + 1) : $file;
+		$extension = strtolower(substr(strrchr($filename, '.'), 1));
+		if (!in_array($extension, $allowed_types)) {
+			return false;
+		}
+
+		// This is the common name for the new photos.
+		if (KEEP_THE_NAME_WHEN_CHANGE_IMAGE && !empty($old_file)) {
+			$old_extension = strtolower(substr(strrchr($old_file, '.'), 1));
+			$filename = in_array($old_extension, $allowed_types) ? substr($old_file, 0, -strlen($old_extension)) : $old_file;
+			$filename = (substr($filename, -1) == '.') ? $filename.$extension : $filename.'.'.$extension;
+		} else {
+			$filename = replace_dangerous_char($filename);
+			if (PREFIX_IMAGE_FILENAME_WITH_UID) {
+				$filename = uniqid('').'_'.$filename;
+			}
+			// We always prefix user photos with user ids, so on setting
+			// api_get_setting('split_users_upload_directory') === 'true'
+			// the correspondent directories to be found successfully.
+			$filename = $user_id.'_'.$filename;
+		}
+
+		// Storing the new photos in 4 versions with various sizes.
+
+		$picture_info = @getimagesize($source_file);
+		$type = $picture_info[2];
+		$small = self::resize_picture($source_file, 22);
+		$medium = self::resize_picture($source_file, 85);
+		$normal = self::resize_picture($source_file, 200);
+		$big = new image($source_file); // This is the original picture.
+
+		$ok = false;
+		$detected = array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG');
+		if (in_array($type, array_keys($detected))) {
+			$ok = $small->send_image($detected[$type], $path.'small_'.$filename)
+				&& $medium->send_image($detected[$type], $path.'medium_'.$filename)
+				&& $normal->send_image($detected[$type], $path.$filename)
+				&& $big->send_image($detected[$type], $path.'big_'.$filename);
+		}
+		return $ok ? $filename : false;
+	}
+
+	/**
+	 * Deletes user pfotos.
+	 * Note: This method relies on configuration setting from dokeos/main/inc/conf/profile.conf.php
+	 * @param int $user_id			The user internal identitfication number.
+	 * @return string/bool			Returns empty string on success, FALSE on error.
+	 */
+	public static function delete_user_picture($user_id) {
+		return self::update_user_picture($user_id);
+	}
+
 /*
 -----------------------------------------------------------
 	PRODUCTIONS FUNCTIONS
@@ -660,13 +774,13 @@ class UserManager {
 			return true; // postpone reading from the filesystem
 		}
 
-		$productions = UserManager::get_user_productions($user_id);
+		$productions = self::get_user_productions($user_id);
 
 		if (empty($productions)) {
 			return false;
 		}
 
-		$production_path = UserManager::get_user_picture_path_by_id($user_id, 'web', true);
+		$production_path = self::get_user_picture_path_by_id($user_id, 'web', true);
 		$production_dir = $production_path['dir'].$user_id.'/';
 		$del_image = api_get_path(WEB_CODE_PATH).'img/delete.gif';
 		$del_text = get_lang('Delete');
@@ -1851,8 +1965,8 @@ class UserManager {
     		$picture['file'] = api_get_path(WEB_CODE_PATH).'img/'.$picture_file;
     		return $picture;
     	}
-        $image_array_sys = UserManager::get_user_picture_path_by_id($user_id, 'system', false, true);
-        $image_array = UserManager::get_user_picture_path_by_id($user_id, 'web', false, true);
+        $image_array_sys = self::get_user_picture_path_by_id($user_id, 'system', false, true);
+        $image_array = self::get_user_picture_path_by_id($user_id, 'web', false, true);
         $file = $image_array_sys['dir'].$size_picture.$picture_file;
     	if (file_exists($file)) {
             $picture['file'] = $image_array['dir'].$size_picture.$picture_file;