3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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 for ($i=0; $i < $wordLen; $i = $i +
2) {
313 // generate word with mix of vowels and consonants
314 $consonant = $consonants[array_rand($consonants)];
315 $vowel = $vowels[array_rand($vowels)];
316 $word .= $consonant . $vowel;
319 if (strlen($word) > $wordLen) {
320 $word = substr($word, 0, $wordLen);
327 * Generate new session ID and new word
329 * @return string session ID
331 public function generate()
333 if (!$this->keepSession
) {
334 $this->session
= null;
336 $id = $this->generateRandomId();
338 $word = $this->generateWord();
339 $this->setWord($word);
344 * Generate a random identifier
348 protected function generateRandomId()
350 return md5(Rand
::getBytes(32));
356 * @see Zend\Validator\ValidatorInterface::isValid()
357 * @param mixed $value
358 * @param mixed $context
361 public function isValid($value, $context = null)
363 if (!is_array($value)) {
364 if (!is_array($context)) {
365 $this->error(self
::MISSING_VALUE
);
371 $name = $this->getName();
373 if (isset($value[$name])) {
374 $value = $value[$name];
377 if (!isset($value['input'])) {
378 $this->error(self
::MISSING_VALUE
);
381 $input = strtolower($value['input']);
382 $this->setValue($input);
384 if (!isset($value['id'])) {
385 $this->error(self
::MISSING_ID
);
389 $this->id
= $value['id'];
390 if ($input !== $this->getWord()) {
391 $this->error(self
::BAD_CAPTCHA
);
399 * Get helper name used to render captcha
403 public function getHelperName()
405 return 'captcha/word';