PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Captcha/Word.php

https://github.com/grjones/qframe
PHP | 396 lines | 171 code | 40 blank | 185 comment | 17 complexity | 33217176d29d8e75d2fa5dc55a271040 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-2010 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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
  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. * @return Zend_Captcha_Word
  129. */
  130. public function setSessionClass($_sessionClass)
  131. {
  132. $this->_sessionClass = $_sessionClass;
  133. return $this;
  134. }
  135. /**
  136. * Retrieve word length to use when genrating captcha
  137. *
  138. * @return integer
  139. */
  140. public function getWordlen()
  141. {
  142. return $this->_wordlen;
  143. }
  144. /**
  145. * Set word length of captcha
  146. *
  147. * @param integer $wordlen
  148. * @return Zend_Captcha_Word
  149. */
  150. public function setWordlen($wordlen)
  151. {
  152. $this->_wordlen = $wordlen;
  153. return $this;
  154. }
  155. /**
  156. * Retrieve captcha ID
  157. *
  158. * @return string
  159. */
  160. public function getId ()
  161. {
  162. if (null === $this->_id) {
  163. $this->_setId($this->_generateRandomId());
  164. }
  165. return $this->_id;
  166. }
  167. /**
  168. * Set captcha identifier
  169. *
  170. * @param string $id
  171. * return Zend_Captcha_Word
  172. */
  173. protected function _setId ($id)
  174. {
  175. $this->_id = $id;
  176. return $this;
  177. }
  178. /**
  179. * Set timeout for session token
  180. *
  181. * @param int $ttl
  182. * @return Zend_Captcha_Word
  183. */
  184. public function setTimeout($ttl)
  185. {
  186. $this->_timeout = (int) $ttl;
  187. return $this;
  188. }
  189. /**
  190. * Get session token timeout
  191. *
  192. * @return int
  193. */
  194. public function getTimeout()
  195. {
  196. return $this->_timeout;
  197. }
  198. /**
  199. * Sets if session should be preserved on generate()
  200. *
  201. * @param $keepSession Should session be kept on generate()?
  202. * @return Zend_Captcha_Word
  203. */
  204. public function setKeepSession($keepSession)
  205. {
  206. $this->_keepSession = $keepSession;
  207. return $this;
  208. }
  209. /**
  210. * Get session object
  211. *
  212. * @return Zend_Session_Namespace
  213. */
  214. public function getSession()
  215. {
  216. if (!isset($this->_session) || (null === $this->_session)) {
  217. $id = $this->getId();
  218. if (!class_exists($this->_sessionClass)) {
  219. require_once 'Zend/Loader.php';
  220. Zend_Loader::loadClass($this->_sessionClass);
  221. }
  222. $this->_session = new $this->_sessionClass('Zend_Form_Captcha_' . $id);
  223. $this->_session->setExpirationHops(1, null, true);
  224. $this->_session->setExpirationSeconds($this->getTimeout());
  225. }
  226. return $this->_session;
  227. }
  228. /**
  229. * Set session namespace object
  230. *
  231. * @param Zend_Session_Namespace $session
  232. * @return Zend_Captcha_Word
  233. */
  234. public function setSession(Zend_Session_Namespace $session)
  235. {
  236. $this->_session = $session;
  237. if($session) {
  238. $this->_keepSession = true;
  239. }
  240. return $this;
  241. }
  242. /**
  243. * Get captcha word
  244. *
  245. * @return string
  246. */
  247. public function getWord()
  248. {
  249. if (empty($this->_word)) {
  250. $session = $this->getSession();
  251. $this->_word = $session->word;
  252. }
  253. return $this->_word;
  254. }
  255. /**
  256. * Set captcha word
  257. *
  258. * @param string $word
  259. * @return Zend_Captcha_Word
  260. */
  261. protected function _setWord($word)
  262. {
  263. $session = $this->getSession();
  264. $session->word = $word;
  265. $this->_word = $word;
  266. return $this;
  267. }
  268. /**
  269. * Generate new random word
  270. *
  271. * @return string
  272. */
  273. protected function _generateWord()
  274. {
  275. $word = '';
  276. $wordLen = $this->getWordLen();
  277. $vowels = $this->_useNumbers ? self::$VN : self::$V;
  278. $consonants = $this->_useNumbers ? self::$CN : self::$C;
  279. for ($i=0; $i < $wordLen; $i = $i + 2) {
  280. // generate word with mix of vowels and consonants
  281. $consonant = $consonants[array_rand($consonants)];
  282. $vowel = $vowels[array_rand($vowels)];
  283. $word .= $consonant . $vowel;
  284. }
  285. if (strlen($word) > $wordLen) {
  286. $word = substr($word, 0, $wordLen);
  287. }
  288. return $word;
  289. }
  290. /**
  291. * Generate new session ID and new word
  292. *
  293. * @return string session ID
  294. */
  295. public function generate()
  296. {
  297. if(!$this->_keepSession) {
  298. $this->_session = null;
  299. }
  300. $id = $this->_generateRandomId();
  301. $this->_setId($id);
  302. $word = $this->_generateWord();
  303. $this->_setWord($word);
  304. return $id;
  305. }
  306. protected function _generateRandomId()
  307. {
  308. return md5(mt_rand(0, 1000) . microtime(true));
  309. }
  310. /**
  311. * Validate the word
  312. *
  313. * @see Zend_Validate_Interface::isValid()
  314. * @param mixed $value
  315. * @return boolean
  316. */
  317. public function isValid($value, $context = null)
  318. {
  319. if (!is_array($value) && !is_array($context)) {
  320. $this->_error(self::MISSING_VALUE);
  321. return false;
  322. }
  323. if (!is_array($value) && is_array($context)) {
  324. $value = $context;
  325. }
  326. $name = $this->getName();
  327. if (isset($value[$name])) {
  328. $value = $value[$name];
  329. }
  330. if (!isset($value['input'])) {
  331. $this->_error(self::MISSING_VALUE);
  332. return false;
  333. }
  334. $input = strtolower($value['input']);
  335. $this->_setValue($input);
  336. if (!isset($value['id'])) {
  337. $this->_error(self::MISSING_ID);
  338. return false;
  339. }
  340. $this->_id = $value['id'];
  341. if ($input !== $this->getWord()) {
  342. $this->_error(self::BAD_CAPTCHA);
  343. return false;
  344. }
  345. return true;
  346. }
  347. /**
  348. * Get captcha decorator
  349. *
  350. * @return string
  351. */
  352. public function getDecorator()
  353. {
  354. return "Captcha_Word";
  355. }
  356. }