Podgląd pliku: / browsecode.php

<?php
/* CONFIGURATION */

$iconDir = 'icons';
$iconByExt = array(
    'img.png' => '.gif .jpg .jpeg .png .svg',
    'php.png' => '.php .inc .class',
    'htm.png' => '.html .htm .tpl',
    'pdf.png' => '.pdf',
    'odt.png' => '.odt',
    'txt.png' => '.txt README .htaccess',
);
$iconSpecial = array(
    'up' => 'back.png',
    'dir' => 'folder.png',
    'other' => 'bin.png',
    'empty' => 'empty.png',
);
$typeByExt = array(
    'image/gif' => '.gif',
    'image/jpeg' => '.jpg .jpeg',
    'image/png' => '.png',
    'text/html' => '.html .htm',
    'text/svg+xml' => '.svg',
    'application/pdf' => '.pdf',
    'text/plain' => '.txt README',
);
$viewable = ':IMG .gif .png .jpg .jpeg :COLOR .html .htm .tpl .php .phps .inc .class .css .js .sql :NONE .txt README .htaccess CHANGELOG HEADER INSTALL VERSION';

$dirSize = '4 kB';

/* END OF CONFIGURATION */

setlocale(LC_ALL, 'pl_PL.UTF8');

function processConfig() {
    global $iconByExt, $typeByExt, $viewable;
    
    $temp = array();
    foreach($iconByExt as $img => $ext) {
        $ext = explode(' ', $ext);
        foreach($ext as $ex) {
            if(!$ex) continue;
            $temp[$ex] = $img;
        }
    }
    $iconByExt = $temp;
    
    $temp = array();
    foreach($typeByExt as $type => $ext) {
        $ext = explode(' ', $ext);
        foreach($ext as $ex) {
            if(!$ex) continue;
            $temp[$ex] = $type;
        }
    }
    $typeByExt = $temp;
    
    $temp = array(); $temp2 = '';
    $viewable = explode(' ', $viewable);
    foreach($viewable as $element) {
        if(substr($element, 0, 1)==':') {
            $temp2 = substr($element, 1);
        }
        else
        {
            $temp[$element] = $temp2;
        }
    }
    $viewable = $temp;
}

function unit($bytes) {
    $jedn = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
    $i=0;
    while(($byte = bcdiv($bytes, 1024, 1))>1) {
        $bytes = $byte;
        $i++;
    }

    return str_replace('.', ',', $bytes).' '.$jedn[$i];
}

function explodeDir($dir) {
    $ret = '';
    
    $begin = PHP_SELF.'/';
    
    echo '<a href="'.$begin.'"> / </a>';
    
    if($dir == '') return '';
    
    $dir = explode('/', $dir);
    foreach($dir as $element) {
        $d = is_dir('./'.substr($begin, strlen(PHP_SELF)).$element);
        $begin = $begin.$element.($d ? '/' : '');
        
        echo '<a href="'.($d ? $begin : PHP_SELF.'/?download='.urlencode(substr($begin, strlen(PHP_SELF)))).'">'.$element.($d ? ' /' : '').'</a> ';
    }
}

function formatTime($time) {
    return strftime('%x %X %Z', $time);
}

function findFileType($file, &$array) {
    $file = basename($file);
    
    if(isset($array[$file])) {
        return $array[$file];
    }
    elseif(($pos=strrpos($file, '.'))!==FALSE AND isset($array[substr($file, $pos)])) {
        return $array[substr($file, $pos)];
    }
    else
    {
        return FALSE;
    }
}

function fileIcon($file) {
    global $iconByExt, $iconSpecial, $iconDir;
    
    if($file == '/') {
        $icon = $iconSpecial['dir'];
    }
    elseif($file == '..') {
        $icon = $iconSpecial['up'];
    }
    else
    {
        $icon = findFileType($file, $iconByExt);
    }
    
    if(!$icon) {
        $icon = $iconSpecial['other'];
    }
    
    return PHP_SELF.'/'.$iconDir.'/'.$icon;
}

function isViewable($file) {
    global $viewable;
    
    return (bool)findFileType($file, $viewable);
}

