luis@1119: # Redmine - project management software luis@1119: # Copyright (C) 2008 Jean-Philippe Lang luis@1119: # luis@1119: # This program is free software; you can redistribute it and/or luis@1119: # modify it under the terms of the GNU General Public License luis@1119: # as published by the Free Software Foundation; either version 2 luis@1119: # of the License, or (at your option) any later version. luis@1119: # luis@1119: # This program is distributed in the hope that it will be useful, luis@1119: # but WITHOUT ANY WARRANTY; without even the implied warranty of luis@1119: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the luis@1119: # GNU General Public License for more details. luis@1119: # luis@1119: # You should have received a copy of the GNU General Public License luis@1119: # along with this program; if not, write to the Free Software luis@1119: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. luis@1119: luis@1119: require 'iconv' luis@1119: luis@1119: class DataFile < ActiveRecord::Base luis@1119: def self.save(directory, zipname, upload) luis@1119: path = File.join(directory, zipname) luis@1119: File.open(path, "wb") { |f| f.write(upload['datafile'].read) } luis@1119: end luis@1119: end luis@1119: luis@1119: class RedmineEmbeddedController < ApplicationController luis@1119: class RedmineEmbeddedControllerError < StandardError; end luis@1119: luis@1119: unloadable luis@1119: layout 'base' luis@1119: before_filter :find_project, :authorize luis@1119: luis@1119: def index luis@1119: file = params[:request_path] luis@1119: path = get_real_path(file) luis@1119: if File.directory?(path) luis@1119: file = get_index_file(path) luis@1119: target = file || [] luis@1119: #target << file luis@1119: # Forces redirect to the index file when the requested path is a directory luis@1119: # so that relative links in embedded html pages work luis@1119: redirect_to :request_path => target luis@1119: return luis@1119: end luis@1119: luis@1119: # Check file extension luis@1119: raise RedmineEmbeddedControllerError.new('This file can not be viewed (invalid extension).') unless Redmine::Plugins::RedmineEmbedded.valid_extension?(path) luis@1119: luis@1119: if Redmine::MimeType.is_type?('image', path) luis@1119: send_file path, :disposition => 'inline', :type => Redmine::MimeType.of(path) luis@1119: else luis@1119: embed_file path luis@1119: end luis@1119: luis@1119: rescue Errno::ENOENT => e luis@1119: @content = "No documentation found" luis@1119: @title = "" luis@1119: render :index luis@1119: rescue Errno::EACCES => e luis@1119: # Can not read the file luis@1119: render_error "Unable to read the file: #{e.message}" luis@1119: rescue RedmineEmbeddedControllerError => e luis@1119: render_error e.message luis@1119: end luis@1119: luis@1119: def upload luis@1119: if params[:upload] luis@1119: file = params[:upload] luis@1119: zipname = sanitize_filename(params[:upload]['datafile'].original_filename) luis@1119: if ["zip"].include?(File.extname(zipname).downcase[1..-1]) luis@1119: dir = get_project_directory.gsub("/html", "") luis@1119: if File.directory? dir luis@1119: `rm -rf #{dir}/*` #clean up any exisiting docs luis@1119: else luis@1119: Dir.mkdir dir luis@1119: end luis@1119: filename = DataFile.save(dir, zipname, params[:upload]) luis@1119: Dir.chdir(dir) luis@1119: `unzip #{zipname}` luis@1119: redirect_to show_embedded_url(@project), :notice => "Documentation uploaded" luis@1119: else luis@1119: render :index, :error => "File must be ZIP format" luis@1119: end luis@1119: else luis@1119: render :index, :error => "No file uploaded" luis@1119: end luis@1119: end luis@1119: luis@1119: private luis@1119: luis@1119: def sanitize_filename(file_name) luis@1119: # get only the filename, not the whole path (from IE) luis@1119: just_filename = File.basename(file_name) luis@1119: # replace all none alphanumeric, underscore or perioids luis@1119: # with underscore luis@1119: just_filename.sub(/[^\w\.\-]/,'_') luis@1119: end luis@1119: luis@1119: def find_project luis@1119: @project = Project.find(params[:id]) luis@1119: rescue ActiveRecord::RecordNotFound luis@1119: render_404 luis@1119: end luis@1119: luis@1119: # Return the path to the html root directory for the current project luis@1119: def get_project_directory luis@1119: @project_directory ||= Setting.plugin_redmine_embedded['path'].to_s.gsub('{PROJECT}', @project.identifier) luis@1119: end luis@1119: luis@1119: # Returns the absolute path of the requested file luis@1119: # Parameter is an Array luis@1119: def get_real_path(path) luis@1119: real = get_project_directory luis@1119: real = File.join(real, path) unless path.nil? || path.empty? luis@1119: dir = File.expand_path(get_project_directory) luis@1119: real = File.expand_path(real) luis@1119: raise Errno::ENOENT unless real.starts_with?(dir) && File.exist?(real) luis@1119: real luis@1119: end luis@1119: luis@1119: # Returns the index file in the given directory luis@1119: # and raises an exception if none is found luis@1119: def get_index_file(dir) luis@1119: indexes = Setting.plugin_redmine_embedded['index'].to_s.split luis@1119: file = indexes.find {|f| File.exist?(File.join(dir, f))} luis@1119: raise RedmineEmbeddedControllerError.new("No index file found in #{dir} (#{indexes.join(', ')}).") if file.nil? luis@1119: file luis@1119: end luis@1119: luis@1119: # Renders a given HTML file luis@1119: def embed_file(path) luis@1119: @content = File.read(path) luis@1119: luis@1119: # Extract html title from embedded page luis@1119: if @content =~ %r{