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