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.



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

def is_image_request
  @is_image_request
end

#kit_requestObject

Returns the value of attribute kit_request.



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

def kit_request
  @kit_request
end

#layout_being_usedObject

Returns the value of attribute layout_being_used.



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

def layout_being_used
  @layout_being_used
end

#requested_urlObject

Returns the value of attribute requested_url.



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

def requested_url
  @requested_url
end

#template_being_usedObject

Returns the value of attribute template_being_used.



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

def template_being_used
  @template_being_used
end

Instance Method Details

#anti_spam_okay?Boolean

Returns:

  • (Boolean)


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

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



340
341
342
# File 'app/controllers/kit_controller.rb', line 340

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

#can_moderateObject



304
305
306
307
# File 'app/controllers/kit_controller.rb', line 304

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

#can_useObject



299
300
301
302
# File 'app/controllers/kit_controller.rb', line 299

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

#captcha_okay?Boolean

Returns:

  • (Boolean)


367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'app/controllers/kit_controller.rb', line 367

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



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/kit_controller.rb', line 42

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

#check_userObject



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

def check_user
  return true unless self.kit_system

  u = User.cookie_authenticate(_sid, cookies[:sign_in])
  if u
   u.(_sid, request, 'c')
   warden.set_user u
  end 
end

#csv_headers(filename) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
# File 'app/controllers/kit_controller.rb', line 439

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



185
186
187
188
189
190
191
192
193
# File 'app/controllers/kit_controller.rb', line 185

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

  l
end

#edit_page_path(page) ⇒ Object



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

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

#feature?(name) ⇒ Boolean

Returns:

  • (Boolean)


452
453
454
# File 'app/controllers/kit_controller.rb', line 452

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

#get_asset(id, code) ⇒ Object



412
413
414
415
416
417
418
419
420
# File 'app/controllers/kit_controller.rb', line 412

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



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'app/controllers/kit_controller.rb', line 313

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



456
457
458
# File 'app/controllers/kit_controller.rb', line 456

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

#index_nameObject



344
345
346
# File 'app/controllers/kit_controller.rb', line 344

def index_name
  ElasticSearch::INDEX_NAME
end

#info_page_path(page) ⇒ Object



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

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

#kit_layout_in_useObject



104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/kit_controller.rb', line 104

def kit_layout_in_use
  l = nil

  if self.layout_being_used # this gets set if kit_render is being used
    l = self.layout_being_used
  else 
    l = @page.layout if @page
  end

  return l
end

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/kit_controller.rb', line 131

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.name
    self.template_being_used = custom_template
    self.layout_id_being_used = custom_template.layout_id
    super_render options
  else
    if options[:layout_o] 
      self.layout_being_used = options[:layout_o]
      options[:layout] = self.layout_being_used.path
    elsif options[:layout]
      self.layout_being_used = Layout.sys(_sid).where(:path=>options[:layout]).first rescue nil
    end
    super_render name, options
  end
end

#kit_sessionObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/kit_controller.rb', line 69

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



116
117
118
119
# File 'app/controllers/kit_controller.rb', line 116

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


336
337
338
# File 'app/controllers/kit_controller.rb', line 336

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

#mailchimp_connectObject



460
461
462
# File 'app/controllers/kit_controller.rb', line 460

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

#mobile_template(l) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/controllers/kit_controller.rb', line 156

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)


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

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

#no_writeObject

Raises:

  • (ActionController::RoutingError)


278
279
280
# File 'app/controllers/kit_controller.rb', line 278

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

#not_foundObject

Raises:

  • (ActionController::RoutingError)


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

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

#not_found_404Object



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

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

#offlineObject



59
60
61
62
63
64
65
66
67
# File 'app/controllers/kit_controller.rb', line 59

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



290
291
292
# File 'app/controllers/kit_controller.rb', line 290

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

#pref(name) ⇒ Object



464
465
466
# File 'app/controllers/kit_controller.rb', line 464

def pref(name)
  Preference.get_cached(_sid, name)
end

#rails_app_nameObject



348
349
350
# File 'app/controllers/kit_controller.rb', line 348

def rails_app_name
  Rails.application.class.to_s.split("::").first
end

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



123
124
125
126
127
128
129
# File 'app/controllers/kit_controller.rb', line 123

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



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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'app/controllers/kit_controller.rb', line 207

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=>Layout.sys(_sid).where(:id=>(Preference.getCached(_sid, "error_layout").path || Layout.sys(_sid).first.id)).first.path, :status=>status
    end
  end

  session[:error_message] = nil
end

#render_page(page) ⇒ Object



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

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



422
423
424
425
426
427
428
429
430
# File 'app/controllers/kit_controller.rb', line 422

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



203
204
205
# File 'app/controllers/kit_controller.rb', line 203

def routing_error(exception)
    render_error(exception)
end

#sanity_check_okay?Boolean

Returns:

  • (Boolean)


382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'app/controllers/kit_controller.rb', line 382

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



55
56
57
# File 'app/controllers/kit_controller.rb', line 55

def session_id
  session[:session_id]
end

#set_requested_urlObject



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

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



352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'app/controllers/kit_controller.rb', line 352

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.layout.path
end

#stylesheetsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/kit_controller.rb', line 88

def stylesheets
  if @page
    return (@page.page_template.layout.stylesheets + @page.page_template.stylesheets).uniq
  elsif @form
    return (@form.stylesheets + @form.layout.stylesheets).uniq
  else
    layout = kit_layout_in_use
    if layout
      return layout.stylesheets
    end
  end

  return []
end

#super_renderObject



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

alias :super_render :render

#user_sees_menu?Boolean

Returns:

  • (Boolean)


309
310
311
# File 'app/controllers/kit_controller.rb', line 309

def user_sees_menu?
  current_user && current_user.sees_menu?
end