Class: KitController

Inherits:
ActionController::Base
  • Object
show all
Includes:
DomainController
Defined in:
app/controllers/kit_controller.rb

Constant Summary collapse

Pagebase =
Page.includes([:page_contents_version0, {:page_template=>:layout}, {:block_instances0=>:block}])

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#is_image_requestObject

Returns the value of attribute is_image_request.



19
20
21
# File 'app/controllers/kit_controller.rb', line 19

def is_image_request
  @is_image_request
end

#kit_requestObject

Returns the value of attribute kit_request.



20
21
22
# File 'app/controllers/kit_controller.rb', line 20

def kit_request
  @kit_request
end

#layout_name_being_usedObject

Returns the value of attribute layout_name_being_used.



15
16
17
# File 'app/controllers/kit_controller.rb', line 15

def layout_name_being_used
  @layout_name_being_used
end

#requested_urlObject

Returns the value of attribute requested_url.



17
18
19
# File 'app/controllers/kit_controller.rb', line 17

def requested_url
  @requested_url
end

#template_being_usedObject

Returns the value of attribute template_being_used.



16
17
18
# File 'app/controllers/kit_controller.rb', line 16

def template_being_used
  @template_being_used
end

Instance Method Details

#anti_spam_okay?Boolean

Returns:

  • (Boolean)


374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'app/controllers/kit_controller.rb', line 374

def anti_spam_okay?
  if honeypot_fields.any? { |f,l| !params[f].blank? }
    head :ok
    logger.info "*** ANTI SPAM: Rejected due to honeypot in #{params[:controller]}##{params[:action]}"
    if current_user
      current_user.update_attributes(:spam_points => current_user.spam_points + 1)
    end
    Event.store("anti-spam", request, current_user ? current_user.id : nil )
    return false
  else
    return true
  end
end

#app_nameObject



323
324
325
# File 'app/controllers/kit_controller.rb', line 323

def app_name
  Preference.get_cached(_sid, "app_name")
end

#can_moderateObject



287
288
289
290
# File 'app/controllers/kit_controller.rb', line 287

def can_moderate
  authenticate_user!
  authorize! :moderate, self.class
end

#can_useObject



282
283
284
285
# File 'app/controllers/kit_controller.rb', line 282

def can_use
  authenticate_user!
  authorize! :use, self.class
end

#captcha_okay?Boolean

Returns:

  • (Boolean)


343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'app/controllers/kit_controller.rb', line 343

def captcha_okay?
    if Form.validate_captcha_answer(params[:q_a], params[:q_q])
      return true
    else

      logger.info "*** ANTI SPAM: Failed captcha #{request.remote_ip} #{params[:controller]}##{params[:action]}"
      if current_user
        current_user.update_attributes(:spam_points => current_user.spam_points + 1) rescue nil
      end
      Event.store("captcha-failure", request, current_user ? current_user.id : nil )
      return false
    end

end

#check_and_record_goalObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/kit_controller.rb', line 31

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

#csv_headers(filename) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
# File 'app/controllers/kit_controller.rb', line 415

def csv_headers(filename)
  if request.env['HTTP_USER_AGENT'] =~ /msie/i
    headers['Pragma'] = 'public'
    headers["Content-type"] = "text/plain"
    headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
    headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
    headers['Expires'] = "0"
  else
    headers["Content-Type"] ||= 'text/csv'
    headers["Content-Disposition"] = "attachment; filename=\"#{filename}\"" 
  end
end

#dif(l) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'app/controllers/kit_controller.rb', line 169

def dif(l)
  if browser_dif
    if is_mobile? || params[:fake_mobile]
      mobile_template(l)
    end 
  end

  l
end

#edit_page_path(page) ⇒ Object



265
266
267
# File 'app/controllers/kit_controller.rb', line 265

def edit_page_path(page)
  "/#{page.class.name.tableize.pluralize}/#{page.id}/edit"
end

#feature?(name) ⇒ Boolean

Returns:

  • (Boolean)


428
429
430
# File 'app/controllers/kit_controller.rb', line 428

def feature?(name)
  Preference.licensed?(_sid, name) 
end

#get_asset(id, code) ⇒ Object



388
389
390
391
392
393
394
395
396
# File 'app/controllers/kit_controller.rb', line 388

def get_asset(id, code)
  @asset = Asset.where(:id=>id).first
  if @asset.code!=code
    redirect_to "/"
    return
  end

  send_file @asset.sys_file_path('original'), :type=>@asset.file_content_type, :x_sendfile=>true
end

#get_view_content(view = nil) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'app/controllers/kit_controller.rb', line 296

def get_view_content(view = nil)
  if view==nil
    view_name = params[:view_name]
    view = View.where(:name=>view_name).sys(_sid).first
  end

  output = ''
  output = render_to_string(:inline=>view.header, :layout=>false)
  
  data = Page.joins("left join terms on terms.page_id = pages.id").where("page_template_id in (#{view.page_template_id})").sys(_sid)
  data = data.order(eval('"' + view.order_by.gsub('"', '\"') + '"')) if view.order_by.not_blank?
  data = data.where(eval('"' + view.where_clause.gsub('"', '\"') + '"')) if view.where_clause.not_blank?

  @pages = data.page(params[:page]).per(view.per_page)
  @pages.each do |page|
    @page = page
    output += render_to_string(:inline=>view.body, :layout=>false, :type=>view.template_type || 'erb')
  end
  output += render_to_string(:inline=>view.footer, :layout=>false, :type=>view.template_type || 'erb')
  return output
end

#host_nameObject



432
433
434
# File 'app/controllers/kit_controller.rb', line 432

def host_name
  Preference.get(_sid, "host_name")
end

#info_page_path(page) ⇒ Object



