ReCaptcha.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This is a PHP library that handles calling reCAPTCHA.
  4. * - Documentation and latest version
  5. * http://recaptcha.net/plugins/php/
  6. * - Get a reCAPTCHA API Key
  7. * https://www.google.com/recaptcha/admin/create
  8. * - Discussion group
  9. * http://groups.google.com/group/recaptcha
  10. *
  11. * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
  12. * AUTHORS:
  13. * Mike Crawford
  14. * Ben Maurer
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this software and associated documentation files (the "Software"), to deal
  18. * in the Software without restriction, including without limitation the rights
  19. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20. * copies of the Software, and to permit persons to whom the Software is
  21. * furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  32. * THE SOFTWARE.
  33. */
  34. /**
  35. *
  36. * PHP Pro Bid $Id$ t5ph3lW4L/rvh8eZc2pHTNSEkY3fi1pk9c9K1mixMow=
  37. *
  38. * @link http://www.phpprobid.com
  39. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  40. * @license http://www.phpprobid.com/license Commercial License
  41. *
  42. * @version 7.5
  43. */
  44. /**
  45. * reCAPTCHA 2 form element for PHP Pro Bid V7
  46. *
  47. * customData accepted:
  48. *
  49. * theme: dark|light (default = light)
  50. * type: audio|image (default = image)
  51. * size: compact|normal (default = normal)
  52. *
  53. */
  54. namespace Ppb\Form\Element;
  55. use Cube\Form\Element,
  56. Cube\Controller\Front,
  57. Ppb\Validate;
  58. class ReCaptcha extends Element
  59. {
  60. /**
  61. *
  62. * type of element - override the variable from the parent class
  63. *
  64. * @var string
  65. */
  66. protected $_element = 'ReCaptcha';
  67. /**
  68. *
  69. * only one instance of this widget can be present at one time
  70. *
  71. * @var bool
  72. */
  73. public static $loaded = false;
  74. /**
  75. *
  76. * class constructor
  77. *
  78. * the recaptcha validator will automatically be added to this form element
  79. *
  80. * @param string $name
  81. */
  82. public function __construct($name)
  83. {
  84. parent::__construct($this->_element, $name);
  85. self::$loaded = true;
  86. $this->addValidator(
  87. new Validate\ReCaptcha()
  88. );
  89. }
  90. /**
  91. *
  92. * render the recaptcha form element
  93. *
  94. * @return string
  95. */
  96. public function render()
  97. {
  98. $settings = Front::getInstance()->getBootstrap()->getResource('settings');
  99. $customData[] = "'sitekey' : '" . $settings['recaptcha_public_key'] . "'";
  100. foreach ((array)$this->_customData as $key => $value) {
  101. $customData[] = "'{$key}' : '{$value}'";
  102. }
  103. $callbackFunctionName = 'onloadCallback' . ucfirst($this->_name);
  104. return '
  105. <script type="text/javascript">
  106. var onloadCallback = function() {
  107. grecaptcha.render("' . $this->_name . '", {
  108. ' . implode(', ', $customData) . '
  109. });
  110. };
  111. </script>
  112. <div id="' . $this->_name . '"></div>
  113. <script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
  114. async defer></script>';
  115. }
  116. }