[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/ -> captcha.php (source)

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


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1