/usr/share/php/Text/CAPTCHA/Driver/Image.php is in php-text-captcha 1.0.2-4.
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 | <?php
/**
* Require Image_Text class for generating the text.
*
* PHP version 5
*
* @category Text
* @package Text_CAPTCHA
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
require_once 'Text/CAPTCHA/Driver/Base.php';
require_once 'Image/Text.php';
/**
* Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs
*
* Class to create a graphical Turing test
*
* @category Text
* @package Text_CAPTCHA
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
* @todo refine the obfuscation algorithm :-)
* @todo consider removing Image_Text dependency
*/
class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA_Driver_Base
{
/**
* Text_Password options.
*
* @var array
*/
private $_textPasswordOptions;
/**
* Width of CAPTCHA
*
* @var int
*/
private $_width;
/**
* Height of CAPTCHA
*
* @var int
*/
private $_height;
/**
* CAPTCHA output format
*
* @var string
*/
private $_output;
/**
* Further options (here: for Image_Text)
*
* @var array
*/
private $_imageOptions = array(
'font_size' => 24,
'font_path' => './',
'font_file' => 'COUR.TTF',
'text_color' => '#000000',
'lines_color' => '#CACACA',
'background_color' => '#555555',
'antialias' => false);
/**
* Init function
*
* Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image
*
* @param array $options CAPTCHA options
*
* @return void
*/
public function initDriver($options = array())
{
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->_textPasswordOptions = $phraseOptions;
} else {
$this->setPhrase($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']
);
}
}
}
/**
* Create CAPTCHA image.
*
* This method creates a CAPTCHA image.
*
* @return void
* @throws Text_CAPTCHA_Exception when image generation with Image_Text produces
* an error
*/
public function createCAPTCHA()
{
$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 {
$imageText = new Image_Text($this->getPhrase(), $options);
$imageText->init();
$result = $imageText->measurize();
} while ($result === false && --$options['font_size'] > 0);
if ($result === false) {
throw new Text_CAPTCHA_Exception(
'The text provided does not fit in the image dimensions'
);
}
$imageText->render();
$image = $imageText->getImg();
if ($this->_imageOptions['antialias'] && function_exists('imageantialias')) {
imageantialias($image, true);
}
$colors = Image_Text::convertString2RGB(
$this->_imageOptions['lines_color']
);
$linesColor = imagecolorallocate(
$image, $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($image, $x1, $y1, $x2, $y2, $linesColor);
$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($image, $x1, $y1, $x2, $y2, $linesColor);
$cx = rand(0, $this->_width - 50) + 25;
$cy = rand(0, $this->_height - 50) + 25;
$w = rand(1, 24);
imagearc($image, $cx, $cy, $w, $w, 0, 360, $linesColor);
}
if ($this->_output == 'gif' && imagetypes() & IMG_GIF) {
$this->setCaptcha($this->_getCAPTCHAAsGIF($image));
} else if (($this->_output == 'jpg' && imagetypes() & IMG_JPG)
|| ($this->_output == 'jpeg' && imagetypes() & IMG_JPEG)
) {
$this->setCaptcha($this->_getCAPTCHAAsJPEG($image));
} else if ($this->_output == 'png' && imagetypes() & IMG_PNG) {
$this->setCaptcha($this->_getCAPTCHAAsPNG($image));
} else if ($this->_output == 'resource') {
$this->setCaptcha($image);
} else {
throw new Text_CAPTCHA_Exception(
"Unknown or unsupported output type specified"
);
}
imagedestroy($image);
}
/**
* Return CAPTCHA as PNG.
*
* This method returns the CAPTCHA as PNG.
*
* @param resource $image generated image
*
* @return string image contents
*/
private function _getCAPTCHAAsPNG($image)
{
ob_start();
imagepng($image);
$data = ob_get_contents();
ob_end_clean();
return $data;
}
/**
* Return CAPTCHA as JPEG.
*
* This method returns the CAPTCHA as JPEG.
*
* @param resource $image generated image
*
* @return string image contents
*/
private function _getCAPTCHAAsJPEG($image)
{
ob_start();
imagejpeg($image);
$data = ob_get_contents();
ob_end_clean();
return $data;
}
/**
* Return CAPTCHA as GIF.
*
* This method returns the CAPTCHA as GIF.
*
* @param resource $image generated image
*
* @return string image contents
*/
private function _getCAPTCHAAsGIF($image)
{
ob_start();
imagegif($image);
$data = ob_get_contents();
ob_end_clean();
return $data;
}
/**
* Create random CAPTCHA phrase, Image edition (with size check).
*
* This method creates a random phrase, maximum 8 characters or width / 25,
* whatever is smaller.
*
* @return void
*/
public function createPhrase()
{
$len = intval(min(8, $this->_width / 25));
$options = $this->_textPasswordOptions;
$textPassword = new Text_Password();
if (!is_array($options) || count($options) === 0) {
$this->setPhrase($textPassword->create($len));
} else {
if (count($options) === 1) {
$this->setPhrase($textPassword->create($len, $options[0]));
} else {
$this->setPhrase(
$textPassword->create($len, $options[0], $options[1])
);
}
}
}
}
|