3 * Text_CAPTCHA - creates a CAPTCHA for Turing tests
5 * Class to create a Turing test for websites by
6 * creating an image, ASCII art or something else
7 * with some (obfuscated) characters
10 * @package Text_CAPTCHA
11 * @author Christian Wenz <wenz@php.net>
12 * @license BSD License
18 * Require PEAR class for error handling.
21 require_once 'PEAR.php';
25 * Require Text_Password class for generating the phrase.
28 require_once 'Text/Password.php';
31 * Text_CAPTCHA - creates a CAPTCHA for Turing tests
33 * Class to create a Turing test for websites by
34 * creating an image, ASCII art or something else
35 * with some (obfuscated) characters
37 * @package Text_CAPTCHA
41 // This is a simple example script
44 if (!function_exists('file_put_contents')) {
45 function file_put_contents($filename, $content) {
46 if (!($file = fopen($filename, 'w'))) {
49 $n = fwrite($file, $content);
51 return $n ? $n : false;
55 // Start PHP session support
60 $msg = 'Please enter the text in the image in the field below!';
62 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
64 if (isset($_POST['phrase']) && is_string($_SESSION['phrase']) && isset($_SESSION['phrase']) &&
65 strlen($_POST['phrase']) > 0 && strlen($_SESSION['phrase']) > 0 &&
66 $_POST['phrase'] == $_SESSION['phrase']) {
69 unset($_SESSION['phrase']);
71 $msg = 'Please try again!';
74 unlink(sha1(session_id()) . '.png');
82 require_once 'Text/CAPTCHA.php';
84 // Set CAPTCHA image options (font must exist!)
85 $imageOptions = array(
88 'font_file' => 'COUR.TTF',
89 'text_color' => '#DDFF99',
90 'lines_color' => '#CCEEDD',
91 'background_color' => '#555555',
95 // Set CAPTCHA options
100 'imageOptions' => $imageOptions
103 // Generate a new Text_CAPTCHA object, Image driver
104 $c = Text_CAPTCHA::factory('Image');
105 $retval = $c->init($options);
106 if (PEAR::isError($retval)) {
107 printf('Error initializing CAPTCHA: %s!',
108 $retval->getMessage());
112 // Get CAPTCHA secret passphrase
113 $_SESSION['phrase'] = $c->getPhrase();
115 // Get CAPTCHA image (as PNG)
116 $png = $c->getCAPTCHA();
117 if (PEAR::isError($png)) {
118 printf('Error generating CAPTCHA: %s!',
122 file_put_contents(sha1(session_id()) . '.png', $png);
124 echo '<form method="post">' .
125 '<img src="' . sha1(session_id()) . '.png?' . time() . '" />' .
126 '<input type="text" name="phrase" />' .
127 '<input type="submit" /></form>';
141 var $_version = '0.4.2';
152 * Create a new Text_CAPTCHA object
154 * @param string $driver name of driver class to initialize
156 * @return mixed a newly created Text_CAPTCHA object, or a PEAR
157 * error object on error
159 * @see PEAR::isError()
161 function &factory($driver)
164 return PEAR::raiseError('No CAPTCHA type specified ... aborting. You must call ::factory() with one parameter, the CAPTCHA type.', true);
166 $driver = basename($driver);
167 include_once "Text/CAPTCHA/Driver/$driver.php";
169 $classname = "Text_CAPTCHA_Driver_$driver";
170 $obj = new $classname;
175 * Create random CAPTCHA phrase
177 * This method creates a random phrase, 8 characters long
179 * @param array $options optionally supply advanced options for the phrase creation
184 function _createPhrase($options = array())
187 if (!is_array($options) || count($options) === 0) {
188 $this->_phrase = Text_Password::create($len);
190 if (count($options) === 1) {
191 $this->_phrase = Text_Password::create($len, $options[0]);
193 $this->_phrase = Text_Password::create($len, $options[0], $options[1]);
199 * Return secret CAPTCHA phrase
201 * This method returns the CAPTCHA phrase
204 * @return phrase secret phrase
208 return $this->_phrase;
212 * Sets secret CAPTCHA phrase
214 * This method sets the CAPTCHA phrase
215 * (use null for a random phrase)
217 * @param string $phrase the (new) phrase
222 function setPhrase($phrase = null)
224 if (!empty($phrase)) {
225 $this->_phrase = $phrase;
227 $this->_createPhrase();
232 * Place holder for the real init() method
233 * used by extended classes to initialize CAPTCHA
240 return PEAR::raiseError('CAPTCHA type not selected', true);
244 * Place holder for the real _createCAPTCHA() method
245 * used by extended classes to generate CAPTCHA from phrase
250 function _createCAPTCHA()
252 return PEAR::raiseError('CAPTCHA type not selected', true);
256 * Place holder for the real getCAPTCHA() method
257 * used by extended classes to return the generated CAPTCHA
258 * (as an image resource, as an ASCII text, ...)
263 function getCAPTCHA()
265 return PEAR::raiseError('CAPTCHA type not selected', true);