Add diagnostics to check integrity of data folder master
Add diagnostics to check integrity of data folder

--- a/common.inc.php
+++ b/common.inc.php
@@ -81,16 +81,20 @@
         return START_DATE;

     }

 }

+

 function getDateFromFilename($filename) {

-    $fnameParts = explode(".",$filename);

+    $fnameParts = explode(".", $filename);

     return $fnameParts[0];

 }

-function getFilenameForDate($date,$fileext = "") {

-    $results = glob(DATA_DIR."/".$date."*".$fileext);

+

+function getFilenameForDate($date, $fileext = "") {

+    $results = glob(DATA_DIR . "/" . $date . "*" . $fileext);

     if (sizeof($results) > 0) {

-    return $results[0];

-    } else return false;

-}

+        return $results[0];

+    } else

+        return false;

+}

+

 function getPreviousDate() {

     $datedFiles = scandir(DATA_DIR); // sorted descendingly by default

     if (sizeof($datedFiles) > 2) {// always at least 2 even for an empty folder because of ./ and ../

@@ -99,15 +103,24 @@
         return false;

     }

 }

+function getFirstDate() {

+    // not romance help

+    $datedFiles = scandir(DATA_DIR); // sorted descendingly by default

+    if (sizeof($datedFiles) > 2) {// always at least 2 even for an empty folder because of ./ and ../

+        return getDateFromFilename($datedFiles[2]);

+    } else {

+        return false;

+    }

+}

 

 function getPhoto($displayDate) {

     $cacheFile = CACHE_DIR . $displayDate . ".png";

     if (file_exists($cacheFile)) {

         return $cacheFile;

     } else {

-        if ($fname = getFilenameForDate($displayDate,".jpg")) {

+        if ($fname = getFilenameForDate($displayDate, ".jpg")) {

             $source_gd_image = imagecreatefromjpeg($fname);

-        } else if ($fname = getFilenameForDate($displayDate,".png")) {

+        } else if ($fname = getFilenameForDate($displayDate, ".png")) {

             $source_gd_image = imagecreatefrompng($fname);

         } else {

             return false;

@@ -118,10 +131,6 @@
         }

         $source_image_width = imagesx($source_gd_image);

         $source_image_height = imagesy($source_gd_image);

-

-        $header_gd_image = imagecreatefrompng("img/header.png");

-        $header_image_width = imagesx($header_gd_image);

-        $header_image_height = imagesy($header_gd_image);

 

         $white = imagecolorallocate($source_gd_image, 255, 255, 255);

         $black = imagecolorallocate($source_gd_image, 0, 0, 0);

@@ -149,29 +158,24 @@
         }

 

         $date = strtotime($displayDate);

-// First we create our bounding box for the first text

         $textDayName = date("l", $date);

-        $sizeDayName = 18;

+        $sizeDayName = 24 + round(($source_image_height - 1000) / 1000) * 2;

         $fontDayName = "./img/mplus-1p-medium.ttf";

         $bboxDayName = calculateTextBox($textDayName, $fontDayName, $sizeDayName);

 

         $textDate = date("jS F Y", $date);

-        $sizeDate = 14;

+        $sizeDate = 24 + floor(($source_image_height - 1000) / 100);

         $fontDate = "./img/mplus-1p-regular.ttf";

         $bboxDate = calculateTextBox($textDate, $fontDate, $sizeDate);

 

         $margin = 15;

-        $maxX = $header_image_width + $margin * 2 + max($bboxDayName['width'], $bboxDate['width']) + $margin * 2;

-        $maxY = max($header_image_height + $margin * 2, ($bboxDayName['height'] + $margin + $bboxDate['height']));

-

-// Draw a white rectangle

-        imagefilledrectangle($source_gd_image, 0, 0, $maxX, $maxY, $white);

-

-        imagecopy($source_gd_image, $header_gd_image, $margin, $margin, 0, 0, $header_image_width, $header_image_height);

+

+// Draw a black rectangle

+        imagefilledrectangle($source_gd_image, 0, 0, $source_image_width, $margin * 2 + $bboxDayName['height'], $black);

 

 // Write it

-        imagettftext($source_gd_image, $sizeDayName, 0, $header_image_width + $margin * 2 + $bboxDayName['left'], $margin + $bboxDayName['top'], $black, $fontDayName, $textDayName);

-        imagettftext($source_gd_image, $sizeDate, 0, $header_image_width + $margin * 2 + $bboxDate['left'], $margin + $bboxDayName['height'] + $margin + $bboxDate['top'], $black, $fontDate, $textDate);

+        imagettftext($source_gd_image, $sizeDayName, 0, $margin + $bboxDayName['left'], $margin + $bboxDayName['top'], $white, $fontDayName, $textDayName);

+        imagettftext($source_gd_image, $sizeDate, 0, (($source_image_width - $bboxDayName['width']) / 2) + $bboxDayName['width'] - $bboxDate['left'], + $margin + $bboxDate['top'], $white, $fontDate, $textDate);

 

         imagepng($source_gd_image, $cacheFile, 9);

         imagedestroy($source_gd_image);

@@ -212,6 +216,4 @@
     $length = strlen($needle);

     return (substr($haystack, 0, $length) === $needle);

 }

-

-

 ?>

file:b/diagnostics.php (new)
--- /dev/null
+++ b/diagnostics.php
@@ -1,1 +1,55 @@
+<?php
 
+include "common.inc.php";
+if (!($firstDate = getFirstDate()))
+    die("Cannot complete diagnotics, cannot get first file date");
+$currentDate = $firstDate;
+if (!($lastDate = getPreviousDate()))
+    die("Cannot complete diagnotics, cannot get last file date");
+$hashes = Array();
+while ($currentDate != $lastDate) {
+    $results = glob(DATA_DIR . "/" . $currentDate . "*");
+    if (sizeof($results) == 0) {
+        echo "Error on $currentDate, no photo found for that date <br>";
+    } else if (sizeof($results) > 1) {
+        echo "Error on $currentDate, more than one photo found for that date: <br>";
+        var_dump($results);
+        echo "<br>";
+    } else {
+        // normal day
+        $fname = basename($results[0]);
+        $fnParts = explode(".", $fname);
+        if (sizeof($fnParts) < 3) {
+            echo "Error on $currentDate, malformed file name {$fname} <br>";
+        } else {
+            if (strtotime($currentDate) != strtotime($fnParts[0])) {
+                echo "Error on $currentDate, file name {$fname} date does not match expected date <br>";
+            }
+            if ($imgsize = getimagesize($results[0])) {
+                if ($imgsize[0] < MIN_IMAGE_SIZE || $imgsize[1] < MIN_IMAGE_SIZE || $imgsize[0] != $imgsize[1]) {
+                    echo "Error on $currentDate, file name {$fname} is not large enough dimensions or non-square dimensions <br>";
+                }
+            } else {
+                echo "Error on $currentDate, file name {$fname} could not be opened as an image file. File may have been corrupted <br>";
+            }
+            $hash = md5_file($results[0]);
+            if ($hash != $fnParts[1]) {
+                echo "Error on $currentDate, file name {$fname} hash does not match expected hash. File may have been corrupted or altered <br>";
+            }
+            if (in_array($hash, array_keys($hashes))) {
+                echo "Error on $currentDate, file {$fname} hash matches that of {$hashes[$hash]}. Files may be identitical duplicates of the same image <br>";
+            }
+            $hashes[$hash] = $fname;
+
+            if (sizeof($fnParts[2]) > 3 || ($fnParts[2] != "png" && $fnParts[2] != "jpg")) {
+                echo "Error on $currentDate, file name {$fname} extension unexpected <br>";
+            }
+        }
+    }
+
+    // go to next day
+    $currentDate = date("Y-m-d", strtotime("+1 day", strtotime($currentDate)));
+}
+echo "Scanned files from $firstDate to $currentDate, no errors detected<br>";
+?>
+