function captcha($profile = 'default') { $captcha = Captcha::findWithSession(); 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; } if (!$user->isAccountable()) { return false; } // Check if this user can bypass captchas. if ($user->canPostWithoutCaptcha($this)) { return true; } // Begin to check captchas for last answers. $ip = new IP(); $session_id = Session::getId(); $lastCaptcha = Captcha::select('created_at', 'cracked_at')->where(function ($query) use($ip, $session_id) { // Find captchas answered by this user. $query->where('client_session_id', hex2bin($session_id)); // 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->toText())->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(); }); include realpath(dirname(__FILE__)) . "/helpers/captcha.php"; $router->pattern('sha1', '[0-9a-f]{5,40}'); $router->pattern('alphanumeric', '[0-9a-z]{1,32}'); $router->group(['middleware' => config('captcha.middleware', 'api')], function () use($router) { /** * 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); }); }); }
public function validateCaptcha($attribute, $value, $parameters) { $captcha = Request::input("{$attribute}_hash"); $answered = !!Captcha::answerCaptcha($captcha, $value); return $answered; }
/** * 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; }