<?php
  ///////////////////////////////////////////////////
  //2009 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////
  require_once "../utils/chlang.php";

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

  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm_film.php";
  require_once "../utils/photo_functions.php";

  if($result2['administrator'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(empty($_POST['login'])) {puterror($result2['skin'],$TEXT['error-login1']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['login'])>255) {puterror($result2['skin'],$TEXT['error-login2']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['familiya'])>255) {puterror($result2['skin'],$TEXT['error-login4']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['password'])>100) {puterror($result2['skin'],$TEXT['error-login5']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['password2'])>100) {puterror($result2['skin'],$TEXT['error-login6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['name'])>255) {puterror($result2['skin'],$TEXT['error-login8']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['administrator'])>5) {puterror($result2['skin'],$TEXT['error-login9']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['user'])>5) {puterror($result2['skin'],$TEXT['error-login20']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['otchestvo'])>255) {puterror($result2['skin'],$TEXT['error-login7']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['phone'])>40) {puterror($result2['skin'],$TEXT['error-login9']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['dom_id'])>5) {puterror($result2['skin'],$TEXT['error-domain6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['icq'])>20) {puterror($result2['skin'],$TEXT['error-login18']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['address'])>255) {puterror($result2['skin'],$TEXT['error-login20']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['skin'])>100) {puterror($result2['skin'],$TEXT['error-login21']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['skin2'])>100) {puterror($result2['skin'],$TEXT['error-login21']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['mail'])>40) {puterror($result2['skin'],$TEXT['error-login22']); require_once "../utils/bottom_adm_film.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_film.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_film.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_film.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['icq'] = rep($_POST['icq']);
  $_POST['address'] = rep($_POST['address']);
  $_POST['skin'] = rep($_POST['skin']);
  $_POST['skin2'] = rep($_POST['skin2']);
  $_POST['administrator'] = rep($_POST['administrator']);
  $_POST['user'] = rep($_POST['user']);

    if($_FILES['photo']['size'] > 1024*2*1024) {puterror($result2['skin'],$TEXT['error-pic3']); require_once "../utils/bottom_adm_film.php"; exit();}

    if(!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");

       $name = $_POST['login'];
       $name_ = "sm_".$_POST['login'];

       $image = "../photo/$name".date("YmdHis",time())."$ext";
       $smallimage = "../photo/$name_".date("YmdHis",time())."$ext";

          if(copy($_FILES['photo']['tmp_name'], $image))
            {
            unlink($_FILES['photo']['tmp_name']);
            chmod($image, 0644);
            }
            if(!resizeimg($image, $smallimage, 130, 185))
           {
            puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm_film.php"; exit();
           }
           if(is_file($image)) {unlink($image);}
     }
      else {$smallimage = "";}

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

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

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

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-login1'];
  $what = $_POST['login'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
  $query = "INSERT INTO `tbl_logins` (
  `login_id`,
  `login`,
  `pass`,
  `administrator`,
  `user`,
  `skin`,
  `skin2`,
  `familiya`,
  `name`,
  `otchestvo`, 
  `phone`, 
  `e_mail`, 
  `dom_id`, 
  `icq`, 
  `photo_user`, 
  `address_user`)
VALUES ( 
  NULL ,
  '".$_POST['login']."',
  '$passwd',
  '$administrator',
  '$user',
  '".$_POST['skin']."',
  '".$_POST['skin2']."',
  '".$_POST['familiya']."',
  '".$_POST['name']."',
  '".$_POST['otchestvo']."',
  '".$_POST['phone']."',
  '".$_POST['mail']."',
  '".$_POST['dom_id']."',
  '".$_POST['icq']."',
  '$smallimage',
  '".$_POST['address']."' )";
  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_film.php"; exit();}
  } else {puterror($result2['skin'],$TEXT['error-login13']); require_once "../utils/bottom_adm_film.php"; exit();}
  require_once "../utils/bottom_adm_film.php";
?>