<?php
class USER {

  public function __construct ($class_needed,$file_ending) { // Konstruktor
    // Die Klasse wird eingebunden
    include_once($class_needed.$file_ending);
    // Es wird eine Instanz der Klasse erzeugt, und "class_obj" zugewiesen
    $this->class_obj = new $class_needed;
  }

  public function test_id($loginid) {
    $ret = $this->class_obj->select("SELECT id,name,dauer,dauereinaus,lid,rolid FROM User WHERE lid = '$loginid' ");
    if(count($ret) > 0) {
      $_SESSION['user_logged_in'] = "menu";
      $_SESSION['id'] = $ret[0]['id'];
      $_SESSION['user'] = $ret[0]['name'];
      $_SESSION['dauer']  = $ret[0]['dauer'];
      $_SESSION['dauereinaus']  = $ret[0]['dauereinaus'];
      $_SESSION['lid'] = $ret[0]['lid'];
      $_SESSION['readonly'] = false;
      $cont="liste";
      $this->debug($ret);
      $this->debug($_SESSION);
      $array = array($ret[0]['id'],$ret[0]['name'],$ret[0]['lid'],$cont);
      return $array;
    }

    $ret = $this->class_obj->select("SELECT id,name,dauer,dauereinaus,lid,rolid FROM User WHERE rolid = '$loginid' ");
    if(count($ret) > 0) {
      $_SESSION['user_logged_in'] = "menu";
      $_SESSION['id'] = $ret[0]['id'];
      $_SESSION['user'] = $ret[0]['name'];
      $_SESSION['dauer']  = $ret[0]['dauer'];
      $_SESSION['dauereinaus']  = $ret[0]['dauereinaus'];
      $_SESSION['lid'] = $ret[0]['rolid'];
      $_SESSION['readonly'] = true;
      $cont="liste";
      $this->debug($ret);
      $this->debug($_SESSION);
      $array = array($ret[0]['id'],$ret[0]['name'],$ret[0]['rolid'],$cont);
      return $array;
    }

  }

  public function user_login() {
    echo "<form action=\"\" method=\"post\">\n";
    echo "<input type=\"text\" name=\"usern\" value=\"\" placeholder=\"Username\" size=\"20\">\n";
    echo "<input type=\"password\" name=\"passw\" value=\"\" placeholder=\"Passwort\" size=\"20\">\n";
    echo "<input type=\"submit\" name=\"logsub\" value=\"Einloggen\"  size=\"20\">\n";
    echo "</form>\n";
  }

  public function check_login($user, $pass) {
    $ret = $this->class_obj->select("SELECT id,name,passwort,dauer,dauereinaus,lid,rolid FROM User WHERE name = '$user' AND passwort = '$pass'");
    $this->debug($ret);
    $this->debug($user);
    $this->debug($pass);

    if(count($ret) > 0) {
      if (strcmp($user,$ret[0]['name']) == 0 && strcmp($pass,$ret[0]['passwort']) == 0 ) {
        $_SESSION['user_logged_in'] = "config";
        $_SESSION['id']   = $ret[0]['id'];
        $_SESSION['user'] = $ret[0]['name'];
        $_SESSION['dauer']  = $ret[0]['dauer'];
        $_SESSION['dauereinaus']  = $ret[0]['dauereinaus'];
        $_SESSION['lid']  = $ret[0]['lid'];
        $_SESSION['rolid']  = $ret[0]['rolid'];
        $array = array($ret[0]['id'],$ret[0]['name'],$ret[0]['lid']);
        // $this->user_config();

        // return $array;
      }
    }

  }

