<!doctype html>
<html lang="en">
<head>
<title>Checking Files and Folders</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style>
    li.verified_file {
        color: green;
        font-weight: normal;
    }
    
    li.unverified_file {
        color: orange;
        font-weight: bold;
    }

    li.new_file {
        color: red;
        font-weight: bold;
    }
    li.directory {
        color: brown;
        font-weight: normal;
    }

</style>
</head>
<body>

<h1>Checking files</h1>

<?php
include "classes/DatabaseConnection.php";
include "classes/DirItem.php";

$folderPath = "/home/yugofile/public_html";

function readFolder( $path, $db_conn ) {

  // Open the folder
  if ( !( $dir = opendir( $path ) ) ) die( "Can't open $path" );
  $filenames = array();

  // Read the contents of the folder, ignoring '.' and '..', and
  // appending '/' to any subfolder names. Add all the files and
  // subfolders to the $filenames array.

  while ( $filename = readdir( $dir ) ) {
    if ( $filename != '.' && $filename != '..' ) {
      if ( is_dir( "$path/$filename" ) ) $filename .= '/';
      $filenames[] = $filename;
    }
  }

  closedir ( $dir );

  // Sort the filenames in alphabetical order
  sort( $filenames );

  // Display the filenames, and process any subfolders

  echo "<ul>";

  foreach ( $filenames as $filename ) {
    if ( substr( $filename, -1 ) == '/' ) {
        echo "<li class=\"directory\">" . substr( $filename, 0, -1 );
        $dir_name = $path . "/" . substr( $filename, 0, -1 );
        readFolder($dir_name, $db_conn );
    } else {
        // check file on database
        $di = new DirItem($filename, $path);
        //$di->update_date = filemtime($path . "/" . $filename);
        $di->size = filesize($path . "/" . $filename);
        $di->store($db_conn);
        if($di->status == "New dir_item added!") {
            echo "<li class=\"new_file\">" . $filename . " (new)";
        } else {
            if($di->is_verified > 0) {
                echo "<li class=\"verified_file\">" . $filename . " (size:" . filesize($path . "/" . $filename) .")";
            } else {
                echo "<li class=\"unverified_file\">" . $filename . "(size:" . filesize($path . "/" . $filename) .")";
            }
        }
    }
    echo "</li>";
  }

  echo "</ul>";
}

echo "<h2>Contents of '$folderPath':</h2>";

$db_conn = DatabaseConnection::getConnection();
readFolder( $folderPath, $db_conn );

?>

</body>
</html>