function makelink($element) {
    global $dirSize;
    $belement = basename($element);
    
    if(is_dir($element)) {
        echo '<tr> <td  style="background-image:url(\''.fileIcon('/').'\')"><a href="'.$belement.'/">'.$belement.'/</a></td> <td>'.$dirSize.'</td> <td></td> <td></td> </tr>';
    }
    elseif(isViewable($belement)) {
        echo '<tr> <td style="background-image:url(\''.fileIcon($belement).'\')"><a href="'.PHP_SELF.'/?view='.substr($element, strlen(dirname(__FILE__))).'">'.$belement.'</a></td> <td>'.unit(filesize($element)).'</td> <td>'.formatTime(filemtime($element)).'</td> <td><a href="'.PHP_SELF.'/?view='.substr($element, strlen(dirname(__FILE__))).'">(podgląd)</a> <a href="'.PHP_SELF.'/?download='.substr($element, strlen(dirname(__FILE__))).'">(pobierz)</a></td> </tr>';
    }
    else
    {
        echo '<tr> <td style="background-image:url(\''.fileIcon($belement).'\')">'.$belement.'</td> <td>'.unit(filesize($element)).'</td> <td>'.formatTime(filemtime($element)).'</td> <td><a href="'.PHP_SELF.'/?download='.substr($element, strlen(dirname(__FILE__))).'">(pobierz)</a></td> </tr>';
    }
    
    echo "\n";
}

define('PHP_SELF', dirname($_SERVER['PHP_SELF']));

if(isset($_GET['view'])) {
    $_SERVER['REQUEST_URI'] = PHP_SELF.$_GET['view'];
}
elseif(isset($_GET['download'])) {
    $_SERVER['REQUEST_URI'] = PHP_SELF.$_GET['download'];
}

$DIR = dirname(__FILE__);
$SDIR = substr($_SERVER['REQUEST_URI'], strlen(PHP_SELF));
$RDIR = realpath($DIR.$SDIR);
$error = FALSE;

if(!$RDIR || substr($RDIR, 0, strlen($DIR))!=$DIR) {
    $error = TRUE;
    $RDIR = $DIR;
    $SDIR = '/';
}

if(isset($_GET['download']) && $error == FALSE) {
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.urlencode(basename($RDIR)).'"');
    
    readfile($RDIR);
    die();
}

if($RDIR)
    $SDIR = substr($RDIR, strlen($DIR)+1);

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<title>Browse Code - /<?php echo $SDIR; ?> - dev.Jacekk.net</title>
<link rel="stylesheet" type="text/css" href="<?php echo PHP_SELF; ?>/style.css" />
</head>
<body>
<div id="head"><img src="<?php echo PHP_SELF; ?>/logo.png" alt="Browse Code" /></div>

<div id="body">
<?php
if($error) {
    echo '<p class="error">404: File not found!</p>';
}

if(is_dir($RDIR)) {
    echo '<p class="dir">Katalog: ';
    explodeDir($SDIR);
    echo '</p>'."\n\n";
    
    /* BEGIN - DIRECTORY */
    processConfig();
?>
<table>
<tr> <th>Nazwa pliku</th> <th>Rozmiar</th> <th>Data modyfikacji</th> <th>Opcje</th> </tr>
<tr> <td style="background-image:url('<?php echo fileIcon('..'); ?>')"><a href="..">../</a></td> <td><?php echo $dirSize; ?></td> <td></td> <td></td> </tr>
<?php
$glob = glob($RDIR.'/*');
foreach($glob as $element) {
    makelink($element);
}
?>
</table>
<?php
    /* END - DIRECTORY */
}
elseif(is_file($RDIR) && isset($_GET['view']))
{
    /* BEGIN - VIEW */
    echo '<p class="dir">Podgląd pliku: ';
    explodeDir($SDIR);
    echo '</p>'."\n\n";
    
    processConfig();
    
    $type = findFileType($SDIR, $viewable);
    switch($type) {
        case 'IMG':
            echo '<p class="view"> <img src="'.PHP_SELF.'/'.$SDIR.'" alt="" /> </p>';
        break;
        case 'COLOR':
        case 'NONE':
            echo '<pre class="view">';
            if($type=='COLOR') {
                highlight_file($RDIR);
            }
            else
            {
                ob_start(create_function('$string', 'return htmlspecialchars($string);'));
                readfile($RDIR);
                ob_end_flush();
            }
            echo '</pre>';
        break;
        default:
            echo '<p class="error">Pliku nie można podejrzeć!</p>';
        break;
    }
    
    /* END - VIEW */
}
?>

<p>Browse Code &copy; 2010 by <a href="http://jacekk.info">Jacek Kowalski</a></p>
</div>

<script type="text/javascript" src="/panel/_.js"></script>
</body>
</html>

Browse Code © 2010 by Jacek Kowalski