<?php   ### directory_scan.php ###

#--------------------------------------------------------------------------------- 
// $dir         Startverzeichnis, von dem aus der Directorybaum aufgelöst werden soll
// $dirtree     Adresse der Directory-Liste, die gefüllt werden soll
// $errors      Fehlerzähler, wird jeweils erhöht, wenn ein Directory nicht geöffnet
//              werden konnte
// $with_links  true -> Links auf Directories werden einbezogen
//
// RETURN       Fehlerzähler, Verzeichnisse, die nicht geöffnet werden konnten
//
// Beim Einbeziehne von Links auf Verzeichnisse können zirkuläre Verläufe entstehen,
// wenn der Link auf ein übergeordnetes Verzeichnis verweist


    function get_dirtree($dir, &$_dirtree, $with_links = true, $with_files = false, $errors=0)
    {
        $dir = rtrim($dir, '/').'/';
        if (!$handle = opendir($dir)) return ++$errors;

        while (false !== ($res = readdir($handle)))
        {
            if(is_dir($dir.$res))
            {
                if ($with_links && is_link($dir.$res))
                {
                    $entry = realpath($dir.$res);
                    if(in_array($entry, $_dirtree)) continue;
                    
                    $_dirtree[] = realpath($dir.$res);
                    $errors = get_dirtree($dir.$res, $_dirtree, $with_links, $with_files, $errors);
                }
                elseif ($res != '.' && $res != '..' &&  !is_link($dir.$res))
                {
                    $_dirtree[] = realpath($dir.$res);
                    $errors = get_dirtree($dir.$res, $_dirtree, $with_links, $with_files, $errors);
                }
            }
            elseif($with_files and is_file($dir.$res))
            {
                if ($with_links && is_link($dir.$res))
                {
                    $entry = realpath($dir.$res);
            #        if(in_array($entry, $_dirtree)) continue;
                    $_dirtree[] = realpath($dir.$res);
                }
                elseif (!is_link($dir.$res))
                {
                    $_dirtree[] = realpath($dir.$res);
                }
            }
        }

        return $errors;
    }

#-----------------------------------------------------------------------------------
    
    function get_timelist(&$_dirtree, $comparetime, $op='newer')
    {
        $_return = array();
 
        foreach ($_dirtree as $key => $filename)
        {
            if ($filemtime = @filemtime($filename))
            {
                $compare = false;
                
                switch ($op)
                {
                    case 'newer': $compare = $filemtime > $comparetime;  break;
                    case 'older': $compare = $filemtime < $comparetime;  break;                     
                    case 'equal': $compare = $filemtime == $comparetime; break;                      
                } 

                if ($compare)
                {
                    $_return['filename'][$key] = $filename;
                    $_return['last-modified-time'][$key] = date('Y-m-d H:i:s', $filemtime);
                }    
            }    
        }
 

        return $_return;
    
    }

#-----------------------------------------------------------------------------------

function make_table_from_h_array($_records, $show_index=true)
{
    $index = 0;
    $out = '<p>keine Einträge vorhanden</p>';

    if (count($_records) > 0)
    {
        $out = "\r\n    <table class=\"record\">\r\n";

        $_heads = array_keys($_records[0]);

        $out .= "        <tr>\r\n";        
        
        if ($show_index)
        {
            $out .= "            <td class=\"caption right\"> ID </td>\r\n";                                    
        }

        foreach($_heads as $name)
        {
            $out .= "            <td class=\"caption\">"
                 . htmlspecialchars(ucfirst(strtolower($name))) ."</td>\r\n";                
        }

        $out .= "        </tr>\r\n";        

        foreach($_records as $key => $_fields)
        {
            $index++;

            $out .= "        <tr>\r\n";        

            if ($show_index)
            {
                $out .= "            <td class=\"caption right\"> $index </td>\r\n";                                    
            }

            foreach($_fields as $data)
            {
                $out .= "            <td class=\"data\">"
                     . htmlspecialchars($data) ."</td>\r\n";                
            }

            $out .= "        </tr>\r\n";        
        }

        $out .= "    </table>\r\n";
    }

    return $out;
}

