PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Captcha/Word.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 443 lines | 180 code | 58 blank | 205 comment | 17 complexity | 6a0983b90998a591916f5a20e0efb3dc MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Captcha
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** @see Zend_Captcha_Base */
  22. require_once 'Zend/Captcha/Base.php';
  23. /**
  24. * Word-based captcha adapter
  25. *
  26. * Generates random word which user should recognise
  27. *
  28. * @category Zend
  29. * @package Zend_Captcha
  30. * @subpackage Adapter
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @version $Id: Word.php 24593 2012-01-05 20:35:02Z matthew $
  34. */
  35. abstract class Zend_Captcha_Word extends Zend_Captcha_Base
  36. {
  37. /**#@+
  38. * @var array Character sets
  39. */
  40. static $V = array( "a", "e", "i", "o", "u", "y" );
  41. static $VN = array( "a", "e", "i", "o", "u", "y", "2", "3", "4", "5", "6", "7", "8", "9" );
  42. static $C = array( "b", "c", "d", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "z" );
  43. 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" );
  44. /**#@-*/
  45. /**
  46. * Random session ID
  47. *
  48. * @var string
  49. */
  50. protected $_id;
  51. /**
  52. * Generated word
  53. *
  54. * @var string
  55. */
  56. protected $_word;
  57. /**
  58. * Session
  59. *
  60. * @var Zend_Session_Namespace
  61. */
  62. protected $_session;
  63. /**
  64. * Class name for sessions
  65. *
  66. * @var string
  67. */
  68. protected $_sessionClass = 'Zend_Session_Namespace';
  69. /**
  70. * Should the numbers be used or only letters
  71. *
  72. * @var boolean
  73. */
  74. protected $_useNumbers = true;
  75. /**
  76. * Should both cases be used or only lowercase
  77. *
  78. * @var boolean
  79. */
  80. // protected $_useCase = false;
  81. /**
  82. * Session lifetime for the captcha data
  83. *
  84. * @var integer
  85. */
  86. protected $_timeout = 300;
  87. /**
  88. * Should generate() keep session or create a new one?
  89. *
  90. * @var boolean
  91. */
  92. protected $_keepSession = false;
  93. /**#@+
  94. * Error codes
  95. */
  96. const MISSING_VALUE = 'missingValue';
  97. const MISSING_ID = 'missingID';
  98. const BAD_CAPTCHA = 'badCaptcha';
  99. /**#@-*/
  100. /**
  101. * Error messages
  102. * @var array
  103. */
  104. protected $_messageTemplates = array(
  105. self::MISSING_VALUE => 'Empty captcha value',
  106. self::MISSING_ID => 'Captcha ID field is missing',
  107. self::BAD_CAPTCHA => 'Captcha value is wrong',
  108. );
  109. /**
  110. * Length of the word to generate
  111. *
  112. * @var integer
  113. */
  114. protected $_wordlen = 8;
  115. /**
  116. * Retrieve session class to utilize
  117. *
  118. * @return string
  119. */
  120. public function getSessionClass ()
  121. {
  122. return $this->_sessionClass;
  123. }
  124. /**
  125. * Set session class for persistence
  126. *
  127. * @param string $_sessionClass
  128. *
  129. * @return Zend_Captcha_Word
  130. */
  131. public function setSessionClass ($_sessionClass)
  132. {
  133. $this->_sessionClass = $_sessionClass;
  134. return $this;
  135. }
  136. /**
  137. * Retrieve word length to use when genrating captcha
  138. *
  139. * @return integer
  140. */
  141. public function getWordlen ()
  142. {
  143. return $this->_wordlen;
  144. }
  145. /**
  146. * Set word length of captcha
  147. *
  148. * @param integer $wordlen
  149. *
  150. * @return Zend_Captcha_Word
  151. */
  152. public function setWordlen ($wordlen)
  153. {
  154. $this->_wordlen = $wordlen;
  155. return $this;
  156. }
  157. /**
  158. * Retrieve captcha ID
  159. *
  160. * @return string
  161. */
  162. public function getId ()
  163. {
  164. if (null === $this->_id) {
  165. $this->_setId($this->_generateRandomId());
  166. }
  167. return $this->_id;
  168. }
  169. /**
  170. * Set captcha identifier
  171. *
  172. * @param string $id
  173. * return Zend_Captcha_Word
  174. */
  175. protected function _setId ($id)
  176. {
  177. $this->_id = $id;
  178. return $this;
  179. }
  180. /**
  181. * Set timeout for session token
  182. *
  183. * @param int $ttl
  184. *
  185. * @return Zend_Captcha_Word
  186. */
  187. public function setTimeout ($ttl)
  188. {
  189. $this->_timeout = (int)$ttl;
  190. return $this;
  191. }
  192. /**
  193. * Get session token timeout
  194. *
  195. * @return int
  196. */
  197. public function getTimeout ()
  198. {
  199. return $this->_timeout;
  200. }
  201. /**
  202. * Sets if session should be preserved on generate()
  203. *
  204. * @param bool $keepSession Should session be kept on generate()?
  205. *
  206. * @return Zend_Captcha_Word
  207. */
  208. public function setKeepSession ($keepSession)
  209. {
  210. $this->_keepSession = $keepSession;
  211. return $this;
  212. }
  213. /**
  214. * Numbers should be included in the pattern?
  215. *
  216. * @return bool
  217. */
  218. public function getUseNumbers ()
  219. {
  220. return $this->_useNumbers;
  221. }
  222. /**
  223. * Set if numbers should be included in the pattern
  224. *
  225. * @param bool $_useNumbers numbers should be included in the pattern?
  226. *
  227. * @return Zend_Captcha_Word
  228. */
  229. public function setUseNumbers ($_useNumbers)
  230. {
  231. $this->_useNumbers = $_useNumbers;
  232. return $this;
  233. }
  234. /**
  235. * Get session object
  236. *
  237. * @return Zend_Session_Namespace
  238. */
  239. public function getSession ()
  240. {
  241. if (!isset( $this->_session ) || ( null === $this->_session )) {
  242. $id = $this->getId();
  243. if (!class_exists($this->_sessionClass)) {
  244. require_once 'Zend/Loader.php';
  245. Zend_Loader::loadClass($this->_sessionClass);
  246. }
  247. $this->_session = new $this->_sessionClass( 'Zend_Form_Captcha_' . $id );
  248. $this->_session->setExpirationHops(1, null, true);
  249. $this->_session->setExpirationSeconds($this->getTimeout());
  250. }
  251. return $this->_session;
  252. }
  253. /**
  254. * Set session namespace object
  255. *
  256. * @param Zend_Session_Namespace $session
  257. *
  258. * @return Zend_Captcha_Word
  259. */
  260. public function setSession (Zend_Session_Namespace $session)
  261. {
  262. $this->_session = $session;
  263. if ($session) {
  264. $this->_keepSession = true;
  265. }
  266. return $this;
  267. }
  268. /**
  269. * Get captcha word
  270. *
  271. * @return string
  272. */
  273. public function getWord ()
  274. {
  275. if (empty( $this->_word )) {
  276. $session = $this->getSession();
  277. $this->_word = $session->word;
  278. }
  279. return $this->_word;
  280. }
  281. /**
  282. * Set captcha word
  283. *
  284. * @param string $word
  285. *
  286. * @return Zend_Captcha_Word
  287. */
  288. protected function _setWord ($word)
  289. {
  290. $session = $this->getSession();
  291. $session->word = $word;
  292. $this->_word = $word;
  293. return $this;
  294. }
  295. /**
  296. * Generate new random word
  297. *
  298. * @return string
  299. */
  300. protected function _generateWord ()
  301. {
  302. $word = '';
  303. $wordLen = $this->getWordLen();
  304. $vowels = $this->_useNumbers ? self::$VN : self::$V;
  305. $consonants = $this->_useNumbers ? self::$CN : self::$C;
  306. for ($i = 0; $i < $wordLen; $i = $i + 2) {
  307. // generate word with mix of vowels and consonants
  308. $consonant = $consonants[array_rand($consonants)];
  309. $vowel = $vowels[array_rand($vowels)];
  310. $word .= $consonant . $vowel;
  311. }
  312. if (strlen($word) > $wordLen) {
  313. $word = substr($word, 0, $wordLen);
  314. }
  315. return $word;
  316. }
  317. /**
  318. * Generate new session ID and new word
  319. *
  320. * @return string session ID
  321. */
  322. public function generate ()
  323. {
  324. if (!$this->_keepSession) {
  325. $this->_session = null;
  326. }
  327. $id = $this->_generateRandomId();
  328. $this->_setId($id);
  329. $word = $this->_generateWord();
  330. $this->_setWord($word);
  331. return $id;
  332. }
  333. protected function _generateRandomId ()
  334. {
  335. return md5(mt_rand(0, 1000) . microtime(true));
  336. }
  337. /**
  338. * Validate the word
  339. *
  340. * @see Zend_Validate_Interface::isValid()
  341. *
  342. * @param mixed $value
  343. *
  344. * @return boolean
  345. */
  346. public function isValid ($value, $context = null)
  347. {
  348. if (!is_array($value) && !is_array($context)) {
  349. $this->_error(self::MISSING_VALUE);
  350. return false;
  351. }
  352. if (!is_array($value) && is_array($context)) {
  353. $value = $context;
  354. }
  355. $name = $this->getName();
  356. if (isset( $value[$name] )) {
  357. $value = $value[$name];
  358. }
  359. if (!isset( $value['input'] )) {
  360. $this->_error(self::MISSING_VALUE);
  361. return false;
  362. }
  363. $input = strtolower($value['input']);
  364. $this->_setValue($input);
  365. if (!isset( $value['id'] )) {
  366. $this->_error(self::MISSING_ID);
  367. return false;
  368. }
  369. $this->_id = $value['id'];
  370. if ($input !== $this->getWord()) {
  371. $this->_error(self::BAD_CAPTCHA);
  372. return false;
  373. }
  374. return true;
  375. }
  376. /**
  377. * Get captcha decorator
  378. *
  379. * @return string
  380. */
  381. public function getDecorator ()
  382. {
  383. return "Captcha_Word";
  384. }
  385. }