<?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.php";
  require_once "../utils/photo_functions.php";

  if(is_file ("../lang/".$lang."/trans.php")) {require_once "../lang/".$lang."/trans.php";}
   else {require_once "../lang/ru/trans.php";}

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

  if(empty($_POST['name'])) {puterror($result2['skin'],$TEXT['error-aut9']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['name'])>255) {puterror($result2['skin'],$TEXT['error-aut8']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['familiya'])) {puterror($result2['skin'],$TEXT['error-aut3']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['familiya'])>255) {puterror($result2['skin'],$TEXT['error-aut4']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['otchestvo'])>255) {puterror($result2['skin'],$TEXT['error-aut7']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['data_rozdeniya'])>4){puterror($result2['skin'],$TEXT['error-aut5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['mesto_rozdeniya'])>255){puterror($result2['skin'],$TEXT['error-aut2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['real_name'])>255) {puterror($result2['skin'],$TEXT['error-aut6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['tip_pr'])>5) {puterror($result2['skin'],$TEXT['error-aut10']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['addr'])>255) {puterror($result2['skin'],$TEXT['error-aut11']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['photo_obr'])>1) {puterror($result2['skin'],$TEXT['error-aut12']); require_once "../utils/bottom_adm.php"; exit();}

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

  if(isset($_POST['data_rozdeniya'])) {$data_r=trim($_POST['data_rozdeniya']);}
    else {$data_r="1970";}
     if(empty($data_r)) {$data_r="1970";}

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

  $_POST['familiya'] = rep($_POST['familiya']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['otchestvo'] = rep($_POST['otchestvo']);
  $_POST['data_rozdeniya'] = rep($_POST['data_rozdeniya']);
  $_POST['mesto_rozdeniya'] = rep($_POST['mesto_rozdeniya']);
  $_POST['real_name'] = rep($_POST['real_name']);
  $_POST['tip_pr'] = rep($_POST['tip_pr']);
  $_POST['addr'] = rep($_POST['addr']);
  $_POST['photo_obr'] = rep($_POST['photo_obr']);

  $first_bt = substr($_POST['familiya'], 0,1);
    if(preg_match("/[A-Z]/i", $first_bt)) {$first_b = substr($_POST['familiya'], 0,1);}
       else {$first_b = substr($_POST['familiya'], 0,2);}

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

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

       $name = translate($_POST['familiya']);
       $name_ = "sm_".translate($_POST['familiya']);

       $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($_POST['photo_obr'] == '1') {
                 $smallimage = $image;
            }
            if($_POST['photo_obr'] == '2') {
                 if(!resizeimg($image, $smallimage, 130, 185))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
            if($_POST['photo_obr'] == '3') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '4') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '5') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '6') {
                 if(!resizeimg_l_r($image, $smallimage, 130, 185))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '7') {
                 if(!resizeimg_r($image, $smallimage, 185, 130))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '8') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '9') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($image)) {unlink($image);}
            }
     }
      else {$smallimage = "";}

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

  $query = "INSERT INTO `tbl_autor` (
  `autor_id` ,
  `name` ,
  `familiya` ,
  `otchestvo` ,
  `data_rozdeniya` ,
  `mesto_rozdeniya` ,
  `real_name` ,
  `photo_autor` ,
  `first_bt`,
  `site_protocol` ,
  `site_name`)
VALUES ( 
  NULL ,
  '".$_POST['name']."',
  '".$_POST['familiya']."',
  '".$_POST['otchestvo']."',
  '$data_r',
  '".$_POST['mesto_rozdeniya']."',
  '".$_POST['real_name']."',
  '$smallimage',
  '$first_b',
  '".$_POST['tip_pr']."',
  '".$_POST['addr']."')";
  //print_r($query);
  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'],$TEXT['error-aut13']); 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";
?>