<?php

/**
 * This file generates a CAPTCHA string, writes it into the $_SESSION['captcha'] and renders a fresh CAPTCHA graphic file to the browser.
 *
 * In the views you can use this by saying:
 * <img src="tools/showCaptcha.php" />
 *
 * Check if the typed captcha is correct by saying:
 * if ($_POST["captcha"] == $_SESSION['captcha']) { ... } else { ... }
 */

// check if php gd extension is loaded
if (!extension_loaded('gd')) {
    die("It looks like GD is not installed");
}

session_start();
class Capt{
public function get_ss(){
// target captcha string length
$iCaptchaLength = rand(4,6);

// following letters are excluded from captcha: I, O, Q, S, 0, 1, 5
$str_choice = 'abcdefghjklmnprtuvwxyz2346789';
$str_captcha = '';
// create target captcha with letters comming from $str_choice
for ($i=0; $i < $iCaptchaLength; $i++) {
    do {
        $ipos = rand(0, strlen($str_choice) - 1);
    // checks that each letter is used only once
    } while (stripos($str_captcha, $str_choice[$ipos]) !== false);

    $str_captcha .= $str_choice[$ipos];
}

// write the captcha into a SESSION variable
$_SESSION['captcha'] = $str_captcha;

// begin to create the image with PHP's GD tools
$im = imagecreatetruecolor(120, 30);

$bg = imagecolorallocate($im, 255, 155, 255);
imagefill($im, 0, 0, $bg);
//imagefilledrectangle($im, 0, 0, 299, 99, $red);
// create background with 1000 short lines
// for($i=0;$i<1000;$i++) {
    // $lines = imagecolorallocate($im, rand(200, 220), rand(200, 220), rand(200, 220));
    // $start_x = rand(0,150);
    // $start_y = rand(0,70);
    // $end_x = $start_x + rand(0,5);
    // $end_y = $start_y + rand(0,5);
    // imageline($im, $start_x, $start_y, $end_x, $end_y, $lines);
// }

// create letters. for more info on how this works, please
// @see php.net/manual/en/function.imagefttext.php
// TODO: put the font path into the config
for ($i=0; $i < $iCaptchaLength; $i++) {
    $text_color = imagecolorallocate($im, rand(0, 50), rand(0, 50), rand(0, 50));
    // font-path relative to this file
    imagefttext($im, 16, rand(-25, 25), 10 + ($i * 18) + rand(-5, +5), 10 + rand(10, 10), $text_color, 'fonts/AngelicWar.ttf', $str_captcha[$i]);
}

// send http-header to prevent image caching (so we always see a fresh captcha image)
//header('Content-type: image/png');
//header('Pragma: no-cache');
//header('Cache-Control: no-store, no-cache, proxy-revalidate');

// send image to browser and destroy image from php "cache"
// imagepng($im);
ob_start();
imagepng($im);
$contents = ob_get_clean();
ob_end_clean();



// imagexbm($im, $RETT);
 imagedestroy($im);
return $contents;
}
}
?>