269
270
271
# File 'app/controllers/kit_controller.rb', line 269

def info_page_path(page)
  "/#{page.class.name.tableize.pluralize}/#{page.id}/info"
end

#kit_layout_in_useObject



93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/kit_controller.rb', line 93

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

#kit_render(name, options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/kit_controller.rb', line 120

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

#kit_sessionObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/kit_controller.rb', line 58

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

#kit_session_endObject



105
106
107
108
# File 'app/controllers/kit_controller.rb', line 105

def kit_session_end
  return if self.is_image_request
  response["handler"] = "Kit/#{params[:controller]}/#{params[:action]}"
end


319
320
321
# File 'app/controllers/kit_controller.rb', line 319

def link_to(name, href) 
  "<a href='#{href}' title='#{name}'>#{name}</a>"
end

#mailchimp_connectObject



436
437
438
# File 'app/controllers/kit_controller.rb', line 436

def mailchimp_connect
  @gibbon = Gibbon.new(Preference.get_cached(_sid,'mailchimp_api_key'))
end

#mobile_template(l) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/kit_controller.rb', line 140

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

#no_readObject

Raises:

  • (ActionController::RoutingError)


257
258
259
# File 'app/controllers/kit_controller.rb', line 257

def no_read
  raise ActionController::RoutingError.new("Page Not Found (cannot read)")
end

#no_writeObject

Raises:

  • (ActionController::RoutingError)


261
262
263
# File 'app/controllers/kit_controller.rb', line 261

def no_write
  raise ActionController::RoutingError.new("Page Not Found (cannot write)")
end

#not_foundObject

Raises:

  • (ActionController::RoutingError)


253
254
255
# File 'app/controllers/kit_controller.rb', line 253

def not_found
  raise ActionController::RoutingError.new("Page Not Found")
end

#not_found_404Object



249
250
251
# File 'app/controllers/kit_controller.rb', line 249

def not_found_404
  super_render :text=>"not found", :status=>404, :layout=>false
end

#offlineObject



48
49
50
51
52
53
54
55
56
# File 'app/controllers/kit_controller.rb', line 48

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

#page_path(page) ⇒ Object



273
274
275
# File 'app/controllers/kit_controller.rb', line 273

def page_path(page)
  "/#{page.class.name.tableize.pluralize}/#{page.id}"
end

#render(name = , options = {}) ⇒ Object



112
113
114
115
116
117
118
# File 'app/controllers/kit_controller.rb', line 112

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

#render_error(exception, detail = '') ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'app/controllers/kit_controller.rb', line 191

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 = "<div id='page_#{page.id}' class='template_#{error_template.id}'>\n\n" + error_template.body + "\n\n</div>"
      render :inline=>inline_template, :layout=>error_template.layout.path, :type=>error_template.template_type || 'erb'
    else
      render "error/application", :layout=>Preference.getCached(_sid, "error_layout") || "application", :status=>status
    end
  end

  session[:error_message] = nil
end

#render_page(page) ⇒ Object



408
409
410
411
412
413
# File 'app/controllers/kit_controller.rb', line 408

def render_page(page)
  template = page.dif_template(use_mobile?)
  inline_template = "<div id='page_#{page.id}' class='#{page.editable ? 'editing' : 'not_editing'} template_#{template.id} #{app_name}_page #{page.page_name}'>\n\n" + template.body + "\n\n</div>"

  render :inline=>inline_template, :layout=>template.layout.path, :type=>template.template_type || 'erb'
end

#render_page_by_url(url) ⇒ Object



398
399
400
401
402
403
404
405
406
# File 'app/controllers/kit_controller.rb', line 398

def render_page_by_url(url)
  @page = Page.sys(_sid).where(:full_path=>url).first
  if (@page==nil || @page.deleted? || !@page.is_published?) 
    render "/error/404", :status=>404, :layout=>@page ? @page.dif_template(use_mobile?).layout.path : false
    return
  end 

  render_page(@page)
end

#routing_error(exception) ⇒ Object



187
188
189
# File 'app/controllers/kit_controller.rb', line 187

def routing_error(exception)
    render_error(exception)
end

#sanity_check_okay?Boolean

Returns:

  • (Boolean)


358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'app/controllers/kit_controller.rb', line 358

def sanity_check_okay?
  check = params[:check]
  unless check 
    logger.info "***** No form check code"
    redirect_to request.referer, :notice=>"Malformed submission" and return false
  end
  if SubmissionCheck.exists?(check)
    logger.info "**** Already submitted this form once"
    redirect_to request.referer, :notice=>"This has already been submitted" and return false
  end
  
  SubmissionCheck.record(check)

  return true
end

#session_idObject



44
45
46
# File 'app/controllers/kit_controller.rb', line 44

def session_id
  session[:session_id]
end

#set_requested_urlObject



22
23
24
25
26
27
28
29
# File 'app/controllers/kit_controller.rb', line 22

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

#show_form(form) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'app/controllers/kit_controller.rb', line 328

def show_form(form)
  @page_title = form.title
  if params[:edit]
    @sub = form.form_submissions.where(:id=>params[:edit]).first
    unless @sub && @sub.can_edit?(current_user)
      if current_user == nil
        redirect_to "/users/sign_in" and return
      end
      redirect_to "/" and return
    end
  end

  render "form/show", :layout=>((form.respond_to?(:layout) && form.layout) ? form.layout : 'application')
end

#stylesheetsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/kit_controller.rb', line 77

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

#super_renderObject



110
# File 'app/controllers/kit_controller.rb', line 110

alias :super_render :render

#user_sees_menu?Boolean

Returns:

  • (Boolean)


292
293
294
# File 'app/controllers/kit_controller.rb', line 292

def user_sees_menu?
  current_user && current_user.sees_menu?
end