class KitController < ActionController::Base include DomainController helper :all helper_method :stylesheets before_filter :set_requested_url, :except=>[:not_found_404] before_filter :set_system, :except=>[:not_found_404] before_filter :offline, :except=>[:not_found_404, :down_for_maintenance] before_filter :kit_session, :except=>[:not_found_404] after_filter :kit_session_end, :except=>[:not_found_404] append_view_path Layout.resolver after_filter :check_and_record_goal, :except=>[:not_found_404] attr_accessor :layout_name_being_used attr_accessor :template_being_used attr_accessor :requested_url attr_accessor :is_image_request attr_accessor :kit_request def set_requested_url self.is_image_request = false self.requested_url = request.fullpath if self.requested_url =~ /\.(?:jpg|png|gif|jpeg)$/i self.is_image_request = true end end def check_and_record_goal return if self.is_image_request use_experiments = Preference.get_cached(_sid, "feature_experiments")=='true' if use_experiments started = cookies[:started] || Time.now cookies[:started] = {:value=>started, :expires=>Time.now+30.minutes} if Goal.has_goals?(_sid) Goal.record_request(_sid, self.requested_url, cookies, current_user, started, session) end end end def session_id session[:session_id] end def offline return if (current_user && current_user.admin?) || params[:overrride] message = Preference.get_cached(_sid, "down_for_maintenance_message") if message render :text=>Preference.get_cached(_sid, "down_for_maintenance_message"), :layout=>false, :status=>503 return false end end def kit_session return if self.is_image_request return if self.is_a?(AdminController) || (self.is_a?(PagesController) && params[:action]!="show") || self.is_a?(CategoryController) || self.is_a?(ImagesController) ks = KitSession.sys(_sid).where(:session_id=>session_id).first unless ks ks = KitSession.create(:session_id=>session_id, :user_id=>0, :first_request=>Time.now, :page_views=>0, :system_id=>_sid) end kr = KitRequest.new kr.kit_session_id = ks.id kr.ip = request.remote_ip kr.url = request.fullpath kr.referer = request.referer kr.save ks.update_attributes(:last_request=>Time.now, :page_views => ks.page_views + 1, :user_id=>current_user ? current_user.id : 0) end def stylesheets if @page return (@page.page_template.layout.stylesheets + "," + @page.page_template.stylesheets).split(',').uniq elsif @form return @form.include_stylesheets else layout = kit_layout_in_use if layout return layout.stylesheets.split(',').uniq else return ["application"] end end end def kit_layout_in_use l = nil if self.layout_name_being_used # this gets set if kit_render is being used l = Layout.sys(_sid).where(:name=>self.layout_name_being_used).first else l = @page.layout if @page end return l end def kit_session_end return if self.is_image_request response["handler"] = "Kit/#{params[:controller]}/#{params[:action]}" end alias :super_render :render def render(name = params[:action], options = {}) if Preference.get_cached(_sid, 'dont_use_overridable_templates')=='true' || (params[:controller] && params[:controller].starts_with?('admin/')) super_render(name, options) else kit_render(name, options) end end def kit_render(name, options = {}) if options[:partial] name = options[:partial] end custom_template = PageTemplate.get_custom_template(_sid, name, request) if custom_template @content = render_to_string name, :layout=>false options[:type] = custom_template.template_type || 'erb' options[:inline] = custom_template.body options[:layout] = custom_template.layout.path self.template_being_used = custom_template self.layout_name_being_used = custom_template.layout.name super_render options else self.layout_name_being_used = options[:layout] super_render name, options end end def mobile_template(l) return Rails.cache.fetch("_mobile_template_#{l}", :expires_in=>1.minute) do parts = l.split('/') fn = '' for i in 0..parts.size-1 fn += '/' unless parts.size==1 fn += 'mobile-' if i==parts.size-1 fn += parts[i] end sep = fn[0]=='/' ? '' : '/' [".haml", ".erb"].each do |type| ActionController::Base.view_paths.each do |path| path = path.to_s if File.exists?(path + sep + fn + type) l = fn break end if File.exists?(path + '/' + params[:controller] + sep + fn + type) l = fn break end end end l end end def dif(l) if browser_dif if is_mobile? || params[:fake_mobile] mobile_template(l) end end l end unless config.consider_all_requests_local rescue_from Exception, :with => :render_error rescue_from ActiveRecord::RecordNotFound, :with => :render_error rescue_from ActionController::RoutingError, :with => :routing_error rescue_from ActionController::UnknownController, :with => :render_error rescue_from AbstractController::ActionNotFound, :with => :render_error end def routing_error(exception) render_error(exception) end def render_error(exception, detail = '') @not_found = exception.instance_of?(ActionController::RoutingError) if @not_found && request.fullpath =~ /\.(gif|png|jpg|jpeg)/ render :text=>"Not found", :status=>404 return end if @not_found && Preference.get_cached(_sid, "page_not_found_url") render_page_by_url Preference.get_cached(_sid, "page_not_found_url") return end @reference = Digest::MD5.hexdigest(Time.now.to_s)[0..8] @exception = exception logger.error "Error reference: ***** #{@reference} #{@exception} #{request.fullpath} *****" session[:error_message] = "Page not found" if @not_found @notes = <<-HERE Request: #{request.method} #{request.fullpath} Controller: #{params.delete(:controller)} Action: #{params.delete(:action)} Parameters: #{params.collect { |k,v| "#{k} = #{v}\n" }.join(' ') } Reference: #{@reference} Timestamp: #{Time.now} Exception Message: #{exception.message} Error Messages: #{session[:error_message]} Debug Error Message: #{session[:debug_error_message]} User: #{current_user ? (current_user.id.to_s + ' ' + current_user.email) : ''} Session: #{session.inspect} #{detail.not_blank? ? detail : ''} Stack Trace:\n #{exception.backtrace.join("\n")} HERE logger.debug @notes if Rails.env.development? && Preference.getCached(_sid, "log_errors")!="true" logger.debug @notes render "error/development", :layout=>false else status = @not_found ? 404 : 500 Event.store("#{status} error", request, current_user ? current_user.id : nil, @notes, @reference) unless status == 404 error_template = PageTemplate.sys(_sid).where(:name=>Preference.get_cached(_sid, "error_template")).first if error_template inline_template = "