XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
image.php
Go to the documentation of this file.
1 <?php
21 include "../../../../mainfile.php";
22 
23 error_reporting(0);
24 $xoopsLogger->activated = false;
25 
26 /*
27 if (empty($_SERVER['HTTP_REFERER']) || !preg_match("/^" . preg_quote(XOOPS_URL, '/') . "/", $_SERVER['HTTP_REFERER'])) {
28  exit();
29 }
30 */
31 
33 {
34  var $config = array();
35  var $code;
36  var $mode = 'gd';
37  var $invalid = false;
38 
39  var $font;
40  var $spacing;
41  var $width;
42  var $height;
43 
45 
46  function __construct()
47  {
48  xoops_load('XoopsCaptcha');
49  $this->captcha_handler = XoopsCaptcha::getInstance();
50  $this->config = $this->captcha_handler->loadConfig("image");
51  }
52 
54  {
55  $this->__construct();
56  }
57 
58  function loadImage()
59  {
60  $this->generateCode();
61  $this->createImage();
62  }
63 
67  function generateCode()
68  {
69  if ($this->invalid) {
70  return false;
71  }
72 
73  if ($this->mode == "bmp") {
74  $this->config["num_chars"] = 4;
75  $this->code = rand( pow(10, $this->config["num_chars"] - 1), intval( str_pad("9", $this->config["num_chars"], "9") ) );
76  } else {
77  $raw_code = md5(uniqid(mt_rand(), 1));
78  if (!empty($this->config["skip_characters"])) {
79  $valid_code = str_replace($this->config["skip_characters"], "", $raw_code);
80  $this->code = substr( $valid_code, 0, $this->config["num_chars"] );
81  } else {
82  $this->code = substr( $raw_code, 0, $this->config["num_chars"] );
83  }
84  if (!$this->config["casesensitive"]) {
85  $this->code = strtoupper( $this->code );
86  }
87  }
88  $this->captcha_handler->setCode($this->code);
89  return true;
90  }
91 
92  function createImage()
93  {
94  if ($this->invalid) {
95  header("Content-type: image/gif");
96  readfile(XOOPS_ROOT_PATH."/images/subject/icon2.gif");
97  return;
98  }
99 
100  if ($this->mode == "bmp") {
101  return $this->createImageBmp();
102  } else {
103  return $this->createImageGd();
104  }
105  }
106 
107  function getList($name, $extension = "")
108  {
109  $items = array();
110  xoops_load('XoopsCache');
111  if ($items = XoopsCache::read("captcha_captcha_{$name}")) {
112  return $items;
113  }
114 
115  require_once XOOPS_ROOT_PATH."/class/xoopslists.php";
116  $file_path = XOOPS_ROOT_PATH . "/class/captcha/image/{$name}";
117  $files = XoopsLists::getFileListAsArray($file_path);
118  foreach( $files as $item ) {
119  if ( empty($extension) || preg_match("/(\.{$extension})$/i",$item) ) {
120  $items[] = $item;
121  }
122  }
123  XoopsCache::write("captcha_captcha_{$name}", $items);
124  return $items;
125  }
126 
131  // --------------------------------------------------------------------------- //
132  // Class : SecurityImage 1.5 //
133  // Author: DuGris aka L. Jen <http://www.dugris.info> //
134  // Email : DuGris@wanadoo.fr //
135  // Licence: GNU //
136  // Project: The XOOPS Project //
137  // --------------------------------------------------------------------------- //
138  function createImageGd()
139  {
140  $this->loadFont();
141  $this->setImageSize();
142 
143  $this->oImage = imagecreatetruecolor($this->width, $this->height);
144  $background = imagecolorallocate($this->oImage, 255, 255, 255);
145  imagefilledrectangle($this->oImage, 0, 0, $this->width, $this->height, $background);
146 
147  switch ($this->config["background_type"]) {
148  default:
149  case 0:
150  $this->drawBars();
151  break;
152 
153  case 1:
154  $this->drawCircles();
155  break;
156 
157  case 2:
158  $this->drawLines();
159  break;
160 
161  case 3:
162  $this->drawRectangles();
163  break;
164 
165  case 4:
166  $this->drawEllipses();
167  break;
168 
169  case 5:
170  $this->drawPolygons();
171  break;
172 
173  case 100:
174  $this->createFromFile();
175  break;
176  }
177  $this->drawBorder();
178  $this->drawCode();
179 
180  header("Content-type: image/jpeg");
181  imagejpeg ($this->oImage);
182  imagedestroy($this->oImage);
183  }
184 
185  function loadFont()
186  {
187  $fonts = $this->getList("fonts", "ttf");
188  $this->font = XOOPS_ROOT_PATH . "/class/captcha/image/fonts/" . $fonts[array_rand($fonts)];
189  }
190 
191  function setImageSize()
192  {
193  $MaxCharWidth = 0;
194  $MaxCharHeight = 0;
195  $oImage = imagecreatetruecolor(100, 100);
196  $text_color = imagecolorallocate($oImage, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
197  $FontSize = $this->config["fontsize_max"];
198  for ($Angle = -30; $Angle <= 30; $Angle++) {
199  for ($i = 65; $i <= 90; $i++) {
200  $CharDetails = imageftbbox($FontSize, $Angle, $this->font, chr($i), array());
201  $_MaxCharWidth = abs($CharDetails[0] + $CharDetails[2]);
202  if ($_MaxCharWidth > $MaxCharWidth ) {
203  $MaxCharWidth = $_MaxCharWidth;
204  }
205  $_MaxCharHeight = abs($CharDetails[1] + $CharDetails[5]);
206  if ($_MaxCharHeight > $MaxCharHeight ) {
207  $MaxCharHeight = $_MaxCharHeight;
208  }
209  }
210  }
211  imagedestroy($oImage);
212 
213  $this->height = $MaxCharHeight + 2;
214  $this->spacing = intval( ($this->config["num_chars"] * $MaxCharWidth) / $this->config["num_chars"] );
215  $this->width = ($this->config["num_chars"] * $MaxCharWidth) + ($this->spacing/2);
216  }
217 
223  function loadBackground()
224  {
225  $RandBackground = null;
226  if ( $backgrounds = $this->getList("backgrounds", "(gif|jpg|png)") ) {
227  $RandBackground = XOOPS_ROOT_PATH . "/class/captcha/image/backgrounds/" . $backgrounds[array_rand($backgrounds)];
228  }
229  return $RandBackground;
230  }
231 
235  function createFromFile()
236  {
237  if ( $RandImage = $this->loadBackground() ) {
238  $ImageType = @getimagesize($RandImage);
239  switch ( @$ImageType[2] ) {
240  case 1:
241  $BackgroundImage = imagecreatefromgif($RandImage);
242  break;
243 
244  case 2:
245  $BackgroundImage = imagecreatefromjpeg($RandImage);
246  break;
247 
248  case 3:
249  $BackgroundImage = imagecreatefrompng($RandImage);
250  break;
251  }
252  }
253  if (!empty($BackgroundImage)) {
254  imagecopyresized($this->oImage, $BackgroundImage, 0, 0, 0, 0, imagesx($this->oImage), imagesy($this->oImage), imagesx($BackgroundImage), imagesy($BackgroundImage));
255  imagedestroy($BackgroundImage);
256  } else {
257  $this->drawBars();
258  }
259  }
260 
264  function drawCode()
265  {
266  for ($i = 0; $i < $this->config["num_chars"] ; $i++) {
267  // select random greyscale colour
268  $text_color = imagecolorallocate($this->oImage, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
269 
270  // write text to image
271  $Angle = mt_rand(10, 30);
272  if (($i % 2)) {
273  $Angle = mt_rand(-10, -30);
274  }
275 
276  // select random font size
277  $FontSize = mt_rand($this->config["fontsize_min"], $this->config["fontsize_max"]);
278 
279  $CharDetails = imageftbbox($FontSize, $Angle, $this->font, $this->code[$i], array());
280  $CharHeight = abs( $CharDetails[1] + $CharDetails[5] );
281 
282  // calculate character starting coordinates
283  $posX = ($this->spacing/2) + ($i * $this->spacing);
284  $posY = 2 + ($this->height / 2) + ($CharHeight / 4);
285 
286  imagefttext($this->oImage, $FontSize, $Angle, $posX, $posY, $text_color, $this->font, $this->code[$i], array());
287  }
288  }
289 
293  function drawBorder()
294  {
295  $rgb = rand(50, 150);
296  $border_color = imagecolorallocate ($this->oImage, $rgb, $rgb, $rgb);
297  imagerectangle($this->oImage, 0, 0, $this->width-1, $this->height-1, $border_color);
298  }
299 
303  function drawCircles()
304  {
305  for($i = 1; $i <= $this->config["background_num"]; $i++) {
306  $randomcolor = imagecolorallocate ($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
307  imagefilledellipse($this->oImage, mt_rand(0, $this->width - 10), mt_rand(0, $this->height - 3), mt_rand(10, 20), mt_rand(20, 30), $randomcolor);
308  }
309  }
310 
314  function drawLines()
315  {
316  for ($i = 0; $i < $this->config["background_num"]; $i++) {
317  $randomcolor = imagecolorallocate($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
318  imageline($this->oImage, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $randomcolor);
319  }
320  }
321 
325  function drawRectangles()
326  {
327  for ($i = 1; $i <= $this->config["background_num"]; $i++) {
328  $randomcolor = imagecolorallocate ($this->oImage , mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
329  imagefilledrectangle($this->oImage, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $randomcolor);
330  }
331  }
332 
336  function drawBars()
337  {
338  for ($i= 0; $i <= $this->height;) {
339  $randomcolor = imagecolorallocate ($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
340  imageline( $this->oImage, 0, $i, $this->width, $i, $randomcolor );
341  $i = $i + 2.5;
342  }
343  for ($i = 0; $i <= $this->width;) {
344  $randomcolor = imagecolorallocate ($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
345  imageline( $this->oImage, $i, 0, $i, $this->height, $randomcolor );
346  $i = $i + 2.5;
347  }
348  }
349 
353  function drawEllipses()
354  {
355  for ($i = 1; $i <= $this->config["background_num"]; $i++){
356  $randomcolor = imagecolorallocate ($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
357  imageellipse($this->oImage, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $randomcolor);
358  }
359  }
360 
364  function drawPolygons()
365  {
366  for ($i = 1; $i <= $this->config["background_num"]; $i++){
367  $randomcolor = imagecolorallocate ($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
368  $coords = array();
369  for ($j = 1; $j <= $this->config["polygon_point"]; $j++) {
370  $coords[] = mt_rand(0, $this->width);
371  $coords[] = mt_rand(0, $this->height);
372  }
373  imagefilledpolygon($this->oImage, $coords, $this->config["polygon_point"], $randomcolor);
374  }
375  }
383  function createImageBmp($file = "")
384  {
385  $image = "";
386 
387  if (empty($file)) {
388  header("Content-type: image/bmp");
389  echo $image;
390  } else {
391  return $image;
392  }
393  }
394 }
395 
397 $image_handler->loadImage();
398 
399 ?>