Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Captcha / AbstractWord.php
blob7c0a942bc0117acfca344daffa1f09fbbd7e7c17
1 <?php
2 /**
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
8 */
10 namespace Zend\Captcha;
12 use Zend\Math\Rand;
13 use Zend\Session\Container;
15 /**
16 * AbstractWord-based captcha adapter
18 * Generates random word which user should recognise
20 abstract class AbstractWord extends AbstractAdapter
22 /**#@+
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");
29 /**#@-*/
31 /**
32 * Random session ID
34 * @var string
36 protected $id;
38 /**
39 * Generated word
41 * @var string
43 protected $word;
45 /**
46 * Session
48 * @var Container
50 protected $session;
52 /**
53 * Class name for sessions
55 * @var string
57 protected $sessionClass = 'Zend\Session\Container';
59 /**
60 * Should the numbers be used or only letters
62 * @var bool
64 protected $useNumbers = true;
66 /**
67 * Should both cases be used or only lowercase
69 * @var bool
71 // protected $useCase = false;
73 /**
74 * Session lifetime for the captcha data
76 * @var int
78 protected $timeout = 300;
80 /**
81 * Should generate() keep session or create a new one?
83 * @var bool
85 protected $keepSession = false;
87 /**#@+
88 * Error codes
90 const MISSING_VALUE = 'missingValue';
91 const MISSING_ID = 'missingID';
92 const BAD_CAPTCHA = 'badCaptcha';
93 /**#@-*/
95 /**
96 * Error messages
97 * @var array
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
108 * @var int
110 protected $wordlen = 8;
113 * Retrieve session class to utilize
115 * @return string
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;
131 return $this;
135 * Retrieve word length to use when generating captcha
137 * @return int
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;
153 return $this;
157 * Retrieve captcha ID
159 * @return string
161 public function getId()
163 if (null === $this->id) {
164 $this->setId($this->generateRandomId());
166 return $this->id;
170 * Set captcha identifier
172 * @param string $id
173 * @return AbstractWord
175 protected function setId($id)
177 $this->id = $id;
178 return $this;
182 * Set timeout for session token
184 * @param int $ttl
185 * @return AbstractWord
187 public function setTimeout($ttl)
189 $this->timeout = (int) $ttl;
190 return $this;
194 * Get session token timeout
196 * @return int
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;
212 return $this;
216 * Numbers should be included in the pattern?
218 * @return bool
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;
234 return $this;
238 * Get session object
240 * @throws Exception\InvalidArgumentException
241 * @return Container
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;
266 if ($session) {
267 $this->keepSession = true;
269 return $this;
273 * Get captcha word
275 * @return string
277 public function getWord()
279 if (empty($this->word)) {
280 $session = $this->getSession();
281 $this->word = $session->word;
283 return $this->word;
287 * Set captcha word
289 * @param string $word
290 * @return AbstractWord
292 protected function setWord($word)
294 $session = $this->getSession();
295 $session->word = $word;
296 $this->word = $word;
297 return $this;
301 * Generate new random word
303 * @return string
305 protected function generateWord()
307 $word = '';
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);
323 return $word;
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();
337 $this->setId($id);
338 $word = $this->generateWord();
339 $this->setWord($word);
340 return $id;
344 * Generate a random identifier
346 * @return string
348 protected function generateRandomId()
350 return md5(Rand::getBytes(32));
354 * Validate the word
356 * @see Zend\Validator\ValidatorInterface::isValid()
357 * @param mixed $value
358 * @param mixed $context
359 * @return bool
361 public function isValid($value, $context = null)
363 if (!is_array($value)) {
364 if (!is_array($context)) {
365 $this->error(self::MISSING_VALUE);
366 return false;
368 $value = $context;
371 $name = $this->getName();
373 if (isset($value[$name])) {
374 $value = $value[$name];
377 if (!isset($value['input'])) {
378 $this->error(self::MISSING_VALUE);
379 return false;
381 $input = strtolower($value['input']);
382 $this->setValue($input);
384 if (!isset($value['id'])) {
385 $this->error(self::MISSING_ID);
386 return false;
389 $this->id = $value['id'];
390 if ($input !== $this->getWord()) {
391 $this->error(self::BAD_CAPTCHA);
392 return false;
395 return true;
399 * Get helper name used to render captcha
401 * @return string
403 public function getHelperName()
405 return 'captcha/word';