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
|
|
|
|
|
2005-01-22 02:35:00 +01:00
|
|
|
before_filter :set_utf8_http_header, :connect_to_model
|
2005-01-18 00:17:28 +01:00
|
|
|
after_filter :remember_location
|
|
|
|
|
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
|
|
|
|
$instiki_wiki_service = the_wiki
|
|
|
|
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
|
|
|
|
$instiki_wiki_service
|
|
|
|
end
|
2005-01-18 00:17:28 +01:00
|
|
|
|
|
|
|
protected
|
2005-01-15 21:26:54 +01:00
|
|
|
|
2005-01-22 03:49:52 +01:00
|
|
|
def authorized?
|
|
|
|
@web.nil? ||
|
|
|
|
@web.password.nil? ||
|
|
|
|
cookies['web_address'] == @web.password ||
|
|
|
|
password_check(@params['password'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_authorization
|
2005-04-03 09:31:11 +02:00
|
|
|
if in_a_web? and needs_authorization?(@action_name) and not authorized? and
|
|
|
|
redirect_to :controller => 'wiki', :action => 'login', :web => @web_name
|
2005-01-22 03:49:52 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect_to_model
|
|
|
|
@action_name = @params['action'] || 'index'
|
|
|
|
@web_name = @params['web']
|
|
|
|
@wiki = wiki
|
2005-03-26 00:40:03 +01:00
|
|
|
if @web_name
|
|
|
|
@web = @wiki.webs[@web_name]
|
|
|
|
if @web.nil?
|
|
|
|
render_text "Unknown web '#{@web_name}'", '404 Not Found'
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2005-01-22 03:49:52 +01:00
|
|
|
@page_name = @file_name = @params['id']
|
|
|
|
@page = @wiki.read_page(@web_name, @page_name) unless @page_name.nil?
|
|
|
|
@author = cookies['author'] || 'AnonymousCoward'
|
|
|
|
check_authorization
|
|
|
|
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
|
|
|
|
|
|
|
def send_file(file, options = {})
|
|
|
|
options[:type] ||= (FILE_TYPES[File.extname(file)] || 'application/octet-stream')
|
|
|
|
super(file, options)
|
|
|
|
end
|
|
|
|
|
2005-01-22 03:49:52 +01:00
|
|
|
def in_a_web?
|
|
|
|
not @web_name.nil?
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
2005-01-18 00:17:28 +01:00
|
|
|
|
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)
|
|
|
|
redirect_to_page('HomePage', web)
|
|
|
|
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-03-29 08:10:01 +02:00
|
|
|
:id => (page_name || 'HomePage')
|
2005-01-28 02:24:31 +01:00
|
|
|
end
|
|
|
|
|
2005-01-27 04:55:19 +01:00
|
|
|
@@REMEMBER_NOT = ['locked', 'save', 'back', 'file', 'pic', 'import']
|
2005-01-18 00:17:28 +01:00
|
|
|
def remember_location
|
|
|
|
if @response.headers['Status'] == '200 OK'
|
2005-01-18 01:36:43 +01:00
|
|
|
unless @@REMEMBER_NOT.include? action_name or @request.method != :get
|
2005-02-15 23:41:58 +01:00
|
|
|
@session[:return_to] = @request.request_uri
|
2005-01-18 01:36:43 +01:00
|
|
|
logger.debug("Session ##{session.object_id}: remembered URL '#{@session[:return_to]}'")
|
|
|
|
end
|
2005-01-18 00:17:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def return_to_last_remembered
|
|
|
|
# Forget the redirect location
|
|
|
|
redirect_target, @session[:return_to] = @session[:return_to], nil
|
|
|
|
# then try to redirect to it
|
|
|
|
if redirect_target.nil?
|
2005-01-18 01:36:43 +01:00
|
|
|
logger.debug("Session ##{session.object_id}: no remembered redirect location, trying /")
|
2005-01-18 00:17:28 +01:00
|
|
|
redirect_to_url '/'
|
|
|
|
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-01-21 20:41:46 +01:00
|
|
|
def set_utf8_http_header
|
|
|
|
@response.headers['Content-Type'] = 'text/html; charset=UTF-8'
|
|
|
|
end
|
|
|
|
|
2005-01-22 03:49:52 +01:00
|
|
|
def wiki
|
|
|
|
$instiki_wiki_service
|
2005-01-22 02:35:00 +01:00
|
|
|
end
|
|
|
|
|
2005-04-03 09:31:11 +02:00
|
|
|
def needs_authorization?(action)
|
|
|
|
not %w( login authenticate published rss_with_content rss_with_headlines ).include?(action)
|
|
|
|
end
|
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|