3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Captcha
;
13 use Zend\Session\Container
;
16 * AbstractWord-based captcha adapter
18 * Generates random word which user should recognise
20 abstract class AbstractWord
extends AbstractAdapter
23 * @var array Character sets
25 public static $V = array("a", "e", "i", "o", "u", "y");
26 public static $VN = array("a", "e", "i", "o", "u", "y","2","3","4","5","6","7","8","9");
27 public static $C = array("b","c","d","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","z");
28 public static $CN = array("b","c","d","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","z","2","3","4","5","6","7","8","9");
53 * Class name for sessions
57 protected $sessionClass = 'Zend\Session\Container';
60 * Should the numbers be used or only letters
64 protected $useNumbers = true;
67 * Should both cases be used or only lowercase
71 // protected $useCase = false;
74 * Session lifetime for the captcha data
78 protected $timeout = 300;
81 * Should generate() keep session or create a new one?
85 protected $keepSession = false;
90 const MISSING_VALUE
= 'missingValue';
91 const MISSING_ID
= 'missingID';
92 const BAD_CAPTCHA
= 'badCaptcha';
99 protected $messageTemplates = array(
100 self
::MISSING_VALUE
=> 'Empty captcha value',
101 self
::MISSING_ID
=> 'Captcha ID field is missing',
102 self
::BAD_CAPTCHA
=> 'Captcha value is wrong',
106 * Length of the word to generate
110 protected $wordlen = 8;
113 * Retrieve session class to utilize
117 public function getSessionClass()
119 return $this->sessionClass
;
123 * Set session class for persistence
125 * @param string $sessionClass
126 * @return AbstractWord
128 public function setSessionClass($sessionClass)
130 $this->sessionClass
= $sessionClass;
135 * Retrieve word length to use when generating captcha
139 public function getWordlen()
141 return $this->wordlen
;
145 * Set word length of captcha
147 * @param int $wordlen
148 * @return AbstractWord
150 public function setWordlen($wordlen)
152 $this->wordlen
= $wordlen;
157 * Retrieve captcha ID
161 public function getId()
163 if (null === $this->id
) {
164 $this->setId($this->generateRandomId());
170 * Set captcha identifier
173 * @return AbstractWord
175 protected function setId($id)
182 * Set timeout for session token
185 * @return AbstractWord
187 public function setTimeout($ttl)
189 $this->timeout
= (int) $ttl;
194 * Get session token timeout
198 public function getTimeout()
200 return $this->timeout
;
204 * Sets if session should be preserved on generate()
206 * @param bool $keepSession Should session be kept on generate()?
207 * @return AbstractWord
209 public function setKeepSession($keepSession)
211 $this->keepSession
= $keepSession;
216 * Numbers should be included in the pattern?
220 public function getUseNumbers()
222 return $this->useNumbers
;
226 * Set if numbers should be included in the pattern
228 * @param bool $useNumbers numbers should be included in the pattern?
229 * @return AbstractWord
231 public function setUseNumbers($useNumbers)
233 $this->useNumbers
= $useNumbers;
240 * @throws Exception\InvalidArgumentException
243 public function getSession()
245 if (!isset($this->session
) ||
(null === $this->session
)) {
246 $id = $this->getId();
247 if (!class_exists($this->sessionClass
)) {
248 throw new Exception\
InvalidArgumentException("Session class $this->sessionClass not found");
250 $this->session
= new $this->sessionClass('Zend_Form_Captcha_' . $id);
251 $this->session
->setExpirationHops(1, null);
252 $this->session
->setExpirationSeconds($this->getTimeout());
254 return $this->session
;
258 * Set session namespace object
260 * @param Container $session
261 * @return AbstractWord
263 public function setSession(Container
$session)
265 $this->session
= $session;
267 $this->keepSession
= true;
277 public function getWord()
279 if (empty($this->word
)) {
280 $session = $this->getSession();
281 $this->word
= $session->word
;
289 * @param string $word
290 * @return AbstractWord
292 protected function setWord($word)
294 $session = $this->getSession();
295 $session->word
= $word;
301 * Generate new random word
305 protected function generateWord()
308 $wordLen = $this->getWordLen();
309 $vowels = $this->useNumbers ?
static::$VN : static::$V;
310 $consonants = $this->useNumbers ?
static::$CN : static::$C;
312 $totIndexCon = count($consonants) - 1;
313 $totIndexVow = count($vowels) - 1;
314 for ($i=0; $i < $wordLen; $i = $i +
2) {
315 // generate word with mix of vowels and consonants
316 $consonant = $consonants[Rand
::getInteger(0, $totIndexCon, true)];
317 $vowel = $vowels[Rand
::getInteger(0, $totIndexVow, true)];
318 $word .= $consonant . $vowel;
321 if (strlen($word) > $wordLen) {
322 $word = substr($word, 0, $wordLen);
329 * Generate new session ID and new word
331 * @return string session ID
333 public function generate()
335 if (!$this->keepSession
) {
336 $this->session
= null;
338 $id = $this->generateRandomId();
340 $word = $this->generateWord();
341 $this->setWord($word);
346 * Generate a random identifier
350 protected function generateRandomId()
352 return md5(Rand
::getBytes(32));
358 * @see Zend\Validator\ValidatorInterface::isValid()
359 * @param mixed $value
360 * @param mixed $context
363 public function isValid($value, $context = null)
365 if (!is_array($value)) {
366 if (!is_array($context)) {
367 $this->error(self
::MISSING_VALUE
);
373 $name = $this->getName();
375 if (isset($value[$name])) {
376 $value = $value[$name];
379 if (!isset($value['input'])) {
380 $this->error(self
::MISSING_VALUE
);
383 $input = strtolower($value['input']);
384 $this->setValue($input);
386 if (!isset($value['id'])) {
387 $this->error(self
::MISSING_ID
);
391 $this->id
= $value['id'];
392 if ($input !== $this->getWord()) {
393 $this->error(self
::BAD_CAPTCHA
);
401 * Get helper name used to render captcha
405 public function getHelperName()
407 return 'captcha/word';