<?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['exusr-title'];
  $helppage = "";

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

  if($result2['administrator'] == 0) puterror3($TEXT['error-loginform4']);

  if(empty($_POST['user_id'])) $_POST['user_id'] = "0";
  if(strlen($_POST['user_id'])>11) puterror3($TEXT['error-user2']);
  if(empty($_POST['id'])) puterror3($TEXT['exusr-error17']);
  if(strlen($_POST['id'])>200) puterror3($TEXT['exusr-error18']);
  if(empty($_POST['mbox_host'])) puterror3($TEXT['error-domain1']);
  if(strlen($_POST['mbox_host'])>128) puterror3($TEXT['error-domain2']);
  if(empty($_POST['passwd'])) puterror3($TEXT['error-login10']);
  if(strlen($_POST['passwd'])>255) puterror3($TEXT['error-login5']);
  if(strlen($_POST['name'])>255) puterror3($TEXT['error-user14']);
  if(strlen($_POST['uid'])>10) puterror3($TEXT['exusr-error1']);
  if(empty($_POST['uid'])) puterror3($TEXT['exusr-error2']);
  if(strlen($_POST['gid'])>10) puterror3($TEXT['exusr-error4']);
  if(empty($_POST['gid'])) puterror3($TEXT['exusr-error5']);
  if(strlen($_POST['shl'])>32) puterror3($TEXT['exusr-error7']);
  if(empty($_POST['shl'])) puterror3($TEXT['exusr-error8']);
  if(strlen($_POST['hom'])>128) puterror3($TEXT['exusr-error10']);
  if(empty($_POST['hom'])) puterror3($TEXT['exusr-error11']);
  if(strlen($_POST['kvota'])>7) puterror3($TEXT['exusr-error13']);
  if(empty($_POST['kvota'])) puterror3($TEXT['exusr-error14']);
  if(strlen($_POST['visible'])>3) puterror3($TEXT['exusr-error16']);

  if(isset($_POST['id'])) $id=trim($_POST['id']);
    else $id="";
     if (empty($id)) puterror3($TEXT['exusr-error1']);

  if(isset($_POST['mbox_host'])) $mbox_host=trim($_POST['mbox_host']);
    else $mbox_host="";
     if (empty($mbox_host)) puterror3($TEXT['error-domain3']);

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

  if(isset($_POST['uid'])) $uid=trim($_POST['uid']);
    else $uid="";
     if (empty($uid)) puterror3($TEXT['exusr-error3']);

  if(isset($_POST['gid'])) $gid=trim($_POST['gid']);
    else $gid="";
     if (empty($gid)) puterror3($TEXT['exusr-error6']);

  if(isset($_POST['shl'])) $shl=trim($_POST['shl']);
    else $shl="";
     if (empty($shl)) puterror3($TEXT['exusr-error9']);

  if(isset($_POST['hom'])) $hom=trim($_POST['hom']);
    else $hom="";
     if (empty($hom)) puterror3($TEXT['exusr-error12']);

  if(isset($_POST['kvota'])) $kvota=trim($_POST['kvota']);
    else $kvota="";
     if (empty($kvota)) puterror3($TEXT['exusr-error15']);

  $_POST['user_id'] = rep($_POST['user_id']);
  $_POST['id'] = rep($_POST['id']);
  $_POST['mbox_host'] = rep($_POST['mbox_host']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['passwd'] = rep($_POST['passwd']);
  $_POST['uid'] = rep($_POST['uid']);
  $_POST['gid'] = rep($_POST['gid']);
  $_POST['shl'] = rep($_POST['shl']);
  $_POST['hom'] = rep($_POST['hom']);
  $_POST['kvota'] = rep($_POST['kvota']);
  $_POST['visible'] = rep($_POST['visible']);

  if($_POST['visible'] == "on") $visible = "Y";
    else $visible = "N";

     $query3 = "SELECT * FROM `domains` WHERE `domain`='".$_POST['mbox_host']."'";
     $result3 = mysql_query($query3);
     if(!$result3) puterror3($TEXT['error-tbl27']);
       if(mysql_num_rows($result3)<1) puterror3($TEXT['exusr-error19']);

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-ex-mb-add'];
  $what = $_POST['id']."@".$_POST['mbox_host'];
  $who = $result2['fullname'];
  //*************************************
  $query = "INSERT INTO users ( `id` , `name`, `user_id`, `crypt` , `passwd`, `uid`, `gid`, `mbox_host`, `shell`, `home`, `quota`, `active` )
            VALUES ( '".$_POST['id']."', '".$_POST['name']."', '".$_POST['user_id']."', ENCRYPT('".$_POST['passwd']."'), '".$_POST['passwd']."', '".$_POST['uid']."', '".$_POST['gid']."', '".$_POST['mbox_host']."', '".$_POST['shl']."', '".$_POST['hom']."', '".$_POST['kvota']."', '$visible' ) ";

  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))
           {
            $organizaciya = $TEXT['admin-podpis']."<br />".$TEXT['exusr-mail'];
            $address = $_POST['id']."@".$_POST['mbox_host'].",".$TEXT['admin-mail'];
            $address_to_mail = $_POST['id']."@".$_POST['mbox_host'];
            $podpis = $organizaciya." ".date("D, d-M-Y H:i:s", time());
            $tema = $TEXT['exusr-mail-tema'];
            $msg = "<b>".$TEXT['user-e-mail']." ".$address_to_mail."&nbsp;".$TEXT['exusr-act9']."</b><br /><br />".$TEXT['exusr-act10']."".$_POST['name']."<br />".$TEXT['exusr-kvota'].":&nbsp;".$_POST['kvota']."<br /><hr />".$podpis."";
             $podpis = stripslashes($podpis);
             $tema = stripslashes($tema);
             $msg = nl2br(stripslashes($msg));

              @mail($address, $tema, $msg, join("\r\n", array(
              "From: phonebook@".$_POST['mbox_host']."",
              "MIME-Version: 1.0",
              "Content-type: text/html; charset=utf-8",
              "Content-Transfer-Encoding: 8bit",
              "Date: ".date("D, d-M-Y H:i:s", time())."",
              "X-Mailer: Phone Book, version 2.3.5"
              )));

             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
                   </head></html>";
           } else puterror3($TEXT['error-log']);
  } else puterror3($TEXT['exusr-error20']);
  require_once "../../util/bottom.php";
?>
