#!/usr/bin/php
<?php
// This script is copyrighted to Thomas Guymer and is licensed under the
// Creative Commons Attribution Non-Commercial 2.0 UK: England & Wales Licence.

// This script can be run in two ways:
// * type "php systemThumbnails.php" into a console window; or
// * type "./systemThumbnails.php" into a console window.

// This script will:
//   * search for all the saved thumbnails on your KDE system; then
//   * check if the original file is present and delete the thumbnail if it is
//     not; or
//   * delete the thumbnail if it references the trash can.

$username = "user1";

// DO NOT EDIT BELOW THIS LINE

// Search for 'normal' thumbnails
echo " > Searching for thumbnails ... ";
$folderPath = "/home/" . $username . "/.thumbnails/normal";
if($handle = opendir($folderPath)) {
    while(false !== ($file = readdir($handle))) {
        if($file !== "." && $file !== "..") {
            $contents[] = $folderPath . "/" . $file;
        }
    }
    closedir($handle);
}
// Search for 'large' thumbnails
$folderPath = "/home/" . $username . "/.thumbnails/large";
if($handle = opendir($folderPath)) {
    while(false !== ($file = readdir($handle))) {
        if($file !== "." && $file !== "..") {
            $contents[] = $folderPath . "/" . $file;
        }
    }
    closedir($handle);
}
// Start checking thumbnails
if(!empty($contents)) {
    sort($contents);
    echo "done. (There are " . count($contents) . ")\n";
    echo "   > Processing ... ";
    $size = 0;
    $counter = 0;
    foreach($contents as $key => $value) {
        $file = file_get_contents($value);
        if(stripos($file, "Thumb::URI") !== false && stripos($file, "IDAT") !== false) {
            if(stripos($file, "tEXtThumb::URI") !== false) {
                // Find chunk length
                $integer = substr($file, stripos($file, "Thumb::URI")-8, 4);
                // Collect data
                $start = stripos($file, "Thumb::URI") + strlen("Thumb::URI") + 1; // This skips the null byte, as defined in the PNG specification https://www.w3.org/TR/2003/REC-PNG-20031110/#11tEXt
                $length = ord(substr($integer, 0, 1)) + ord(substr($integer, 1, 1)) + ord(substr($integer, 2, 1)) + ord(substr($integer, 3, 1)) - strlen("Thumb::URI") - 1;
                unset($integer);
                $original = substr($file, $start, $length);
                unset($start, $length);
                // Decode
                $original = utf8_encode($original);
                $original = urldecode($original);
            }
            elseif(stripos($file, "zTXtThumb::URI") !== false) {
                // Find chunk length
                $integer = substr($file, stripos($file, "Thumb::URI")-8, 4);
                // Collect data
                $start = stripos($file, "Thumb::URI") + strlen("Thumb::URI") + 2; // This skips special characters, as defined in the PNG specification https://www.w3.org/TR/2003/REC-PNG-20031110/#11zTXt
                $length = ord(substr($integer, 0, 1)) + ord(substr($integer, 1, 1)) + ord(substr($integer, 2, 1)) + ord(substr($integer, 3, 1)) - strlen("Thumb::URI") - 2;
                unset($integer);
                $original = substr($file, $start, $length);
                unset($start, $length);
                // Uncompress
                $original = gzuncompress($original);
                // Decode
                $original = utf8_encode($original);
                $original = urldecode($original);
            }
            else {
                echo "\nERROR: The meta data in " . $value . " is not in a recognised format.\n";
                exit(1);
            }
            // Check if the file can be deleted
            if(stripos($original, "file://") === 0) {
                // Remove bumf
                $original = substr($original, strlen("file://"));
                // Check if original exists
                if(!file_exists($original)) {
                    $size = $size + filesize($value);
                    $counter = $counter + 1;
                    unlink($value);
                }
            }
            elseif(stripos($original, "trash:/") === 0) {
                $size = $size + filesize($value);
                $counter = $counter + 1;
                unlink($value);
            }
            else {
                echo "\nERROR: " . $original . " is not a local file. (" . $value . ")\n";
            }
            unset($original);
        }
        else {
            echo "\nERROR: " . $value . " does not contain correct meta data.\n";
        }
        unset($file);
    }
    $size = round($size/(1024*1024),1);
    echo "done.\n";
    echo " > " . $counter . " thumbnails were deleted which totaled " . $size . "MB.\n";
}
else {
    echo "done. (There are no thumbnails on your system.)\n";
}
?>
