<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

class DataMenuPasswortVergessenFactory
{
  
  public static function getDataMenu($pUrlParameter)
  {
    switch ($pUrlParameter->mode) {
      case 'auth':
        return new DataMenuPasswortVergessenStep2($pUrlParameter);
      default:
        return new DataMenuPasswortVergessenStep1($pUrlParameter);
    }
  }
}

class DataMenuPasswortVergessenParent extends DataMenuMain
{
  protected $wbLoginCheck;
  protected $requestPrefix = 'wbl';
  protected $dbwWbLDo;

  
  public function __construct($pUrlParameter)
  {
    parent::__construct($pUrlParameter);
    $this->exportadapter = new ExpLogin();
    $this->wbLoginCheck = new CheckPasswortVergessenLogin();
    $this->dbwWbLDo = new DBWorkWbLoginDbloptin();
  }

  
  public function prepareMainmenu()
  {
    
    
    $this->getsight();
    $this->getdataarrayheader();
    $this->getdataarraybody();
    $this->getdataarrayfooter();
    $this->getdataarraynodata();
    $this->getdataarraynocolumn();
  }

  
  protected function showFormular($pPflichtfeldFehler = array(), $pFormatFehler = array())
  {
    $this->showheader();

    if ((is_array($pPflichtfeldFehler) && is_array($pFormatFehler))
           && (count(array_merge($pPflichtfeldFehler, $pFormatFehler)) > 0)) {
      echo '<ul class="warnungpflichtfelder">';
      foreach ($pPflichtfeldFehler as $k => $v) {
        echo '<li class="warnungpflichtfelder ' . CleanOperations::cleanStringForHtml($k) . '">' . CleanOperations::cleanStringForHtml($v) . '</li>';
      }
      foreach ($pFormatFehler as $k => $v) {
        echo '<li class="warnungvalidierung ' . CleanOperations::cleanStringForHtml($k) . '">' . CleanOperations::cleanStringForHtml($v) . '</li>';
      }
      echo '</ul>';
    }

    $this->showbody();
    $this->showfooter();
  }
}

