<commit_before>require 'sinatra'
require 'sinatra/activerecord'
require 'will_paginate'
require 'will_paginate/active_record'
require './config/env'
require 'sinatra/recaptcha'
require 'bcrypt'
require 'rack-flash'

# wow models
require './models/Quote'
require './models/User'

abort "Set $COOKIE_SECRET to a cookie secret!" unless ENV['COOKIE_SECRET']
use Rack::Session::Cookie, :expire_after => 604800,
                           :secret => ENV['COOKIE_SECRET']

use Rack::Flash, :accessorize => [:info, :success, :error]

configure do
  set :auth_flags, {
    :post_quotes => 1,
    :edit_quotes => 2,
    :delete_quotes => 4,
    :list_users => 8,
    :approve_quotes => 16,
    :set_flags => 32,
    :edit_users => 64
  }

  set(:auth) do |*roles|
    condition do
      # new pseudo-role: logged_in
      if roles.include? :logged_in
        unless session[:username]
          flash[:error] = 'You need to be logged in to go here.'
          redirect '/user/login'
        end
        return
      end

      unless session[:username]
        flash[:error] = 'You need to be logged in to go here.'
        redirect '/user/login'
        return
      end

      curr_flags = session[:flags]
      auth_flags = settings.auth_flags

      allowed = roles.length > 0

      roles.each do |role|
        # skip the :logged_in role
        next if role == :logged_in
        flag_exists = (curr_flags & auth_flags[role]) != 0
        allowed = allowed && flag_exists
      end
      unless allowed
        flash[:error] = 'You aren\'t allowed to go here! :('
        redirect '/user/login'
      end
    end

    abort "Set $RECAPTCHA_CLIENTKEY to your recaptcha public/client key!" unless ENV['RECAPTCHA_CLIENTKEY']
    Sinatra::ReCaptcha.public_key = ENV['RECAPTCHA_CLIENTKEY']

    abort "Set $RECAPTCHA_SECRET to your recaptcha secret!" unless ENV['RECAPTCHA_SECRET']
    Sinatra::ReCaptcha.private_key = ENV['RECAPTCHA_SECRET']
  end

  # Open a new file for moderation action logging
  $moderationLog = File.new './log/moderation.log', 'a'
  $moderationLog.sync = true

  def logModAction(name, action, on)
    time = DateTime.now.iso8601
    $moderationLog.puts "time=#{time} name=#{name} action=#{action} on=#{on}"
  end

  set :logModAction, lambda { |name, act, on| logModAction(name, act, on) }<commit_msg>mod logs are now stdout because heroku<commit_after>require 'sinatra'
require 'sinatra/activerecord'
require 'will_paginate'
require 'will_paginate/active_record'
require './config/env'
require 'sinatra/recaptcha'
require 'bcrypt'
require 'rack-flash'

# wow models
require './models/Quote'
require './models/User'

abort "Set $COOKIE_SECRET to a cookie secret!" unless ENV['COOKIE_SECRET']
use Rack::Session::Cookie, :expire_after => 604800,
                           :secret => ENV['COOKIE_SECRET']

use Rack::Flash, :accessorize => [:info, :success, :error]

configure do
  set :auth_flags, {
    :post_quotes => 1,
    :edit_quotes => 2,
    :delete_quotes => 4,
    :list_users => 8,
    :approve_quotes => 16,
    :set_flags => 32,
    :edit_users => 64
  }

  set(:auth) do |*roles|
    condition do
      # new pseudo-role: logged_in
      if roles.include? :logged_in
        unless session[:username]
          flash[:error] = 'You need to be logged in to go here.'
          redirect '/user/login'
        end
        return
      end

      unless session[:username]
        flash[:error] = 'You need to be logged in to go here.'
        redirect '/user/login'
        return
      end

      curr_flags = session[:flags]
      auth_flags = settings.auth_flags

      allowed = roles.length > 0

      roles.each do |role|
        # skip the :logged_in role
        next if role == :logged_in
        flag_exists = (curr_flags & auth_flags[role]) != 0
        allowed = allowed && flag_exists
      end
      unless allowed
        flash[:error] = 'You aren\'t allowed to go here! :('
        redirect '/user/login'
      end
    end

    abort "Set $RECAPTCHA_CLIENTKEY to your recaptcha public/client key!" unless ENV['RECAPTCHA_CLIENTKEY']
    Sinatra::ReCaptcha.public_key = ENV['RECAPTCHA_CLIENTKEY']

    abort "Set $RECAPTCHA_SECRET to your recaptcha secret!" unless ENV['RECAPTCHA_SECRET']
    Sinatra::ReCaptcha.private_key = ENV['RECAPTCHA_SECRET']
  end

  # Mod logs are now also stdout
  def logModAction(name, action, on)
    puts "modaction name=#{name} action=#{action} on=#{on}"
  end

  set :logModAction, lambda { |name, act, on| logModAction(name, act, on) }
{'fraction_non_alphanumeric': 0.11707317073170732, 'fraction_numerical': 0.008869179600886918, 'mean_word_length': 2.922608695652174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3147946', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1485, 'n_words': 402}