<?php
  ///////////////////////////////////////////////////
  //2008 (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['alias-ed-tit'];
  $helppage = "";

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

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

  if(empty($_POST['local_part'])) puterror3($TEXT['alias-error1']);
  if(strlen($_POST['local_part'])>65) puterror3($TEXT['alias-error2']);
  if(empty($_POST['domain'])) puterror3($TEXT['error-domain1']);
  if(strlen($_POST['domain'])>200) puterror3($TEXT['error-domain2']);
  if(empty($_POST['recipients'])) puterror3($TEXT['alias-error3']);
  if(strlen($_POST['recipients'])>2001) puterror3($TEXT['alias-error4']);
  if(empty($_POST['old_address'])) puterror3($TEXT['alias-error1']);
  if(strlen($_POST['old_address'])>65) puterror3($TEXT['alias-error2']);

  if(isset($_POST['local_part'])) $local_part=trim($_POST['local_part']);
    else $local_part="";
     if (empty($local_part)) puterror3($TEXT['alias-error5']);

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

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

  if(isset($_POST['old_address'])) $old_address=trim($_POST['old_address']);
    else $old_address="";
     if (empty($old_address)) puterror3($TEXT['alias-error5']);

  $_POST['old_address'] = rep($_POST['old_address']);
  $_POST['local_part'] = rep($_POST['local_part']);
  $_POST['domain'] = rep($_POST['domain']);
  $_POST['recipients'] = rep($_POST['recipients']);

  //*************************************
  $query = "SELECT * FROM `aliases` WHERE `local_part`='".$_POST['old_address']."' ";
  $result = mysql_query($query);
  if (!$result) puterror3($TEXT['error-tbl32']);
      $result2 = mysql_fetch_array($result);

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-alias-ed'];
  $what = $result2['local_part']."@".$result2['domain']." (".$result2['recipients'].") => ".$_POST['local_part']."@".$_POST['domain']." (".$_POST['recipients'].")";
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `aliases` SET `local_part` = '".$_POST['local_part']."', `domain` = '".$_POST['domain']."', `recipients` = '".$_POST['recipients']."' WHERE `local_part`='".$_POST['old_address']."' LIMIT 1";
  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 puterror3($TEXT['error-log']);
  } else puterror3($TEXT['error-tbl32']);
  require_once "../../util/bottom.php";
?>
