Word.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. if (!class_exists($this->_sessionClass)) {
  203. require_once 'Zend/Loader.php';
  204. Zend_Loader::loadClass($this->_sessionClass);
  205. }
  206. $this->_session = new $this->_sessionClass('Zend_Form_Captcha_' . $id);
  207. $this->_session->setExpirationHops(1, null, true);
  208. $this->_session->setExpirationSeconds($this->getTimeout());
  209. }
  210. return $this->_session;
  211. }
  212. /**
  213. * Set session namespace object
  214. *
  215. * @param Zend_Session_Namespace $session
  216. * @return Zend_Captcha_Word
  217. */
  218. public function setSession(Zend_Session_Namespace $session)
  219. {
  220. $this->_session = $session;
  221. return $this;
  222. }
  223. /**
  224. * Get captcha word
  225. *
  226. * @return string
  227. */
  228. public function getWord()
  229. {
  230. if (empty($this->_word)) {
  231. $session = $this->getSession();
  232. $this->_word = $session->word;
  233. }
  234. return $this->_word;
  235. }
  236. /**
  237. * Set captcha word
  238. *
  239. * @param string $word
  240. * @return Zend_Captcha_Word
  241. */
  242. protected function _setWord($word)
  243. {
  244. $session = $this->getSession();
  245. $session->word = $word;
  246. $this->_word = $word;
  247. return $this;
  248. }
  249. /**
  250. * Generate new random word
  251. *
  252. * @return string
  253. */
  254. protected function _generateWord()
  255. {
  256. $word = '';
  257. $wordLen = $this->getWordLen();
  258. $vowels = $this->_useNumbers ? self::$VN : self::$V;
  259. $consonants = $this->_useNumbers ? self::$CN : self::$C;
  260. for ($i=0; $i < $wordLen; $i = $i + 2) {
  261. // generate word with mix of vowels and consonants
  262. $consonant = $consonants[array_rand($consonants)];
  263. $vowel = $vowels[array_rand($vowels)];
  264. $word .= $consonant . $vowel;
  265. }
  266. if (strlen($word) > $wordLen) {
  267. $word = substr($word, 0, $wordLen);
  268. }
  269. return $word;
  270. }
  271. /**
  272. * Generate new session ID and new word
  273. *
  274. * @return string session ID
  275. */
  276. public function generate()
  277. {
  278. $this->_session = null;
  279. $id = $this->_generateRandomId();
  280. $this->_setId($id);
  281. $word = $this->_generateWord();
  282. $this->_setWord($word);
  283. return $id;
  284. }
  285. protected function _generateRandomId()
  286. {
  287. return md5(mt_rand(0, 1000) . microtime(true));
  288. }
  289. /**
  290. * Validate the word
  291. *
  292. * @see Zend_Validate_Interface::isValid()
  293. * @param mixed $value
  294. * @return boolean
  295. */
  296. public function isValid($value, $context = null)
  297. {
  298. if (!is_array($value) && !is_array($context)) {
  299. $this->_error(self::MISSING_VALUE);
  300. return false;
  301. }
  302. if (!is_array($value) && is_array($context)) {
  303. $value = $context;
  304. }
  305. $name = $this->getName();
  306. if (isset($value[$name])) {
  307. $value = $value[$name];
  308. }
  309. if (!isset($value['input'])) {
  310. $this->_error(self::MISSING_VALUE);
  311. return false;
  312. }
  313. $input = strtolower($value['input']);
  314. $this->_setValue($input);
  315. if (!isset($value['id'])) {
  316. $this->_error(self::MISSING_ID);
  317. return false;
  318. }
  319. $this->_id = $value['id'];
  320. if ($input !== $this->getWord()) {
  321. $this->_error(self::BAD_CAPTCHA);
  322. return false;
  323. }
  324. return true;
  325. }
  326. /**
  327. * Get captcha decorator
  328. *
  329. * @return string
  330. */
  331. public function getDecorator()
  332. {
  333. return "Captcha_Word";
  334. }
  335. }