CAPTCHA.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Text_CAPTCHA - creates a CAPTCHA for Turing tests.
  4. * Base class file for using Text_CAPTCHA.
  5. *
  6. * PHP version 5
  7. *
  8. * @category Text
  9. * @package Text_CAPTCHA
  10. * @author Christian Wenz <wenz@php.net>
  11. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  12. * @link http://pear.php.net/package/Text_CAPTCHA
  13. */
  14. /**
  15. * Require Exception class for error handling.
  16. */
  17. //require_once 'Text/CAPTCHA/Exception.php';
  18. /**
  19. * Require Text_Password class for generating the phrase.
  20. */
  21. //require_once 'Text/Password.php';
  22. /**
  23. * Text_CAPTCHA - creates a CAPTCHA for Turing tests.
  24. * Class to create a Turing test for websites by creating an image, ASCII art or
  25. * something else with some (obfuscated) characters.
  26. *
  27. * @category Text
  28. * @package Text_CAPTCHA
  29. * @author Christian Wenz <wenz@php.net>
  30. * @author Michael Cramer <michael@bigmichi1.de>
  31. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  32. * @link http://pear.php.net/package/Text_CAPTCHA
  33. */
  34. class Text_CAPTCHA
  35. {
  36. /**
  37. * driver for Text_CAPTCHA
  38. *
  39. * @var Text_CAPTCHA_Driver_Base
  40. */
  41. private $_driver;
  42. /**
  43. * check if an initial driver init was done.
  44. *
  45. * @var bool
  46. */
  47. private $_driverInitDone = false;
  48. /**
  49. * Constructor for the TEXT_CAPTCHA object with the given driver.
  50. *
  51. * @param Text_CAPTCHA_Driver $driver driver
  52. *
  53. * @throws Text_CAPTCHA_Exception no driver given
  54. */
  55. function __construct($driver)
  56. {
  57. if (is_null($driver)) {
  58. throw new Text_CAPTCHA_Exception("No driver given");
  59. }
  60. $this->_driver = $driver;
  61. $this->_driverInitDone = false;
  62. }
  63. /**
  64. * Create a new Text_CAPTCHA object.
  65. *
  66. * @param string $driver name of driver class to initialize
  67. *
  68. * @return Text_CAPTCHA a newly created Text_CAPTCHA object
  69. * @throws Text_CAPTCHA_Exception when driver could not be loaded
  70. *
  71. */
  72. public static function factory($driver)
  73. {
  74. $driver = basename($driver);
  75. $class = 'Text_CAPTCHA_Driver_' . $driver;
  76. /*$file = str_replace('_', '/', $class) . '.php';
  77. //check if it exists and can be loaded
  78. if (!@fclose(@fopen($file, 'r', true))) {
  79. throw new Text_CAPTCHA_Exception(
  80. 'Driver ' . $driver . ' cannot be loaded.'
  81. );
  82. }
  83. //continue with including the driver
  84. include_once $file;*/
  85. $driver = new $class;
  86. return new Text_CAPTCHA($driver);
  87. }
  88. /**
  89. * Create random CAPTCHA phrase
  90. *
  91. * @param boolean|string $newPhrase new Phrase to use or true to generate a new
  92. * one
  93. *
  94. * @return void
  95. * @throws Text_CAPTCHA_Exception when driver is not initialized
  96. */
  97. public final function generate($newPhrase = false)
  98. {
  99. if (!$this->_driverInitDone) {
  100. throw new Text_CAPTCHA_Exception("Driver not initialized");
  101. }
  102. if ($newPhrase === true || is_null($this->_driver->getPhrase())) {
  103. $this->_driver->createPhrase();
  104. } else if (strlen($newPhrase) > 0) {
  105. $this->_driver->setPhrase($newPhrase);
  106. }
  107. $this->_driver->createCAPTCHA();
  108. }
  109. /**
  110. * Reinitialize the entire Text_CAPTCHA object.
  111. *
  112. * @param array $options Options to pass in.
  113. *
  114. * @return void
  115. */
  116. public final function init($options = array())
  117. {
  118. $this->_driver->resetDriver();
  119. $this->_driver->initDriver($options);
  120. $this->_driverInitDone = true;
  121. $this->generate();
  122. }
  123. /**
  124. * Place holder for the real getCAPTCHA() method used by extended classes to
  125. * return the generated CAPTCHA (as an image resource, as an ASCII text, ...).
  126. *
  127. * @return string|object
  128. */
  129. public final function getCAPTCHA()
  130. {
  131. return $this->_driver->getCAPTCHA();
  132. }
  133. /**
  134. * Return secret CAPTCHA phrase.
  135. *
  136. * @return string secret phrase
  137. */
  138. public final function getPhrase()
  139. {
  140. return $this->_driver->getPhrase();
  141. }
  142. /**
  143. * Place holder for the real getCAPTCHA() method used by extended classes to
  144. * return the generated CAPTCHA (as an image resource, as an ASCII text, ...).
  145. *
  146. * @return string|object
  147. */
  148. public function getCAPTCHAAsJPEG()
  149. {
  150. return $this->_driver->_getCAPTCHAAsJPEG();
  151. }
  152. }