/** * Move a single file named $p_basename from $p_file_path_from to $p_file_path_to */ function file_move_bug_attachment( $p_basename, $p_file_path_from, $p_file_path_to ) { $t_method = config_get( 'file_upload_method' ); $t_disk_file_name_from = file_path_combine( $p_file_path_from, $p_basename ); $t_disk_file_name_to = file_path_combine( $p_file_path_to, $p_basename ); switch( $t_method ) { case FTP: case DISK: file_ensure_valid_upload_path( $p_file_path_from ); file_ensure_valid_upload_path( $p_file_path_to ); if( !file_exists( $t_disk_file_name_to ) ) { if ( function_exists('link') ) { if( !link( $t_disk_file_name_from, $t_disk_file_name_to ) ) { trigger_error( FILE_MOVE_FAILED, ERROR ); } chmod( $t_disk_file_name_to, config_get( 'attachments_file_permissions' ) ); file_delete_local( $t_disk_file_name_from ); } else { // Windows doesn't support link, so simulate a link 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' ) ); } } else { trigger_error( ERROR_FILE_DUPLICATE, ERROR ); } break; case DATABASE: break; default: trigger_error( ERROR_GENERIC, ERROR ); } return true; } /** * Move any attachements as needed when a bug is moved from project to project * * @param integer $p_bug_id the bug id * @param integer $p_project_id_from the project id the bug was in * @param integer $p_project_id_to the project id the bug was moved to */ function file_move_bug_attachments( $p_bug_id, $p_project_id_from, $p_project_id_to ) { if ( $p_project_id_from == $p_project_id_to ) { return true; } if ( file_bug_has_attachments($p_bug_id) == true && config_get( 'file_upload_method' ) != DATABASE ){ $t_path_from = project_get_field( $p_project_id_from, 'file_path' ); if ( is_blank( $t_path_from ) ) { $t_path_from = config_get( 'absolute_path_default_upload_folder' ); } $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' ); } if ( $t_path_from == $t_path_to ) { return true; } $t_attachment_rows = bug_get_attachments( $p_bug_id ); $t_attachments_count = count( $t_attachment_rows ); $t_bug_file_table = db_get_table( 'mantis_bug_file_table' ); $c_bug_id = db_prepare_int( $p_bug_id ); for( $i = 0;$i < $t_attachments_count;$i++ ) { $t_row = $t_attachment_rows[$i]; $t_basename = basename( $t_row['diskfile'] ); file_move_bug_attachment( $t_basename, $t_path_from, $t_path_to); # Update the corresponding db record $query = "UPDATE $t_bug_file_table SET folder=" . db_param() . " WHERE bug_id=" . db_param() . " AND id =" . db_param(); db_query_bound( $query, Array( db_prepare_string( $t_path_to ), $c_bug_id, db_prepare_int( $t_row['id'] ) ) ); } } }