PageRenderTime 52ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Form/Decorator/Captcha/ReCaptcha.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 128 lines | 73 code | 8 blank | 47 comment | 7 complexity | 15020377665bb18e6d12e48059a32983 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @subpackage Decorator
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** @see Zend_Form_Decorator_Abstract */
  22. require_once 'Zend/Form/Decorator/Abstract.php';
  23. /**
  24. * ReCaptcha-based captcha decorator
  25. *
  26. * Adds hidden fields for challenge and response input, and JS for populating
  27. * from known recaptcha IDs
  28. *
  29. * @category Zend
  30. * @package Zend_Form
  31. * @subpackage Element
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Form_Decorator_Captcha_ReCaptcha extends Zend_Form_Decorator_Abstract
  36. {
  37. /**
  38. * Render captcha
  39. *
  40. * @param string $content
  41. * @return string
  42. */
  43. public function render($content)
  44. {
  45. $element = $this->getElement();
  46. if (!$element instanceof Zend_Form_Element_Captcha) {
  47. return $content;
  48. }
  49. $view = $element->getView();
  50. if (null === $view) {
  51. return $content;
  52. }
  53. $id = $element->getId();
  54. $name = $element->getBelongsTo();
  55. $placement = $this->getPlacement();
  56. $separator = $this->getSeparator();
  57. $challengeName = empty($name) ? 'recaptcha_challenge_field' : $name . '[recaptcha_challenge_field]';
  58. $responseName = empty($name) ? 'recaptcha_response_field' : $name . '[recaptcha_response_field]';
  59. $challengeId = $id . '-challenge';
  60. $responseId = $id . '-response';
  61. $captcha = $element->getCaptcha();
  62. $markup = $captcha->render($view, $element);
  63. // Create hidden fields for holding the final recaptcha values
  64. // Placing "id" in "attribs" to ensure it is not overwritten with the name
  65. $hidden = $view->formHidden(array(
  66. 'name' => $challengeName,
  67. 'attribs' => array('id' => $challengeId),
  68. ));
  69. $hidden .= $view->formHidden(array(
  70. 'name' => $responseName,
  71. 'attribs' => array('id' => $responseId),
  72. ));
  73. // Create a window.onload event so that we can bind to the form.
  74. // Once bound, add an onsubmit event that will replace the hidden field
  75. // values with those produced by ReCaptcha
  76. // zendBindEvent mediates between Mozilla's addEventListener and
  77. // IE's sole support for addEvent.
  78. $js =<<<EOJ
  79. <script type="text/javascript" language="JavaScript">
  80. function windowOnLoad(fn) {
  81. var old = window.onload;
  82. window.onload = function() {
  83. if (old) {
  84. old();
  85. }
  86. fn();
  87. };
  88. }
  89. function zendBindEvent(el, eventName, eventHandler) {
  90. if (el.addEventListener){
  91. el.addEventListener(eventName, eventHandler, false);
  92. } else if (el.attachEvent){
  93. el.attachEvent('on'+eventName, eventHandler);
  94. }
  95. }
  96. windowOnLoad(function(){
  97. zendBindEvent(
  98. document.getElementById("$challengeId").form,
  99. 'submit',
  100. function(e) {
  101. document.getElementById("$challengeId").value = document.getElementById("recaptcha_challenge_field").value;
  102. document.getElementById("$responseId").value = document.getElementById("recaptcha_response_field").value;
  103. }
  104. );
  105. });
  106. </script>
  107. EOJ;
  108. // Always place the hidden fields before the captcha markup, and follow
  109. // with the JS from above
  110. switch ($placement) {
  111. case 'PREPEND':
  112. $content = $hidden . $markup . $js . $separator . $content;
  113. break;
  114. case 'APPEND':
  115. default:
  116. $content = $content . $separator . $hidden . $markup . $js;
  117. }
  118. return $content;
  119. }
  120. }