Word.php 3.6 KB

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