<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
  ///////////////////////////////////////////////////
  //2007 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang.".php")) include "../lang/".$lang.".php";
     else include "../lang/ru.php";

  require_once "../config.php";

  if(! @mysql_select_db($dbname,$dbcnx))
  {
    echo("<p>At this moment database is not available, therefore correct display of page is impossible.<br />If you do not install project previously, run script <a href=install.php>http://".$_SERVER['SERVER_ADDR']."/personal/install.php</a></p>");
    exit();
  }

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW" />
<title><?php echo $TEXT['support33']; ?></title>
<link rel="StyleSheet" type="text/css" href="../utils/reader.css" />
<link rel="icon" href="../Favicon/support.ico" type="image/x-icon" />
<link rel="shortcut icon" href="../Favicon/support.ico" type="image/x-icon" />
</head>
<body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" bottommargin="0" topmargin="0" >
<table class="topadmin" border="0" cellspacing="0">
    <tr align="center">
     <td class="supp_iniz2"><?php echo $TEXT['support33']; ?></td>
    </tr>
</table>
<br />
<span align="shut_img"><a href="logout.php" title="<?php echo $TEXT['global-logout-tit']; ?>"><img class="shut_logo" alt="<?php echo $TEXT['global-logout2']; ?>" src="../img/clr.png"></a></a></span>
<?php
  if(empty($_COOKIE['login'])) puterror2($TEXT['error-loginform5']);
  if(empty($_COOKIE['passwrd'])) puterror2($TEXT['error-loginform5']);

  $query = "SELECT * FROM `tbl_logins` WHERE `login`='".$_COOKIE['login']."' ";
  $result = mysql_query($query);
  if(!$result) puterror2($TEXT['error-tbl20']);
      $result2 = mysql_fetch_array($result,MYSQL_ASSOC);
      $passw1 = $_COOKIE['passwrd'];
      $passw2 = $result2['pass'];
       if($passw1 !== $passw2) puterror2($TEXT['error-loginform3']);
       if(empty($passw1)) puterror2($TEXT['error-loginform2']);

      $query_cl = "SELECT * FROM `tbl_clear`";
      $result_cl = mysql_query($query_cl);
      if(!$result_cl) puterror($TEXT['error-tbl37']);
       if(mysql_num_rows($result_cl)>0);
         {
          while($result_cl2 = mysql_fetch_array($result_cl))
           {
           $tbl_name = $result_cl2['col_tbl_name'];
           $tbl_col = $result_cl2['col_name'];
           $tbl_day = $result_cl2['by_data'];
           $tbl_rows = $result_cl2['by_count'];

           $query_log_coun = mysql_query("SELECT COUNT($tbl_col) FROM $tbl_name");
           $coun_log = mysql_result($query_log_coun, 0);
            if($coun_log > 0)
           {
           $query10 = "SELECT ADDDATE((SELECT DATE((SELECT MAX($tbl_col) FROM $tbl_name))),-$tbl_day)";
           $result10_del_cl1 = mysql_query($query10);
            if(!$result10_del_cl1) puterror($TEXT['error-tbl37']);
             $r2 = mysql_fetch_array($result10_del_cl1);
             if($result_cl2['by_data'] > 0)
             {
              $clr_id = "";
             //удалить всё если отклонение в днях больше указанного
             $query_iskh_par = "SELECT * FROM `$tbl_name` WHERE `".$tbl_col."` < '".$r2['0']."'";
              $result_iskh_par = mysql_query($query_iskh_par);
               if(!$result_iskh_par) puterror($TEXT['error-tbl37']);
               while($iskh_par = mysql_fetch_array($result_iskh_par))
               {
               if($tbl_name == "tbl_supp_archive") $clr_id = "arch_id";
                else $clr_id = "log_id";

               //$query_del_cl1 = "DELETE FROM `$tbl_name` WHERE `".$tbl_col."` < '".$r2['0']."'";
               $query_del_cl1 = "DELETE FROM `$tbl_name` WHERE `".$clr_id."` = '".$iskh_par['0']."'";
               $result_del_cl1 = mysql_query($query_del_cl1);
               print $query_del_cl1;
                if(!$result_del_cl1) puterror($TEXT['error-tbl37']);
                if($iskh_par['pic_tr'] !== "" && is_file("../".$iskh_par['pic_tr'])) unlink("../".$iskh_par['pic_tr']);
               }
             }
             if($result_cl2['by_count'] > 0)
             {
             if($coun_log > $tbl_rows)
             {
             //удалить все если отклонение в количестве строк больше указанного
             $del_rows = $coun_log - $tbl_rows;
             $query_iskh_par = "SELECT * FROM `$tbl_name` ORDER BY $tbl_col LIMIT $del_rows";
              $result_iskh_par = mysql_query($query_iskh_par);
               if(!$result_iskh_par) puterror($TEXT['error-tbl37']);
               while($iskh_par = mysql_fetch_array($result_iskh_par))
               {
               //$query_del_cl2 = "DELETE FROM `$tbl_name` ORDER BY data LIMIT $del_rows";
                $query_del_cl2 = "DELETE FROM `$tbl_name` WHERE `".$tbl_col."` = '".$iskh_par['0']."'";
                $result_del_cl2 = mysql_query($query_del_cl2);
                if(!$result_del_cl2) puterror($TEXT['error-tbl37']);
                if($iskh_par['pic_tr'] !== "" && is_file("../".$iskh_par['pic_tr'])) unlink("../".$iskh_par['pic_tr']);
               }
              }
             }
           }
        }
      }
?>
<table border="0" width="60%" align="center">
    <tr><td class="boxmenu"><a href="maintree.php" target="_blank" title="<?php echo $TEXT['support31']; ?>"><?php echo $TEXT['support31']; ?></a></td></tr>
    <tr><td class="boxmenu"><a href="mast_maintree.php" target="_blank" title="<?php echo $TEXT['support35']; ?>"><?php echo $TEXT['support35']; ?></a></td></tr>
    <tr><td class="boxmenu"><a href="rpt_maintree.php" target="_blank" title="<?php echo $TEXT['support37']; ?>"><?php echo $TEXT['support36']; ?></a></td></tr>
    <tr><td class="boxmenu"><a href="prn_main.php" target="_blank" title="<?php echo $TEXT['prn-title']; ?>"><?php echo $TEXT['prn-title']; ?></a></td></tr>
    <tr><td class="boxmenu"><a href="zhurn_maintree.php" target="_blank" title="<?php echo $TEXT['zhurn-izm-title']; ?>"><?php echo $TEXT['zhurn-izm']; ?></a></td></tr>
    <tr><td class="boxmenu"><a href="know_maintree.php" target="_blank" title="<?php echo $TEXT['know-izm-title']; ?>"><?php echo $TEXT['know-izm']; ?></a></td></tr>
    <tr><td class="boxmenu"><a href="pass_maintree.php" target="_blank" title="<?php echo $TEXT['pass-title']; ?>"><?php echo $TEXT['pass-title2']; ?></a></td></tr>
</table>
</body>
</html>