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


abstract class User
{
  
  protected $name;
  
  private $password;
  
  private $salt;
  
  private $active;

  private $hashversion;

  
  public function __construct($name, $password, $isPwEncrypted = false, $hashversion = '')
  {
    $this->name = $name;
    $this->active = true;

    if (!$isPwEncrypted) {
      $pw = PasswordFactory::getLatest();
      $this->password = $pw->getHash($password);
      $this->SetHashversion($pw);
    } else {
      $this->password = $password;
      $this->hashversion = $hashversion;
    }
  }

  
  public function getName()
  {
    return $this->name;
  }

  public function getPasswordHash()
  {
    return $this->password;
  }

  public function getHashVersion()
  {
    return $this->hashversion;
  }

  
  public function setName($name)
  {
    if (trim($name) == '') {
      return false;
    }

    $this->name = $name;

    return true;
  }

  
  final public function checkLogin($password)
  {
    if (!$this->active) {
      return false;
    }

    return $this->checkPasswordOnly($password);
  }

  
  final public function checkPasswordOnly($password)
  {
    
    
    if ((!is_numeric($this->getHashVersion()) || $this->getHashVersion() <= 0) && ($this->password === $this->saltPassword($password))) {
      return true;
    }

    $pwVerify = PasswordFactory::getByHashVersion($this->getHashVersion());

    return $pwVerify->verifyHash($password, $this->password);
  }

  
  public function SetHashversion($pPw)
  {
    $this->hashversion = $pPw->getHashversion();
  }

  
  final public function setPassword($password, $pIgnoreValidation = false)
  {
    
    $pwValidator = new PasswordOperations();
    if (!$pIgnoreValidation && !$pwValidator->isValid($password)) {
      return false;
    }

    $pw = PasswordFactory::getLatest();
    $this->password = $pw->getHash($password);
    $this->SetHashversion($pw);

    return true;
  }

  
  public function hasPassword()
  {
    return isset($this->password) && $this->password != '';
  }

  
  public function isActive()
  {
    return $this->active;
  }

  
  final public function setActive($value)
  {
    $this->active = (bool)$value;
  }

  
  private function saltPassword($password)
  {
    if (!$this->salt) {
      return md5($password);
    } else {
      return md5($this->salt . $password . SALT . $this->salt);
    }
  }

}


class WebadminUser extends User
{
  
  private $isAdmin;
  
  private $access = array(array());

  
  public function __construct($name, $password, $hashversion, $isAdmin, $isPwEncrypted = false)
  {
    parent::__construct($name, $password, $isPwEncrypted, $hashversion);

    $this->isAdmin = (bool)$isAdmin;
  }

  
  public function hasAccess($action, $sub = false)
  {
    if ($this->isAdmin === true) {
      return true;
    }

    $access = $this->getRights();

    if (!$sub && array_key_exists($action, $access)) {
      foreach ($access[$action] as $subitem) {
        if ($subitem === true) {
          return true;
        }
      }
    } elseif (array_key_exists($action, $access)
           && array_key_exists((string)$sub, $access[$action])
           && $access[$action][$sub] === true) {
      return true;
    }

    return false;
  }

  
  public function getRights()
  {
    $rights = array();
    foreach (self::getAvailableRights() as $key => $right) {
      if (array_key_exists($key, $this->access)) {
        $rights[$key] = array_merge($right, $this->access[$key]);
      } else {
        $rights[$key] = $right;
      }
    }

    return $rights;
  }

  
  public static function getAvailableRights()
  {
    

    return array(
      'overview' => array(
          'ContentOverviewCommon' => true,     
          'ContentOverviewLog' => false,
          'ContentOverviewAnmLog' => false,
          'ContentOverviewLoginLog' => false,
          'ContentStatistic' => false,
          'ContentOverviewUserEdit' => false, 
          'ContentOverviewUserAdmin' => false
        ),
      'functions' => array(
          'ContentPreviewMode' => false,
          'ContentMehrsprachigkeit' => false,
          'ContentUpdt' => false,
          'ContentBackup' => false,
          'ContentNews' => false,
          'ContentUpload' => false
        ),
      'settings' => array(
          'ContentSettingsSystem' => false,
          'ContentSettingsLayout' => false,
          'ContentSettingsConstant' => false,
          'ContentSettingsDb' => false
        ),
      'editlayout' => array(
          'ContentEditLayoutInfo' => false,
          'ContentFileUpload' => false,
          'ContentCss' => false,
          'ContentSights' => false
        ),
      'tools' => array(
          'ContentSettingsTools' => false,
          'ContentWssTools' => false,
          'ContentSightsTools' => false
        )
    );
  }

  
  public function setRight($value, $action, $sub)
  {
    $list = self::getRights();

    
    if (array_key_exists($action, $list)
       && array_key_exists($sub, $list[$action])) {
      if ($value) {
        $this->access[$action][$sub] = true;
      } else {
        unset($this->access[$action][$sub]);
      }

      return true;
    } else {
      return false;
    }
  }

  
  public function isAdmin()
  {
    return $this->isAdmin;
  }

  
  public function setAdmin($value)
  {
    $this->isAdmin = (bool)$value;
  }
}