/usr/share/php/Text/CAPTCHA/Driver/Image.php is in php-text-captcha 0.4.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | <?php
/**
* Require Image_Text class for generating the text.
*/
require_once 'Text/CAPTCHA.php';
require_once 'Image/Text.php';
/**
* Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs
*
* Class to create a graphical Turing test
*
* @author Christian Wenz <wenz@php.net>
* @license BSD License
* @todo refine the obfuscation algorithm :-)
* @todo consider removing Image_Text dependency
*/
class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
{
/**
* Image object
*
* @access private
* @var resource
*/
var $_im;
/**
* Image_Text object
*
* @access private
* @var resource
*/
var $_imt;
/**
* Width of CAPTCHA
*
* @access private
* @var int
*/
var $_width;
/**
* Height of CAPTCHA
*
* @access private
* @var int
*/
var $_height;
/**
* CAPTCHA output format
*
* @access private
* @var string
*/
var $_output;
/**
* Further options (here: for Image_Text)
*
* @access private
* @var array
*/
var $_imageOptions = array(
'font_size' => 24,
'font_path' => './',
'font_file' => 'COUR.TTF',
'text_color' => '#000000',
'lines_color' => '#CACACA',
'background_color' => '#555555',
'antialias' => false);
/**
* Whether the immage resource has been created
*
* @access private
* @var boolean
*/
var $_created = false;
/**
* Last error
*
* @access protected
* @var PEAR_Error
*/
var $_error = null;
/**
* init function
*
* Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image
*
* @param array $options CAPTCHA options
*
* @access public
* @return mixed true upon success, PEAR error otherwise
*/
function init($options = array())
{
if (!is_array($options)) {
// Compatibility mode ... in future versions, these two
// lines of code will be used:
// $this->_error = PEAR::raiseError('You need to provide a set of CAPTCHA options!');
// return $this->_error;
$o = array();
$args = func_get_args();
if (isset($args[0])) {
$o['width'] = $args[0];
}
if (isset($args[1])) {
$o['height'] = $args[1];
}
if (isset($args[2]) && $args[2] != null) {
$o['phrase'] = $args[2];
}
if (isset($args[3]) && is_array($args[3])) {
$o['imageOptions'] = $args[3];
}
$options = $o;
}
if (is_array($options)) {
if (isset($options['width']) && is_int($options['width'])) {
$this->_width = $options['width'];
} else {
$this->_width = 200;
}
if (isset($options['height']) && is_int($options['height'])) {
$this->_height = $options['height'];
} else {
$this->_height = 80;
}
if (!isset($options['phrase']) || empty($options['phrase'])) {
$phraseoptions = (isset($options['phraseOptions']) && is_array($options['phraseOptions'])) ? $options['phraseOptions'] : array();
$this->_createPhrase($phraseoptions);
} else {
$this->_phrase = $options['phrase'];
}
if (!isset($options['output']) || empty($options['output'])) {
$this->_output = 'resource';
} else {
$this->_output = $options['output'];
}
if (isset($options['imageOptions']) && is_array($options['imageOptions']) && count($options['imageOptions']) > 0) {
$this->_imageOptions = array_merge($this->_imageOptions, $options['imageOptions']);
}
return true;
}
}
/**
* Create random CAPTCHA phrase, Image edition (with size check)
*
* This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller
*
* @param array $options Optionally supply advanced options for the phrase creation
*
* @access private
* @return void
*/
function _createPhrase($options = array())
{
$len = intval(min(8, $this->_width / 25));
if (!is_array($options) || count($options) === 0) {
$this->_phrase = Text_Password::create($len);
} else {
if (count($options) === 1) {
$this->_phrase = Text_Password::create($len, $options[0]);
} else {
$this->_phrase = Text_Password::create($len, $options[0], $options[1]);
}
}
$this->_created = false;
}
/**
* Create CAPTCHA image
*
* This method creates a CAPTCHA image
*
* @access private
* @return void PEAR_Error on error
*/
function _createCAPTCHA()
{
if ($this->_error) {
return $this->_error;
}
if ($this->_created) {
return;
}
$options['canvas'] = array(
'width' => $this->_width,
'height' => $this->_height
);
$options['width'] = $this->_width - 20;
$options['height'] = $this->_height - 20;
$options['cx'] = ceil(($this->_width) / 2 + 10);
$options['cy'] = ceil(($this->_height) / 2 + 10);
$options['angle'] = rand(0, 30) - 15;
$options['font_size'] = $this->_imageOptions['font_size'];
$options['font_path'] = $this->_imageOptions['font_path'];
$options['font_file'] = $this->_imageOptions['font_file'];
$options['color'] = array($this->_imageOptions['text_color']);
$options['background_color'] = $this->_imageOptions['background_color'];
$options['max_lines'] = 1;
$options['mode'] = 'auto';
do {
$this->_imt = new Image_Text(
$this->_phrase,
$options
);
if (PEAR::isError($e = $this->_imt->init())) {
$this->_error = PEAR::raiseError(
sprintf('Error initializing Image_Text (%s)', $e->getMessage()));
return $this->_error;
} else {
$this->_created = true;
}
$result = $this->_imt->measurize();
} while ($result === false && --$options['font_size'] > 0);
if ($result === false) {
$this->_error = PEAR::raiseError('The text provided does not fit in the image dimensions');
return $this->_error;
}
$this->_imt->render();
$this->_im =& $this->_imt->getImg();
if (isset($this->_imageOptions['antialias']) && $this->_imageOptions['antialias'] === true && function_exists('imageantialias')) {
imageantialias($this->_im, true);
}
$colors = $this->_imt->_convertString2RGB($this->_imageOptions['lines_color']);
$lines_color = imagecolorallocate($this->_im, $colors['r'], $colors['g'], $colors['b']);
//some obfuscation
for ($i = 0; $i < 3; $i++) {
$x1 = rand(0, $this->_width - 1);
$y1 = rand(0, round($this->_height / 10, 0));
$x2 = rand(0, round($this->_width / 10, 0));
$y2 = rand(0, $this->_height - 1);
imageline($this->_im, $x1, $y1, $x2, $y2, $lines_color);
$x1 = rand(0, $this->_width - 1);
$y1 = $this->_height - rand(1, round($this->_height / 10, 0));
$x2 = $this->_width - rand(1, round($this->_width / 10, 0));
$y2 = rand(0, $this->_height - 1);
imageline($this->_im, $x1, $y1, $x2, $y2, $lines_color);
$cx = rand(0, $this->_width - 50) + 25;
$cy = rand(0, $this->_height - 50) + 25;
$w = rand(1, 24);
imagearc($this->_im, $cx, $cy, $w, $w, 0, 360, $lines_color);
}
}
/**
* Return CAPTCHA as image resource
*
* This method returns the CAPTCHA depending on the output format
*
* @access public
* @return mixed image resource or PEAR error
*/
function getCAPTCHA()
{
$retval = $this->_createCAPTCHA();
if (PEAR::isError($retval)) {
return PEAR::raiseError($retval->getMessage());
}
if ($this->_output == 'gif' && !function_exists('imagegif')) {
$this->_output = 'png';
}
switch ($this->_output) {
case 'png':
return $this->getCAPTCHAAsPNG();
break;
case 'jpg':
case 'jpeg':
return $this->getCAPTCHAAsJPEG();
break;
case 'gif':
return $this->getCAPTCHAAsGIF();
break;
case 'resource':
default:
return $this->_im;
}
}
/**
* Return CAPTCHA as PNG
*
* This method returns the CAPTCHA as PNG
*
* @access public
* @return mixed image contents or PEAR error
*/
function getCAPTCHAAsPNG()
{
$retval = $this->_createCAPTCHA();
if (PEAR::isError($retval)) {
return PEAR::raiseError($retval->getMessage());
}
if (is_resource($this->_im)) {
ob_start();
imagepng($this->_im);
$data = ob_get_contents();
ob_end_clean();
return $data;
} else {
$this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
return $this->_error;
}
}
/**
* Return CAPTCHA as JPEG
*
* This method returns the CAPTCHA as JPEG
*
* @access public
* @return mixed image contents or PEAR error
*/
function getCAPTCHAAsJPEG()
{
$retval = $this->_createCAPTCHA();
if (PEAR::isError($retval)) {
return PEAR::raiseError($retval->getMessage());
}
if (is_resource($this->_im)) {
ob_start();
imagejpeg($this->_im);
$data = ob_get_contents();
ob_end_clean();
return $data;
} else {
$this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
return $this->_error;
}
}
/**
* Return CAPTCHA as GIF
*
* This method returns the CAPTCHA as GIF
*
* @access public
* @return mixed image contents or PEAR error
*/
function getCAPTCHAAsGIF()
{
$retval = $this->_createCAPTCHA();
if (PEAR::isError($retval)) {
return PEAR::raiseError($retval->getMessage());
}
if (is_resource($this->_im)) {
ob_start();
imagegif($this->_im);
$data = ob_get_contents();
ob_end_clean();
return $data;
} else {
$this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
return $this->_error;
}
}
/**
* __wakeup method (PHP 5 only)
*
* @return void
*/
function __wakeup()
{
$this->_created = false;
}
}
|