<?php
session_start();
if (!$_SESSION['autorizovan'] == 1) {
    // proveri da li je izvrsena autorizacija
    // ako nije, zavrsi sa greskom
    echo "<a href='login.php'>Strana za logovanje</a><br /><br />";
    die ("GRESKA: Neautorizovan pristup!");
}

// ime XML fajla
$fajl = "nalozi.xml";
// load file
$xml = simplexml_load_file($fajl) or die ("Ne mogu da ucitam XML fajl $fajl!");
$oldpass = "";
foreach ($xml->user as $user){
       if($user->username == $_SESSION['user']){
           $oldpass = $user->password;
       }
}

echo "<form style='text-align:center' method='POST' action='".$_SERVER['PHP_SELF']."'>";  
echo "Stara lozinka: <input type='text' name='oldpass' value='$oldpass'><br />";   
echo "Nova lozinka: <input type='text' name='newpass' value=''><br />";   
echo "<input type='submit' name='submit' value='Posalji'>";  
echo "</form>";

if (isset($_POST['submit'])){
    if($_POST['oldpass']=="")
        echo "stara sifra se mora uneti<br />";
    elseif($_POST['newpass']=="")
        echo "nova sifra se mora uneti";
    elseif(preg_match("/^[0-9]{2}_[a-zA-Z]+[0-9]*$/", $_POST['newpass']))
    foreach ($xml->user as $user){
       if($user->username == $_SESSION['user']){
           $user->password = $_POST['newpass'];
           file_put_contents($fajl, $xml->asXML());
       }
    }
    else 
        echo "neuspesan unos";
    
    
}
  
?>