<?php
///////////////////////////////////////////////////
//2010 (C)
//Карпухин Павел
///////////////////////////////////////////////////

require_once "/var/www/personal/config.php";
//require_once "/var/www/manydhcp/utils/functions.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();
  }

//*************************************
$ip = "127.0.0.1";
$doing = "Запущена проверка";
$what = "Проверка использования картинок в базе данных";
$who = "Robot";
//*************************************
$querylog = mysql_query("INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')");

echo `clear`;
echo "Utilites for checking picture using in data base of Personal project\r\n\r\n";

// Проверяем базу знаний
echo "\r\n\033[33m\033[1mChecking Knows directory.\033[0m\033[0m\r\n";

$srch_dir = "/var/www/personal/photo-knows";
$files = array();
$count = 0;
$i = 0;

$handle = opendir($srch_dir);
    while(false !== ($file = readdir($handle))) {
        if($file !== "." && $file !== ".." && !@is_dir($file)) {
            $files[] = $file;
        }
    }
    $count = count($files);
    closedir($handle);
    echo "Found - \033[33m\033[1m".$count." files\033[0m\033[0m\r\n\r\n";

for($i=0;$i<$count;$i++)
{
$query_ch = "SELECT COUNT(zhurn_id) AS cnt FROM tbl_bd_zhnaniy WHERE zh_soobschenie LIKE('%".$files[$i]."%' ) ";
 $res_ch = mysql_query($query_ch);
  if(!$res_ch) exit();
   $result_ch = mysql_fetch_array($res_ch);
    if($result_ch['cnt'] == 0)
     {
     $del = unlink($srch_dir."/".$files[$i]);
     if(!$del) echo "\033[31m\033[1mError deleting file - ".$files[$i]."\033[0m\033[0m\r\n";
      else echo "File - ".$files[$i]."\033[33m\033[1m do not use, deleted\033[0m\033[0m\r\n";
     }
     else
      {
      echo "File - ".$files[$i]." \033[30m\033[1musing \033[0m\033[0m \r\n";
      }
 }
 echo "\r\n\033[32m\033[1mChecking knows directory complete.\033[0m\033[0m\r\n";

// Проверяем новости
echo "\r\n\033[33m\033[1mChecking News directory.\033[0m\033[0m\r\n";

$srch_dir2 = "/var/www/personal/photo-news";
$files2 = array();
$count2 = 0;
$n = 0;

$handle2 = opendir($srch_dir2);
    while(false !== ($file2 = readdir($handle2))) {
        if($file2 !== "." && $file2 !== ".." && !@is_dir($file2)) {
            $files2[] = $file2;
        }
    }
    $count2 = count($files2);
    closedir($handle2);
    echo "Found - \033[33m\033[1m".$count2." files\033[0m\033[0m\r\n\r\n";

for($n=0;$n<$count2;$n++)
{
$query_ch2 = "SELECT COUNT(new_id) AS cnt2 FROM tbl_news WHERE news LIKE('%".$files2[$n]."%' ) ";
 $res_ch2 = mysql_query($query_ch2);
  if(!$res_ch2) exit();
   $result_ch2 = mysql_fetch_array($res_ch2);
    if($result_ch2['cnt2'] == 0)
     {
     $del2 = unlink($srch_dir2."/".$files2[$n]);
     if(!$del) echo "\033[31m\033[1mError deleting file - ".$files2[$n]."\033[0m\033[0m\r\n";
      else echo "File - ".$files2[$n]."\033[33m\033[1m do not use, deleted\033[0m\033[0m\r\n";
     }
     else
      {
      echo "File - ".$files2[$n]." \033[30m\033[1musing \033[0m\033[0m \r\n";
      }
 }
 echo "\r\n\033[32m\033[1mChecking news directory complete.\033[0m\033[0m\r\n";
?>