PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/src/phpMyFAQ/Helper/CaptchaHelper.php

http://github.com/thorsten/phpMyFAQ
PHP | 88 lines | 47 code | 8 blank | 33 comment | 2 complexity | 31edf784ad1455049ee92542d6d112c6 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Helper class for the default phpMyFAQ captcha.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public License,
  6. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. * obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * @package phpMyFAQ
  10. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  11. * @copyright 2010-2021 phpMyFAQ Team
  12. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  13. * @link https://www.phpmyfaq.de
  14. * @since 2010-01-11
  15. */
  16. namespace phpMyFAQ\Helper;
  17. use phpMyFAQ\Captcha;
  18. use phpMyFAQ\Configuration;
  19. use phpMyFAQ\Helper;
  20. /**
  21. * Class CaptchaHelper
  22. *
  23. * @package phpMyFAQ\Helper
  24. */
  25. class CaptchaHelper extends Helper
  26. {
  27. private const FORM_ID = 'captcha';
  28. private const FORM_BUTTON = 'captcha-button';
  29. /**
  30. * Constructor.
  31. *
  32. * @param Configuration $config
  33. */
  34. public function __construct(Configuration $config)
  35. {
  36. $this->config = $config;
  37. }
  38. /**
  39. * Renders the captcha check.
  40. *
  41. * @param Captcha $captcha
  42. * @param string $action
  43. * @param string $legend
  44. * @param bool $auth
  45. * @return string
  46. */
  47. public function renderCaptcha(Captcha $captcha, string $action, string $legend, $auth = false): string
  48. {
  49. $html = '';
  50. if (true === $this->config->get('spam.enableCaptchaCode') && is_null($auth)) {
  51. $html .= '<div class="form-group row">';
  52. $html .= sprintf('<label class="col-sm-3 col-form-label">%s</label>', $legend);
  53. $html .= ' <div class="col-sm-4">';
  54. $html .= ' <p class="form-control-static">';
  55. $html .= $captcha->renderCaptchaImage($action);
  56. $html .= ' </p>';
  57. $html .= ' </div>';
  58. $html .= ' <div class="col-sm-5">';
  59. $html .= ' <div class="input-group">';
  60. $html .= sprintf(
  61. '<input type="text" class="form-control" name="%s" id="%s" size="%d" autocomplete="off" required>',
  62. self::FORM_ID,
  63. self::FORM_ID,
  64. $captcha->captchaLength
  65. );
  66. $html .= ' <span class="input-group-btn">';
  67. $html .= sprintf(
  68. '<a class="btn btn-primary" id="%s" data-action="%s">' .
  69. '<i aria-hidden="true" class="fa fa-refresh"></i></a>',
  70. self::FORM_BUTTON,
  71. $action
  72. );
  73. $html .= ' </span>';
  74. $html .= ' </div>';
  75. $html .= ' </div>';
  76. $html .= '</div>';
  77. }
  78. return $html;
  79. }
  80. }