<?php
session_start();
include_once 'classes/TestProjektSmarty.class.php';
require_once("config.inc.php");
$templatename = substr(basename($_SERVER['PHP_SELF']),0,-3)."html";
$smarty = new Smarty();
require_once "language/german.inc.php";

$action = $_GET['action'];


if($action == ''){


# Wenn Seite neu aufgerufen wird, dann alle Sessions, die mit "passwort_" beginnen löschen
if($_GET['new'] == 1){;
  $search_prefix = 'passwort_';
  $search_len = strlen($search_prefix);
  foreach( $_SESSION as $key => $value){
    if ( substr( $key, 0, $search_len) == $search_prefix) {
      unset( $_SESSION[$key]);
    }
  }
}

    if($_GET['error'] == 1){
      $smarty->assign('passwort_pflichtfelder', "1");
      $smarty->assign('passwort_error_text', "Es ist zu einem Fehler gekommen. Bitte f&uuml;llen Sie alle Passwortfelder aus.");
    }
    
    if($_GET['error'] == 2){
      $smarty->assign('passwort_pflichtfelder', "1");
      $smarty->assign('passwort_error_text', "Das neue Passwort stimmt nicht mit der Passwortwiederholung &uuml;berein!");
    }
    
    if($_GET['error'] == 3){
      $smarty->assign('passwort_pflichtfelder', "1");
      $smarty->assign('passwort_error_text', "Das alte Passwort ist falsch!");
    }
    
    $smarty->assign('passwort_pwd_alt', $_SESSION["passwort_pwd_alt"]);
    $smarty->assign('passwort_pwd_neu', $_SESSION["passwort_pwd_neu"]);
    $smarty->assign('passwort_pwd_wied', $_SESSION["passwort_pwd_wied"]);
}

if($action == 'step2'){
  
  if(isset($_POST["pwd_alt"])){
    $pwd_alt = $_POST["pwd_alt"];
    $_SESSION["passwort_pwd_alt"] = $pwd_alt;
  }
  
  if(isset($_POST["pwd_neu"])){
    $pwd_neu = $_POST["pwd_neu"];
    $_SESSION["passwort_pwd_neu"] = $pwd_neu;
  }  
    
  if(isset($_POST["pwd_wied"])){
    $pwd_wied = $_POST["pwd_wied"];
    $_SESSION["passwort_pwd_wied"] = $pwd_wied;
  }  
 

  if($_SESSION["passwort_pwd_alt"] == '' OR $_SESSION["passwort_pwd_neu"] == '' OR $_SESSION["passwort_pwd_wied"] == ''){
    $fehler = 1;
    echo "<meta http-equiv=\"refresh\" content=\"0; URL=".$_SERVER['PHP_SELF']."?error=1\">";
  }
  
  if($_SESSION["passwort_pwd_neu"] != $_SESSION["passwort_pwd_wied"]){
    $fehler = 1;
    echo "<meta http-equiv=\"refresh\" content=\"0; URL=".$_SERVER['PHP_SELF']."?error=2\">";
  }
  
  $db = dbconnect();
  $query1 = $db->query("SELECT count(*) Anz
                           FROM imt_user
                          WHERE imtuid='$uid'
                            AND passwort = md5('$pwd_alt')
                       ") or die(mysql_error()); // Change users to the database where you keep your usernames, and likewise with username
  $row1 = $query1->fetch_array();

  if($row1[Anz] != 1){
    $fehler = 1;
    echo "<meta http-equiv=\"refresh\" content=\"0; URL=".$_SERVER['PHP_SELF']."?error=3\">";
  }
    

  if(!isset($fehler)){
    $pw_neu_md5 = md5($pwd_neu);
    $sql_update = $db->query("UPDATE imt_user
                                  SET passwort='$pw_neu_md5'
                                WHERE imtuid='$uid'");
    if($sql_update){
      $smarty->assign('passwort_success', "1");
    }else{
      echo "Passwort nicht ge&auml;ndert. Systemfehler<br /><br /><br /><br />";
    }
  }
}
$smarty->assign('action', "$action");
$smarty->display("$template/$templatename");

?>