<?php
  /* class admin */

  class admin
  {
      var $userId;
      var $name;
      var $email;
      var $login;
      var $windowslogin;
      var $password;
      var $role;
      var $lastLogIn;
      var $erstellungsdatum;
      var $aenderungsdatum;
      var $ersteller;
      var $ipcheck ;
      var $ip;
      var $disabled = false;
      
      /**
      * constructor admin
      * @param string $login User Login
      * @param int $userId  User Id
      * @return object admin
      * @author Carsten Schaedlich
      **/
      function __construct($login='', $userId='')
      {
        
          global $_SERVER;

          if ($login == '' && $userId == '') {
              return $this;
          }
          $query= "SELECT * FROM admins WHERE ";
          if ($userId)
          {
              $query .= "userId = '$userId'";
          } else {
              $query .= "login = '$login'";
          }
          global $_SERVER;
          $result = dbQuery($query)
              or error ("Konnte Logindaten nicht aus der Datenbank lesen: " . dbError());
          $numRows = dbNumRows($result);
          if ($numRows != 1 )
          {
              return false;
          }
          $row = dbFetchRow($result);
          dbFreeResult($result);

          $this->userId = $row['userId'];
          $this->name = $row['name'];
          $this->email = $row['email'];
          $this->login = $row['login'];
          $this->windowslogin = $row['windowslogin'];
          $this->password = $row['password'];
          $this->role = $row['role'];
          $this->lastLogin = $row['lastLogIn'];
          $this->erstellungsdatum = $row['erstellungsdatum']+0;
          $this->aenderungsdatum = $row['aenderungsdatum']+0;
          $this->ersteller = $row['ersteller'];
          $this->ipcheck = $row['ipcheck'];
          (isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
           ($forwards = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])) &&
           $remoteip = trim(end($forwards))) || $remoteip = $_SERVER['REMOTE_ADDR'];
          $this->ip = $remoteip;

          // disable admin by changing password *ouch*
          if (preg_match('/101203_secvi_ZHUGX$/', $this->password))
              $this->disabled = true;

          return $this;
      }

      function login($password)
      {
          if ($password != $this->password)
          {
              return false;
          }

          // Trage Login-Zeit und IP-Adresse in die Log-Tabelle ein
          $this->log('login', $this->userId);
          
          return true;
      }

      function insertData()
      {
          $this->lastLogin = 0;
          $this->erstellungsdatum = time();
          $this->aenderungsdatum = $this->erstellungsdatum;
          $query = "INSERT INTO admins SET ".
              "name = '" . $this->name . "', " .
              "email = '" . $this->email . "', ".
              "login = '" . $this->login . "', " .
              "password = '" . $this->password . "', " .
              "role = '" . $this->role . "', " .
              "lastlogIn = " . $this->lastLogin . ", " .
              "windowslogin = '" . $this->windowslogin . "', " .
              "erstellungsdatum = " . $this->erstellungsdatum . "," .
              "aenderungsdatum = " . $this->aenderungsdatum . "," .
              "ersteller = " . $this->ersteller . "," . 
              "ipcheck = '" . $this->ipcheck . "'";
          if (! dbQuery($query))
          {
              error ("Konnte Teilnehmer nicht in die Datenbank einfügen: " . dbError(). "<br>\n".
                      "Query: $query");
              return false;
          }
      return true;
      }

      function deactivate()
      {
          $this->password = $this->password . '::101203_secvi_ZHUGX';
          $this->updateData();
      }

      function delete()
      {
          $query = "DELETE FROM admins
              WHERE userId = " . $this->userId;
          $this->password = $this->password . '::101203_secvi_ZHUGX';
          $this->updateData();
          $result = dbQuery($query)
              or error ("Konnte BearbeiterIn nicht aus der "
                      ."Datenbank l&ouml;schen: " . dbError()); 
          @dbFreeResult($result);
      }



      function updateData()
      {
          $query = "UPDATE admins SET ".
              "name = '" . $this->name . "'," .
              "email = '" . $this->email . "', ".
              "login = '" . $this->login . "'," .
              "password = '" . $this->password . "'," .
              "role = '" . $this->role . "'," .
              "lastLogIn = " . $this->lastLogin . "," .
              "windowslogin = '" . $this->windowslogin . "'," .
              "erstellungsdatum = " . $this->erstellungsdatum . "," .
              "aenderungsdatum = " . time() . "," .
              "ersteller = " . $this->ersteller . ',' . 
              "ipcheck = '" . $this->ipcheck . "' " .
              " WHERE userId = " . $this->userId;

          $result = dbQuery($query)
              or error ("Konnte Änderungen nicht in der Datenbank speichern: " . dbError()); 
          @dbFreeResult($result);
      }

      function log($action, $object)
      {
          $query = "INSERT INTO userlogs VALUES (" .
                $this->userId . ", " . 
                "'admin', " .
                "'" . dbEscapeString($action) . "', " .
                "'" . dbEscapeString($object) . "', " .
                "'" . $this->ip . "', ".
                time() . 
                ")";
          $result = dbQuery($query) or 
                error ("Kann log nicht eintragen: " . dbError());
          dbFreeResult($result);
      }
  }
