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

class CheckPasswortVergessenLogin extends WBAnmeldungCheck
{
  public function __construct()
  {
    parent::__construct();
    $this->exportAdapter = new ExpLogin();
    $this->exportAdapter->initiate();
    $this->exportAdapter->export->initiate();
    $this->exportAdapter->export->exportresults = array($this->datamodul);
    $this->exportAdapter->export->selectResultset(0);
    $this->sessionSuffix = '';
  }

  public function setDataModul($pDataModul)
  {
    $this->datamodul = $pDataModul;
  }

  
  public function check($pPrefix = 'wbledit')
  {
    if ($this->exportAdapter == null) {
      return false;
    }

    $this->prepareVorlage();
    $pflichtFelder = $this->getPflichtFelder($pPrefix); 

    
    foreach ($pflichtFelder as $key => $value) {
      $fieldname = $pPrefix . '_' . $value;
      if (!$this->_checkPflichtFeld($fieldname)) {
        $this->pflichtfeldFehlerArray[$key] = urldecode($this->getFehlerMeldung($key));

        continue;
      }
      
      if ($key == 'login') {
        $dbwwbl = new DBWorkWBLogins();
        $login = $dbwwbl->getByLogin(RequestOperations::getVarValue($fieldname));
        if ($login->isEmpty()) {
          $this->pflichtfeldFehlerArray[] = urldecode($this->getFehlerMeldung($key));
        }
      }

      
      if ($key == 'password2') {
        if (RequestOperations::getVarValue($fieldname, '--')
          != (RequestOperations::getVarValue($pPrefix . '_password', '--'))) {
          $this->pflichtfeldFehlerArray[] = urldecode($this->getFehlerMeldung($key));
        }
      }
    }

    
    if ($this->datamodul != null) {
      $this->_checkValidierung($pPrefix);
    }
    if (count($this->pflichtfeldFehlerArray) > 0 || count($this->formatFehlerArray) > 0) {
      return false;
    }

    return true;
  }

  
  public function _checkValidierung($pPraefix = 'w', $pSessSuffix = '', $pAbwBankdaten = false)
  {
    if ($this->datamodul != null) {
      for ($i = 0; $i < $this->datamodul->getVarCount(); $i++) {
        $value = '';
        $vName = $this->datamodul->getVarName($i);
        if (trim(RequestOperations::getVarValue($pPraefix . '_' . $vName)) != '') {
          $value = RequestOperations::getVarValue($pPraefix . '_' . $vName);
        }
        if ($value !== '') {
          $tmp = $this->validation->isValidVar($this->datamodul->getVarName($i), $value);
          if ($tmp !== true) {
            $msg = $this->getFehlerMeldung($tmp);
            $key = array_search($tmp, $this->varnamenMapping);
            if ($msg == $tmp && ($key !== false && $key !== null)) {
              $msg = $this->getFehlerMeldung($key);
            }
            $this->formatFehlerArray[$tmp] = $msg;
          }

          if ($vName == 'password') {
            $pwdop = new PasswordOperations();
            if (!$pwdop->validSize($value) || !$pwdop->validSpecialChars($value) || !$pwdop->validNumbers($value) || !$pwdop->validLowerCaseChar($value) || !$pwdop->validUpperCaseChar($value)) {
              $this->formatFehlerArray[$vName] = urldecode($this->getFehlerMeldung($vName));
            }
          }
        }
      }
    }
  }

  public function _checkPflichtfeld($pVarName, $pKompat1 = 'w', $pKompat2 = '', $pKompat3 = true, $pKompat4 = true)
  {
    return (RequestOperations::getVarValue($pVarName, '') != '');
  }
}