<?php
  ///////////////////////////////////////////////////
  //2007 (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=$titlepage = $TEXT['login-title'];
  $helppage = "";

  require_once "../config.php";
  require_once "../util/top.php";

  if($result2['administrator'] == 0) puterror2($TEXT['error-loginform2']);

  if(empty($_POST['login'])) puterror2($TEXT['error-login1']);
  if(strlen($_POST['login'])>30) puterror2($TEXT['error-login2']);
  if(strlen($_POST['fullname'])>255) puterror2($TEXT['error-login4']);
  if(strlen($_POST['password'])>300) puterror2($TEXT['error-login5']);
  if(strlen($_POST['password2'])>300) puterror2($TEXT['error-login6']);
  if(strlen($_POST['moderator'])>5) puterror2($TEXT['error-login8']);
  if(strlen($_POST['administrator'])>5) puterror2($TEXT['error-login9']);
  if(strlen($_POST['user'])>5) puterror2($TEXT['error-login20']);
  if(strlen($_POST['manager'])>5) puterror2($TEXT['error-login21']);
  if(strlen($_POST['master'])>5) puterror2($TEXT['error-login22']);
  if(strlen($_POST['otvetstvenniy'])>5) puterror2($TEXT['error-login24']);
  if(strlen($_POST['user_id'])>250) puterror2($TEXT['error-login14']);
  if(strlen($_POST['iduser'])>5) puterror2($TEXT['error-login23']);

  if(isset($_POST['login'])) $login=trim($_POST['login']);
    else $login="";
     if (empty($login)) puterror2($TEXT['error-login3']);

  if(isset($_POST['password'])) $password=trim($_POST['password']);
    else $password="";
     if (empty($password)) puterror2($TEXT['error-login10']);

  if(isset($_POST['password2'])) $password2=trim($_POST['password2']);
    else $password2="";
     if (empty($password2)) puterror2($TEXT['error-login11']);

  $_POST['login'] = rep($_POST['login']);
  $_POST['fullname'] = rep($_POST['fullname']);
  $_POST['password'] = rep($_POST['password']);
  $_POST['password2'] = rep($_POST['password2']);
  $_POST['moderator'] = rep($_POST['moderator']);
  $_POST['administrator'] = rep($_POST['administrator']);
  $_POST['user'] = rep($_POST['user']);
  $_POST['manager'] = rep($_POST['manager']);
  $_POST['master'] = rep($_POST['master']);
  $_POST['otvetstvenniy'] = rep($_POST['otvetstvenniy']);

  if($_POST['moderator'] == "on") $moderator = "1";
    else $moderator = "0";

  if($_POST['administrator'] == "on") $administrator = "1";
    else $administrator = "0";

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

  if($_POST['manager'] == "on") $manager = "1";
    else $manager = "0";

  if($_POST['master'] == "on") $master = "1";
    else $master = "0";

  if($_POST['otvetstvenniy'] == "on") $otvetstvenniy = "1";
    else $otvetstvenniy = "0";

  if($_POST['iduser'] == "on") $iduser = $_POST['user_id'];
    else $iduser = "0";

  if($_POST['password'] == $_POST['password2']) $passwd = md5($_POST['password']);
    else puterror2($TEXT['error-login12']);

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

  $query_chk = "SELECT `login` FROM `tbl_logins` WHERE `login`='".$_POST['login']."' ";
     $result_chk = mysql_query($query_chk);
     if(!$result_chk) puterror2($TEXT['error-tbl20']);
       if(mysql_num_rows($result_chk)>0) puterror2($TEXT['global-est']);

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-login1'];
  $what = $_POST['login'];
  $who = $result2['fullname'];
  //*************************************
  $query = "INSERT INTO `tbl_logins` ( `login_id` , `login` , `fullname` , `pass` , `moderator` , `administrator` , `user` , `manager` , `master` , `otvetstvenniy` , `usr_id`, `chk_upd`)
      VALUES ( NULL , '".$_POST['login']."' , '".$_POST['fullname']."' , '$passwd' , '$moderator' , '$administrator' , '$user' , '$manager' , '$master' , '$otvetstvenniy' , '$iduser', '$chk_upd' )";
  if(mysql_query($query))
  {
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-login13']);
  require_once "../util/bottom.php";
?>