<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Captchas;



class SimpleCaptcha implements Captcha
{
  public function getCaptcha($pNoBr = false, $pVorbelegung = '', $pPrefix = 'tedit')
  {
    $path = '';

    $isKuferTools = \SessionOperations::isKuferTools();

    if (!$isKuferTools && \CmsAnb::isCmsUsed()) {
      $path = \CmsAnb::getAnbindung()->getKuferWebUrl() . 'webbasys/';
    }

    if ($isKuferTools) {
      $simpleCaptchaReload = true;
    } else {
      $settings = \ToolSettingsAPI::getToolSettings(4);
      $simpleCaptchaReload = $settings->simpleCaptchaReload;
    }


    if ($simpleCaptchaReload) {
      $str = '<script type="text/javascript">
                jQuery(function($){
                  if($("#captcha_reload").length > 0) { 
                    /* Start Captcha-Reload */
                    /* Erstellt für das Simplecaptcha einen Reload-Button, damit man das captcha neu laden kann */
                    var captcha_identifier = "#img_captcha";
                    var button_content = \'<img id="reload_captcha_button" title="Captcha neuladen" alt="Captcha neuladen" src="' . $path . 'layout/reload.png" data-value="\'+captcha_identifier+\'"/>\';
                    $("#captcha_reload").html(\'<div class="left">\' + $("#captcha_reload").html() + \'</div><div class="right">\' + button_content + \'</div><div style="clear: both"></div>\');

                  $("#reload_captcha_button").click(function(){
                    var cp_identi = this.dataset.value;
                    var path = $(cp_identi).prop("src");
                    
                    if(path.indexOf("?") < 1) path+= "?";
                      path+= Math.round(Math.random()*9).toString();

                    $(cp_identi).prop("src", path);
                    return false;
                  }); 
                }
                /* Ende Captcha-Reload */
              })
            </script>';
      $str .= '<div id="captcha_reload">';
    } else {
      $str = '<div id="captcha">';
    }

    $str .= \HTMLOperations::CreateImageTag(
      $path . 'captcha.php' . '?=' . time(), 
      'Sicherheitscode',
      'img_captcha'
    );
    if (!$pNoBr) {
      $str .= BR;
    }
    $str .= \HTMLOperations::CreateInputTag(
      $pPrefix . '_captcha',
      'text',
      $pPrefix . '_captcha',
      '',
      '',
      '',
      '',
      '',
      '',
      $pVorbelegung
    );
    $str .= '</div>';

    return $str;
  }

  public function isValid($pObject = null, $pValidateFromPost = false, $pPrefix = 'tedit', $pForceWbPrefix = false)
  {
    if ($pValidateFromPost) {
      return $this->_isValidFromPost($pPrefix, $pForceWbPrefix);
    }

    if (\SessionOperations::getVarValueIntern('captcha_value', '', true) != '') {
      $obj = $pObject;
      if ($obj == null) {
        $tnAry = \SessionOperations::getTeilnehmerFromSession();
        $obj = $tnAry[0];
      }
      if (is_object($obj)) {
        if (\SessionOperations::getVarValueIntern('captcha_value') == $obj->captcha) {
          $urlparam = new \URLParameterNavigation();
          $urlparam->explodeSettingStream();

          if (!$urlparam->isstatic) {
            \SessionOperations::setVarValueIntern('captcha_value', null);
          }

          return true;
        }
      }
    }

    return false;
  }

  
  private function _isValidFromPost($pPrefix = 'tedit', $pForceWbPrefix = false)
  {
    if (\SessionOperations::getVarValueIntern('captcha_value', '', $pForceWbPrefix) == \PostOperations::getVarValue($pPrefix . '_captcha')) {
      \SessionOperations::setVarValueIntern('captcha_value', null);

      return true;
    }

    return false;
  }
}