Word.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Text_CAPTCHA_Driver_Word - Text_CAPTCHA driver word CAPTCHAs
  4. * Class to create a textual Turing test
  5. *
  6. * PHP version 5
  7. *
  8. * @category Text
  9. * @package Text_CAPTCHA
  10. * @author Tobias Schlitt <schlitt@php.net>
  11. * @author Christian Wenz <wenz@php.net>
  12. * @author Michael Cramer <michael@bigmichi1.de>
  13. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  14. * @link http://pear.php.net/package/Text_CAPTCHA
  15. */
  16. /**
  17. * Require Numbers_Words class for generating the text.
  18. *
  19. * @category Text
  20. * @package Text_CAPTCHA
  21. * @author Tobias Schlitt <schlitt@php.net>
  22. * @author Christian Wenz <wenz@php.net>
  23. * @author Michael Cramer <michael@bigmichi1.de>
  24. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  25. * @link http://pear.php.net/package/Text_CAPTCHA
  26. */
  27. class Text_CAPTCHA_Driver_Word extends Text_CAPTCHA_Driver_Base
  28. {
  29. /**
  30. * Phrase length.
  31. * This variable holds the length of the Word.
  32. *
  33. * @var integer
  34. */
  35. private $_length;
  36. /**
  37. * Numbers_Words mode.
  38. * This variable holds the mode for Numbers_Words.
  39. *
  40. * @var String
  41. */
  42. private $_mode;
  43. /**
  44. * Locale
  45. * This variable holds the locale for Numbers_Words
  46. *
  47. * @var string
  48. */
  49. private $_locale;
  50. /**
  51. * Initializes the new Text_CAPTCHA_Driver_Word object.
  52. *
  53. * @param array $options CAPTCHA options with these keys:<br>
  54. * phrase The "secret word" of the CAPTCHA<br>
  55. * length The number of characters in the phrase<br>
  56. * locale The locale for Numbers_Words<br>
  57. * mode The mode for Numbers_Words
  58. *
  59. * @return void
  60. */
  61. public function initDriver($options = array())
  62. {
  63. if (isset($options['length']) && is_int($options['length'])) {
  64. $this->_length = $options['length'];
  65. } else {
  66. $this->_length = 4;
  67. }
  68. if (isset($options['phrase']) && !empty($options['phrase'])) {
  69. $this->setPhrase((string)$options['phrase']);
  70. } else {
  71. $this->createPhrase();
  72. }
  73. if (isset($options['mode']) && !empty($options['mode'])) {
  74. $this->_mode = $options['mode'];
  75. } else {
  76. $this->_mode = 'single';
  77. }
  78. if (isset($options['locale']) && !empty($options['locale'])) {
  79. $this->_locale = $options['locale'];
  80. } else {
  81. $this->_locale = 'en_US';
  82. }
  83. }
  84. /**
  85. * Create random CAPTCHA phrase, "Word edition" (numbers only).
  86. * This method creates a random phrase
  87. *
  88. * @return void
  89. */
  90. public function createPhrase()
  91. {
  92. $phrase = new Text_Password();
  93. $this->setPhrase(
  94. $phrase->create(
  95. $this->_length, 'unpronounceable', 'numeric'
  96. )
  97. );
  98. }
  99. /**
  100. * Place holder for the real _createCAPTCHA() method
  101. * used by extended classes to generate CAPTCHA from phrase
  102. *
  103. * @return void
  104. */
  105. public function createCAPTCHA()
  106. {
  107. $res = '';
  108. $numberWords = new Numbers_Words();
  109. $phrase = $this->getPhrase();
  110. if ($this->_mode == 'single') {
  111. $phraseArr = str_split($phrase);
  112. for ($i = 0; $i < strlen($phrase); $i++) {
  113. $res .= ' ' . $numberWords->toWords($phraseArr[$i], $this->_locale);
  114. }
  115. } else {
  116. $res = $numberWords->toWords($phrase, $this->_locale);
  117. }
  118. $this->setCaptcha($res);
  119. }
  120. }