diff --git a/Gemfile b/Gemfile index 0d9b581f..c18f7518 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ gem 'sinatra-flash', require: 'sinatra/flash' gem 'sinatra-xsendfile', require: 'sinatra/xsendfile' gem 'puma', require: nil gem 'rubyzip' +gem 'rack-recaptcha', require: 'rack/recaptcha' platform :mri do gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic diff --git a/Gemfile.lock b/Gemfile.lock index 83bd354b..c3b1d652 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -21,6 +21,7 @@ GEM hashie (2.0.5) hiredis (0.4.5) i18n (0.6.4) + json (1.8.0) kgio (2.8.0) magic (0.2.6) ffi (>= 0.6.3) @@ -51,6 +52,8 @@ GEM rack (1.5.2) rack-protection (1.5.0) rack + rack-recaptcha (0.6.6) + json rack-test (0.6.2) rack (>= 1.0) rainbows (4.5.0) @@ -112,6 +115,7 @@ DEPENDENCIES pry pry-debugger puma + rack-recaptcha rack-test rainbows rake diff --git a/app.rb b/app.rb index f3effd05..e41def55 100644 --- a/app.rb +++ b/app.rb @@ -5,6 +5,9 @@ use Rack::Session::Cookie, key: 'neocities', expire_after: 31556926, # one year in seconds secret: $config['session_secret'] +use Rack::Recaptcha, public_key: $config['recaptcha_public_key'], private_key: $config['recaptcha_private_key'] +helpers Rack::Recaptcha::Helpers + get %r{.+} do pass if request.host == '127.0.0.1' subname = request.host.match /[\w-]+/ @@ -50,7 +53,9 @@ post '/create' do dashboard_if_signed_in @site = Site.new username: params[:username], password: params[:password], email: params[:email], new_tags: params[:tags] - if @site.valid? + recaptcha_is_valid = recaptcha_valid? + + if @site.valid? && recaptcha_is_valid base_path = site_base_path @site.username @@ -69,6 +74,8 @@ post '/create' do session[:id] = @site.id redirect '/dashboard' else + @site.errors.add :captcha, 'You must type in the two words correctly! Try again.' if !recaptcha_is_valid + slim :'/new' end end diff --git a/config.yml.template b/config.yml.template index 6728f339..0c59f1b8 100644 --- a/config.yml.template +++ b/config.yml.template @@ -1,3 +1,5 @@ development: database: 'postgres://localhost/neocities' - session_secret: SETSOMETHINGHERE \ No newline at end of file + session_secret: SETSOMETHINGHERE + recaptcha_public_key: ddsfsdfsdf + recaptcha_private_key: fsdgfdsdfd \ No newline at end of file diff --git a/public/css/styles.css b/public/css/styles.css index b896ff3f..ec7e2c2a 100644 --- a/public/css/styles.css +++ b/public/css/styles.css @@ -21,4 +21,4 @@ textarea, input[type="text"], input[type="password"], input[type="datetime"], in select, textarea, input[type="text"], input[type="password"], input[type="datetime"], input[type="datetime-local"], input[type="date"], input[type="month"], input[type="time"], input[type="week"], input[type="number"], input[type="email"], input[type="url"], input[type="search"], input[type="tel"], input[type="color"], .uneditable-input { color: #000000; -} \ No newline at end of file +} diff --git a/views/new.slim b/views/new.slim index e9c9b51f..7355fa9b 100644 --- a/views/new.slim +++ b/views/new.slim @@ -1,4 +1,9 @@ +javascript: + var RecaptchaOptions = { + theme : 'clean' + }; + - if !@site.errors.empty? .row .span8.offset2 @@ -54,10 +59,16 @@ p: input name="tags" type="text" style="width: 400px" placeholder="pokemon, video games, bulbasaur" value="#{params[:tags]}" + .row + .span6 + p Last thing! Enter these two words correctly (with spaces) so we know you're not a robot (don't worry robots, we still love you). + div + == recaptcha_tag :challenge + .row .span6 h3 You're done. Just click the button below! - .row style="margin-top: 30px" + .row style="margin-top: 10px" .span3.offset1 input.btn.btn-success.btn-large type="submit" value="Create Home Page" \ No newline at end of file