app_helpers.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. def dashboard_if_signed_in
  2. redirect '/dashboard' if signed_in?
  3. end
  4. def csrf_safe?
  5. csrf_token == params[:csrf_token] || csrf_token == request.env['HTTP_X_CSRF_TOKEN']
  6. end
  7. def csrf_token
  8. session[:_csrf_token] ||= SecureRandom.base64(32)
  9. end
  10. def is_education?
  11. current_site && current_site.is_education
  12. end
  13. def require_login
  14. redirect '/' unless signed_in? && current_site
  15. end
  16. def signed_in?
  17. return false if current_site.nil?
  18. true
  19. end
  20. def signout
  21. @_site = nil
  22. @_parent_site = nil
  23. session[:id] = nil
  24. end
  25. def current_site
  26. return nil if session[:id].nil?
  27. @_site ||= Site[id: session[:id]]
  28. @_parent_site ||= @_site.parent
  29. if @_site.is_banned || @_site.is_deleted || (@_parent_site && (@_parent_site.is_banned || @_parent_site.is_deleted))
  30. signout
  31. end
  32. @_site
  33. end
  34. def parent_site
  35. @_parent_site || current_site
  36. end
  37. def meta_robots(newtag=nil)
  38. if newtag
  39. @_meta_robots = newtag
  40. end
  41. @_meta_robots
  42. end
  43. def title
  44. out = "Neocities"
  45. return out if request.path == '/'
  46. return "#{out} - #{@title}" if @title
  47. "#{out} - #{request.path.gsub('/', '').capitalize}"
  48. end
  49. def encoding_fix(file)
  50. begin
  51. Rack::Utils.escape_html file
  52. rescue ArgumentError => e
  53. if e.message =~ /invalid byte sequence in UTF-8/ ||
  54. e.message =~ /incompatible character encodings/
  55. return Rack::Utils.escape_html(file.force_encoding('BINARY'))
  56. end
  57. fail
  58. end
  59. end
  60. def send_confirmation_email(site=current_site)
  61. if site.email_confirmation_count > Site::MAXIMUM_EMAIL_CONFIRMATIONS
  62. flash[:error] = 'You sent too many email confirmation requests, cannot continue.'
  63. redirect request.referrer
  64. end
  65. DB['UPDATE sites set email_confirmation_count=email_confirmation_count+1 WHERE id=?', site.id].first
  66. EmailWorker.perform_async({
  67. from: 'web@neocities.org',
  68. reply_to: 'contact@neocities.org',
  69. to: site.email,
  70. subject: "[Neocities] Confirm your email address",
  71. body: Tilt.new('./views/templates/email/confirm.erb', pretty: true).render(self, site: site)
  72. })
  73. end
  74. def dont_browser_cache
  75. headers['Cache-Control'] = 'private, no-store, max-age=0, no-cache, must-revalidate, post-check=0, pre-check=0'
  76. headers['Pragma'] = 'no-cache'
  77. headers['Expires'] = 'Fri, 01 Jan 1990 00:00:00 GMT'
  78. @dont_browser_cache = true
  79. end
  80. def sanitize_comment(text)
  81. Rinku.auto_link Sanitize.fragment(text), :all, 'target="_blank" rel="nofollow"'
  82. end
  83. def flash_display(opts={})
  84. erb :'_flash', layout: false, locals: {opts: opts}
  85. end
  86. def hcaptcha_valid?
  87. return true if ENV['RACK_ENV'] == 'test' || ENV['CI']
  88. return false unless params[:'h-captcha-response']
  89. resp = HTTP.get('https://hcaptcha.com/siteverify', params: {
  90. secret: $config['hcaptcha_secret_key'],
  91. response: params[:'h-captcha-response']
  92. })
  93. resp = JSON.parse resp
  94. if resp['success'] == true
  95. true
  96. else
  97. false
  98. end
  99. end
  100. JS_ESCAPE_MAP = {"\\" => "\\\\", "</" => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'", "`" => "\\`", "$" => "\\$"}
  101. def escape_javascript(javascript)
  102. javascript = javascript.to_s
  103. if javascript.empty?
  104. result = ""
  105. else
  106. result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u, JS_ESCAPE_MAP)
  107. end
  108. result
  109. end