<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */
if (!function_exists('imagecreatefrompng') && class_exists('\\Logger')) {
  \Logger::logg('PHP-Extension gd muss erlaubt sein!', 40);
}

  if (session_id() === '') {
    session_start();
  }

  function make_seed(): int
  {
    list($usec, $sec) = explode(' ', microtime());

    return (int)((float)$sec + ((float)$usec * 100000));
  }

  
  function randomCode($chars, $onlyNumeric = false)
  {
    srand(make_seed());

    
    if (!$onlyNumeric) {
      $pool = 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789';
    } else {
      $pool = '0123456789';
    }

    $str = '';
    while (strlen($str) < $chars) {
      $str .= StringOperations::substr($pool, (rand() % (strlen($pool))), 1);
    }

    return($str);
  }

  
  $settings = ToolSettingsAPI::getToolSettings(4);

  $text = randomCode(5, ($settings->simpleCaptchaOnlyNumeric));  
  SessionOperations::setVarValueIntern('captcha_value', $text);

  header('Content-type: image/png');
  $img = imagecreatefrompng(BASEDIR . 'classes' . DIRECTORY_SEPARATOR . 'captchas'
    . DIRECTORY_SEPARATOR . 'simplecaptcha' . DIRECTORY_SEPARATOR . 'resources'
    . DIRECTORY_SEPARATOR . $settings->simpleCaptchaBack);    
  $font = BASEDIR . 'classes' . DIRECTORY_SEPARATOR . 'captchas' . DIRECTORY_SEPARATOR
    . 'simplecaptcha' . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR
    . 'times.ttf';                                            
  $fontsize = 25;                                           
  $color = imagecolorallocate($img, 0, 0, 0);               

  
  
  for ($i = 0; $i < strlen($text); $i++) {
    $angle = rand(-15, 15);
    $x = $i * ($fontsize) + 5;
    $y = rand($fontsize, 39);
    imagettftext($img, $fontsize, $angle, $x, $y, $color, $font, $text[$i]);
  }

  imagepng($img);
  imagedestroy($img);
?>