PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Captcha/Word.php

https://bitbucket.org/baruffaldi/cms-php-bfcms
PHP | 356 lines | 146 code | 35 blank | 175 comment | 10 complexity | 6245bc1bc13a86f5a9683be3ffa64a13 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @version $Id: $
  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. * Error codes
  89. * @const string
  90. */
  91. const MISSING_VALUE = 'missingValue';
  92. const MISSING_ID = 'missingID';
  93. const BAD_CAPTCHA = 'badCaptcha';
  94. /**#@-*/
  95. /**
  96. * Error messages
  97. * @var array
  98. */
  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',
  103. );
  104. /**
  105. * Length of the word to generate
  106. *
  107. * @var integer
  108. */
  109. protected $_wordlen = 8;
  110. /**
  111. * Retrieve session class to utilize
  112. *
  113. * @return string
  114. */
  115. public function getSessionClass()
  116. {
  117. return $this->_sessionClass;
  118. }
  119. /**
  120. * Set session class for persistence
  121. *
  122. * @param string $_sessionClass
  123. * @return Zend_Captcha_Word
  124. */
  125. public function setSessionClass($_sessionClass)
  126. {
  127. $this->_sessionClass = $_sessionClass;
  128. return $this;
  129. }
  130. /**
  131. * Retrieve word length to use when genrating captcha
  132. *
  133. * @return integer
  134. */
  135. public function getWordlen()
  136. {
  137. return $this->_wordlen;
  138. }
  139. /**
  140. * Set word length of captcha
  141. *
  142. * @param integer $wordlen
  143. * @return Zend_Captcha_Word
  144. */
  145. public function setWordlen($wordlen)
  146. {
  147. $this->_wordlen = $wordlen;
  148. return $this;
  149. }
  150. /**
  151. * Retrieve captcha ID
  152. *
  153. * @return string
  154. */
  155. public function getId ()
  156. {
  157. if (null === $this->_id) {
  158. $this->_setId($this->_generateRandomId());
  159. }
  160. return $this->_id;
  161. }
  162. /**
  163. * Set captcha identifier
  164. *
  165. * @param string $id
  166. * return Zend_Captcha_Word
  167. */
  168. protected function _setId ($id)
  169. {
  170. $this->_id = $id;
  171. return $this;
  172. }
  173. /**
  174. * Set timeout for session token
  175. *
  176. * @param int $ttl
  177. * @return Zend_Captcha_Word
  178. */
  179. public function setTimeout($ttl)
  180. {
  181. $this->_timeout = (int) $ttl;
  182. return $this;
  183. }
  184. /**
  185. * Get session token timeout
  186. *
  187. * @return int
  188. */
  189. public function getTimeout()
  190. {
  191. return $this->_timeout;
  192. }
  193. /**
  194. * Get session object
  195. *
  196. * @return Zend_Session_Namespace
  197. */
  198. public function getSession()
  199. {
  200. if (!isset($this->_session) || (null === $this->_session)) {
  201. $id = $this->getId();
  202. $this->_session = new $this->_sessionClass('Zend_Form_Captcha_' . $id);
  203. $this->_session->setExpirationHops(1, null, true);
  204. $this->_session->setExpirationSeconds($this->getTimeout());
  205. }
  206. return $this->_session;
  207. }
  208. /**
  209. * Set session namespace object
  210. *
  211. * @param Zend_Session_Namespace $session
  212. * @return Zend_Captcha_Word
  213. */
  214. public function setSession(Zend_Session_Namespace $session)
  215. {
  216. $this->_session = $session;
  217. return $this;
  218. }
  219. /**
  220. * Get captcha word
  221. *
  222. * @return string
  223. */
  224. public function getWord()
  225. {
  226. if (empty($this->_word)) {
  227. $session = $this->getSession();
  228. $this->_word = $session->word;
  229. }
  230. return $this->_word;
  231. }
  232. /**
  233. * Set captcha word
  234. *
  235. * @param string $word
  236. * @return Zend_Captcha_Word
  237. */
  238. protected function _setWord($word)
  239. {
  240. $session = $this->getSession();
  241. $session->word = $word;
  242. $this->_word = $word;
  243. return $this;
  244. }
  245. /**
  246. * Generate new random word
  247. *
  248. * @return string
  249. */
  250. protected function _generateWord()
  251. {
  252. $word = '';
  253. $wordLen = $this->getWordLen();
  254. $vowels = $this->_useNumbers ? self::$VN : self::$V;
  255. $consonants = $this->_useNumbers ? self::$CN : self::$C;
  256. for ($i=0; $i < $wordLen; $i = $i + 2) {
  257. // generate word with mix of vowels and consonants
  258. $consonant = $consonants[array_rand($consonants)];
  259. $vowel = $vowels[array_rand($vowels)];
  260. $word .= $consonant . $vowel;
  261. }
  262. if (strlen($word) > $wordLen) {
  263. $word = substr($word, 0, $wordLen);
  264. }
  265. return $word;
  266. }
  267. /**
  268. * Generate new session ID and new word
  269. *
  270. * @return string session ID
  271. */
  272. public function generate()
  273. {
  274. $this->_session = null;
  275. $id = $this->_generateRandomId();
  276. $this->_setId($id);
  277. $word = $this->_generateWord();
  278. $this->_setWord($word);
  279. return $id;
  280. }
  281. protected function _generateRandomId()
  282. {
  283. return md5(mt_rand(0, 1000) . microtime(true));
  284. }
  285. /**
  286. * Validate the word
  287. *
  288. * @see Zend_Validate_Interface::isValid()
  289. * @param mixed $value
  290. * @return boolean
  291. */
  292. public function isValid($value, $context = null)
  293. {
  294. $name = $this->getName();
  295. if (!isset($context[$name]['input'])) {
  296. $this->_error(self::MISSING_VALUE);
  297. return false;
  298. }
  299. $value = strtolower($context[$name]['input']);
  300. $this->_setValue($value);
  301. if (!isset($context[$name]['id'])) {
  302. $this->_error(self::MISSING_ID);
  303. return false;
  304. }
  305. $this->_id = $context[$name]['id'];
  306. if ($value != $this->getWord()) {
  307. $this->_error(self::BAD_CAPTCHA);
  308. return false;
  309. }
  310. return true;
  311. }
  312. /**
  313. * Get captcha decorator
  314. *
  315. * @return string
  316. */
  317. public function getDecorator()
  318. {
  319. return "Captcha_Word";
  320. }
  321. }