Chris@154: # Redmine - project management software Chris@154: # Copyright (C) 2008 Jean-Philippe Lang Chris@154: # Chris@154: # This program is free software; you can redistribute it and/or Chris@154: # modify it under the terms of the GNU General Public License Chris@154: # as published by the Free Software Foundation; either version 2 Chris@154: # of the License, or (at your option) any later version. Chris@154: # Chris@154: # This program is distributed in the hope that it will be useful, Chris@154: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@154: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@154: # GNU General Public License for more details. Chris@154: # Chris@154: # You should have received a copy of the GNU General Public License Chris@154: # along with this program; if not, write to the Free Software Chris@154: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@154: Chris@154: require 'iconv' Chris@154: Chris@154: class EmbeddedController < ApplicationController Chris@154: class EmbeddedControllerError < StandardError; end Chris@154: Chris@154: unloadable Chris@154: layout 'base' Chris@154: before_filter :find_project, :authorize Chris@154: Chris@154: def index Chris@154: path = get_real_path(params[:path]) Chris@154: if File.directory?(path) Chris@154: file = get_index_file(path) Chris@154: target = params[:path] || [] Chris@154: target << file Chris@154: # Forces redirect to the index file when the requested path is a directory Chris@154: # so that relative links in embedded html pages work Chris@154: redirect_to :path => target Chris@154: return Chris@154: end Chris@154: Chris@154: # Check file extension Chris@154: raise EmbeddedControllerError.new('This file can not be viewed (invalid extension).') unless Redmine::Plugins::Embedded.valid_extension?(path) Chris@154: Chris@154: if Redmine::MimeType.is_type?('image', path) Chris@154: send_file path, :disposition => 'inline', :type => Redmine::MimeType.of(path) Chris@154: else Chris@154: embed_file path Chris@154: end Chris@154: Chris@154: rescue Errno::ENOENT => e Chris@154: # File was not found Chris@154: render_404 Chris@154: rescue Errno::EACCES => e Chris@154: # Can not read the file Chris@154: render_error "Unable to read the file: #{e.message}" Chris@154: rescue EmbeddedControllerError => e Chris@154: render_error e.message Chris@154: end Chris@154: Chris@154: private Chris@154: Chris@154: def find_project Chris@154: @project = Project.find(params[:id]) Chris@154: rescue ActiveRecord::RecordNotFound Chris@154: render_404 Chris@154: end Chris@154: Chris@154: # Return the path to the html root directory for the current project Chris@154: def get_project_directory Chris@154: @project_directory ||= Setting.plugin_embedded['path'].to_s.gsub('{PROJECT}', @project.identifier) Chris@154: end Chris@154: Chris@154: # Returns the absolute path of the requested file Chris@154: # Parameter is an Array Chris@154: def get_real_path(path) Chris@154: real = get_project_directory Chris@154: real = File.join(real, path) unless path.nil? || path.empty? Chris@154: dir = File.expand_path(get_project_directory) Chris@154: real = File.expand_path(real) Chris@154: raise Errno::ENOENT unless real.starts_with?(dir) && File.exist?(real) Chris@154: real Chris@154: end Chris@154: Chris@154: # Returns the index file in the given directory Chris@154: # and raises an exception if none is found Chris@154: def get_index_file(dir) Chris@154: indexes = Setting.plugin_embedded['index'].to_s.split Chris@154: file = indexes.find {|f| File.exist?(File.join(dir, f))} Chris@154: raise EmbeddedControllerError.new("No index file found in #{dir} (#{indexes.join(', ')}).") if file.nil? Chris@154: file Chris@154: end Chris@154: Chris@154: # Renders a given HTML file Chris@154: def embed_file(path) Chris@154: @content = File.read(path) Chris@154: Chris@154: # Extract html title from embedded page Chris@154: if @content =~ %r{