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

  require_once "../config.php";
  require_once "../util/top.php";
  include "util.php";

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

  if(empty($_POST['news_id'])) puterror2($TEXT['error-new16']);
  if(strlen($_POST['news_id'])>11) puterror2($TEXT['error-new17']);
  if(empty($_POST['header'])) puterror2($TEXT['error-new1']);
  if(strlen($_POST['header'])>300) puterror2($TEXT['error-new2']);
  if(empty($_POST['message'])) puterror2($TEXT['error-new4']);
  if(strlen($_POST['message'])>60000) puterror2($TEXT['error-new5']);
  if(empty($_POST['position'])) puterror2($TEXT['error-new7']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-new8']);
  if(strlen($_POST['description'])>300) puterror2($TEXT['error-new10']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  if(strlen($_POST['old_org_id'])>11) puterror2($TEXT['error-org2']);
  if(strlen($_POST['url'])>500) puterror2($TEXT['error-new12']);
  if(strlen($_POST['url_text'])>300) puterror2($TEXT['error-new13']);
  if(strlen($_POST['old_picture'])>250) puterror2($TEXT['error-pic1']);
  if(strlen($_POST['old_picture_big'])>250) puterror2($TEXT['error-pic2']);
  if(strlen($_POST['clear_picture'])>5) puterror2($TEXT['error-new11']);

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

  if(isset($_POST['message'])) $new=trim($_POST['message']);
    else $new="";
     if (empty($new)) puterror2($TEXT['error-new6']);

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

  $_POST['news_id'] = rep($_POST['news_id']);
  $_POST['header'] = rep($_POST['header']);
  $_POST['message'] = rep2($_POST['message']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['description'] = rep($_POST['description']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['old_org_id'] = rep($_POST['old_org_id']);
  $_POST['url'] = rep($_POST['url']);
  $_POST['url_text'] = rep($_POST['url_text']);
  $_POST['old_picture'] = rep($_POST['old_picture']);
  $_POST['old_picture_big'] = rep($_POST['old_picture_big']);
  $_POST['clear_picture'] = rep($_POST['clear_picture']);
  $_POST['news_id'] = rep($_POST['news_id']);

    $new_org = $_POST['org_id'];
    $url = $_POST['url'];

            ////////////////////////////////////////////////////////
            // mail
            ////////////////////////////////////////////////////////
            $query21 = "SELECT * FROM `tbl_news` WHERE `new_id` = ".$_POST['news_id']." AND `visible` = 'show'";
              $result_query21 = mysql_query($query21);
                if(!$result_query21) puterror2($TEXT['error-tbl17']);
                 {
                  $result_new = mysql_fetch_array($result_query21);
                  $res_vis = $result_new['visible'];
                  $res_org_id = $result_new['org_id'];

            $query5 = "SELECT * FROM `tbl_organizaciya` WHERE `org_id` = '".$res_org_id."'";
                  $result_query5 = mysql_query($query5);
                    if(!$result_query5) puterror2($TEXT['error-tbl7']);
                     {
                      $result_organizaciya = mysql_fetch_array($result_query5);
                      $user_org = $result_organizaciya['organizaciya'];
                     }
                 }
                 if($user_org=="-")
                  {
                    $query7 = "SELECT * FROM `tbl_users` WHERE `subscribe` = 'scribe'";
                      $result_query7 = mysql_query($query7);
                       if(!$result_query7) puterror2($TEXT['error-tbl2']);
                       {
                        while($result_mail2 = mysql_fetch_array($result_query7))
                         {
                          $user_mail2 = $result_mail2['mail'];

                          $query8 = "SELECT * FROM `tbl_domain` WHERE `dom_id` = '".$result_mail2['dom_id']."'";
                            $result_query8 = mysql_query($query8);
                             if(!$result_query8) puterror2($TEXT['error-tbl5']);
                             {
                              $result_domain2 = mysql_fetch_array($result_query8);
                              $user_dom2 = $result_domain2['domain'];

                              if ($result_news['url'] == "") $url = "";
                                else $url = "<a href=".$_POST['url']." target=_blank>".$_POST['url_text']."</a>";

                              $organizaciya = $TEXT['new-glob1'];
                              $address = $user_mail2."@".$user_dom2;
                              $podpis = $organizaciya." ".$result_new['data'];
                              $tema = $TEXT['new-tema'];
                              $msg = "<h2><b>".$_POST['header']."</b></h2>".$_POST['new']."<br />".$url."<br /><hr />".$podpis."";

                              $podpis = stripslashes($podpis);
                              $tema = stripslashes($tema);
                              $msg = nl2br($msg);

                              @mail($address, $tema, $msg, join("\r\n", array(
                              "From: phonebook@".$_SERVER['SERVER_NAME']."",
                              "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"
                              )));
                             }
                         }
                       }
                  }
                  else
                   {
                    $query3 = "SELECT * FROM `tbl_users` WHERE `org_id` = '".$new_org."' AND `subscribe` = 'scribe'";
                     $result_query3 = mysql_query($query3);
                       if(!$result_query3) puterror2($TEXT['error-tbl2']);
                        {
                         while($result_mail = mysql_fetch_array($result_query3))
                         {
                          $user_mail = $result_mail['mail'];

                          $query4 = "SELECT * FROM `tbl_domain` WHERE `dom_id` = '".$result_mail['dom_id']."'";
                            $result_query4 = mysql_query($query4);
                              if(!$result_query4) puterror2($TEXT['error-tbl5']);
                               {
                                $result_domain = mysql_fetch_array($result_query4);
                                $user_dom = $result_domain['domain'];

                                if ($result_news['url'] == "") $url = "";
                                else $url = "<a href=".$_POST['url']." target=_blank>".$_POST['url_text']."</a>";

                                $organizaciya = $TEXT['new-glob21'].$user_org.$TEXT['new-glob22'];
                                $address = $user_mail."@".$user_dom;
                                $podpis = $organizaciya." ".$result_new['data'];
                                $tema = $TEXT['new-tema'];
                                $msg = "<h2><b>".$_POST['header']."</b></h2>".$_POST['new']."<br />".$url."<br /><hr />".$podpis."";

                                $podpis = stripslashes($podpis);
                                $tema = stripslashes($tema);
                                $msg = nl2br($msg);

                                @mail($address, $tema, $msg, join("\r\n", array(
                                "From: phonebook@".$_SERVER['SERVER_NAME']."",
                                "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"
                                )));
                               }
                         }
                        }
                   }
            ///////////////////////////////////////////////////////
            // end mail
            ////////////////////////////////////////////////////////

  //*************************************
  $query1 = "SELECT * FROM `tbl_news` WHERE `new_id`='".$_POST['news_id']."' ";
  $result = mysql_query($query1);
      if(!$result) puterror2($TEXT['error-tbl17']);
        $result2 = mysql_fetch_array($result);

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-new3'];
  $what = $TEXT['log-new4'].$result2['header'].$TEXT['log-id'].$result2['new_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_news` SET `org_id` = '$new_org', `header` = '".$_POST['header']."', `news` = '".$_POST['message']."', `position` = '".$_POST['position']."', `description` = '".$_POST['description']."', `url` = '$url', `url_text` = '".$_POST['url_text']."', `picture` = '$smallimage', `picture_big` = '$image' WHERE `new_id`='".$_POST['news_id']."' 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 puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-new14']);
  require_once "../util/bottom.php";
?>