tetlphp /library/captcha.php

Language PHP Lines 168
MD5 Hash f20a26cf65a70e477139c10e302567d1 Estimated Cost $2,181 (why?)
Repository git://github.com/pateketrueke/tetlphp.git View Raw File
  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
<?php

/**
 * Basic captcha library
 */
// TODO: wrap in a class too?
/**
 * Retrieve/set unique identifier
 *
 * @param     mixed  Code length|File
 * @param     string Unique key
 * @staticvar string Charset
 * @return    string
 */
function captcha_id($test = 6, $key = 0) {
  static $chars = 'jklmn-JKLMN#rstuvwx@RSTUVWX+56789=ABCDEFG';


  if ( ! is_num($test)) {
    if ( ! is_file($test)) {
      return FALSE;
    }

    $test = preg_split('/\b|[\s\t]+/', read($test));
    $test = array_filter(array_unique($test));
    $hash = $test[array_rand($test, 1)];
  } else {
    $hash = '';
    $max  = strlen($chars) - 1;

    if ($test > 10) {
      $test = 10;
    }

    while(strlen($hash) < $test) {
      $old = substr($chars, mt_rand(0, $max), 1);

      if ( ! is_false(strpos($hash, $old))) {
        continue;
      }

      $hash .= $old;
    }
  }


  session("--captcha-id$$key", $hash);

  return $hash;
}


/**
 * Generate the captcha image
 *
 * @param     string  Hash
 * @param     integer Width
 * @param     integer Height
 * @param     string  Fontfile
 * @param     boolean Invert color?
 * @staticvar mixed   Function callback
 * @return    string
 */
function captcha_src($hash, $width = 120, $height = 24, $font = '', $invert = FALSE) {
  static $negative = NULL;


  if (is_null($negative)) {
    $negative = function ($num) {
      return $num * -1;
    };
  }


  $length   = strlen($hash);
  $resource = imagecreatetruecolor($width, $height);

  imagesavealpha($resource, TRUE);
  imagealphablending($resource, TRUE);


  $dark  = array(mt_rand(0, 96), mt_rand(0, 96), mt_rand(0, 96));
  $light = array(mt_rand(127, 240), mt_rand(127, 240), mt_rand(127, 240));

  if (is_true($invert)) {
    $bgcolor = imagecolorallocatealpha($resource, 0, 0, 0, 0);
    $light   = array_map($negative, $light);
    $dark    = array_map($negative, $dark);
  } else {
    $bgcolor = imagecolorallocatealpha($resource, 255, 255, 255, 127);
  }

  imagefill($resource, 0, 0, $bgcolor);


  $bg = imagecolorallocatealpha($resource, $light[0], $light[1], $light[2], mt_rand(45, 85));
  $fg = imagecolorallocatealpha($resource, $dark[0], $dark[1], $dark[2], mt_rand(33, 52));


  $left   = substr($hash, 0, ceil($length / 2));
  $right  = substr($hash, strlen($left));
  $factor = floor($width / $length);
  $font   = realpath($font);

  if ( ! is_file($font)) {
    $top = ($width * $height) / 3.3;

    for ($i = 0; $i < $top; $i += 1) {
      imagefilledellipse($resource, mt_rand(0, $width), mt_rand(0, $height), 1, mt_rand(1, 3), $bg);
    }

    for ($i = 0; $i < $length; $i += 1) {
      imagestring($resource, 5, ($factor / 3) + $i * $factor, mt_rand(0, $height - mt_rand(13, 20)), substr($hash, $i, 1), $fg);

      if (mt_rand(0, 75) % 9) {
        $img = imagerotate($resource, is_odd($i) ? - .8 : .7, $bgcolor);
      }
    }
  } else {
    $box = imageftbbox($factor, 0, $font, 'Q');

    $w   = max(abs($box[2]) - abs($box[0]), 1);
    $h   = max(abs($box[7]) - abs($box[6]), 1);

    imagettftext($resource, $factor, mt_rand(-4, 5), $x = mt_rand($w / strlen($left), $w * 1.3), mt_rand($h, $h * 1.33), $fg, $font, $left);

    for ($i = 2; $i < $factor; $i += 1) {
      imagettftext($resource, 16, mt_rand(20, 33), ($i - 1.5) * $factor, $i * 15, $bg, $font, str_shuffle($hash));
    }


    $a = $x + ($w * strlen($left));
    $b = $width - ($w * strlen($right)) - $w;

    imagettftext($resource, $factor, mt_rand(-3, 4), mt_rand(min($a, $b), max($a, $b)), $h * 1.20, ($fg * 1.3) / 96, $font, $right);
  }

  header('Content-Type: image/png');

  imagepng($resource);
  imagedestroy($resource);

  exit;
}


/**
 * Captcha code validation
 *
 * @param  string  Received code
 * @param  string  Unique key
 * @param  boolean Strict?
 * @return boolean
 */
function captcha_valid($test, $key = 0, $strict = FALSE) {
  if ( ! empty($test)) {
    $old = session("--captcha-id$$key");

    if (is_true($strict)) {
      return ! strcmp($old, $test);
    }
    return strtolower($old) === strtolower($test);
  }
  return FALSE;
}

/* EOF: ./library/captcha.php */
Back to Top