fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Captcha / AbstractWord.php
blobdc45550f212f6e1db12b555fd042862502719fff
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-2015 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 $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);
325 return $word;
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();
339 $this->setId($id);
340 $word = $this->generateWord();
341 $this->setWord($word);
342 return $id;
346 * Generate a random identifier
348 * @return string
350 protected function generateRandomId()
352 return md5(Rand::getBytes(32));
356 * Validate the word
358 * @see Zend\Validator\ValidatorInterface::isValid()
359 * @param mixed $value
360 * @param mixed $context
361 * @return bool
363 public function isValid($value, $context = null)
365 if (!is_array($value)) {
366 if (!is_array($context)) {
367 $this->error(self::MISSING_VALUE);
368 return false;
370 $value = $context;
373 $name = $this->getName();
375 if (isset($value[$name])) {
376 $value = $value[$name];
379 if (!isset($value['input'])) {
380 $this->error(self::MISSING_VALUE);
381 return false;
383 $input = strtolower($value['input']);
384 $this->setValue($input);
386 if (!isset($value['id'])) {
387 $this->error(self::MISSING_ID);
388 return false;
391 $this->id = $value['id'];
392 if ($input !== $this->getWord()) {
393 $this->error(self::BAD_CAPTCHA);
394 return false;
397 return true;
401 * Get helper name used to render captcha
403 * @return string
405 public function getHelperName()
407 return 'captcha/word';