<?php
//===============================================================================
//  Read a remote file or a directory
//===============================================================================
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);  //all except warnings & notices

    $allow_dir = true;                              #allow directory read?                                                      

    $filename = $_GET['file'];                      #server file to be read (required)
    $page     = $_GET['page'];                      #starting page num to read 
    $limit    = $_GET['limit'];                     #number of lines to read

    if (!$page)  $page  = 1;
    if (!$limit) $limit = 20;                       #Page size = 20 lines (default)

    if ($filename)
    {
        read_data();
        send_data();
    }

//===============================================================================
//  Read a file or a directory
//  If a file, limit the number of records to 20 (default)
//===============================================================================
    function read_data()
    {
        global $filename, $page, $limit, $output, $allow_dir;   
 
        $line_num   = 0;                                        //line number within the file
        $line_count = 0;                                        //line count in the output array
        $line_start = ($page-1) * $limit;                       //line number to start readling from

        if (is_dir($filename) && !$allow_dir)                   //if directory listing not allowed
            die ("error|Diectory listing not allowed");         //exit 
                
        if (is_dir($filename))                                  //if the file is a directory
        {
            $handle = opendir ($filename)  
                      or die ("error|Cannot open $filename as a directory"); 
                
            while ($line = readdir($handle)) 
                if ($line != '.' && $line != '..')              //if not current or parent directory 
                    $output[] = $line;                          //add to the output array
                            
            closedir($handle);           
            sort($output);
        }                 
        else                                                    //if the file is a regular file
        { 
            $handle = fopen ($filename, 'r')  
                      or die ("error|Cannot open $filename - File does not exist, or permission denied"); 

            while($line = fgets($handle))
            {
                $line_num++;                

                if ($line_num >= $line_start && $line_count < $limit)
                    $output[$line_count++] = rtrim($line,"\n");         //add to the output array
            }
            fclose($handle);
        }
    }

//===============================================================================
//  Send the content of the file back
//  If content is a file, send 1 page only.  default each page = 20 records
//  output format for directory listing:
//                                         dir|dirname
//                                         file|filename
//  output format for file content:
//                                         page|123
//                                         9999|record
//  output format for error:
//                                         error|message
//===============================================================================
    function send_data()
    {
        global $filename, $page, $limit, $output;   
        
        $output_str = "";

        if (is_dir($filename))					//if it is a directory:
        {
            if ($output)                                        	//only if there is content
                $output_str .= "dirList|directory listing \n";		//"dirList|directory Listing"

            foreach($output as $rec)
            {
                if (is_dir($filename.'/'.$rec))
                    $output_str .= "Dir|$rec \n";			//if the file is a directory
                else
                    $output_str .= "File|$rec \n";			//if the file is a file
            }   
        }
        else							//else it is a file:    
        {            
            $num = ($page-1) * $limit;                          	//starting line number 
                                                     
            if ($output)                                        	//only if there is content
                $output_str .= "page|$page \n";				//"page|999"

            foreach($output as $rec)
            {
                $num++;
                $output_str .= "$num|$rec \n";				//"9999|<record>"
            }        
        }

        print $output_str;
    }
//===============================================================================
?>