PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Captcha/Word.php

https://bitbucket.org/luizbrandaoj/mini-blog
PHP | 418 lines | 180 code | 42 blank | 196 comment | 17 complexity | a29b8870e6ffbe5f83546d3a54f0daf1 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-2011 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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 bool $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. * Numbers should be included in the pattern?
  211. *
  212. * @return bool
  213. */
  214. public function getUseNumbers()
  215. {
  216. return $this->_useNumbers;
  217. }
  218. /**
  219. * Set if numbers should be included in the pattern
  220. *
  221. * @param bool $_useNumbers numbers should be included in the pattern?
  222. * @return Zend_Captcha_Word
  223. */
  224. public function setUseNumbers($_useNumbers)
  225. {
  226. $this->_useNumbers = $_useNumbers;
  227. return $this;
  228. }
  229. /**
  230. * Get session object
  231. *
  232. * @return Zend_Session_Namespace
  233. */
  234. public function getSession()
  235. {
  236. if (!isset($this->_session) || (null === $this->_session)) {
  237. $id = $this->getId();
  238. if (!class_exists($this->_sessionClass)) {
  239. require_once 'Zend/Loader.php';
  240. Zend_Loader::loadClass($this->_sessionClass);
  241. }
  242. $this->_session = new $this->_sessionClass('Zend_Form_Captcha_' . $id);
  243. $this->_session->setExpirationHops(1, null, true);
  244. $this->_session->setExpirationSeconds($this->getTimeout());
  245. }
  246. return $this->_session;
  247. }
  248. /**
  249. * Set session namespace object
  250. *
  251. * @param Zend_Session_Namespace $session
  252. * @return Zend_Captcha_Word
  253. */
  254. public function setSession(Zend_Session_Namespace $session)
  255. {
  256. $this->_session = $session;
  257. if($session) {
  258. $this->_keepSession = true;
  259. }
  260. return $this;
  261. }
  262. /**
  263. * Get captcha word
  264. *
  265. * @return string
  266. */
  267. public function getWord()
  268. {
  269. if (empty($this->_word)) {
  270. $session = $this->getSession();
  271. $this->_word = $session->word;
  272. }
  273. return $this->_word;
  274. }
  275. /**
  276. * Set captcha word
  277. *
  278. * @param string $word
  279. * @return Zend_Captcha_Word
  280. */
  281. protected function _setWord($word)
  282. {
  283. $session = $this->getSession();
  284. $session->word = $word;
  285. $this->_word = $word;
  286. return $this;
  287. }
  288. /**
  289. * Generate new random word
  290. *
  291. * @return string
  292. */
  293. protected function _generateWord()
  294. {
  295. $word = '';
  296. $wordLen = $this->getWordLen();
  297. $vowels = $this->_useNumbers ? self::$VN : self::$V;
  298. $consonants = $this->_useNumbers ? self::$CN : self::$C;
  299. for ($i=0; $i < $wordLen; $i = $i + 2) {
  300. // generate word with mix of vowels and consonants
  301. $consonant = $consonants[array_rand($consonants)];
  302. $vowel = $vowels[array_rand($vowels)];
  303. $word .= $consonant . $vowel;
  304. }
  305. if (strlen($word) > $wordLen) {
  306. $word = substr($word, 0, $wordLen);
  307. }
  308. return $word;
  309. }
  310. /**
  311. * Generate new session ID and new word
  312. *
  313. * @return string session ID
  314. */
  315. public function generate()
  316. {
  317. if(!$this->_keepSession) {
  318. $this->_session = null;
  319. }
  320. $id = $this->_generateRandomId();
  321. $this->_setId($id);
  322. $word = $this->_generateWord();
  323. $this->_setWord($word);
  324. return $id;
  325. }
  326. protected function _generateRandomId()
  327. {
  328. return md5(mt_rand(0, 1000) . microtime(true));
  329. }
  330. /**
  331. * Validate the word
  332. *
  333. * @see Zend_Validate_Interface::isValid()
  334. * @param mixed $value
  335. * @return boolean
  336. */
  337. public function isValid($value, $context = null)
  338. {
  339. if (!is_array($value) && !is_array($context)) {
  340. $this->_error(self::MISSING_VALUE);
  341. return false;
  342. }
  343. if (!is_array($value) && is_array($context)) {
  344. $value = $context;
  345. }
  346. $name = $this->getName();
  347. if (isset($value[$name])) {
  348. $value = $value[$name];
  349. }
  350. if (!isset($value['input'])) {
  351. $this->_error(self::MISSING_VALUE);
  352. return false;
  353. }
  354. $input = strtolower($value['input']);
  355. $this->_setValue($input);
  356. if (!isset($value['id'])) {
  357. $this->_error(self::MISSING_ID);
  358. return false;
  359. }
  360. $this->_id = $value['id'];
  361. if ($input !== $this->getWord()) {
  362. $this->_error(self::BAD_CAPTCHA);
  363. return false;
  364. }
  365. return true;
  366. }
  367. /**
  368. * Get captcha decorator
  369. *
  370. * @return string
  371. */
  372. public function getDecorator()
  373. {
  374. return "Captcha_Word";
  375. }
  376. }