<?php
  ///////////////////////////////////////////////////
  //2011 (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['supp-tip-title3'];
  $helppage = "";

  require_once "../lang/".$lang."/trans.php";
  require_once "../config.php";
  require_once "../util/top_layer1.php";

  if($result2['user'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);

  if(empty($_POST['tip_id'])) puterror2($TEXT['error-tip5']);
  if(strlen($_POST['tip_id'])>11) puterror2($TEXT['error-tip4']);
  if(empty($_POST['tip_name'])) puterror2($TEXT['error-tip1']);
  if(strlen($_POST['tip_name'])>255) puterror2($TEXT['error-tip2']);
  if(empty($_POST['position'])) puterror2($TEXT['error-nd4']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-nd5']);
  if(strlen($_POST['prefix'])>255) puterror2($TEXT['error-tip9']);
  if(strlen($_POST['old_photo'])>250) puterror2($TEXT['error-inst9']);

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

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

  $_POST['tip_name'] = rep($_POST['tip_name']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['prefix'] = rep($_POST['prefix']);
  $_POST['tip_id'] = rep($_POST['tip_id']);
  $_POST['old_photo'] = rep($_POST['old_photo']);

    if($_FILES['photo']['size'] > 1024*2*1024) puterror2($TEXT['error-pic3']);
    if(!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $name = $_POST['tip_name'];
       $tip = "tip_".translate($name)."_";
       $image = "../photo/$tip".date("YmdHis",time())."$ext";
          if(copy($_FILES['photo']['tmp_name'], "../".$image))
            {
            unlink($_FILES['photo']['tmp_name']);
            chmod("../".$image, 0644);
            }
            if(is_file("../".$_POST['old_photo'])) unlink("../".$_POST['old_photo']);
     }
      else $image = $_POST['old_photo'];

  $query10 = "SELECT `tip_name` FROM `tbl_tip_resurs` WHERE `tip_id`='".$_POST['tip_id']."' ";
  $result10 = mysql_query($query10);
  if(!$result10) puterror2($TEXT['error-tbl38']);
      $result_lib = mysql_fetch_array($result10);
      if($_POST['tip_name']!=$result_lib['tip_name']) {
          $query9 = "SELECT `tip_name` FROM `tbl_tip_resurs` WHERE `tip_name`='".$_POST['tip_name']."' ";
            $result9 = mysql_query($query9);
              if(!$result9) puterror2($TEXT['error-tbl38']);
                 if(mysql_num_rows($result9)>0) puterror2($TEXT['global-est']);
                   else $name_tip_name = "`tip_name` = '".$_POST['tip_name']."', ";
        }
       else $name_tip_name = "";

  //*************************************
  if($result_lib['tip_name'] !== $_POST['tip_name']) $log_tip = $TEXT['log-tip4']."&laquo;".$result_lib['tip_name']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['tip_name']."&raquo;<br />";
    else $log_tip = "";

  if($result_lib['tip_name'] !== $_POST['tip_name']) $log_tip_visible = $_POST['tip_name'];
    else $log_tip_visible = $result_lib['tip_name'];

  if($result_lib['prefix'] !== $_POST['prefix']) $log_prefix = $TEXT['log-tip6']."&laquo;".$log_tip_visible."&raquo;"." ".$TEXT['log-tip5']."&laquo;".$result_lib['prefix']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['prefix']."&raquo;<br />";
    else $log_prefix = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-tip3'];
  $what = $log_tip.$log_prefix.$TEXT['log-id'].$_POST['tip_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_tip_resurs` SET `pic_tip` = '$image', $name_tip_name `position` = '".$_POST['position']."', `prefix` = '".$_POST['prefix']."' WHERE `tip_id`='".$_POST['tip_id']."' ";
  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-tip.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-tip6']);
  require_once "../util/bottom.php";
?>