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

  require_once "../config.php";
  require_once "../util/top_layer1.php";
  require_once "../lang/ru/trans.php";
  if(is_file ("../lang/".$lang."/trans.php")) require_once "../lang/".$lang."/trans.php";

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

  if(empty($_POST['room_id'])) puterror2($TEXT['meet-err-edit2']);
  if(strlen($_POST['room_id'])>11) puterror2($TEXT['meet-err-edit3']);
  if(empty($_POST['room_name'])) puterror2($TEXT['meet-err-add4']);
  if(strlen($_POST['room_name'])>255) puterror2($TEXT['meet-err-add2']);
  if(strlen($_POST['message'])>1000) puterror2($TEXT['meet-err-add3']);
  if(empty($_POST['position'])) puterror2($TEXT['meet-err-add5']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['meet-err-add6']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  if(empty($_POST['org_id'])) puterror2($TEXT['error-org10']);

  if(isset($_POST['room_name'])) $room_name=trim($_POST['room_name']);
    else $room_name="";
     if(empty($room_name)) puterror2($TEXT['meet-err-add4']);

  if(isset($_POST['position'])) $position=trim($_POST['position']);
    else $position="";
     if (empty($position)) puterror2($TEXT['meet-err-add5']);

  $_POST['room_name'] = rep($_POST['room_name']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['room_id'] = rep($_POST['room_id']);
  $_POST['org_id'] = rep($_POST['org_id']);

  $new_org = $_POST['org_id'];

  if($_POST['visible'] == "on") $visible = "yes";
    else $visible = "no";

  $query10 = "SELECT `room_name` FROM `tbl_meet_room` WHERE `room_id`='".$_POST['room_id']."' ";
  $result10 = mysql_query($query10);
  if(!$result10) puterror2($TEXT['error-tbl48']);
      $result_lib = mysql_fetch_array($result10);
      if($_POST['room_name']!=$result_lib['room_name']) {
          $query9 = "SELECT `room_name` FROM `tbl_meet_room` WHERE `room_name`='".$_POST['room_name']."' ";
            $result9 = mysql_query($query9);
              if(!$result9) puterror2($TEXT['error-tbl48']);
                 if(mysql_num_rows($result9)>0) puterror2($TEXT['global-est']);
                   else $name_razdel = "`room_name` = '".$_POST['room_name']."', ";
        }
       else $name_razdel = "";

  if($_POST['clear_picture'] == "on")
  {
  $image = "";
      unlink("../".$_POST['old_photo']);
  }
    else
    {
    if($_FILES['photo']['size'] > 1024*2*1024) puterror2($TEXT['error-pic3']);
    if(!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $name = $_POST['room_name'];
       $razd = "meet_".translate($name)."_";
       $image = "../Instructions/$razd".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'];
  }

  //*************************************
  $query = "SELECT `room_name` FROM `tbl_meet_room` WHERE `room_id`='".$_POST['room_id']."' ";
  $result = mysql_query($query);
  if(!$result) puterror2($TEXT['error-tbl48']);
   $result2 = mysql_fetch_array($result,MYSQL_ASSOC);

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-meet2'];
  $what = $result2['room_name']." ".$TEXT['log-id']." ".$_POST['room_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_meet_room` SET `room_photo` = '$image', $name_razdel `room_descr` = '".$_POST['message']."', `position` = '".$_POST['position']."', `org_id` = '$new_org', `room_visible` = '$visible' WHERE `room_id`='".$_POST['room_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.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['meet-err-edit1']);
  require_once "../util/bottom.php";
?>