<?php
/**
 * ****************************************************************
 * Project:      *
 * Copyright:    Copyright (c) 19.06.2008
 * Company:      Thomas Niemann
 * ****************************************************************
 * Workfile:     CaptchaLoader.php
 * ****************************************************************
 * Description:
 *
 * ***************************************************************/
 class CaptchaLoader implements SingletonInterface{
   private static $instance;
  /********************************************************************
   * default constructor
   *******************************************************************/
  private function __construct() { }

  /*****************************************************
   * singleton, provides only one instance
   ****************************************************/
  public static function getInstance() {
    if (!isset (self :: $instance)) {
      $class = __CLASS__;
      self :: $instance = new $class ();
    }
    return self :: $instance;
  }
  public function getCaptcha ($_captchaLength) {
    $string = $this->generateCaptchaString($_captchaLength);
    $backroundImage = 'gui/image/background/captcha.jpg';
    $im = imagecreatefromjpeg($backroundImage);
    $white_color = imagecolorallocate($im, 255, 255, 255);
      imagefttext ($im, 20, 7, 25, 25,$white_color,'gui/font/COLLEGE.ttf',$string);
      for($i = 0; $i < 5; $i++) {
          imageline($im, rand(0, 100),rand(0, 20), rand(50, 100), rand(50, 100), $white_color);
      }
      $_destinationFolder = ConfigLoader::getInstance()->getConfigPath('captcha');
      imagejpeg($im, $_destinationFolder.'verify.jpg');
      imagedestroy($im);
    return $string;
  }
  public static public function generateCaptchaString($_captchaLength) {
    $all = array(
      range(2, 9),
      range('a', 'z'),
      range('A', 'Z')
    );
    foreach ($all as $key => $value)
      $captchaString[] = $value[array_rand($value)];

    while(count($captchaString) < $_captchaLength) {
      $value = $all[array_rand($all)];
      $captchaString[] = $value[array_rand($value)];
    }
    shuffle($captchaString);
    $captchaString = implode($captchaString);
    $_SESSION['captchaString'] = $captchaString;
    return $captchaString;
  }
  public static public function checkCaptchaString($_postCaptcha) {
	if(empty($_postCaptcha)){
			return false;
	}elseif(strtolower($_postCaptcha) === strtolower($_SESSION['captchaString'])){
		return true;
	}
    return false;
  }
}
?>