class DataMenuPasswortVergessenStep1 extends DataMenuPasswortVergessenParent
{
  public function __construct($pUrlParameter)
  {
    parent::__construct($pUrlParameter);
  }

  
  public function _getClickPath($pName = 'Stammhörer-Login - Passwort vergessen')
  {
    $tmp = $this->toolsettings->clickKundenLoginPasswdVergessen != '' ? $this->toolsettings->clickKundenLoginPasswdVergessen : $pName;

    DataMenuMain::_getClickPath();
    if ($this->toolsettings->titleKursBereich != '') {
      $this->clickpfad = '<span class="clickadresse"> ' . CleanOperations::cleanStringForHtml(stripslashes($this->toolsettings->titleKursBereich)) . ' </span>' . PHP_EOL;
      $this->clickpfad .= '<span class="trennzeichen"> ' . CleanOperations::cleanStringContainingHtml($this->toolsettings->trennzeichenKategorie) . ' </span>' . PHP_EOL;
    }

    $this->clickpfad .= '<span class="clickadrbold">' . CleanOperations::cleanStringForHtml($tmp) . "</span>\n";

    return $this->clickpfad;
  }

  
  public function getsight($pSigthID = 436)
  {
    parent::getsight($pSigthID);
  }

  
  public function mainmenu()
  {
    $this->prepareMainmenu();
    if ($this->toolsettings->showClickpfad) {
      echo $this->getClickPath();
    }
    echo '<div class="hauptseite">' . PHP_EOL;
    $this->statusmenu();
    echo $this->getContentAnker();
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $this->wbLoginCheck->vorlage = $this->sight->body;
      $this->wbLoginCheck->setDataModul(new WBLogin());
      $result = $this->wbLoginCheck->check($this->requestPrefix . 'edit');

      if ($result === true) {
        $login = RequestOperations::getVarValue($this->requestPrefix . 'edit_login');
        $dbwbldo = new DBWorkWBLogins();
        $wbLogin = $dbwbldo->getByLogin($login);
        $wbLoginDblOptin = WbLoginDbloptin::createNewObject();
        $wbLoginDblOptin->setValue2Var('web_wblogin_id', $wbLogin->getVarValue('id'));

        $wbLoginDblOptin = $this->dbwWbLDo->insertWbLoginDbloptin($wbLoginDblOptin);
        $this->sendMail($wbLogin, $wbLoginDblOptin);
        $this->shownodata();
      } else {
        $tool0Settings = ToolSettingsAPI::getToolSettings(0);
        if ($tool0Settings->wbPwVergessenVorRateAngriffSchuetzen) {
          $this->shownodata();
        } else {
          $pflichtfeldFehlerArray = $this->wbLoginCheck->pflichtfeldFehlerArray;
          $formatFehlerArray = $this->wbLoginCheck->formatFehlerArray;
          $this->showFormular($pflichtfeldFehlerArray, $formatFehlerArray);
        }
      }
    } else {
      $this->showFormular();
    }
    echo '</div>';
  }

  
  public function sendMail($pWbLogin, $pWbLoginDblOptin)
  {
    $settings = ToolSettingsAPI::getToolSettings(4);

    $this->dataarrayheader = array($pWbLogin);
    $this->dataarraybody = array($pWbLoginDblOptin);
    $this->dataarrayfooter = array($pWbLogin);
    $this->getSight(437);

    $content = $this->getheader() . $this->getbody() . $this->getfooter();
    $matches = array();
    $subject = '';
    if (preg_match('/<betreff>(.*)<\\/betreff>/', $content, $matches)) {
      $subject = $matches[1];
      $content = str_replace($matches[0], '', $content);
    }
    $email = $this->checkLoginEmail($pWbLogin);

    $smtp = new smtpMail();
    $smtp->SetVar2Value('to', $email);
    $smtp->SetVar2Value('subject', $subject);
    $smtp->SetVar2Value('body', $content);
    if ($settings->infoMailTextContent) {
      $smtp->SetVar2Value('contenttype', SMTP_CONTENTTYPE_TEXT);
    } else {
      $smtp->SetVar2Value('contenttype', SMTP_CONTENTTYPE_HTML);
    }
    $smtp->SetVar2Value('doNotSplitHTMLBody', !$settings->infoMailMultipleContent);
    $retour = $smtp->sendMail();

    $this->prepareMainmenu();
  }

  private function checkLoginEmail($pWblogin)
  {
    $email = $pWblogin->email;
    if (SessionOperations::isDmzBetrieb()) {
      try {
        $emails = array();
        $komminfo = KommunikationsAPI::getKommunikationInfosPerDMZ($pWblogin->tnr);

        foreach ($komminfo as $object) {
          if ($object->Art == 'E') {
            $emails[] = $object->Wert;
          }
        }
        if (in_array($pWblogin->login, $emails)) {
          $email = $pWblogin->login;
        }
      } catch (AbgleichApiException $aae) {
        
      }
    }

    return $email;
  }
}

class DataMenuPasswortVergessenStep2 extends DataMenuPasswortVergessenParent
{
  public function __construct($pUrlParameter)
  {
    parent::__construct($pUrlParameter);
  }

  
  public function getsight($pSigthID = 438)
  {
    parent::getsight($pSigthID);
  }

  public function mainmenu()
  {
    $this->prepareMainmenu();
    if ($this->toolsettings->showClickpfad) {
      echo $this->getClickPath();
    }
    echo '<div class="hauptseite">' . PHP_EOL;
    $this->statusmenu();
    echo $this->getContentAnker();
    $data = $this->dbwWbLDo->getWbLoginDbloptinForToken(RequestOperations::getVarValue('token'));

    if ($data->isEmpty() || $data->isExpired()) {
      $this->shownodata();
    } else {
      if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $this->wbLoginCheck->vorlage = $this->getbody();
        $this->wbLoginCheck->setDataModul(new WBLogin());
        $result = $this->wbLoginCheck->check($this->requestPrefix . 'edit');

        if ($result === true) {
          $wbLogin = $data->getWbLogin();
          $dbwWbLPa = new DBWorkWBLoginsPasswords();
          $dbwWbLPa->insertOrUpdate(RequestOperations::getVarValue($this->requestPrefix . 'edit_password'), $wbLogin->login, $wbLogin->tnr);
          $this->dbwWbLDo->deleteByWebWbLoginID($wbLogin->id);
          $this->shownocolumn();
        } else {
          $pflichtfeldFehlerArray = $this->wbLoginCheck->pflichtfeldFehlerArray;
          $formatFehlerArray = $this->wbLoginCheck->formatFehlerArray;
          $this->showFormular($pflichtfeldFehlerArray, $formatFehlerArray);
        }
      } else {
        $this->showFormular();
      }
    }

    echo '</div>';
  }

  public function getdataarraybody()
  {
    $item = new WbLoginDbloptin();
    $item->token = RequestOperations::getVarValue('token');
    $this->dataarraybody = array($item);
  }
}