<?php
 include("config/config.inc.php");
 include("zebra_form/zebra_form.php");
 
 $UserId = $_SESSION['userId'];
 


 $mydb= new DB_MySQL($db_name,$db_user,$db_pass,$db_server);
 $mydb->query("SELECT * FROM AdminUser as u LEFT JOIN AdminUserInformation as i ON u.userId = i.userId LEFT JOIN AdminAuthInformation as a ON u.authId = a.authId WHERE u.userId = $UserId");

 $row = $mydb->fetchRow();

 $Login = $row['userLogin'];
 $UserName = $row['userName'];
 $AuthText = $row['authText'];  
 $Email = $row['eMail'];
 $Password = $row['userPassword'];    

 echo "<h3>Login: $Login</h3>";

 $form = new Zebra_Form('form');
 
 $form->language('deutsch');
 
 $form->add('label', 'label_name', 'name', 'Benutzername:');
 $obj = $form->add('text', 'name');
 $obj->set_attributes(['value' => $UserName]);
 $obj->set_rule(array(
    // error messages will be sent to a variable called "error", usable in custom templates
    'required' => array('error', 'Name is required!'),
 ));

// "email"
    $form->add('label', 'label_email', 'email', 'E-Mailadresse:');
    $obj = $form->add('text', 'email');
    $obj->set_attributes(['value' => $Email]);
    $obj->set_rule(array(
        'required'  =>  array('error', 'Email is required!'),
        'email'     =>  array('error', 'Email address seems to be invalid!'),
    ));

    // "upload"
    // add a file upload control to the form
    $obj = $form->add('file','my_file_upload');
   
    $obj->set_rule(array(
      'upload'    =>  array('/tmp', ZEBRA_FORM_UPLOAD_RANDOM_NAMES, 'error', 'Could not upload file!'),
      'filetype'  =>  array('png', 'error', 'File must be a PNG/JPG-Image!'),
      'filesize'  =>  array(102400, 'error', 'File size must not exceed 100Kb!'),
    ));
    
// "password"
    $form->add('label', 'label_password', 'password', 'neues Passwort:');
    $obj = $form->add('password', 'password');
    $obj->set_rule(array(
        'length'    => array(6, 10, 'error', 'The password must have between 6 and 10 characters'),
    ));
    $form->add('note', 'note_password', 'password', 'Password must be have between 6 and 10 characters.', array('style' => 'width: 180px'));

    // "confirm password"
    $form->add('label', 'label_confirm_password', 'confirm_password', 'Passwort bestätigen:');
    $obj = $form->add('password', 'confirm_password');
    $obj->set_rule(array(
        'compare' => array('password', 'error', 'Password not confirmed correctly!')
    ));
    

 // "submit"
 $form->add('submit', 'btnsubmit', 'Änderung übernehmen');
  
  
if ($form->validate())
{
   
   
    $changed = 0;
    $formName =   $mydb-> mysql_escape_string($_POST['name']);
    $formEmail =  $mydb-> mysql_escape_string($_POST['email']);
    
    
    $formPassword =  $mydb-> mysql_escape_string($_POST['password']);
    $p = sha1($formPassword, true);
    $p = sha1($p);
    $formPasswordMysql41 = '*'. strtoupper($p);
    
    //DB-Update nur wenn sich die Werte geändert haben
    if ( (strcmp($formName,$UserName) != 0) || (strcmp($formEmail,$Email) != 0) )
    {
      $mydb->query("UPDATE AdminUserInformation SET userName = '$formName', eMail = '$formEmail', lastUpdate = now() WHERE userId='$UserId' ");
      $changed = 1;
    }
     
    if ( (strcmp($formPasswordMysql41,$Password) != 0) && (strlen($formPassword) > 0))
    {
      //Datenbankupdate
      $mydb->query("UPDATE AdminUser SET userPassword = '$formPasswordMysql41', lastUpdate = now() WHERE userId='$UserId' ");
      $changed = 1;
    } 
    
    if($changed)
    {
      $InfoBoxContent = "<div class=\"alert alert-success alert-dismissable\">";
      $InfoBoxContent .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
      $InfoBoxContent .= "Benutzerdaten wurden geändert !</div>";
      echo $InfoBoxContent;
    }
     
    if($form->file_upload)
    {
      //DEBUG: print_r($form->file_upload);
 echo $form->file_upload;
      $tempFilePath = $form->file_upload[my_file_upload][path];
      $tempFileName = $form->file_upload[my_file_upload][file_name];
   
      $imagedata = addslashes(file_get_contents($tempFilePath.$tempFileName));
     
      $mydb->query("UPDATE AdminUserInformation SET UserImage = '$imagedata', lastUpdate = now() WHERE userId='$UserId' ");
      
      $InfoBoxContent = "<div class=\"alert alert-success alert-dismissable\">";
      $InfoBoxContent .= "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
      $InfoBoxContent .= "Benutzerbild wurde geändert - zum aktuallisieren bitte neu einloggen !</div>";
      echo $InfoBoxContent;
    }
     
    
    $form->render('templates/ProfilForm.tpl');
 
 } 
 else
 {
      $form->render('templates/ProfilForm.tpl');
 }
 

 $mydb->disconnect();
?>
