diff --git a/.gitignore b/.gitignore index ab230176..f365e9cd 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ files/map.txt .sass-cache/* files/sslsites.zip .tm_properties +black_box.rb diff --git a/app.rb b/app.rb index cabf725b..7ce31199 100644 --- a/app.rb +++ b/app.rb @@ -249,9 +249,19 @@ get '/?' do @sites_count = SimpleCache.get :sites_count end + @blackbox_question = BlackBox.generate + @question_first_number, @question_last_number = generate_question + erb :index, layout: false end +def generate_question + question_first_number = rand 5 + question_last_number = rand 5 + session[:question_answer] = (question_first_number + question_last_number).to_s + [question_first_number, question_last_number] +end + get '/plan/?' do @title = 'Supporter' erb :'plan/index' @@ -487,6 +497,15 @@ get '/new' do erb :'new' end +post '/create_validate_all' do + content_type :json + fields = params.select {|p| p.match /username|password|email|new_tags_string/} + + site = Site.new fields + return [].to_json if site.valid? + site.errors.collect {|e| [e.first, e.last.first]}.to_json +end + post '/create_validate' do content_type :json @@ -519,7 +538,20 @@ post '/create' do ip: request.ip ) - if !@site.valid? + black_box_answered = BlackBox.valid? params[:blackbox_answer], request.ip + question_answered_correctly = params[:question_answer] == session[:question_answer] + + if !question_answered_correctly + question_first_number, question_last_number = generate_question + return { + result: 'bad_answer', + question_first_number: question_first_number, + question_last_number: question_last_number + }.to_json + end + + if !black_box_answered || !@site.valid? + flash[:error] = 'There was an unknown error, please try again.' return {result: 'error'}.to_json end diff --git a/environment.rb b/environment.rb index e5fb61d4..ea977c0d 100644 --- a/environment.rb +++ b/environment.rb @@ -126,4 +126,12 @@ if ENV['RACK_ENV'] != 'development' Sass::Plugin.options[:style] = :compressed Sass::Plugin.options[:never_update] = true Sass::Plugin.options[:full_exception] = false +end + +unless ENV['RACK_ENV'] == 'test' + if File.exist?('./black_box.rb') + require './black_box.rb' + else + puts "WARNING: Black box was not loaded!" + end end \ No newline at end of file diff --git a/models/site.rb b/models/site.rb index c97de7df..a9ad1a31 100644 --- a/models/site.rb +++ b/models/site.rb @@ -643,7 +643,7 @@ class Site < Sequel::Model super if !self.class.valid_username?(values[:username]) - errors.add :username, 'A valid user/site name is required.' + errors.add :username, 'Usernames can only contain letters, numbers, underscores and hyphens.' end if new? && !values[:username].nil? && !values[:username].empty? diff --git a/views/browse.erb b/views/browse.erb index 7602fea6..69081d5f 100644 --- a/views/browse.erb +++ b/views/browse.erb @@ -63,7 +63,7 @@
@@ -154,29 +153,37 @@ <% else %> -