/** * Move any attachments as needed when a bug is moved from project to project. * * @param integer $p_bug_id ID of bug containing attachments to be moved. * @param integer $p_project_id_to Destination project ID for the bug. * @return void * * @todo: this function can't cope with source or target storing attachments in DB */ 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 $c_bug_id = (int) $p_bug_id; $t_query_disk_attachment_update = 'UPDATE {bug_file} 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_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(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($t_query_disk_attachment_update, array(db_prepare_string($t_path_to), $c_bug_id, (int) $t_row['id'])); } else { trigger_error(ERROR_FILE_DUPLICATE, ERROR); } } }
/** * Nomalizes the disk file path based on the following algorithm: * 1. If disk file exists, then return as is. * 2. If not, and a project path is available, then check with that, if exists return it. * 3. If not, then use default upload path, then check with that, if exists return it. * 4. If disk file doesn't include a path, then return expected path based on project path or default path. * 5. Otherwise return as is. * * @param string $p_diskfile The disk file (full path or just filename). * @param integer The project id - shouldn't be 0 (ALL_PROJECTS). * @return The normalized full path. */ function file_normalize_attachment_path($p_diskfile, $p_project_id) { if (file_exists($p_diskfile)) { return $p_diskfile; } $t_basename = basename($p_diskfile); $t_expected_file_path = ''; if ($p_project_id != ALL_PROJECTS) { $t_path = project_get_field($p_project_id, 'file_path'); if (!is_blank($t_path)) { $t_diskfile = file_path_combine($t_path, $t_basename); if (file_exists($t_diskfile)) { return $t_diskfile; } // if we don't find the file, then this is the path we want to return. $t_expected_file_path = $t_diskfile; } } $t_path = config_get('absolute_path_default_upload_folder'); if (!is_blank($t_path)) { $t_diskfile = file_path_combine($t_path, $t_basename); if (file_exists($t_diskfile)) { return $t_diskfile; } // if the expected path not set to project directory, then set it to default directory. if (is_blank($t_expected_file_path)) { $t_expected_file_path = $t_diskfile; } } // if diskfile doesn't include a path, then use the expected filename. if ((strstr($p_diskfile, DIRECTORY_SEPARATOR) === false || strstr($p_diskfile, '\\') === false) && !is_blank($t_expected_file_path)) { return $t_expected_file_path; } // otherwise return as is. return $p_diskfile; }