Advertisement
Guest User

Ilst image indexing script

a guest
Feb 11th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <?php
  2. ini_set('max_execution_time', 0); ini_set('display_errors', 'On');
  3.  
  4.  
  5. $progress = file_get_contents("process_log.txt");
  6. if($progress != "0/0")
  7. die("Update already in progress");
  8.  
  9. $conf_encoded = file_get_contents("configuration.json");
  10. $conf = json_decode($conf_encoded);
  11. $time = time();
  12.  
  13. //load neened info from configuratin
  14. $directory = $conf->ilst->images->dir;
  15. $supported_extensions = $conf->ilst->images->supported;
  16.  
  17. //we dont need this anymore
  18. unset($conf_encoded);
  19.  
  20. //open directory
  21. $handle = opendir($directory);
  22.    
  23.     //Establish MySQL connection
  24.     $db_server = $conf->ilst->db->server;      
  25.     $db_login = $conf->ilst->db->login;
  26.     $db_pass = $conf->ilst->db->pass;      
  27.     $db_database = $conf->ilst->db->database;
  28.     //magic!
  29.     $mysqli = new mysqli($db_server,$db_login,$db_pass,$db_database);  
  30.    
  31.     if (mysqli_connect_error()) {
  32.     die("Database connection failed: " . mysqli_connect_error());
  33.     }
  34.     $mysqli->query("SET CHARACTER SET utf8");
  35.  
  36.    
  37.     //prepare list of files
  38.     $filecount = 0;
  39.     $files = array();
  40.     while (false !== ($file_name = readdir($handle))) {
  41.         if ($file_name != "." && $file_name != "..") {
  42.             //Get file extension
  43.             $ext = pathinfo($file_name, PATHINFO_EXTENSION);
  44.             //Do we want this extension?
  45.             if(in_array($ext, $supported_extensions, FALSE)){
  46.                 $files[] = $file_name;
  47.                         $filecount++;      
  48.             }
  49.         }
  50.     }  
  51.  
  52.     //process images
  53.     include("create_thumbnails.php");
  54.     $log = "process_log.txt";
  55.     $progress = 0; 
  56.     shuffle($files);
  57.     foreach($files as $file){
  58.                 //debug
  59.                 echo"$progress ";
  60.                 $fileuri = $directory."".$file;
  61.                 //calculate MD5 hash
  62.                 $content = file_get_contents($directory."".$file);
  63.                 $hash = md5($content);
  64.                
  65.                 //get file info
  66.                 $last_modif = filemtime($fileuri);
  67.                 $filesize = filesize($directory."". $file);
  68.                                        
  69.                 //database jobs
  70.                 $sqlfile = mysqli_real_escape_string($mysqli, $file);
  71.                 $re = $mysqli->query("SELECT * FROM `images` WHERE NAME='".$sqlfile."'");
  72.                 if (!$re) {
  73.                     printf("Error: %s\n", mysqli_error($mysqli));
  74.                     exit();
  75.                 }
  76.                 $info = mysqli_fetch_array($re);
  77.                 if(isset($info["ID"])){
  78.                     if($info["HASH"] == $hash){
  79.                         //OK.
  80.                         $mysqli->query("UPDATE `images` SET REVISION=\"".$time."\", SIZE=\"".$filesize."\" WHERE ID=\"".$info["ID"]."\"");
  81.                         //debug
  82.                         echo"ok -";
  83.                     }
  84.                     else{
  85.                         //regenerate thumbnail
  86.                         makeThumb($directory, $file);
  87.  
  88.                         //debug
  89.                         echo"re -";
  90.                     }
  91.                 }
  92.                 else{
  93.                     $re = $mysqli->query("SELECT * FROM `images` WHERE HASH='".$hash."'");
  94.                     if($info["HASH"] == $hash){
  95.                         echo "duplicate -";
  96.                     }
  97.                     else {
  98.                         //Create new entry
  99.                         $mysqli->query("INSERT INTO `images` (NAME, HASH,LAST_MODIF,SIZE,CREATED,REVISION)
  100.                     VALUES (\"" . $sqlfile . "\", \"" . $hash . "\",\"" . $last_modif . "\",\"" . $filesize . "\",\"" . $time . "\",\"" . $time . "\")");
  101.                         //debug
  102.                         echo "new -";
  103.  
  104.                         //generate new thumbnail
  105.                         makeThumb($directory, $file);
  106.                     }
  107.                 }
  108.                 //debug
  109.                 echo"$fileuri [$hash] <br/>";
  110.                
  111.                
  112.                 //logfile
  113.                 $text = $progress."/".$filecount;
  114.                 file_put_contents($log, $text);
  115.                 $progress++;
  116.     }
  117.     $text = "0/0";
  118.     file_put_contents($log, $text);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement