<?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,lid 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['lid'] = $ret[0]['lid'];
      $this->debug($ret);
      $this->debug($_SESSION);
      $array = array($ret[0]['id'],$ret[0]['name'],$ret[0]['lid']);
      return $array;
    }
    // else {
    //   $_SESSION['user_logged_in'] = false;
    //   $his->user_login();
    // }
  }

  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,lid 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['lid']  = $ret[0]['lid'];
        $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']));
      $ret = $this->class_obj->select("SELECT id,name,passwort,lid FROM User WHERE id = '".$_SESSION['id']."'");
      $_SESSION['id']   = $ret[0]['id'];
      $_SESSION['user'] = $ret[0]['name'];
      $_SESSION['lid']  = $ret[0]['lid'];
    }
    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 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.ddnss.de/Logbuch/index.php?LID=".$_SESSION['lid']."\">https://eblok.ddnss.de/Logbuch/index.php?LID=".$_SESSION['lid']."</a>";
    }
    echo "<br /><br /><hr />\n";
    echo "<a href=\"index.php?genID\">Neue 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
}
?>
