function captcha($profile = 'default') { $captcha = Captcha::findWithIP(); if ($captcha instanceof Captcha) { return $captcha->getAsHtml($profile); } return app('captcha')->getAsHtml($profile); }
public function canPostWithoutCaptcha(PermissionUser $user) { // Check if site requires captchas. if (!site_setting('captchaEnabled')) { return true; } // Check if this user can bypass captchas. if ($user->canPostWithoutCaptcha($this)) { return true; } // Begin to check captchas for last answers. $ip = new IP(); $lastCaptcha = Captcha::select('created_at', 'cracked_at')->where(function ($query) use($ip) { // Find captchas answered by this user. $query->where('client_ip', $ip); // Pull the lifespan of a captcha. // This is the number of minutes between successful entries. $captchaLifespan = (int) site_setting('captchaLifespanTime', 0); if ($captchaLifespan > 0) { $query->whereNotNull('cracked_at'); $query->where('cracked_at', '>=', \Carbon\Carbon::now()->subMinutes($captchaLifespan)); } })->orderBy('cracked_at', 'desc')->first(); $requireCaptcha = !$lastCaptcha instanceof Captcha; if (!$requireCaptcha) { $captchaLifespan = (int) site_setting('captchaLifespanPosts'); if ($captchaLifespan > 0) { $postsWithCaptcha = Post::select('created_at')->where('author_ip', $ip)->where('created_at', '>=', $lastCaptcha->created_at)->count(); $requireCaptcha = $postsWithCaptcha >= $captchaLifespan; } } return !$requireCaptcha; }
/** * Boot the service provider. * * @return null */ public function boot(Router $router) { $this->publishes([__DIR__ . '/config/captcha.php' => config_path('captcha.php')]); $this->mergeConfigFrom(__DIR__ . '/config/captcha.php', 'captcha'); $this->registerValidationRules($this->app['validator']); $this->app->singleton('captcha', function ($app) { return new Captcha(); }); $router->pattern('sha1', '[0-9a-f]{5,40}'); $router->pattern('alphanumeric', '[0-9a-z]{1,32}'); /** * Destroys an existing captcha and returns a new one. * * @return Response A redirect */ $router->get(config('captcha.route') . '/replace', function () { $captcha = Captcha::replace(Request::get('hash', null)); return redirect(config('captcha.route') . '/' . $captcha->profile . '/' . $captcha->getHash() . '.png'); }); /** * Destroys an existing captcha and returns a new one using JSON. * * @return \Intervention\Image\ImageManager */ $router->get(config('captcha.route') . '/replace.json', function () { return Captcha::replace(Request::get('hash', null)); }); /** * Redirects the user to a default profile captcha. * * @return Response A redirect */ $router->get(config('captcha.route'), function () { $captcha = Captcha::findOrCreateCaptcha(); return redirect(config('captcha.route') . '/' . $captcha->getHash() . '.png'); }); /** * Redirects the user to a specific profile captcha. * * @param string $profile * @return Response A redirect */ $router->get(config('captcha.route') . '/{alphanumeric}', function ($profile) { $captcha = Captcha::findOrCreateCaptcha($profile); return redirect(config('captcha.route') . '/' . $profile . '/' . $captcha->getHash() . '.png'); }); /** * Returns a JSON response with a new captcha hash. * * @return \Intervention\Image\ImageManager */ $router->get(config('captcha.route') . '.json', function () { return Captcha::findOrCreateCaptcha(); }); /** * Returns a JSON response with a new captcha hash. * * @param string $profile * @return response Redirect */ $router->get(config('captcha.route') . '/{alphanumeric}.json', function ($profile) { return Captcha::findOrCreateCaptcha($profile); }); /** * Displays a default profile captcha. * * @param string $hash * @return Response Image with headers */ $router->get(config('captcha.route') . '/{sha1}.png', function ($sha1) { $captcha = Captcha::findWithHex($sha1); if ($captcha instanceof Captcha) { return $captcha->getAsResponse(); } return abort(404); }); /** * Displays a specific profile captcha. * * @param string $profile * @param string $config * @return Response Image with headers */ $router->get(config('captcha.route') . '/{alphanumeric}/{sha1}.png', function ($profile, $sha1) { $captcha = Captcha::findWithHex($sha1); if ($captcha instanceof Captcha) { return $captcha->getAsResponse(); } return abort(404); }); }
/** * Retrieves the datum for the captcha sibling. * * @return \App\Captcha|boolean False if no captcha required. */ protected function buildSiblingCaptcha() { $needCaptcha = !App::make('App\\Board')->canPostWithoutCaptcha(App::make('App\\Services\\UserManager')->user); return $needCaptcha ? Captcha::findOrCreateCaptcha() : false; }
public function canPostWithoutCaptcha(PermissionUser $user) { if ($user->canPostWithoutCaptcha($this)) { return true; } $lastCaptcha = Captcha::select('created_at', 'cracked_at')->where('client_ip', inet_pton(Request::ip()))->where('created_at', '>=', \Carbon\Carbon::now()->subHour())->whereNotNull('cracked_at')->orderBy('cracked_at', 'desc')->first(); if ($lastCaptcha instanceof Captcha) { $postsWithCaptcha = Post::select('created_at')->where('author_ip', inet_pton(Request::ip()))->where('created_at', '>=', $lastCaptcha->created_at)->count(); return $postsWithCaptcha <= 10; } return false; }
public function validateCaptcha($attribute, $value, $parameters) { return Captcha::answerCaptcha(Request::input("{$attribute}_hash"), $value); }
public function canPostWithoutCaptcha(PermissionUser $user) { if ($user->canPostWithoutCaptcha($this)) { return true; } $lastCaptcha = Captcha::where('client_ip', inet_pton(Request::ip()))->where('created_at', '>=', \Carbon\Carbon::now()->subHour()->timestamp)->whereNotNull('cracked_at')->first(); if ($lastCaptcha instanceof Captcha && $lastCaptcha->cracked_at) { return true; } return false; }