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


class PasswordOperations
{
  
  public $settings;

  
  public function getInfoText(): string
  {
    $min = '  <li>mindestens ' . CleanOperations::cleanStringForHtml($this->settings->pwdMinLength) . ' Zeichen lang sein</li>';
    $num = '  <li>mindestens eine Zahl enthalten</li>';
    $chars = '<li>mindestens einen Klein- und einen Großbuchstaben enthalten</li>';
    $spe = '  <li>mindestens ein Sonderzeichen enthalten (' . CleanOperations::cleanStringForHtml(PW_SPECIALCHARS) . ')</li>';

    $retStr = '<p><strong>Das Passwort muss</strong></p><ul>';

    $langId = SessionOperations::getCurrentLanguageKey();
    if ($langId !== KUFERWEB_SPRACHE_STANDARD) {
      $min = '  <li>be at least ' . CleanOperations::cleanStringForHtml($this->settings->pwdMinLength) . ' characters long</li>';
      $num = '  <li>include at least one number</li>';
      $chars = '<li>include at least one lowercase and one uppercase character</li>';
      $spe = '  <li>include at least one special character (' . CleanOperations::cleanStringForHtml(PW_SPECIALCHARS) . ')</li>';
      $retStr = '<strong>The password has to:</strong><ul>';
    }

    $retStr .= $min;
    if ($this->settings->pwdNumbersRequired) {
      $retStr .= $num;
    }
    if ($this->settings->pwdLowerUpperRequired) {
      $retStr .= $chars;
    }
    if ($this->settings->pwdSpecialCharsRequired) {
      $retStr .= $spe;
    }

    return $retStr . '</ul>';
  }

  
  public function __construct()
  {
    $this->settings = ToolSettingsAPI::getToolSettings(0);
  }

  
  public function validSize($pwd)
  {
    if (($this->settings->pwdMinLength != '')
        && ($this->settings->pwdMinLength > 0)) {
      return (StringOperations::strlen($pwd) >= $this->settings->pwdMinLength);
    }

    return true;
  }

  
  public function validSpecialChars($pwd)
  {
    if ($this->settings->pwdSpecialCharsRequired) {
      $regexEscapedSpecialChars = implode('\\', \StringOperations::str_split(PW_SPECIALCHARS));

      return StringOperations::containsPattern($pwd, '[' . $regexEscapedSpecialChars . ']');
    }

    return true;
  }

  
  public function validNumbers($pwd)
  {
    if ($this->settings->pwdNumbersRequired) {
      return StringOperations::containsPattern($pwd, '[0-9]');
    }

    return true;
  }

  public function validLowerCaseChar($pwd)
  {
    if ($this->settings->pwdLowerUpperRequired) {
      return StringOperations::containsPattern($pwd, '[a-zäöü]');
    }

    return true;
  }

  public function validUpperCaseChar($pwd)
  {
    if ($this->settings->pwdLowerUpperRequired) {
      return StringOperations::containsPattern($pwd, '[A-ZÄÖÜ]');
    }

    return true;
  }

  
  public function isValid($pwd)
  {
    return ($this->validSize($pwd)
            && $this->validSpecialChars($pwd)
            && $this->validNumbers($pwd)
            && $this->validLowerCaseChar($pwd)
            && $this->validUpperCaseChar($pwd));
  }
}