Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('max_execution_time', 0); ini_set('display_errors', 'On');
- $progress = file_get_contents("process_log.txt");
- if($progress != "0/0")
- die("Update already in progress");
- $conf_encoded = file_get_contents("configuration.json");
- $conf = json_decode($conf_encoded);
- $time = time();
- //load neened info from configuratin
- $directory = $conf->ilst->images->dir;
- $supported_extensions = $conf->ilst->images->supported;
- //we dont need this anymore
- unset($conf_encoded);
- //open directory
- $handle = opendir($directory);
- //Establish MySQL connection
- $db_server = $conf->ilst->db->server;
- $db_login = $conf->ilst->db->login;
- $db_pass = $conf->ilst->db->pass;
- $db_database = $conf->ilst->db->database;
- //magic!
- $mysqli = new mysqli($db_server,$db_login,$db_pass,$db_database);
- if (mysqli_connect_error()) {
- die("Database connection failed: " . mysqli_connect_error());
- }
- $mysqli->query("SET CHARACTER SET utf8");
- //prepare list of files
- $filecount = 0;
- $files = array();
- while (false !== ($file_name = readdir($handle))) {
- if ($file_name != "." && $file_name != "..") {
- //Get file extension
- $ext = pathinfo($file_name, PATHINFO_EXTENSION);
- //Do we want this extension?
- if(in_array($ext, $supported_extensions, FALSE)){
- $files[] = $file_name;
- $filecount++;
- }
- }
- }
- //process images
- include("create_thumbnails.php");
- $log = "process_log.txt";
- $progress = 0;
- shuffle($files);
- foreach($files as $file){
- //debug
- echo"$progress ";
- $fileuri = $directory."".$file;
- //calculate MD5 hash
- $content = file_get_contents($directory."".$file);
- $hash = md5($content);
- //get file info
- $last_modif = filemtime($fileuri);
- $filesize = filesize($directory."". $file);
- //database jobs
- $sqlfile = mysqli_real_escape_string($mysqli, $file);
- $re = $mysqli->query("SELECT * FROM `images` WHERE NAME='".$sqlfile."'");
- if (!$re) {
- printf("Error: %s\n", mysqli_error($mysqli));
- exit();
- }
- $info = mysqli_fetch_array($re);
- if(isset($info["ID"])){
- if($info["HASH"] == $hash){
- //OK.
- $mysqli->query("UPDATE `images` SET REVISION=\"".$time."\", SIZE=\"".$filesize."\" WHERE ID=\"".$info["ID"]."\"");
- //debug
- echo"ok -";
- }
- else{
- //regenerate thumbnail
- makeThumb($directory, $file);
- //debug
- echo"re -";
- }
- }
- else{
- $re = $mysqli->query("SELECT * FROM `images` WHERE HASH='".$hash."'");
- if($info["HASH"] == $hash){
- echo "duplicate -";
- }
- else {
- //Create new entry
- $mysqli->query("INSERT INTO `images` (NAME, HASH,LAST_MODIF,SIZE,CREATED,REVISION)
- VALUES (\"" . $sqlfile . "\", \"" . $hash . "\",\"" . $last_modif . "\",\"" . $filesize . "\",\"" . $time . "\",\"" . $time . "\")");
- //debug
- echo "new -";
- //generate new thumbnail
- makeThumb($directory, $file);
- }
- }
- //debug
- echo"$fileuri [$hash] <br/>";
- //logfile
- $text = $progress."/".$filecount;
- file_put_contents($log, $text);
- $progress++;
- }
- $text = "0/0";
- file_put_contents($log, $text);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement