PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Centurion/Captcha/Simple.php

http://github.com/centurion-project/Centurion
PHP | 117 lines | 62 code | 18 blank | 37 comment | 5 complexity | b4ec2f0eb2eb29b7910352854695803d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Centurion
  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. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@centurion-project.org so we can send you a copy immediately.
  12. *
  13. * @category Centurion
  14. * @package Centurion_Captcha
  15. * @copyright Copyright (c) 2008-2011 Octave & Octave (http://www.octaveoctave.com)
  16. * @license http://centurion-project.org/license/new-bsd New BSD License
  17. * @version $Id$
  18. */
  19. /**
  20. * @category Centurion
  21. * @package Centurion_Captcha
  22. * @copyright Copyright (c) 2008-2011 Octave & Octave (http://www.octaveoctave.com)
  23. * @license http://centurion-project.org/license/new-bsd New BSD License
  24. * @author Antoine Roesslinger <ar@octaveoctave.com>
  25. * @todo move it to Centurion_Validate
  26. */
  27. class Centurion_Captcha_Simple extends Zend_Captcha_Word
  28. {
  29. protected $_useNumbers = false;
  30. protected $_pointer;
  31. /**#@+
  32. * Error codes
  33. */
  34. const LABEL = 'label';
  35. const MISSING_VALUE = 'missingValue';
  36. const MISSING_ID = 'missingID';
  37. const BAD_CAPTCHA = 'badCaptcha';
  38. /**#@-*/
  39. /**
  40. * Error messages
  41. * @var array
  42. */
  43. protected $_messageTemplates = array(
  44. self::LABEL => 'Please, fill in the field with the charactere %s of this string',
  45. self::MISSING_VALUE => 'Empty captcha value',
  46. self::MISSING_ID => 'Captcha ID field is missing',
  47. self::BAD_CAPTCHA => 'Captcha value is wrong',
  48. );
  49. public function getPointer()
  50. {
  51. if (empty($this->_pointer)) {
  52. $session = $this->getSession();
  53. $this->_pointer = $session->pointer;
  54. }
  55. return $this->_pointer;
  56. }
  57. /**
  58. * @param $pointer
  59. * @return $this
  60. */
  61. private function _setPointer($pointer)
  62. {
  63. $session = $this->getSession();
  64. $session->pointer = $pointer;
  65. $this->_pointer = $pointer;
  66. return $this;
  67. }
  68. public function render(Zend_View_Interface $view = null, $element = null)
  69. {
  70. $this->_randomPointer();
  71. return $view->translate($this->_createMessage(self::LABEL, null), $this->getPointer())
  72. . ' <b>' . $this->getWord() . '</b>';
  73. }
  74. public function isValid($value, $context = null)
  75. {
  76. if (empty($value['input'])) {
  77. $this->_error(self::MISSING_VALUE);
  78. }
  79. if (!isset($value['id'])) {
  80. $this->_error(self::MISSING_ID);
  81. return false;
  82. }
  83. $this->_id = $value['id'];
  84. $word = $this->getWord();
  85. $pointer = $this->getPointer();
  86. $char = substr($word, $pointer - 1, 1);
  87. if ($char === $value['input']) {
  88. return true;
  89. } else {
  90. $this->_error(self::BAD_CAPTCHA);
  91. return false;
  92. }
  93. }
  94. private function _randomPointer()
  95. {
  96. $wordlen = $this->getWordlen();
  97. $this->_setPointer(mt_rand(1, $wordlen));
  98. }
  99. }