<commit_before>require './config/environment'
require 'sinatra/base'
require 'uri'

class App < Sinatra::Base

  configure do
    if ENV['google_maps_api_key'] # In Production and Travis
      google_maps_api_key = ENV['google_maps_api_key']
    else # Test or Development
      require 'yaml'
      google_maps_api_key = YAML.load_file("config/config.yml")[settings.environment.to_s]["google_maps_api_key"]
    end

    set :google_maps_api_key, google_maps_api_key
  end

  get '/' do
    erb :'index'
  end

  get '/error' do
    @error = error_msg(params[:msg])
    erb :'index'
  end

  get '/success' do
    @success = success_msg(params[:msg])
    erb :'index'
  end

  get '/:map_name' do
    @location = Location.find_by(map_name: params[:map_name])
    if @location
      erb :'show'
    else
      redirect to('/error?msg=map_not_found')
    end
  end

  post '/' do
    location = Location.new(location_params(params[:location]))
    if location.save
      msg = "map_saved"
      if params[:addresses] && params[:addresses].length > 0
        message_notice = MessageHelper.send_messages(params[:addresses], location.map_name)
        if message_notice.include?("error")
          location.destroy
          redirect to("/error?msg=#{message_notice}")
        else
          msg << "_messages_sent"
        end
      end
      redirect to("/success?msg=#{msg}")
    else
      if location.errors[:map_name]
        redirect to('/error?msg=map_name_in_use')
      else
        redirect to('/error')
      end
    end
  end

  private

    def location_params(location)
      whitelisted = {}
      # Protect against HTML injection by encoding map_name
      whitelisted[:map_name] = URI.encode(normalize_map_name(location[:map_name]))
      # to_f would reject any HTML in the string already, so this is enough
      whitelisted[:latitude] = location[:latitude].to_f
      whitelisted[:longitude] = location[:longitude].to_f

      whitelisted
    end

    def normalize_map_name(map_name)
      map_name.gsub(/\s+/, "-")
    end

    def error_msg(msg)<commit_msg>Protect against random symbols in map name<commit_after>require './config/environment'
require 'sinatra/base'
require 'uri'

class App < Sinatra::Base

  configure do
    if ENV['google_maps_api_key'] # In Production and Travis
      google_maps_api_key = ENV['google_maps_api_key']
    else # Test or Development
      require 'yaml'
      google_maps_api_key = YAML.load_file("config/config.yml")[settings.environment.to_s]["google_maps_api_key"]
    end

    set :google_maps_api_key, google_maps_api_key
  end

  get '/' do
    erb :'index'
  end

  get '/error' do
    @error = error_msg(params[:msg])
    erb :'index'
  end

  get '/success' do
    @success = success_msg(params[:msg])
    erb :'index'
  end

  get '/:map_name' do
    @location = Location.find_by(map_name: params[:map_name])
    if @location
      erb :'show'
    else
      redirect to('/error?msg=map_not_found')
    end
  end

  post '/' do
    location = Location.new(location_params(params[:location]))
    if location.save
      msg = "map_saved"
      if params[:addresses] && params[:addresses].length > 0
        message_notice = MessageHelper.send_messages(params[:addresses], location.map_name)
        if message_notice.include?("error")
          location.destroy
          redirect to("/error?msg=#{message_notice}")
        else
          msg << "_messages_sent"
        end
      end
      redirect to("/success?msg=#{msg}")
    else
      if location.errors[:map_name]
        redirect to('/error?msg=map_name_in_use')
      else
        redirect to('/error')
      end
    end
  end

  private

    def location_params(location)
      whitelisted = {}
      # Protect against HTML injection by encoding map_name
      whitelisted[:map_name] = URI.encode(normalize_map_name(location[:map_name]))
      # to_f would reject any HTML in the string already, so this is enough
      whitelisted[:latitude] = location[:latitude].to_f
      whitelisted[:longitude] = location[:longitude].to_f

      whitelisted
    end

    def normalize_map_name(map_name)
      map_name.gsub(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/, "").gsub(/\s+/, "-")
    end

    def error_msg(msg)
{'fraction_non_alphanumeric': 0.12023668639053255, 'fraction_numerical': 0.00047337278106508875, 'mean_word_length': 2.9275092936802976, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '24878', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1349, 'n_words': 294}