From d08dbe65f8566b5ef422c3a79bfe1f59564d3d66 Mon Sep 17 00:00:00 2001
From: Jacob Hoover <jacob.hoover@greenheck.com>
Date: Tue, 12 Apr 2011 19:56:56 -0500
Subject: [PATCH] Patch for #11687 off of the 1.2.4 tag, including the mod to report orphans when looking at my_view_page.

---
 bug_actiongroup.php |    8 +++-
 core/file_api.php   |   72 +++++++++++++++++++++++++
 my_view_page.php    |    1 +
 orphans.php         |  144 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 224 insertions(+), 1 deletions(-)
 create mode 100644 orphans.php

diff --git a/bug_actiongroup.php b/bug_actiongroup.php
index 7d72793..3068082 100644
--- a/bug_actiongroup.php
+++ b/bug_actiongroup.php
@@ -28,6 +28,8 @@
 
 	require_once( 'bug_api.php' );
 
+	require_once( 'file_api.php' );
+
 	auth_ensure_user_authenticated();
 	helper_begin_long_process();
 
@@ -101,7 +103,11 @@
 			if ( access_has_bug_level( config_get( 'move_bug_threshold' ), $t_bug_id ) ) {
 				/** @todo we need to issue a helper_call_custom_function( 'issue_update_validate', array( $t_bug_id, $t_bug_data, $f_bugnote_text ) ); */
 				$f_project_id = gpc_get_int( 'project_id' );
-				bug_set_field( $t_bug_id, 'project_id', $f_project_id );
+				
+				// Attempt to move disk based attachments to new project file directory.
+                file_move_bug_attachments( $t_bug_id, $f_project_id );
+
+				bug_set_field( $t_bug_id, 'project_id', $f_project_id );				
 				helper_call_custom_function( 'issue_update_notify', array( $t_bug_id ) );
 			} else {
 				$t_failed_ids[$t_bug_id] = lang_get( 'bug_actiongroup_access' );
diff --git a/core/file_api.php b/core/file_api.php
index 3c61769..b7dddf9 100644
--- a/core/file_api.php
+++ b/core/file_api.php
@@ -853,3 +853,75 @@ function file_get_extension( $p_filename ) {
 	}
 	return $t_extension;
 }
+
+/**
+ * Move any attachments as needed when a bug is moved from project to project.
+ *
+ * @param int $p_bug_id ID of bug containing attachments to be moved
+ * @param int $p_project_id_to destination project ID for the bug
+ * @return null
+ */
+function file_move_bug_attachments( $p_bug_id, $p_project_id_to ) {
+        $t_project_id_from = bug_get_field( $p_bug_id, 'project_id' );
+        if ( $t_project_id_from == $p_project_id_to ) {
+                return;
+        }
+
+        $t_method = config_get( 'file_upload_method' );
+        if ( $t_method != DISK ) {
+                return;
+        }
+
+        if ( !file_bug_has_attachments( $p_bug_id ) ) {
+                return;
+        }
+
+        $t_path_from = project_get_field( $t_project_id_from, 'file_path' );
+        if ( is_blank( $t_path_from ) ) {
+                $t_path_from = config_get( 'absolute_path_default_upload_folder', null, null, $t_project_id_from );
+        }
+        file_ensure_valid_upload_path( $t_path_from );
+        $t_path_to = project_get_field( $p_project_id_to, 'file_path' );
+        if ( is_blank( $t_path_to ) ) {
+                $t_path_to = config_get( 'absolute_path_default_upload_folder', null, null, $p_project_id_to );
+        }
+        file_ensure_valid_upload_path( $t_path_to );
+        if ( $t_path_from == $t_path_to ) {
+                return;
+        }
+
+        # Initialize the update query to update a single row
+        $t_bug_file_table = db_get_table( 'mantis_bug_file_table' );
+        $c_bug_id = db_prepare_int( $p_bug_id );
+        $query_disk_attachment_update = "UPDATE $t_bug_file_table
+                                         SET folder=" . db_param() . "
+                                         WHERE bug_id=" . db_param() . "
+                                         AND id =" . db_param();
+
+        $t_attachment_rows = bug_get_attachments( $p_bug_id );
+        $t_attachments_count = count( $t_attachment_rows );
+        for ( $i = 0; $i < $t_attachments_count; $i++ ) {
+			$t_row = $t_attachment_rows[$i];
+			$t_basename = basename( $t_row['diskfile'] );
+
+			$t_disk_file_name_from = file_path_combine( $t_path_from, $t_basename );
+			$t_disk_file_name_to = file_path_combine( $t_path_to, $t_basename );
+
+			if ( file_exists($t_disk_file_name_from) ) {
+				if ( !file_exists( $t_disk_file_name_to ) ) {
+					chmod( $t_disk_file_name_from, 0775 );
+					if ( !rename( $t_disk_file_name_from, $t_disk_file_name_to ) ) {
+						if ( !copy( $t_disk_file_name_from, $t_disk_file_name_to ) ) {
+							trigger_error( FILE_MOVE_FAILED, ERROR );
+						}
+						file_delete_local( $t_disk_file_name_from );
+					}
+					chmod( $t_disk_file_name_to, config_get( 'attachments_file_permissions' ) );
+					db_query_bound( $query_disk_attachment_update, Array( db_prepare_string( $t_path_to ), $c_bug_id, db_prepare_int( $t_row['id'] ) ) );
+				} else {
+					trigger_error( ERROR_FILE_DUPLICATE, ERROR );
+				}
+			}
+        }
+}
+
diff --git a/my_view_page.php b/my_view_page.php
index d03764a..b315165 100644
--- a/my_view_page.php
+++ b/my_view_page.php
@@ -171,4 +171,5 @@
 </div>
 
 <?php
