From d1d54004983e9ade0409ef5b5cc2034842a6ff4e Mon Sep 17 00:00:00 2001
From: unknown <Peter@.(none)>
Date: Wed, 17 Nov 2010 12:10:05 +0100
Subject: [PATCH] Simple local avatar feature in core

---
 core/file_api.php  |   47 +++++++++++++++++++++++++++-------------
 core/print_api.php |    3 +-
 core/user_api.php  |   60 ++++++++++++++++++++++++++++++++-------------------
 3 files changed, 72 insertions(+), 38 deletions(-)

diff --git a/core/file_api.php b/core/file_api.php
index b5b2e43..0759223 100644
--- a/core/file_api.php
+++ b/core/file_api.php
@@ -486,6 +486,8 @@ function file_delete( $p_file_id, $p_table = 'bug' ) {
 	if ( $p_table == 'bug' ) {
 		$t_bug_id = file_get_field( $p_file_id, 'bug_id', $p_table );
 		$t_project_id = bug_get_field( $t_bug_id, 'project_id' );
+	} elseif ( $p_table == 'avatar' ) {
+		$t_project_id = ALL_PROJECTS;
 	} else {
 		$t_project_id = file_get_field( $p_file_id, 'project_id', $p_table );
 	}
@@ -614,7 +616,7 @@ function file_is_name_unique( $p_name, $p_bug_id ) {
  * @param integer $p_bug_id the bug id
  * @param array $p_file the uploaded file info, as retrieved from gpc_get_file()
  */
-function file_add( $p_bug_id, $p_file, $p_table = 'bug', $p_title = '', $p_desc = '', $p_user_id = null ) {
+function file_add( $p_key_id, $p_file, $p_table = 'bug', $p_title = '', $p_desc = '', $p_user_id = null ) {
 
 	file_ensure_uploaded( $p_file );
 	$t_file_name = $p_file['name'];
@@ -624,16 +626,35 @@ function file_add( $p_bug_id, $p_file, $p_table = 'bug', $p_title = '', $p_desc
 		trigger_error( ERROR_FILE_NOT_ALLOWED, ERROR );
 	}
 
-	if( !file_is_name_unique( $t_file_name, $p_bug_id ) ) {
+	if( !file_is_name_unique( $t_file_name, $p_key_id ) ) {
 		trigger_error( ERROR_DUPLICATE_FILE, ERROR );
 	}
 
-	if( 'bug' == $p_table ) {
-		$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
-		$t_bug_id = bug_format_id( $p_bug_id );
-	} else {
-		$t_project_id = helper_get_current_project();
-		$t_bug_id = 0;
+	# prepare variables for insertion
+	$c_key_id = db_prepare_int( $p_key_id );
+	
+	switch ( $p_table ) {
+		case 'bug':
+			$t_project_id = bug_get_field( $p_key_id, 'project_id' );
+			$c_project_id = db_prepare_int( $t_project_id );
+			$t_key_id     = bug_format_id( $p_key_id );
+			$t_file_hash  = $t_key_id;
+			$c_id         = $c_key_id;
+			break;
+		case 'avatar':
+			$t_project_id = ALL_PROJECTS;
+			$c_project_id = db_prepare_int( $t_project_id );
+			$t_key_id     = bug_format_id( $p_key_id );
+			$t_file_hash  = config_get( 'avatar_files_prefix' ) . '-' . $t_key_id;
+			$c_id         = $c_key_id;
+			break;
+		default:
+			$t_project_id = helper_get_current_project();
+			$c_project_id = db_prepare_int( $t_project_id );
+			$t_file_hash  = config_get( 'document_files_prefix' ) . '-' . $t_project_id;
+			$t_key_id     = 0;
+			$c_id         = $c_project_id;
+			break;
 	}
 
 	if( $p_user_id === null ) {
@@ -643,8 +664,6 @@ function file_add( $p_bug_id, $p_file, $p_table = 'bug', $p_title = '', $p_desc
 	}
 
 	# prepare variables for insertion
-	$c_bug_id = db_prepare_int( $p_bug_id );
-	$c_project_id = db_prepare_int( $t_project_id );
 	$c_file_type = db_prepare_string( $p_file['type'] );
 	$c_title = db_prepare_string( $p_title );
 	$c_desc = db_prepare_string( $p_desc );
@@ -657,10 +676,9 @@ function file_add( $p_bug_id, $p_file, $p_table = 'bug', $p_title = '', $p_desc
 			$t_file_path = config_get( 'absolute_path_default_upload_folder' );
 		}
 	}
+
 	$c_file_path = db_prepare_string( $t_file_path );
 	$c_new_file_name = db_prepare_string( $t_file_name );
-
-	$t_file_hash = ( 'bug' == $p_table ) ? $t_bug_id : config_get( 'document_files_prefix' ) . '-' . $t_project_id;
 	$t_unique_name = file_generate_unique_name( $t_file_hash . '-' . $t_file_name, $t_file_path );
 	$t_disk_file_name = $t_file_path . $t_unique_name;
 	$c_unique_name = db_prepare_string( $t_unique_name );
@@ -708,7 +726,6 @@ function file_add( $p_bug_id, $p_file, $p_table = 'bug', $p_title = '', $p_desc
 	}
 
 	$t_file_table = db_get_table( 'mantis_' . $p_table . '_file_table' );
-	$c_id = ( 'bug' == $p_table ) ? $c_bug_id : $c_project_id;
 
 	$query = "INSERT INTO $t_file_table
 						(" . $p_table . "_id, title, description, diskfile, filename, folder, filesize, file_type, date_added, content, user_id)
@@ -719,10 +736,10 @@ function file_add( $p_bug_id, $p_file, $p_table = 'bug', $p_title = '', $p_desc
 	if( 'bug' == $p_table ) {
 
 		# updated the last_updated date
-		$result = bug_update_date( $p_bug_id );
+		$result = bug_update_date( $p_key_id );
 
 		# log new bug
-		history_log_event_special( $p_bug_id, FILE_ADDED, $t_file_name );
+		history_log_event_special( $p_key_id, FILE_ADDED, $t_file_name );
 	}
 }
 
diff --git a/core/print_api.php b/core/print_api.php
index eaec5f2..c754d6f 100644
--- a/core/print_api.php
+++ b/core/print_api.php
@@ -152,7 +152,8 @@ function print_avatar( $p_user_id, $p_size = 80 ) {
 			$t_avatar_url = htmlspecialchars( $t_avatar[0] );
 			$t_width = $t_avatar[1];
 			$t_height = $t_avatar[2];
-			echo '<a rel="nofollow" href="http://site.gravatar.com"><img class="avatar" src="' . $t_avatar_url . '" alt="User avatar" width="' . $t_width . '" height="' . $t_height . '" /></a>';
+			$t_href = $t_avatar[3];
+			echo '<a rel="nofollow" href="'.$t_href.'"><img class="avatar" src="' . $t_avatar_url . '" alt="User avatar" width="' . $t_width . '" height="' . $t_height . '" /></a>';
 		}
 	}
 }
diff --git a/core/user_api.php b/core/user_api.php
index 1b3f1ac..343bacd 100644
--- a/core/user_api.php
+++ b/core/user_api.php
@@ -797,32 +797,48 @@ function user_get_name( $p_user_id ) {
 * @return array|bool an array( URL, width, height ) or false when the given user has no avatar
 */
 function user_get_avatar( $p_user_id, $p_size = 80 ) {
-	$t_email = utf8_strtolower( user_get_email( $p_user_id ) );
-	if( is_blank( $t_email ) ) {
-		$t_result = false;
-	} else {
-		$t_default_image = config_get( 'default_avatar' );
+	$c_user_id = db_prepare_int ( $p_user_id );
+	$t_avatar_table = db_get_table ( 'mantis_avatar_file_table' );
+	$query = "SELECT id
+			  FROM $t_avatar_table
+			  WHERE avatar_id='$c_user_id'";
+	$t_db_result = db_query ( $query );
+	if ( 1 == db_num_rows( $t_db_result )) {
+		$t_found_local_avatar = true;
+		$t_avatar_id = db_result ( $t_db_result );
+		$t_avatar_url = "file_download.php?type=avatar&file_id=".$t_avatar_id."&show_inline=1".form_security_param( 'file_show_inline' );
+		$t_avatar_ref = "manage_user_edit_page.php?user_id=".$p_user_id;
 		$t_size = $p_size;
-
-		$t_use_ssl = false;
-		if( isset( $_SERVER['HTTPS'] ) && ( utf8_strtolower( $_SERVER['HTTPS'] ) != 'off' ) ) {
-			$t_use_ssl = true;
-		}
-
-		if( !$t_use_ssl ) {
-			$t_gravatar_domain = 'http://www.gravatar.com/';
+		$t_result = array( $t_avatar_url, $t_size, $t_size, $t_avatar_ref );
+	} else {
+		$t_email = utf8_strtolower( user_get_email( $p_user_id ) );
+		if( is_blank( $t_email ) ) {
+			$t_result = false;
 		} else {
-			$t_gravatar_domain = 'https://secure.gravatar.com/';
+			$t_default_image = config_get( 'default_avatar' );
+			$t_size = $p_size;
+	
+			$t_use_ssl = false;
+			if( isset( $_SERVER['HTTPS'] ) && ( utf8_strtolower( $_SERVER['HTTPS'] ) != 'off' ) ) {
+				$t_use_ssl = true;
+			}
+	
+			if( !$t_use_ssl ) {
+				$t_gravatar_domain = 'http://www.gravatar.com/';
+			} else {
+				$t_gravatar_domain = 'https://secure.gravatar.com/';
+			}
+	
+			$t_avatar_url = $t_gravatar_domain . 'avatar.php?gravatar_id=' . md5( $t_email ) . '&default=' . urlencode( $t_default_image ) . '&size=' . $t_size . '&rating=G';
+			$t_result = array(
+				$t_avatar_url,
+				$t_size,
+				$t_size,
+				'http://site.gravatar.com'
+			);
 		}
-
-		$t_avatar_url = $t_gravatar_domain . 'avatar.php?gravatar_id=' . md5( $t_email ) . '&default=' . urlencode( $t_default_image ) . '&size=' . $t_size . '&rating=G';
-		$t_result = array(
-			$t_avatar_url,
-			$t_size,
-			$t_size,
-		);
 	}
-
+	
 	return $t_result;
 }
 
-- 
1.6.2.msysgit.0.186.gf7512

