<?php
  require_once( 'core.php' );

  require_once( 'bug_api.php' );

  require_once( 'file_api.php' );
  
/**
 * 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' );

	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) );
	}
	$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
        $c_bug_id = db_prepare_int( $p_bug_id );

        $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);
$project_ids = bug_get_bugs();
$rowcount = count($project_ids);
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'] );
}
echo "</table>";
?>
