<?php

class USER extends LB{

  private $class_obj;


  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 adduser($user,$pass) {
    $passwort=md5($pass);
    $arr = array($user, $passwort, "", 0, "", "", "Mein Logbuch");
    $this->debug($arr);
    $this->class_obj->insert("INSERT INTO User (user, passwort, dauer, dauereinaus, lid, rolid, lbname) VALUES (?,?,?,?,?,?,?)", $arr);
    $uid = $this->class_obj->lastInsertId();
    $this->class_obj->query("CREATE TABLE logbuch$uid LIKE logbuch1");
    $this->class_obj->query("CREATE TABLE orte$uid LIKE orte1");
    echo "eintragen";
  }


  public function test_id($loginid) {
    $ret = $this->class_obj->select("SELECT id,user,dauer,dauereinaus,lid,rolid,lbname FROM User WHERE lid = '$loginid' ");
    if(count($ret) > 0) {
      #$LB->wlogs("test");
      $_SESSION['user_logged_in'] = "menu";
      $_SESSION['id'] = $ret[0]['id'];
      $_SESSION['user'] = $ret[0]['user'];
      $_SESSION['dauer']  = $ret[0]['dauer'];
      $_SESSION['dauereinaus']  = $ret[0]['dauereinaus'];
      $_SESSION['lid'] = $ret[0]['lid'];
      $_SESSION['readonly'] = false;
      $_SESSION['lbname']  = $ret[0]['lbname'];

      $cont="liste";
      $this->debug($ret);
      $this->debug($_SESSION);
      $array = array($ret[0]['id'],$ret[0]['user'],$ret[0]['lid'],$cont);
      return $array;
    }

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

      $cont="liste";
      $this->debug($ret);
      $this->debug($_SESSION);
      $array = array($ret[0]['id'],$ret[0]['user'],$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, $pass1) {
    $pass=md5($pass1);
    $ret = $this->class_obj->select("SELECT id,user,passwort,dauer,dauereinaus,lid,rolid,lbname FROM User WHERE user = '$user' AND passwort = '$pass'");
    $this->debug($ret);
    $this->debug($user);
    $this->debug($pass);

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

        // return $array;
      }
    }

  }

  public function user_config() {
    if(isset($_POST['lbname'])) {
      $ret = $this->class_obj->update("UPDATE User SET lbname=?  WHERE id = ? LIMIT 1", array($_POST['lbname'],$_SESSION['id']));
      $_SESSION['lbname']  = $_POST['lbname'];
    }
    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,user,passwort,dauer,dauereinaus,lid,rolid,lbname FROM User WHERE id = '".$_SESSION['id']."'");
      $_SESSION['id']   = $ret[0]['id'];
      $_SESSION['user'] = $ret[0]['user'];
      $_SESSION['dauer']  = $ret[0]['dauer'];
      $_SESSION['dauereinaus']  = $ret[0]['dauereinaus'];
      $_SESSION['lid']  = $ret[0]['lid'];
      $_SESSION['rolid']  = $ret[0]['rolid'];
      $_SESSION['lbname']  = $ret[0]['lbname'];

    if(isset($_POST['sub'])) {
      if($_POST['pw'] == $_POST['pw2']) {
        $pass=md5($_POST['pw']);
        $ret = $this->class_obj->update("UPDATE User SET passwort=?  WHERE id = ? LIMIT 1", array($pass,$_SESSION['id']));
      } else {
        echo "Passw&ouml;rter sind verschieden.";
      }
    }
    echo "<h3>".$_SESSION['user']."</h3>\n";
    echo "<br /><hr />\n";
    echo "Logbuch Namen &auml;ndern.<br /><br />\n";
    echo "<form action=\"index.php\" method=\"post\">\n";
    echo "<input type=\"text\" name=\"lbname\" value=\"".$_SESSION['lbname']."\" placeholder=\"Mein Logbuch\">\n";
    echo "<input type=\"submit\" name=\"sub_name\" value=\"&auml;ndern\">\n";
    echo "</form>\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 "<br /><a href=\"https://eblok.de/Logbuch/index.php?LID=".$_SESSION['lid']."\">https://eblok.de/Logbuch/index.php?LID=".$_SESSION['lid']."</a>";
    }
    echo "<br /><br /><br /><a href=\"index.php?genID\">Neue Owner-ID generieren.</a>\n";
    echo "<br /><br />Dadurch &auml;ndert sich der Seitenaufruf.<br />\n";
    echo "<hr />\n";
    if ($_SESSION['rolid'] == "") {
      echo "<span style=\"color: #ff0000;\"><br />Bitte erst die Read-Only Login-ID generieren!<br/></span>";
    } else {
      echo "<br />Link für den Read-Only Seitenaufruf:<br />\n<br />Mit diesem Link werden nur die letzten 20 Eintr&auml;ge angezeigt und es kann nichts ge&auml;ndert werden.<br />";
      echo "<br /><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 "<br /><br /><br /><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
}
?>
