Advertisement
Bannip73

captcha_helper

Oct 21st, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.33 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 5.1.6 or newer
  6.  *
  7.  * @package     CodeIgniter
  8.  * @author      EllisLab Dev Team
  9.  * @copyright       Copyright (c) 2008 - 2014, EllisLab, Inc.
  10.  * @copyright       Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11.  * @license     http://codeigniter.com/user_guide/license.html
  12.  * @link        http://codeigniter.com
  13.  * @since       Version 1.0
  14.  * @filesource
  15.  */
  16.  
  17. // ------------------------------------------------------------------------
  18.  
  19. /**
  20.  * CodeIgniter CAPTCHA Helper
  21.  *
  22.  * @package     CodeIgniter
  23.  * @subpackage  Helpers
  24.  * @category    Helpers
  25.  * @author      EllisLab Dev Team
  26.  * @link        http://codeigniter.com/user_guide/helpers/xml_helper.html
  27.  */
  28.  
  29. // ------------------------------------------------------------------------
  30.  
  31. /**
  32.  * Create CAPTCHA
  33.  *
  34.  * @access  public
  35.  * @param   array   array of data for the CAPTCHA
  36.  * @param   string  path to create the image in
  37.  * @param   string  URL to the CAPTCHA image folder
  38.  * @param   string  server path to font
  39.  * @return  string
  40.  */
  41. if ( ! function_exists('create_captcha'))
  42. {
  43.     function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  44.     {
  45.         $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
  46.  
  47.         foreach ($defaults as $key => $val)
  48.         {
  49.             if ( ! is_array($data))
  50.             {
  51.                 if ( ! isset($$key) OR $$key == '')
  52.                 {
  53.                     $$key = $val;
  54.                 }
  55.             }
  56.             else
  57.             {
  58.                 $$key = ( ! isset($data[$key])) ? $val : $data[$key];
  59.             }
  60.         }
  61.  
  62.         if ($img_path == '' OR $img_url == '')
  63.         {
  64.             return FALSE;
  65.         }
  66.  
  67.         if ( ! @is_dir($img_path))
  68.         {
  69.             return FALSE;
  70.         }
  71.  
  72.         if ( ! is_writable($img_path))
  73.         {
  74.             return FALSE;
  75.         }
  76.  
  77.         if ( ! extension_loaded('gd'))
  78.         {
  79.             return FALSE;
  80.         }
  81.  
  82.         // -----------------------------------
  83.         // Remove old images
  84.         // -----------------------------------
  85.  
  86.         list($usec, $sec) = explode(" ", microtime());
  87.         $now = ((float)$usec + (float)$sec);
  88.  
  89.         $current_dir = @opendir($img_path);
  90.  
  91.         while ($filename = @readdir($current_dir))
  92.         {
  93.             if ($filename != "." and $filename != ".." and $filename != "index.html")
  94.             {
  95.                 $name = str_replace(".jpg", "", $filename);
  96.  
  97.                 if (($name + $expiration) < $now)
  98.                 {
  99.                     @unlink($img_path.$filename);
  100.                 }
  101.             }
  102.         }
  103.  
  104.         @closedir($current_dir);
  105.  
  106.         // -----------------------------------
  107.         // Do we have a "word" yet?
  108.         // -----------------------------------
  109.  
  110.         // -----------------------------------
  111.         // Do we have a "word" yet?
  112.         // -----------------------------------
  113.  
  114.         if (empty($word))
  115.         {
  116.             $word = '';
  117.             $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  118.             $pool_length = strlen($pool);
  119.             $rand_max = $pool_length - 1;
  120.  
  121.             // PHP7 or a suitable polyfill
  122.             if (function_exists('random_int'))
  123.             {
  124.                 try
  125.                 {
  126.                     for ($i = 0; $i < $word_length; $i++)
  127.                     {
  128.                         $word .= $pool[random_int(0, $rand_max)];
  129.                     }
  130.                 }
  131.                 catch (Exception $e)
  132.                 {
  133.                     // This means fallback to the next possible
  134.                     // alternative to random_int()
  135.                     $word = '';
  136.                 }
  137.             }
  138.         }
  139.  
  140.         if (empty($word))
  141.         {
  142.             // To avoid numerous get_random_bytes() calls, we'll
  143.             // just try fetching as much bytes as we need at once.
  144.             if (($bytes = _ci_captcha_get_random_bytes($pool_length)) !== FALSE)
  145.             {
  146.                 $byte_index = $word_index = 0;
  147.                 while ($word_index < $word_length)
  148.                 {
  149.                     if (($rand_index = unpack('C', $bytes[$byte_index++])) > $rand_max)
  150.                     {
  151.                         // Was this the last byte we have?
  152.                         // If so, try to fetch more.
  153.                         if ($byte_index === $pool_length)
  154.                         {
  155.                             // No failures should be possible if
  156.                             // the first get_random_bytes() call
  157.                             // didn't return FALSE, but still ...
  158.                             for ($i = 0; $i < 5; $i++)
  159.                             {
  160.                                 if (($bytes = _ci_captcha_get_random_bytes($pool_length)) === FALSE)
  161.                                 {
  162.                                     continue;
  163.                                 }
  164.  
  165.                                 $byte_index = 0;
  166.                                 break;
  167.                             }
  168.  
  169.                             if ($bytes === FALSE)
  170.                             {
  171.                                 // Sadly, this means fallback to mt_rand()
  172.                                 $word = '';
  173.                                 break;
  174.                             }
  175.                         }
  176.  
  177.                         continue;
  178.                     }
  179.  
  180.                     $word .= $pool[$rand_index];
  181.                     $word_index++;
  182.                 }
  183.             }
  184.         }
  185.  
  186.         if (empty($word))
  187.         {
  188.             for ($i = 0; $i < $word_length; $i++)
  189.             {
  190.                 $word .= $pool[mt_rand(0, $rand_max)];
  191.             }
  192.         }
  193.         elseif ( ! is_string($word))
  194.         {
  195.             $word = (string) $word;
  196.         }
  197.  
  198.         // -----------------------------------
  199.         // Determine angle and position
  200.         // -----------------------------------
  201.  
  202.         $length = strlen($word);
  203.         $angle  = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
  204.         $x_axis = rand(6, (360/$length)-16);
  205.         $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
  206.  
  207.         // -----------------------------------
  208.         // Create image
  209.         // -----------------------------------
  210.  
  211.         // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  212.         if (function_exists('imagecreatetruecolor'))
  213.         {
  214.             $im = imagecreatetruecolor($img_width, $img_height);
  215.         }
  216.         else
  217.         {
  218.             $im = imagecreate($img_width, $img_height);
  219.         }
  220.  
  221.         // -----------------------------------
  222.         //  Assign colors
  223.         // -----------------------------------
  224.  
  225.         $bg_color       = imagecolorallocate ($im, 255, 255, 255);
  226.         $border_color   = imagecolorallocate ($im, 153, 102, 102);
  227.         $text_color     = imagecolorallocate ($im, 204, 153, 153);
  228.         $grid_color     = imagecolorallocate($im, 255, 182, 182);
  229.         $shadow_color   = imagecolorallocate($im, 255, 240, 240);
  230.  
  231.         // -----------------------------------
  232.         //  Create the rectangle
  233.         // -----------------------------------
  234.  
  235.         ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
  236.  
  237.         // -----------------------------------
  238.         //  Create the spiral pattern
  239.         // -----------------------------------
  240.  
  241.         $theta      = 1;
  242.         $thetac     = 7;
  243.         $radius     = 16;
  244.         $circles    = 20;
  245.         $points     = 32;
  246.  
  247.         for ($i = 0; $i < ($circles * $points) - 1; $i++)
  248.         {
  249.             $theta = $theta + $thetac;
  250.             $rad = $radius * ($i / $points );
  251.             $x = ($rad * cos($theta)) + $x_axis;
  252.             $y = ($rad * sin($theta)) + $y_axis;
  253.             $theta = $theta + $thetac;
  254.             $rad1 = $radius * (($i + 1) / $points);
  255.             $x1 = ($rad1 * cos($theta)) + $x_axis;
  256.             $y1 = ($rad1 * sin($theta )) + $y_axis;
  257.             imageline($im, $x, $y, $x1, $y1, $grid_color);
  258.             $theta = $theta - $thetac;
  259.         }
  260.  
  261.         // -----------------------------------
  262.         //  Write the text
  263.         // -----------------------------------
  264.  
  265.         $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
  266.  
  267.         if ($use_font == FALSE)
  268.         {
  269.             $font_size = 5;
  270.             $x = rand(0, $img_width/($length/3));
  271.             $y = 0;
  272.         }
  273.         else
  274.         {
  275.             $font_size  = 16;
  276.             $x = rand(0, $img_width/($length/1.5));
  277.             $y = $font_size+2;
  278.         }
  279.  
  280.         for ($i = 0; $i < strlen($word); $i++)
  281.         {
  282.             if ($use_font == FALSE)
  283.             {
  284.                 $y = rand(0 , $img_height/2);
  285.                 imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  286.                 $x += ($font_size*2);
  287.             }
  288.             else
  289.             {
  290.                 $y = rand($img_height/2, $img_height-3);
  291.                 imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  292.                 $x += $font_size;
  293.             }
  294.         }
  295.  
  296.  
  297.         // -----------------------------------
  298.         //  Create the border
  299.         // -----------------------------------
  300.  
  301.         imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
  302.  
  303.         // -----------------------------------
  304.         //  Generate the image
  305.         // -----------------------------------
  306.  
  307.         $img_name = $now.'.jpg';
  308.  
  309.         ImageJPEG($im, $img_path.$img_name);
  310.  
  311.         $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
  312.  
  313.         ImageDestroy($im);
  314.  
  315.         return array('word' => $word, 'time' => $now, 'image' => $img);
  316.     }
  317.  
  318.     function _ci_captcha_get_random_bytes($length)
  319.     {
  320.         if (defined('MCRYPT_DEV_URANDOM'))
  321.         {
  322.             return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
  323.         }
  324.         elseif (function_exists('openssl_random_pseudo_bytes'))
  325.         {
  326.             return openssl_random_pseudo_bytes($length);
  327.         }
  328.  
  329.         return FALSE;
  330.     }
  331. }
  332.  
  333. // ------------------------------------------------------------------------
  334.  
  335. /* End of file captcha_helper.php */
  336. /* Location: ./system/heleprs/captcha_helper.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement