[ Index ]

PHP Cross Reference of MyBB 1.8.37

title

Body

[close]

/ -> captcha.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  define("IN_MYBB", 1);
  12  define("NO_ONLINE", 1);
  13  define('THIS_SCRIPT', 'captcha.php');
  14  define("ALLOWABLE_PAGE", 1);
  15  
  16  require_once  "./global.php";
  17  
  18  $img_width = 200;
  19  $img_height = 60;
  20  
  21  // The following settings are only used for TTF fonts
  22  $min_size = 20;
  23  $max_size = 32;
  24  
  25  $min_angle = -30;
  26  $max_angle = 30;
  27  
  28  $mybb->input['imagehash'] = $mybb->get_input('imagehash');
  29  if($mybb->input['imagehash'])
  30  {
  31      $query = $db->simple_select("captcha", "*", "imagehash='".$db->escape_string($mybb->get_input('imagehash'))."' AND used=0", array("limit" => 1));
  32      $regimage = $db->fetch_array($query);
  33      if(!$regimage)
  34      {
  35          exit;
  36      }
  37      // Mark captcha as used
  38      $db->update_query('captcha', array('used' => 1), "imagehash='".$db->escape_string($regimage['imagehash'])."'");
  39      $imagestring = $regimage['imagestring'];
  40  }
  41  else
  42  {
  43      exit;
  44  }
  45  
  46  $ttf_fonts = array();
  47  
  48  // We have support for true-type fonts (FreeType 2)
  49  if(function_exists("imagefttext"))
  50  {
  51      // Get a list of the files in the 'catpcha_fonts' directory
  52      $ttfdir  = @opendir(MYBB_ROOT."inc/captcha_fonts");
  53      if($ttfdir !== false)
  54      {
  55          while(($file = readdir($ttfdir)) !== false)
  56          {
  57              // If this file is a ttf file, add it to the list
  58              if(is_file(MYBB_ROOT."inc/captcha_fonts/".$file) && get_extension($file) == "ttf")
  59              {
  60                  $ttf_fonts[] = MYBB_ROOT."inc/captcha_fonts/".$file;
  61              }
  62          }
  63          closedir($ttfdir);
  64      }
  65  }
  66  
  67  // Have one or more TTF fonts in our array, we can use TTF captha's
  68  if(count($ttf_fonts) > 0)
  69  {
  70      $use_ttf = 1;
  71  }
  72  else
  73  {
  74      $use_ttf = 0;
  75  }
  76  
  77  // Check for GD >= 2, create base image
  78  if(gd_version() >= 2)
  79  {
  80      $im = imagecreatetruecolor($img_width, $img_height);
  81  }
  82  else
  83  {
  84      $im = imagecreate($img_width, $img_height);
  85  }
  86  
  87  // No GD support, die.
  88  if(!$im)
  89  {
  90      die("No GD support.");
  91  }
  92  
  93  // Fill the background with white
  94  $bg_color = imagecolorallocate($im, 255, 255, 255);
  95  imagefill($im, 0, 0, $bg_color);
  96  
  97  // Draw random circles, squares or lines?
  98  $to_draw = rand(0, 2);
  99  if($to_draw == 1)
 100  {
 101      draw_circles($im);
 102  }
 103  else if($to_draw == 2)
 104  {
 105      draw_squares($im);
 106  }
 107  else
 108  {
 109      draw_lines($im);
 110  }
 111  
 112  // Draw dots on the image
 113  draw_dots($im);
 114  
 115  // Write the image string to the image
 116  draw_string($im, $imagestring);
 117  
 118  // Draw a nice border around the image
 119  $border_color = imagecolorallocate($im, 0, 0, 0);
 120  imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
 121  
 122  // Output the image
 123  header("Content-type: image/png");
 124  imagepng($im);
 125  imagedestroy($im);
 126  
 127  /**
 128   * Draws a random number of lines on the image.
 129   *
 130   * @param resource $im The image.
 131   */
 132  function draw_lines(&$im)
 133  {
 134      global $img_width, $img_height;
 135  
 136      for($i = 10; $i < $img_width; $i += 10)
 137      {
 138          $color = imagecolorallocate($im, rand(150, 255), rand(150, 255), rand(150, 255));
 139          imageline($im, $i, 0, $i, $img_height, $color);
 140      }
 141      for($i = 10; $i < $img_height; $i += 10)
 142      {
 143          $color = imagecolorallocate($im, rand(150, 255), rand(150, 255), rand(150, 255));
 144          imageline($im, 0, $i, $img_width, $i, $color);
 145      }
 146  }
 147  
 148  /**
 149   * Draws a random number of circles on the image.
 150   *
 151   * @param resource $im The image.
 152   */
 153  function draw_circles(&$im)
 154  {
 155      global $img_width, $img_height;
 156  
 157      $circles = $img_width*$img_height / 100;
 158      for($i = 0; $i <= $circles; ++$i)
 159      {
 160          $color = imagecolorallocate($im, rand(180, 255), rand(180, 255), rand(180, 255));
 161          $pos_x = rand(1, $img_width);
 162          $pos_y = rand(1, $img_height);
 163          $circ_width = ceil(rand(1, $img_width)/2);
 164          $circ_height = rand(1, $img_height);
 165          imagearc($im, $pos_x, $pos_y, $circ_width, $circ_height, 0, rand(200, 360), $color);
 166      }
 167  }
 168  
 169  /**
 170   * Draws a random number of dots on the image.
 171   *
 172   * @param resource $im The image.
 173   */
 174  function draw_dots(&$im)
 175  {
 176      global $img_width, $img_height;
 177  
 178      $dot_count = $img_width*$img_height/5;
 179      for($i = 0; $i <= $dot_count; ++$i)
 180      {
 181          $color = imagecolorallocate($im, rand(200, 255), rand(200, 255), rand(200, 255));
 182          imagesetpixel($im, rand(0, $img_width), rand(0, $img_height), $color);
 183      }
 184  }
 185  
 186  /**
 187   * Draws a random number of squares on the image.
 188   *
 189   * @param resource $im The image.
 190   */
 191  function draw_squares(&$im)
 192  {
 193      global $img_width, $img_height;
 194  
 195      $square_count = 30;
 196      for($i = 0; $i <= $square_count; ++$i)
 197      {
 198          $color = imagecolorallocate($im, rand(150, 255), rand(150, 255), rand(150, 255));
 199          $pos_x = rand(1, $img_width);
 200          $pos_y = rand(1, $img_height);
 201          $sq_width = $sq_height = rand(10, 20);
 202          $pos_x2 = $pos_x + $sq_height;
 203          $pos_y2 = $pos_y + $sq_width;
 204          imagefilledrectangle($im, $pos_x, $pos_y, $pos_x2, $pos_y2, $color);
 205      }
 206  }
 207  
 208  /**
 209   * Writes text to the image.
 210   *
 211   * @param resource $im The image.
 212   * @param string $string The string to be written
 213   *
 214   * @return bool False if string is empty, true otherwise
 215   */
 216  function draw_string(&$im, $string)
 217  {
 218      global $use_ttf, $min_size, $max_size, $min_angle, $max_angle, $ttf_fonts, $img_height, $img_width;
 219  
 220      if(empty($string))
 221      {
 222          return false;
 223      }
 224  
 225      $spacing = $img_width / my_strlen($string);
 226      $string_length = my_strlen($string);
 227      for($i = 0; $i < $string_length; ++$i)
 228      {
 229          // Using TTF fonts
 230          if($use_ttf)
 231          {
 232              // Select a random font size
 233              $font_size = rand($min_size, $max_size);
 234  
 235              // Select a random font
 236              $font = array_rand($ttf_fonts);
 237              $font = $ttf_fonts[$font];
 238  
 239              // Select a random rotation
 240              $rotation = rand($min_angle, $max_angle);
 241  
 242              // Set the colour
 243              $r = rand(0, 200);
 244              $g = rand(0, 200);
 245              $b = rand(0, 200);
 246              $color = imagecolorallocate($im, $r, $g, $b);
 247  
 248              // Fetch the dimensions of the character being added
 249              $dimensions = imageftbbox($font_size, $rotation, $font, $string[$i], array());
 250              $string_width = $dimensions[2] - $dimensions[0];
 251              $string_height = $dimensions[3] - $dimensions[5];
 252  
 253              // Calculate character offsets
 254              //$pos_x = $pos_x + $string_width + ($string_width/4);
 255              $pos_x = $spacing / 4 + $i * $spacing;
 256              $pos_y = ceil(($img_height-$string_height/2));
 257  
 258              // Draw a shadow
 259              $shadow_x = rand(-3, 3) + $pos_x;
 260              $shadow_y = rand(-3, 3) + $pos_y;
 261              $shadow_color = imagecolorallocate($im, $r+20, $g+20, $b+20);
 262              imagefttext($im, $font_size, $rotation, $shadow_x, $shadow_y, $shadow_color, $font, $string[$i], array());
 263  
 264              // Write the character to the image
 265              imagefttext($im, $font_size, $rotation, $pos_x, $pos_y, $color, $font, $string[$i], array());
 266          }
 267          else
 268          {
 269              // Get width/height of the character
 270              $string_width = imagefontwidth(5);
 271              $string_height = imagefontheight(5);
 272  
 273              // Calculate character offsets
 274              $pos_x = $spacing / 4 + $i * $spacing;
 275              $pos_y = $img_height / 2 - $string_height -10 + rand(-3, 3);
 276  
 277              // Create a temporary image for this character
 278              if(gd_version() >= 2)
 279              {
 280                  $temp_im = imagecreatetruecolor(15, 20);
 281              }
 282              else
 283              {
 284                  $temp_im = imagecreate(15, 20);
 285              }
 286              $bg_color = imagecolorallocate($temp_im, 255, 255, 255);
 287              imagefill($temp_im, 0, 0, $bg_color);
 288              imagecolortransparent($temp_im, $bg_color);
 289  
 290              // Set the colour
 291              $r = rand(0, 200);
 292              $g = rand(0, 200);
 293              $b = rand(0, 200);
 294              $color = imagecolorallocate($temp_im, $r, $g, $b);
 295  
 296              // Draw a shadow
 297              $shadow_x = rand(-1, 1);
 298              $shadow_y = rand(-1, 1);
 299              $shadow_color = imagecolorallocate($temp_im, $r+50, $g+50, $b+50);
 300              imagestring($temp_im, 5, 1+$shadow_x, 1+$shadow_y, $string[$i], $shadow_color);
 301  
 302              imagestring($temp_im, 5, 1, 1, $string[$i], $color);
 303  
 304              // Copy to main image
 305              imagecopyresized($im, $temp_im, $pos_x, $pos_y, 0, 0, 40, 55, 15, 20);
 306              imagedestroy($temp_im);
 307          }
 308      }
 309      return true;
 310  }
 311  


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref