<?php
  ///////////////////////////////////////////////////
  //2011 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////
  require_once "/var/www/personal/admin/config.php";

  if(! @mysql_select_db($dbname,$dbcnx))
    {
    echo "At this moment the database is not accessible.";
    exit();
    }

// /var/www/personal/
echo `clear`;
echo "Utilites for import print logs\r\n";

// Ищем файлы
echo "\r\n\033[33m\033[1mSearch files for import...\033[0m\033[0m\r\n";

   $files = array();

   $repl_dir = opendir("/var/www/personal/print-logs/");
    while(($dir = readdir($repl_dir)))
      {
       if(@is_file("/var/www/personal/print-logs/".$dir) && $dir !== "." && $dir !== "..")
        {
//         $dir_name = $repl_dir . '/' . $dir;
         $files[] = $dir;
        }
      }
   closedir($repl_dir);
   $cnt_fl = count($files);
  
  $delimiter = ";";
  
  echo "Found - \033[32m\033[1m".$cnt_fl."\033[0m\033[0m files\r\n\r\n";

  for($i=0;$i<$cnt_fl;$i++){
   $n = 0;
   $handle = file("/var/www/personal/print-logs/".$files[$i]);
   $size = filesize("/var/www/personal/print-logs/".$files[$i])/1024;
   $numrows_w = count($handle)-1;
   $numrows = count($handle);
   echo "File - \033[32m\033[1m".$files[$i]."\033[0m\033[0m, Size - \033[32m\033[1m".round($size)." Kb\033[0m\033[0m, Rows for import - \033[32m\033[1m".$numrows_w."\033[0m\033[0m\r\n";

   echo "Open file - \033[33m\033[1m".$files[$i]."\033[0m\033[0m\r\n";
   echo "Import data...\r\n";

       foreach($handle as $line_num => $line) 
        {
         $line_total = ($line_num + 1);
         $linedump=trim($line);
         if(!($linedump ==''))
         {
          if($line_num > 0)
          {
          $n = $n+1;
          $csv_line = explode($delimiter,$linedump,10);
          $prn_data = trim($csv_line[0]);
          $prn_username = trim($csv_line[1]);
          $prn_fullname = trim($csv_line[2]);
          if($prn_fullname == "") $prn_fullname = "---";
          $prn_client = trim($csv_line[3]);
          $printer_name = trim($csv_line[4]);
          $printer_port = trim($csv_line[5]);
          $prn_print_size = trim($csv_line[6]);
          $prn_pages = trim($csv_line[7]);
          $prn_document = trim($csv_line[8]);
          $prn_srv_comp = trim($csv_line[9]);

          //Ищем знакомых и строим структуру
          $query_prn_client = mysql_query("SELECT * FROM `tbl_prn_pc` WHERE LOWER(pc_name)=LOWER('".$prn_client."')");
           if(!$query_prn_client) puterror2 (mysql_error());
           $prn_client_rows = mysql_numrows($query_prn_client);
            if($prn_client_rows == 0)
            {
             $query_prn_client2 = mysql_query("INSERT INTO `tbl_prn_pc` ( `prn_pc_id`, `pc_name` ) VALUES ( NULL ,'".$prn_client."')");
              if(!$query_prn_client2) puterror2 (mysql_error());
             $query_prn_client3 = mysql_query("SELECT * FROM `tbl_prn_pc` WHERE LOWER(pc_name)=LOWER('".$prn_client."')");
              if(!$query_prn_client3) puterror2 (mysql_error());
               $res_prn_client = mysql_fetch_array($query_prn_client3,MYSQL_ASSOC);
               $prn_pc_id = $res_prn_client['prn_pc_id'];
            }
            elseif($prn_client_rows == 1)
              {
                $res_prn_client = mysql_fetch_array($query_prn_client,MYSQL_ASSOC);
                $prn_pc_id = $res_prn_client['prn_pc_id'];
              }

          $query_usr_login = mysql_query("SELECT * FROM `tbl_prn_usr_login` WHERE LOWER(login_name)=LOWER('".$prn_username."')");
           if(!$query_usr_login) puterror2 (mysql_error());
           $usr_login_rows = mysql_numrows($query_usr_login);
            if($usr_login_rows == 0)
            {
             $query_usr_login2 = mysql_query("INSERT INTO `tbl_prn_usr_login` ( `usr_login_id`, `login_name` ) VALUES ( NULL ,'".$prn_username."')");
              if(!$query_usr_login2) puterror2 (mysql_error());
             $query_usr_login3 = mysql_query("SELECT * FROM `tbl_prn_usr_login` WHERE LOWER(login_name)=LOWER('".$prn_username."')");
              if(!$query_usr_login3) puterror2 (mysql_error());
               $res_usr_login = mysql_fetch_array($query_usr_login3,MYSQL_ASSOC);
               $usr_login_id = $res_usr_login['usr_login_id'];
            }
            elseif($usr_login_rows == 1)
              {
                $res_usr_login = mysql_fetch_array($query_usr_login,MYSQL_ASSOC);
                $usr_login_id = $res_usr_login['usr_login_id'];
              }

          $query_usr_name = mysql_query("SELECT * FROM `tbl_prn_usr_name` WHERE LOWER(usr_name)=LOWER('".$prn_fullname."')");
           if(!$query_usr_name) puterror2 (mysql_error());
           $usr_name_rows = mysql_numrows($query_usr_name);
            if($usr_name_rows == 0)
            {
             $query_usr_name2 = mysql_query("INSERT INTO `tbl_prn_usr_name` ( `usr_name_id`, `usr_name` ) VALUES ( NULL ,'".$prn_fullname."')");
              if(!$query_usr_name2) puterror2 (mysql_error());
             $query_usr_name3 = mysql_query("SELECT * FROM `tbl_prn_usr_name` WHERE LOWER(usr_name)=LOWER('".$prn_fullname."')");
              if(!$query_usr_name3) puterror2 (mysql_error());
               $res_usr_name = mysql_fetch_array($query_usr_name3,MYSQL_ASSOC);
               $usr_name_id = $res_usr_name['usr_name_id'];
            }
            elseif($usr_name_rows == 1)
              {
                $res_usr_name = mysql_fetch_array($query_usr_name,MYSQL_ASSOC);
                $usr_name_id = $res_usr_name['usr_name_id'];
              }

          $query_prn_name = mysql_query("SELECT * FROM `tbl_prn_name` WHERE LOWER(prn_name)=LOWER('".$printer_name."')");
           if(!$query_prn_name) puterror2 (mysql_error());
           $prn_name_rows = mysql_numrows($query_prn_name);
            if($prn_name_rows == 0)
            {
             $query_prn_name2 = mysql_query("INSERT INTO `tbl_prn_name` ( `prn_id`, `prn_name` ) VALUES ( NULL ,'".$printer_name."')");
              if(!$query_prn_name2) puterror2 (mysql_error());
             $query_prn_name3 = mysql_query("SELECT * FROM `tbl_prn_name` WHERE LOWER(prn_name)=LOWER('".$printer_name."')");
              if(!$query_prn_name3) puterror2 (mysql_error());
               $res_prn_name = mysql_fetch_array($query_prn_name3,MYSQL_ASSOC);
               $prn_id = $res_prn_name['prn_id'];
            }
            elseif($prn_name_rows == 1)
              {
                $res_prn_name = mysql_fetch_array($query_prn_name,MYSQL_ASSOC);
                $prn_id = $res_prn_name['prn_id'];
              }

          $query_port_name = mysql_query("SELECT * FROM `tbl_prn_port` WHERE LOWER(port_name)=LOWER('".$printer_port."')");
           if(!$query_port_name) puterror2 (mysql_error());
           $port_name_rows = mysql_numrows($query_port_name);
            if($port_name_rows == 0)
            {
             $query_port_name2 = mysql_query("INSERT INTO `tbl_prn_port` ( `port_id`, `port_name` ) VALUES ( NULL ,'".$printer_port."')");
              if(!$query_port_name2) puterror2 (mysql_error());
             $query_port_name3 = mysql_query("SELECT * FROM `tbl_prn_port` WHERE LOWER(port_name)=LOWER('".$printer_port."')");
              if(!$query_port_name3) puterror2 (mysql_error());
               $res_port_name = mysql_fetch_array($query_port_name3,MYSQL_ASSOC);
               $port_id = $res_port_name['port_id'];
            }
            elseif($port_name_rows == 1)
              {
                $res_port_name = mysql_fetch_array($query_port_name,MYSQL_ASSOC);
                $port_id = $res_port_name['port_id'];
              }

          $query_srv_name = mysql_query("SELECT * FROM `tbl_prn_srv_comp` WHERE LOWER(srv_name)=LOWER('".$prn_srv_comp."')");
           if(!$query_srv_name) puterror2 (mysql_error());
           $srv_name_rows = mysql_numrows($query_srv_name);
            if($srv_name_rows == 0)
            {
             $query_srv_name2 = mysql_query("INSERT INTO `tbl_prn_srv_comp` ( `srv_comp_id`, `srv_name` ) VALUES ( NULL ,'".$prn_srv_comp."')");
              if(!$query_srv_name2) puterror2 (mysql_error());
             $query_srv_name3 = mysql_query("SELECT * FROM `tbl_prn_srv_comp` WHERE LOWER(srv_name)=LOWER('".$prn_srv_comp."')");
              if(!$query_srv_name3) puterror2 (mysql_error());
               $res_srv_name = mysql_fetch_array($query_srv_name3,MYSQL_ASSOC);
               $srv_comp_id = $res_srv_name['srv_comp_id'];
            }
            elseif($srv_name_rows == 1)
              {
                $res_srv_name = mysql_fetch_array($query_srv_name,MYSQL_ASSOC);
                $srv_comp_id = $res_srv_name['srv_comp_id'];
              }

             $query_usr = "INSERT INTO tbl_prn_logs (
             `prn_log_id` ,
             `prn_data`,
             `usr_login_id`,
             `usr_name_id`,
             `pc_id`,
             `prn_id`,
             `port_id`,
             `size`,
             `pages`,
             `document`,
             `srv_comp_id` )
              VALUES (
             NULL ,
             '$prn_data',
             '$usr_login_id',
             '$usr_name_id',
             '$prn_pc_id',
             '$prn_id',
             '$port_id',
             '$prn_print_size',
             '$prn_pages',
             '".$prn_document."',
             '$srv_comp_id' ) ";
             $result_usr = mysql_query($query_usr);
              if(!$result_usr) puterror2(mysql_error());
          }
        }
      }
      //*************************************
      $ip = "127.0.0.1";
      $doing = "Import print logs";
      $what = "File - ".$files[$i].", Size - ".round($size)." Kb, Rows inserted - ".$n;
      $who = "Robot";
      //*************************************
      $query_rep = mysql_query("INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NULL , '$ip' , '$who' , '$doing' , '$what')");
      $ip = "";
      $doing = "";
      $what = "";
      $who = "";
      unlink("/var/www/personal/print-logs/".$files[$i]);
      echo "Close and delete file - \033[33m\033[1m".$files[$i]."\033[0m\033[0m\r\n";
      echo "Add information to project log\r\n";
  }
  echo "\r\n\033[32m\033[1mImporting complete.\033[0m\033[0m\r\n";
?>