#-----------------------------------------------------------------------------------
function make_table_from_v_array($_records, $show_index = true)
{
    $lfdnr = 0; 
    $out = '<p>keine Einträge vorhanden</p>';

    if (count($_records) > 0)
    {
        $out = "\r\n    <table class=\"record\">\r\n";

        $_heads = array_keys($_records);

        $out .= "        <tr>\r\n";        
        
        if ($show_index)
        {
            $out .= "            <td class=\"caption right\"> ID </td>\r\n";                                    
        }

        foreach($_heads as $name)
        {
            $out .= "            <td class=\"caption\">"
                 . htmlspecialchars(ucfirst(strtolower($name))) ."</td>\r\n";                
        }

        $out .= "        </tr>\r\n";        

        foreach($_records[$_heads[0]] as $index => $_line)
        {
            $lfdnr++;

            $out .= "        <tr>\r\n";        

            if ($show_index)
            {
                $out .= "            <td class=\"caption right\"> $lfdnr </td>\r\n";                                    
            }

            foreach($_heads as $name)
            {
                $out .= "            <td class=\"data\">"
                     . htmlspecialchars($_records[$name][$index]) ."</td>\r\n";                
            }

            $out .= "        </tr>\r\n";        
        }

        $out .= "    </table>\r\n";
    }

    return $out;
}


#===================================================================================
# PHP main
#===================================================================================
# defaults

   $_out['ctrl']['lowerbound']['days']    = 0;
   $_out['ctrl']['lowerbound']['hours']   = 1;
   $_out['ctrl']['lowerbound']['minutes'] = 0;

   $_out['list'] = "<p>Bitte Zeitraum wählen</p>\r\n";
   


   header('Content-Type: text/html; Charset=ISO-8859-1');
   
   if (isset($_POST['ctrl']['lowerbound']['days']))
   {
       $_out['ctrl']['lowerbound']['days'] = intval($_POST['ctrl']['lowerbound']['days']);
   }
   
   if (isset($_POST['ctrl']['lowerbound']['hours']))
   {
       $_out['ctrl']['lowerbound']['hours'] = intval($_POST['ctrl']['lowerbound']['hours']);
   }
   
   if (isset($_POST['ctrl']['lowerbound']['minutes']))
   {
       $_out['ctrl']['lowerbound']['minutes'] = intval($_POST['ctrl']['lowerbound']['minutes']);
   }

   $comparetime = time() - (
                  $_out['ctrl']['lowerbound']['days']    * 86400 +
                  $_out['ctrl']['lowerbound']['hours']   * 3600 + 
                  $_out['ctrl']['lowerbound']['minutes'] * 60 );
                  
   if (isset($_POST['btn']['search']))
   {

       $_dirtree = array();
       $errors = get_dirtree(rtrim($_SERVER['DOCUMENT_ROOT'], '/').'/', $_dirtree, true, true);
   
       if($_records = get_timelist ($_dirtree, $comparetime))
       {
           natsort($_records['filename']);    
           $_out['list'] = make_table_from_v_array($_records);
       }
       else
       {
           $_out['list'] = "<p>keine veränderten Objekte gefunden</p>\r\n" . $_out['list'];
       }
   }


/*
   echo "<pre>\r\n";
   echo htmlspecialchars(print_r($_dirtree,1));
   echo "<p><b>Fehler:</b> $errors</p>\r\n";
   echo "</pre>\r\n";
*/


####################################################################################
# HTML-Output
####################################################################################

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>Veränderte Dateien in den letzten 15min</title>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <meta name="robots" content="index,follow">

    <style type="text/css">

        * html div, * html ul 
        {
           height:1%;  /* notwendiges hasLayout für IE 6 */
        }

        body
        {
          font-family:century gothic, tahoma, arial,sans-serif;
          font-size: 0.9em;
          height: 100%;
          margin:10px;
        }

        table.record
        {
            border-collapse: collapse;
            empty-cells: show;
            border: 1px solid #000080;
            margin-bottom: 10px;
        }

        table.record td
        {
            border: 1px solid #000080;
            padding:4px;
        }

        td.caption
        {
            width: 20px;
            font-weight: bold;
            background-color: #C0C0FF;
        }

        td.data
        {
            white-space:nowrap;
        }
        
        .right
        {
            text-align: right;
        }

    </style>

</head>
 
<body>

    <?php 
        echo "    <form action=\"{$_SERVER['SCRIPT_NAME']}\" method=\"post\" enctype=\"multipart/form-data\">\r\n";     

        echo $_out['list']; 
    ?>   

        <p> Tage |  Std  | Min <br>
         
            <input type="text" name="ctrl[lowerbound][days]" size="3" value="<?php echo $_out['ctrl']['lowerbound']['days']; ?>">
            <input type="text" name="ctrl[lowerbound][hours]" size="2" value="<?php echo $_out['ctrl']['lowerbound']['hours']; ?>">
            <input type="text" name="ctrl[lowerbound][minutes]" size="2" value="<?php echo $_out['ctrl']['lowerbound']['minutes']; ?>">
            <input type="submit" name="btn[search]" value="suchen">
        </p>
        <br>
    </form>

</body>
</html>