app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  
  include OpenidServerSystem
  include AuthenticatedSystem
  
  helper_method :email_as_login?
  
  protect_from_forgery
  
  rescue_from(
    ActiveRecord::RecordNotFound,
    ActionController::UnknownAction, :with => :render_404)
  rescue_from ActionController::InvalidAuthenticityToken, :with => :render_422
  
  helper_method :extract_host, :extract_login_from_identifier, :checkid_request,
    :identifier, :endpoint_url, :scheme
  
  before_filter :set_locale
  
  protected
  
  # before_filter for every account-based controller
  def find_account
    @account = current_account
  end
  
  def endpoint_url
    server_url(:protocol => scheme)
  end
  
  # Returns the OpenID identifier for an account
  def identifier(account)
    identity_url(:account => account, :protocol => scheme)
  end
  
  # Extracts the hostname from the given url, which is used to
  # display the name of the requesting website to the user
  def extract_host(u)
    URI.split(u).compact[1]
  end
  
  def extract_login_from_identifier(openid_url)
    openid_url.gsub(/^https?:\/\/.*\//, '')
  end
  
  def checkid_request
    unless @checkid_request
      req = openid_server.decode_request(current_openid_request.parameters) if current_openid_request
      @checkid_request = req.is_a?(OpenID::Server::CheckIDRequest) ? req : false
    end
    @checkid_request
  end
  
  def current_openid_request
    @current_openid_request ||= OpenIdRequest.find_by_token(session[:request_token]) if session[:request_token]
  end
  
  def render_404
    render_error(404)
  end
  
  def render_422
    render_error(422)
  end
  
  def render_500
    render_error(500)
  end
  
  def render_error(status_code)
    render :file => "#{Rails.root}/public/#{status_code}.html", :status => status_code, :layout => false
  end
  
  # Set site locale from
  # * params[:locale]
  # * HTTP_ACCEPT_LANGUAGE header
  # 
  # If desired locale is not supported or nothing is set, fallback to English ("en")
  def set_locale 
    locale = params[:locale]
    locale ||= request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first if request.env['HTTP_ACCEPT_LANGUAGE']
    I18n.locale = (locale && I18n.available_locales.include?(locale.to_sym)) ? locale : I18n.default_locale
  end 
  
  private
  
  def scheme
    Masquerade::Application::Config['use_ssl'] ? 'https' : 'http'
  end

  def email_as_login?
    Masquerade::Application::Config['email_as_login']
  end

end