+	require_once( 'orphans.php');
 	html_page_bottom();
diff --git a/orphans.php b/orphans.php
new file mode 100644
index 0000000..a7810e2
--- /dev/null
+++ b/orphans.php
@@ -0,0 +1,144 @@
+<?php
+  require_once( 'core.php' );
+
+  require_once( 'bug_api.php' );
+
+  require_once( 'file_api.php' );
+  
+//  require_once( 'last_visited_api.php' );
+
+//global $g_session_validation;
+//$g_session_validation = OFF;
+//$tpl_file = __FILE__;
+//$tpl_mantis_dir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
+//$tpl_show_page_header = true;
+//$tpl_force_readonly = false;
+
+  //auth_ensure_user_authenticated();
+
+/**
+ * Returns a list of all possible bug ID's
+ * @return array() of bug ID's
+ */
+function bug_get_bugs( ) {
+	$t_project_id = helper_get_current_project();
+	$t_bug_table = db_get_table( 'mantis_bug_table' );
+	$t_bug_file_table = db_get_table( 'mantis_bug_file_table' );
+	#$query = "SELECT id FROM $t_bug_table";
+	#$query = "SELECT bug_id FROM $t_bug_file_table";
+
+	if ( $t_project_id == ALL_PROJECTS) {
+		$query = "SELECT bug_id FROM $t_bug_file_table";
+		$db_result = db_query( $query );
+	} else {
+		$query = "SELECT distinct F.bug_id FROM $t_bug_file_table F, $t_bug_table B WHERE F.bug_id = B.ID AND B.project_id = " . db_param();
+		$db_result = db_query_bound( $query , Array( (int) $t_project_id) );
+	}
+	# $query = "SELECT F.bug_id FROM $t_bug_file_table F, $t_bug_table B WHERE F.bug_id = B.ID AND B.project_id = " . db_param();
+	#$db_result = db_query_bound( $query , Array( (int) $t_project_id) );
+	$num_files = db_num_rows( $db_result );
+
+	$t_result = array();
+
+	for( $i = 0;$i < $num_files;$i++ ) {
+		$t_result[] = db_fetch_array( $db_result );
+	}
+
+	return $t_result;
+}
+
+function bug_get_attachments_withfolder( $p_bug_id ) {
+        if( !file_can_view_bug_attachments( $p_bug_id ) ) {
+                return;
+        }
+
+        $c_bug_id = db_prepare_int( $p_bug_id );
+
+        $t_bug_file_table = db_get_table( 'mantis_bug_file_table' );
+
+        $query = "SELECT id, title, diskfile, filename, filesize, file_type, date_added, folder
+                                FROM $t_bug_file_table
+                                WHERE bug_id=" . db_param() . "
+                                ORDER BY date_added";
+        $db_result = db_query_bound( $query, Array( $c_bug_id ) );
+        $num_files = db_num_rows( $db_result );
+
+        $t_result = array();
+
+        for( $i = 0;$i < $num_files;$i++ ) {
+                $t_result[] = db_fetch_array( $db_result );
+        }
+
+        return $t_result;
+}
+
+/**
+ * Validate any attachments as needed when a bug is moved from project 
+to project.
+ *
+ * @param int $p_bug_id ID of bug containing attachments to be verified
+ * @return null
+ */
+function file_verify_bug_attachments( $p_bug_id ) {
+        $t_project_id_from = bug_get_field( $p_bug_id, 'project_id' );
+        $t_method = config_get( 'file_upload_method' );
+
+        if ( $t_method != DISK ) {
+                return;
+        }
+
+        if ( !file_bug_has_attachments( $p_bug_id ) ) {
+                return;
+        }
+
+        $t_path_from = project_get_field( $t_project_id_from, 'file_path' );
+
+        if ( is_blank( $t_path_from ) ) {
+                $t_path_from = config_get( 'absolute_path_default_upload_folder', null, null, $t_project_id_from );
+        }
+
+        file_ensure_valid_upload_path( $t_path_from );
+
+        # Initialize the update query to update a single row
+        #$t_bug_file_table = db_get_table( 'mantis_bug_file_table' );
+        $c_bug_id = db_prepare_int( $p_bug_id );
+        #$query_disk_attachment_update = "UPDATE $t_bug_file_table
+        #                                 SET folder=" . db_param() . "
+        #                                 WHERE bug_id=" . db_param() . "
+        #                                 AND id =" . db_param();
+
+        $t_attachment_rows = bug_get_attachments_withfolder( $p_bug_id );
+        $t_attachments_count = count( $t_attachment_rows );
+        for ( $i = 0; $i < $t_attachments_count; $i++ ) {
+                $t_row = $t_attachment_rows[$i];
+                $t_basename = basename( $t_row['diskfile'] );
+
+                $t_disk_file_name_from = file_path_combine( $t_path_from, $t_basename );
+		$t_disk_file_name_from_db = file_path_combine( $t_row['folder'], $t_basename );
+
+                if ( file_exists( $t_disk_file_name_from ) == false && file_exists( $t_disk_file_name_from_db ) == false)
+                {
+			$filename = $t_row['filename'];
+			$folder = $t_row['folder'];
+                	//error_log("Bug: $c_bug_id, File: $t_disk_file_name_from, FileName: $filename, Folder: $folder is orphaned.");
+			echo "<tr bgcolor='#c9ccc4'> <td>";
+			print_bug_link($c_bug_id);
+			echo "</td><td>$t_disk_file_name_from</td><td>$filename</td><td>$folder</td></tr>";
+                }
+        }
+}
+
+set_time_limit(60 * 2);
+//error_log('calling bug_get_bugs');
+$project_ids = bug_get_bugs();
+$rowcount = count($project_ids);
+//error_log("Found $rowcount bugs");
+echo "<table class='width100' cellspacing='1'><tr><td class='form-title'>ID</td><td class='form-title'>File</td><td class='form-title'>FileName</td><td class='form-title'>folder</td></tr>";
+for ( $i = 0; $i < $rowcount; $i++ ) {
+	file_verify_bug_attachments( $project_ids[$i]['bug_id'] );
+	//if ($i % 1000 == 0) {
+		//error_log("Processing $i of $rowcount");
+	//}
+}
+echo "</table>";
+?>
-- 
1.7.0.2.msysgit.0

