<?php
session_start();
ini_set('max_execution_time', 300000);
include("include/setsession.php");
include("include/functions.php");
require_once("include/connect.inc");
mysqli_query($link, "SET character_set_results=utf8");

$txt="0";
$id = 0;
$lastname="";
$name="";
$email="";
$usr="";
$pwd="";
$pwdold="";
$idlevel=0;
$enabled=0;

$idchannel=0;
$genmsg=0;
$language='';
$able_edit = 0;
$tb_width = 0;
$tb_height = 0;
$able_multiuser = 0;

if (isset($_POST['id']) && $_POST['id']!='') 				$id = $_POST['id'];
if (isset($_POST['lastname']) && $_POST['lastname']!='') 	$lastname = $_POST['lastname'];
if (isset($_POST['name']) && $_POST['name']!='') 			$name = $_POST['name'];
if (isset($_POST['email']) && $_POST['email']!='') 			$email = $_POST['email'];
if (isset($_POST['email']) && $_POST['email']!='') 			$usr = $_POST['email'];
if (isset($_POST['pwd']) && $_POST['pwd']!='') 				$pwd = $_POST['pwd'];
if (isset($_POST['pwdold']) && $_POST['pwdold']!='')		$pwdold = $_POST['pwdold'];
if (isset($_POST['idlevel']) && $_POST['idlevel']!='') 		$idlevel = $_POST['idlevel'];
if (isset($_POST['enabled']) && $_POST['enabled']!='') 		$enabled = $_POST['enabled'];

$tmppwd="";
if($pwd!=$pwdold) $tmppwd=sha1($pwd);
else $tmppwd=$pwdold;

if($id==0) {
	echo 'here1';
	$id=1;
	$sql="SELECT MAX(id) AS maxid FROM ".$s_prefix."_tb_users";
	$val = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
	if($res = mysqli_fetch_array($val, MYSQLI_ASSOC)) {
		$id=($res['maxid']+1);
	}
	
	//$sql="INSERT INTO ".$s_prefix."_tb_users (id, lastname, name, email, password, level, enabled) VALUE (".$id.", '".checkStringa($lastname)."', '".checkStringa($name)."', '".checkStringa($email)."', '".checkStringa($tmppwd)."', ".$idlevel.", ".$enabled.")";
	$sql="INSERT INTO ".$s_prefix."_tb_users
	 (id, lastname, name, usr, pwd, userlevel, idchannel, genmsg, tb_width, tb_height, language, able_edit) VALUES 
	 (".$id.", '".checkStringa($lastname)."', '".checkStringa($name)."', '".$usr."', '".checkStringa($tmppwd)."', ".$idlevel.", ".$idchannel.", 
	 ".$genmsg.", ".$tb_width.", ".$tb_height.", '".$language."', ".$able_edit.")";

	 $res = mysqli_query($link, $sql) or die('Query failed: '.$sql.'<br>'.mysqli_error($link));
} else {
	echo 'here2';
	//$sql="UPDATE ".$s_prefix."_tb_users SET lastname='".checkStringa($lastname)."', name='".checkStringa($name)."', email='".checkStringa($email)."', password='".checkStringa($tmppwd)."', level=".$idlevel.", enabled=".$enabled." WHERE id=".$id;
	$tmp=",pwd='".$tmppwd."'";
	$sql="UPDATE ".$s_prefix."_tb_users SET lastname='".checkStringa($lastname)."', name='".checkStringa($name)."', usr='".$usr."'".$tmp.", 
	userlevel=".$idlevel.", idchannel=".$idchannel.", genmsg=".$genmsg.", tb_width=".$tb_width.", tb_height=".$tb_height.", 
	language='".$language."', able_edit=".$able_edit."  WHERE id=".$id;
	$res = mysqli_query($link, $sql) or die('Query failed: '.$sql.'<br>'.mysqli_error($link));

	$sql="DELETE FROM ".$s_prefix."_editionsuser WHERE iduser=".$id;
	$res = mysqli_query($link, $sql) or die('Query failed: '.$sql.'<br>'.mysqli_error($link));
}

// TIPO EDIZIONI ABILITATI
$type=$_POST['type'];
while (list ($key,$val) = @each ($type)) {
	$sql="INSERT INTO ".$s_prefix."_editionsuser (iduser, idedition) VALUE (".$id.", ".$val.")";
	$values = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
}

header("Location: index.php?sz=1");
?>