<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang."/".$lang.".php")) {include "../lang/".$lang."/".$lang.".php";}
     else {include "../lang/ru/ru.php";}

  $title = $TEXT['login-title-add'];

  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm.php";

  if($result2['administrator'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm.php"; exit();}

  if(empty($_POST['login'])) {puterror($result2['skin'],$TEXT['error-login1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['login'])>255) {puterror($result2['skin'],$TEXT['error-login2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['familiya'])>255) {puterror($result2['skin'],$TEXT['error-login4']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['password'])>100) {puterror($result2['skin'],$TEXT['error-login5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['password2'])>100) {puterror($result2['skin'],$TEXT['error-login6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['name'])>255) {puterror($result2['skin'],$TEXT['error-login8']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['administrator'])>5) {puterror($result2['skin'],$TEXT['error-login9']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['user'])>5) {puterror($result2['skin'],$TEXT['error-login20']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['otchestvo'])>255) {puterror($result2['skin'],$TEXT['error-login7']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['phone'])>40) {puterror($result2['skin'],$TEXT['error-login9']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['dom_id'])>5) {puterror($result2['skin'],$TEXT['error-domain6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['skin'])>100) {puterror($result2['skin'],$TEXT['error-login21']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['mail'])>40) {puterror($result2['skin'],$TEXT['error-login22']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['fltr_size'])>4) {puterror($result2['skin'],$TEXT['error-login23']); require_once "../utils/bottom_adm.php"; exit();}

  if(isset($_POST['login'])) {$login=trim($_POST['login']);}
    else {$login="";}
     if(empty($login)) {puterror($result2['skin'],$TEXT['error-login3']); require_once "../utils/bottom_adm.php"; exit();}

  if(isset($_POST['password'])) {$password=trim($_POST['password']);}
    else {$password="";}
     if(empty($password)) {puterror($result2['skin'],$TEXT['error-login10']); require_once "../utils/bottom_adm.php"; exit();}

  if(isset($_POST['password2'])) {$password2=trim($_POST['password2']);}
    else {$password2="";}
     if(empty($password2)) {puterror($result2['skin'],$TEXT['error-login11']); require_once "../utils/bottom_adm.php"; exit();}

  $_POST['login'] = rep($_POST['login']);
  $_POST['familiya'] = rep($_POST['familiya']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['otchestvo'] = rep($_POST['otchestvo']);
  $_POST['password'] = rep($_POST['password']);
  $_POST['password2'] = rep($_POST['password2']);
  $_POST['phone'] = rep($_POST['phone']);
  $_POST['mail'] = rep($_POST['mail']);
  $_POST['dom_id'] = rep($_POST['dom_id']);
  $_POST['skin'] = rep($_POST['skin']);
  $_POST['administrator'] = rep($_POST['administrator']);
  $_POST['user'] = rep($_POST['user']);
  $_POST['fltr_size'] = rep($_POST['fltr_size']);
  $_POST['fltr_size'] = intval($_POST['fltr_size']);

  $query9 = "SELECT `login` FROM `tbl_logins` WHERE `login`='".$_POST['login']."' ";
     $result9 = mysqli_query($dbcnx,$query9);
     if (!$result9) {puterror($result2['skin'],$TEXT['error-tbl4']); require_once "../utils/bottom_adm.php"; exit();}
       if (mysqli_num_rows($result9)>0) {puterror($result2['skin'],$TEXT['error-double']); require_once "../utils/bottom_adm.php"; exit();}

  //Проверим принадлежность к группе
   if($_POST['administrator'] == "on") {$administrator = "1";}
    else {$administrator = "0";}

  if($_POST['user'] == "on") {$user = "1";}
    else {$user = "0";}

  if($_POST['chk_upd'] == "on") {$chk_upd = "yes";}
    else {$chk_upd = "no";}

  //Проверяем правильность пароля и вычисляем его для хранения в базе
  if($_POST['password'] == $_POST['password2']) {$passwd = md5($_POST['password']);}
    else {puterror($result2['skin'],$TEXT['error-login12']); require_once "../utils/bottom_adm.php"; exit();}

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-login1'];
  $what = $_POST['login']." (".$_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo'].")";
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************

  $query = "INSERT INTO `tbl_logins` (
  `login_id`,
  `login`,
  `pass`,
  `administrator`,
  `user`,
  `skin` ,
  `familiya`,
  `name`,
  `otchestvo`, 
  `phone`, 
  `e_mail`, 
  `dom_id`,
  `fltr_size`,
  `chk_upd`,
  `maintenance`)
VALUES ( 
  NULL ,
  '".$_POST['login']."',
  '$passwd',
  '$administrator',
  '$user',
  '".$_POST['skin']."',
  '".$_POST['familiya']."',
  '".$_POST['name']."',
  '".$_POST['otchestvo']."',
  '".$_POST['phone']."',
  '".$_POST['mail']."',
  ".$_POST['dom_id'].",
  ".$_POST['fltr_size'].",
  '".$chk_upd."',
  'no')";
  if(mysqli_query($dbcnx,$query))
  {
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
                   </head></html>";
           } else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
  } else {puterror($result2['skin'],mysqli_error($dbcnx)); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>