  public function user_config() {
    if(isset($_GET['genID'])) {
      $LID = $this->generateCode(40);
      $this->debug($LID);
      $ret = $this->class_obj->update("UPDATE User SET lid=?  WHERE id = ? LIMIT 1", array($LID,$_SESSION['id']));
    }
    if(isset($_GET['genROID'])) {
      $ROLID = $this->generateCode(40);
      $this->debug($ROLID);
      $ret = $this->class_obj->update("UPDATE User SET rolid=?  WHERE id = ? LIMIT 1", array($ROLID,$_SESSION['id']));
    }
      $ret = $this->class_obj->select("SELECT id,name,passwort,dauer,dauereinaus,lid,rolid FROM User WHERE id = '".$_SESSION['id']."'");
      $_SESSION['id']   = $ret[0]['id'];
      $_SESSION['user'] = $ret[0]['name'];
      $_SESSION['dauer']  = $ret[0]['dauer'];
      $_SESSION['dauereinaus']  = $ret[0]['dauereinaus'];
      $_SESSION['lid']  = $ret[0]['lid'];
      $_SESSION['rolid']  = $ret[0]['rolid'];

    if(isset($_POST['sub'])) {
      if($_POST['pw'] == $_POST['pw2']) {
        $ret = $this->class_obj->update("UPDATE User SET passwort=?  WHERE id = ? LIMIT 1", array($_POST['pw'],$_SESSION['id']));
      } else {
        echo "Passw&ouml;rter sind verschieden.";
      }
    }
    echo "<h3>".$_SESSION['user']."</h3>\n";
    echo "<hr />\n";
    echo "<br />Link für den eigenen Seitenaufruf:<br />\n";
    if ($_SESSION['lid'] == "") {
      echo "<span style=\"color: #ff0000;\"><br />Bitte erst die Login-ID generieren!<br/></span>";
    } else {
      echo "<a href=\"https://eblok.de/Logbuch/index.php?LID=".$_SESSION['lid']."\">https://eblok.de/Logbuch/index.php?LID=".$_SESSION['lid']."</a>";
    }
    if ($_SESSION['rolid'] == "") {
      echo "<span style=\"color: #ff0000;\"><br />Bitte erst die Read-Only Login-ID generieren!<br/></span>";
    } else {
      echo "<br /><br />Link für den Read-Only Seitenaufruf:<br />\n";
      echo "<a href=\"https://eblok.de/Logbuch/index.php?LID=".$_SESSION['rolid']."\">https://eblok.de/Logbuch/index.php?LID=".$_SESSION['rolid']."</a>";
    }
    echo "<br /><br /><hr />\n";
    echo "<a href=\"index.php?genID\">Neue Owner-ID generieren.</a>\n";
    echo "<br /><br />Dadurch &auml;ndert sich der Seitenaufruf.<hr />\n";
    // echo "<br /><br /><hr />\n";
    echo "<a href=\"index.php?genROID\">Neue Read-Only-ID generieren.</a>\n";
    echo "<br /><br />Dadurch &auml;ndert sich der Seitenaufruf.<hr />\n";
    echo "Loginpasswort &auml;ndern.<br /><br />\n";
    echo "<form action=\"index.php\" method=\"post\">\n";
    echo "<input type=\"password\" name=\"pw\" value=\"\" placeholder=\"neues Passwort\">\n";
    echo "<input type=\"password\" name=\"pw2\" value=\"\" placeholder=\"Passwort wiederholen\">\n";
    echo "<input type=\"submit\" name=\"sub\" value=\"&auml;ndern\" >\n";
    echo "</form>\n";
    echo "<br /><hr />\n";
    echo "<a href=\"index.php?logoff=1\">Logout</a>\n";
    // exit();
    $_SESSION['user_logged_in'] = "config";

  }

  private function generateCode($characters) {
  		/* list all possible characters, similar looking characters and vowels have been removed */
  		$possible = '23456789abcdefghjkmnopqrstvwxyzABCDEFGHJKMNOPQRSTVWXYZ';
  		// $possible = '1234567890';
  		$code = '';
  		$i = 0;
  		while ($i < $characters) {
  			$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
  			$i++;
  		}
  		return $code;
  	}

  private function debug ($ausgabe="", $array=true) {
  	if ($ausgabe !="") {
  		if (isset($_SESSION['debug']) and ($_SESSION['debug'] == true)) {
  			if (is_string($ausgabe)) echo "<font color=\"#FF0000\">".$ausgabe."</font><br>\n";
  			if ($array) {
  				echo "<pre>\n";
  				print_r($ausgabe);
  				echo "</pre>\n";
  			} else
  			if (is_array($ausgabe)) {
  				foreach ($ausgabe as $ausgabe) {
  					echo "<font color=\"#FF0000\">".$ausgabe."</font><br>\n";
  				}
  			}
  		}
  	}
  }

// Klassen ende
}
?>
