2005-01-15 21:26:54 +01:00
|
|
|
# The filters added to this controller will be run for all controllers in the application.
|
|
|
|
# Likewise will all the methods added be available for all controllers.
|
|
|
|
class ApplicationController < ActionController::Base
|
|
|
|
|
2006-03-24 08:53:20 +01:00
|
|
|
before_filter :connect_to_model, :check_authorization, :setup_url_generator, :set_content_type_header, :set_robots_metatag
|
2005-09-10 13:07:40 +02:00
|
|
|
after_filter :remember_location, :teardown_url_generator
|
2005-01-18 00:17:28 +01:00
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
# For injecting a different wiki model implementation. Intended for use in tests
|
|
|
|
def self.wiki=(the_wiki)
|
|
|
|
# a global variable is used here because Rails reloads controller and model classes in the
|
|
|
|
# development environment; therefore, storing it as a class variable does not work
|
|
|
|
# class variable is, anyway, not much different from a global variable
|
2005-08-09 04:20:28 +02:00
|
|
|
#$instiki_wiki_service = the_wiki
|
2005-01-15 21:26:54 +01:00
|
|
|
logger.debug("Wiki service: #{the_wiki.to_s}")
|
|
|
|
end
|
2005-01-18 00:17:28 +01:00
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
def self.wiki
|
2005-08-09 04:20:28 +02:00
|
|
|
Wiki.new
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
2005-01-18 00:17:28 +01:00
|
|
|
|
|
|
|
protected
|
2006-03-23 08:14:51 +01:00
|
|
|
|
2005-01-22 03:49:52 +01:00
|
|
|
def check_authorization
|
2005-11-04 06:23:34 +01:00
|
|
|
if in_a_web? and authorization_needed? and not authorized?
|
2005-04-03 09:31:11 +02:00
|
|
|
redirect_to :controller => 'wiki', :action => 'login', :web => @web_name
|
2005-01-22 03:49:52 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-24 08:53:20 +01:00
|
|
|
def connect_to_model
|
2005-01-22 03:49:52 +01:00
|
|
|
@action_name = @params['action'] || 'index'
|
|
|
|
@web_name = @params['web']
|
|
|
|
@wiki = wiki
|
2006-03-23 08:14:51 +01:00
|
|
|
@author = cookies['author'] || 'AnonymousCoward'
|
2005-03-26 00:40:03 +01:00
|
|
|
if @web_name
|
2005-11-13 14:37:47 +01:00
|
|
|
@web = @wiki.webs[@web_name]
|
2005-03-26 00:40:03 +01:00
|
|
|
if @web.nil?
|
2006-03-23 08:14:51 +01:00
|
|
|
render(:status => 404, :text => "Unknown web '#{@web_name}'")
|
|
|
|
return false
|
2005-03-26 00:40:03 +01:00
|
|
|
end
|
|
|
|
end
|
2005-01-22 03:49:52 +01:00
|
|
|
end
|
|
|
|
|
2005-01-22 15:58:43 +01:00
|
|
|
FILE_TYPES = {
|
|
|
|
'.exe' => 'application/octet-stream',
|
|
|
|
'.gif' => 'image/gif',
|
|
|
|
'.jpg' => 'image/jpeg',
|
|
|
|
'.pdf' => 'application/pdf',
|
|
|
|
'.png' => 'image/png',
|
|
|
|
'.txt' => 'text/plain',
|
|
|
|
'.zip' => 'application/zip'
|
2005-04-07 07:14:02 +02:00
|
|
|
} unless defined? FILE_TYPES
|
2005-01-22 15:58:43 +01:00
|
|
|
|
2006-03-12 05:53:39 +01:00
|
|
|
DISPOSITION = {
|
|
|
|
'application/octet-stream' => 'attachment',
|
|
|
|
'image/gif' => 'inline',
|
|
|
|
'image/jpeg' => 'inline',
|
|
|
|
'application/pdf' => 'inline',
|
|
|
|
'image/png' => 'inline',
|
|
|
|
'text/plain' => 'inline',
|
|
|
|
'application/zip' => 'attachment'
|
|
|
|
} unless defined? DISPOSITION
|
|
|
|
|
|
|
|
def determine_file_options_for(file_name, original_options = {})
|
|
|
|
original_options[:type] ||= (FILE_TYPES[File.extname(file_name)] or 'application/octet-stream')
|
|
|
|
original_options[:disposition] ||= (DISPOSITION[original_options[:type]] or 'attachment')
|
|
|
|
original_options[:stream] ||= false
|
|
|
|
original_options
|
2005-11-13 14:37:47 +01:00
|
|
|
end
|
|
|
|
|
2005-01-22 15:58:43 +01:00
|
|
|
def send_file(file, options = {})
|
2006-03-12 05:53:39 +01:00
|
|
|
determine_file_options_for(file, options)
|
2005-01-22 15:58:43 +01:00
|
|
|
super(file, options)
|
|
|
|
end
|
|
|
|
|
2005-03-26 16:43:59 +01:00
|
|
|
def password_check(password)
|
|
|
|
if password == @web.password
|
|
|
|
cookies['web_address'] = password
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-02-05 14:34:12 +01:00
|
|
|
def password_error(password)
|
|
|
|
if password.nil? or password.empty?
|
|
|
|
'Please enter the password.'
|
|
|
|
else
|
|
|
|
'You entered a wrong password. Please enter the right one.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-29 01:07:42 +02:00
|
|
|
def redirect_home(web = @web_name)
|
2005-05-09 07:53:47 +02:00
|
|
|
if web
|
|
|
|
redirect_to_page('HomePage', web)
|
|
|
|
else
|
|
|
|
redirect_to_url '/'
|
|
|
|
end
|
2005-04-29 01:07:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_page(page_name = @page_name, web = @web_name)
|
2005-01-28 02:24:31 +01:00
|
|
|
redirect_to :web => web, :controller => 'wiki', :action => 'show',
|
2005-11-02 10:04:53 +01:00
|
|
|
:id => (page_name or 'HomePage')
|
2005-01-28 02:24:31 +01:00
|
|
|
end
|
|
|
|
|
2005-01-18 00:17:28 +01:00
|
|
|
def remember_location
|
2005-11-02 10:04:53 +01:00
|
|
|
if @request.method == :get and
|
|
|
|
@response.headers['Status'] == '200 OK' and not
|
|
|
|
%w(locked save back file pic import).include?(action_name)
|
|
|
|
@session[:return_to] = @request.request_uri
|
|
|
|
logger.debug "Session ##{session.object_id}: remembered URL '#{@session[:return_to]}'"
|
2005-01-18 00:17:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-05-09 06:31:02 +02:00
|
|
|
def rescue_action_in_public(exception)
|
2005-11-02 10:04:53 +01:00
|
|
|
render :status => 500, :text => <<-EOL
|
2005-05-09 07:16:20 +02:00
|
|
|
<html><body>
|
2005-11-02 10:04:53 +01:00
|
|
|
<h2>Internal Error</h2>
|
2005-05-09 07:16:20 +02:00
|
|
|
<p>An application error occurred while processing your request.</p>
|
2005-11-02 10:04:53 +01:00
|
|
|
<!-- \n#{exception}\n#{exception.backtrace.join("\n")}\n -->
|
2005-05-09 07:16:20 +02:00
|
|
|
</body></html>
|
2005-05-09 06:31:02 +02:00
|
|
|
EOL
|
|
|
|
end
|
2005-05-09 07:16:20 +02:00
|
|
|
|
2005-01-18 00:17:28 +01:00
|
|
|
def return_to_last_remembered
|
|
|
|
# Forget the redirect location
|
|
|
|
redirect_target, @session[:return_to] = @session[:return_to], nil
|
2005-05-09 07:53:47 +02:00
|
|
|
tried_home, @session[:tried_home] = @session[:tried_home], false
|
|
|
|
|
2005-01-18 00:17:28 +01:00
|
|
|
# then try to redirect to it
|
|
|
|
if redirect_target.nil?
|
2005-05-09 07:53:47 +02:00
|
|
|
if tried_home
|
|
|
|
raise 'Application could not render the index page'
|
|
|
|
else
|
|
|
|
logger.debug("Session ##{session.object_id}: no remembered redirect location, trying home")
|
|
|
|
redirect_home
|
|
|
|
end
|
2005-01-18 00:17:28 +01:00
|
|
|
else
|
2005-01-18 01:36:43 +01:00
|
|
|
logger.debug("Session ##{session.object_id}: " +
|
|
|
|
"redirect to the last remembered URL #{redirect_target}")
|
2005-01-18 00:17:28 +01:00
|
|
|
redirect_to_url(redirect_target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-09-12 03:12:00 +02:00
|
|
|
def set_content_type_header
|
|
|
|
if %w(rss_with_content rss_with_headlines).include?(action_name)
|
|
|
|
@response.headers['Content-Type'] = 'text/xml; charset=UTF-8'
|
|
|
|
else
|
|
|
|
@response.headers['Content-Type'] = 'text/html; charset=UTF-8'
|
|
|
|
end
|
2005-01-21 20:41:46 +01:00
|
|
|
end
|
|
|
|
|
2005-11-01 08:31:44 +01:00
|
|
|
def set_robots_metatag
|
|
|
|
if controller_name == 'wiki' and %w(show published).include? action_name
|
|
|
|
@robots_metatag_value = 'index,follow'
|
|
|
|
else
|
|
|
|
@robots_metatag_value = 'noindex,nofollow'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-09-10 13:07:40 +02:00
|
|
|
def setup_url_generator
|
|
|
|
PageRenderer.setup_url_generator(UrlGenerator.new(self))
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown_url_generator
|
|
|
|
PageRenderer.teardown_url_generator
|
|
|
|
end
|
|
|
|
|
2005-01-22 03:49:52 +01:00
|
|
|
def wiki
|
2005-08-09 04:20:28 +02:00
|
|
|
self.class.wiki
|
2005-01-22 02:35:00 +01:00
|
|
|
end
|
|
|
|
|
2005-11-04 06:23:34 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def in_a_web?
|
|
|
|
not @web_name.nil?
|
|
|
|
end
|
|
|
|
|
2005-11-02 10:04:53 +01:00
|
|
|
def authorization_needed?
|
|
|
|
not %w( login authenticate published rss_with_content rss_with_headlines ).include?(action_name)
|
2005-04-03 09:31:11 +02:00
|
|
|
end
|
|
|
|
|
2005-11-04 06:23:34 +01:00
|
|
|
def authorized?
|
2006-01-23 07:56:30 +01:00
|
|
|
@web.nil? or
|
2005-11-04 06:23:34 +01:00
|
|
|
@web.password.nil? or
|
|
|
|
cookies['web_address'] == @web.password or
|
|
|
|
password_check(@params['password'])
|
|
|
|
end
|
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|