All pastes #824521 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-12-20 00:44 UTC
rendered paste body
<?php
/*
 *   Copyright (C) 2007  Peter Hoskin
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2 as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not,
 *                      - download it from http://codemonkey.criten.org/LICENSE.txt
 *                      - download it from http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 *                      - write to
 *                              Free Software Foundation, Inc.,
 *                              51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Created on Nov 29, 2007
 */

require_once('../include/apache.php');
require_once('../include/smarty.php');

if (array_key_exists('file',$_GET) && $_GET['file']) {
        $url = htmlspecialchars($_GET['file'], ENT_QUOTES);
        $file = $_SERVER['DOCUMENT_ROOT'].$url;
} else {
        $url = '/';
        $file = $_SERVER['DOCUMENT_ROOT'];
}
$legal_dirs = array (
    'viewsource'                                                        => 1,
    'include'                                                           => 1,
    'include/smarty'                                            => 1,
    'include/smarty/templates'                          => 1,
    'include/smarty/templates/apache'           => 1,
    'include/smarty/templates/viewsource'       => 1,
    'include/smarty/templates/codemonkey'       => 1,
    'openid'                                                            => 1,
    'tvguide/html'                                                      => 1,
    '.'                                                                         => 1,
    '/'                                                                         => 1,
);

if ($url != '/') {
        $path = '/'.$url;
} else {
        $path = $url;
}

if (!file_exists($file)) {
        // Spoof Apache's 404 Page
        $apache = new apache();
        $apache->path = $path;
        $apache->e404();
        die;
}
if (!array_key_exists(dirname($url),$legal_dirs)) {
        // Spoof Apache's 403 Page
        $apache = new apache();
        $apache->path = $path;
        $apache->e403();
        die();
}
if (!is_dir($file)) {
        $smarty = smarty_setup();
        $smarty->caching = true;
        $smarty->cache_lifetime = 3600;
        $smarty->cache_modified_check = true;
        if ((time() - filemtime($file)) < 3600) {
                $smarty->clear_cache('viewsource/viewsource.tpl',$_SERVER['REQUEST_URI']);
        }
        //echo time() - filemtime($file);
        if (!$smarty->is_cached('viewsource/viewsource.tpl',$_SERVER['REQUEST_URI'])) {
                $source = file_get_contents($file);
                $source = highlight_string($source,true);

                $smarty->assign('path',$path);
                $smarty->assign('source',$source);
                $smarty->assign('serversig',$_SERVER['SERVER_SIGNATURE']);
        }
        $smarty->display('viewsource/viewsource.tpl',$_SERVER['REQUEST_URI']);
} else {
        // Redirect to true location for directories
        if ($url != '/' && substr($file,(strlen($file) - 1),strlen($file)) != '/') {
                header("Location: ".$_SERVER['REQUEST_URI'].'/');
                die();
        }

        $apache = new apache();
        $apache->func = 'directorylist';
        $apache->cache = 0;
        $apache->cachetime = 3600;
        //if (!$apache->is_cached()) {
                // Spoof Apache's Directory List
                $scandir = scandir($file);
                $i = 0;
                foreach ($scandir as $f) {
                        if ($f != '.' && $f != '..') {
                                if (is_dir($file.$f)) {
                                        $list[$i] = array('icon'=>'folder','path'=>$f,'date'=>filemtime($file.$f));
                                        if ($file.$f == $_SERVER['DOCUMENT_ROOT'].'index.php') {
                                                $list[$i]['name'] = 'index.php';
                                                $list[$i]['path'] = 'index.php?file=/index.php';
                                        }
                                } else {
                                        $list[$i] = array('icon'=>'f','path'=>$f,'size'=>filesize($file.$f),'date'=>filemtime($file.$f));
                                }
                                $i++;
                        }
                }
                $apache->list = $list;
        //}
        $apache->path = $path;
        $apache->directorylist();
}


//
// Contents of apache.php:
//

<?php
/*
 *   Copyright (C) 2007  Peter Hoskin
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2 as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not,
 *                      - download it from http://codemonkey.criten.org/LICENSE.txt
 *                      - download it from http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 *                      - write to
 *                              Free Software Foundation, Inc.,
 *                              51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Created on Nov 28, 2007
 */

require_once('smarty.php');
require_once('filesystem.php');

/*
 * Apache Class
 *
 *
 * Functions:
 *   - clearcache                       - Clears cache entries
 *   - directorylist            - Spoofs a directory listing page
 *   - e403                                     - Spoofs a 403 page
 *   - e404                                     - Spoofs a 404 page
 *   - is_cached                        - Checks if a page is cached
 *
 * $apache->directorylist();
 *      Used to spoof the default directory listing page from Apache, with your
 *      own list from an array.
 *
 *              Example:
 *                      $apache = new apache();
 *                      $apache->list = array();
 *                      $apache->path = $_SERVER['REQUEST_URI'];
 *                      $apache->directorylist();
 *
 *              $apache->list Syntax:
 *                      Array (
 *                              [0] => Array (
 *                                      [path] => 'filename.html'
 *                                      [date] => seconds since epoch, if null the present time is assumed
 *                                      [name] => optional name spoof, will use name for readable text and path for the link
 *                                      [icon] => which apache icon should we use? .gif extention is assumed. if icon is unset, f is assumed
 *                              )
 *                              [1] => Array (
 *                                      [path] => 'index.php'
 *                                      [date] =>
 *                                      [icon] => 'script'
 *                              )
 *                      )
 *
 * Caching:
 *      Caching is provided with this class, presently with the directorylist
 *      function only. There really seems no need to cache the 403 and 404 pages
 *      considering they are rather simplistic.
 *  It is assumed your PHP code uses mod_rewrite in some form, to redirect
 *      all requests to your PHP script. Because of this, cache objects are
 *      identified internally by $_SERVER['REQUEST_URI']
 *
 *      Caching Example:
 *              $apache = new apache();
 *              $apache->cachetime = expiry time in seconds
 *              $apache->cache = 1;
 *              $apache->func = 'directorylist';
 *              if (!$apache->is_cached()) {
 *                      do complex stuff to generate a list
 *                      $apache->list = array();
 *              }
 *              $apache->directorylist();
 */
class apache {
        public $path = NULL;
        public $list = NULL;
        public $cache = 0;
        public $cachetime = 86400;
        public $func = NULL;

        // Global setup function
        function setvars() {
                if ($this->path == NULL) {
                        $this->path = $_SERVER['REQUEST_URI'];
                }
        }
        // Generate a 404 Page
        function e404() {
                $this->setvars();
                $smarty = smarty_setup();
                $smarty->assign('path',$this->path);
                $smarty->assign('serversig',$_SERVER['SERVER_SIGNATURE']);
                header("HTTP/1.0 404 Not Found");
                $smarty->display('apache/404.tpl');
        }

        // Generate a 403 Page
        function e403() {
                $this->setvars();
                $smarty = smarty_setup();
                $smarty->assign('path',$this->path);
                $smarty->assign('serversig',$_SERVER['SERVER_SIGNATURE']);
                header("HTTP/1.0 403 Forbidden");
                $smarty->display('apache/403.tpl');
        }

        // Generate a Directory List
        function directorylist() {
                $this->setvars();
                $smarty = smarty_setup();
                // Caching enabled?
                if ($this->cache == 1) {
                        $smarty->cache_lifetime = $this->cachetime;
                        $smarty->caching = true;
                } else {
                        $smarty->caching = false;
                }

                /* Page cached?
                 *
                 *  Utilizing caching here avoids some looping and is a performance benefit
                 */
                //if (!$smarty->is_cached('apache/directorylist.tpl',$_SERVER['REQUEST_URI'])) {
                        if (is_array($this->list)) {
                                // Convert sizes into readable values
                                // This is a bit of performance bog, perhaps the OPTION
                                // to do this outside the class and disable this function?
                                // This really does need to exist tho, as 'off the shelf'
                                // PHP has no human readable format and most use of this class
                                // will use filesize()
                                foreach ($this->list as $key => $row) {
                                        if (array_key_exists('size',$row)) {
                                                $this->list[$key]['size'] = size_readable($row['size']);
                                        }
                                }
                                $smarty->assign('list',$this->list);
                        }
                        $smarty->assign('path',$this->path);

                        // TODO: fix parent link
                        $data = split("/",$this->path);
                        $parent = print_r($data,true);

                        $smarty->assign('parent',$parent);
                        $smarty->assign('serversig',$_SERVER['SERVER_SIGNATURE']);
                //}
                // Fetch page
                $smarty->display('apache/directorylist.tpl',$_SERVER['REQUEST_URI']);
        }


        function is_cached() {
                if ($this->func == NULL) {
                        echo "<b>Error:</b> class apache->is_cached called, but apache->func is null<br><br>";
                        return 0;
                        die();
                }
                $smarty = smarty_setup();
                $smarty->cache_lifetime = $this->cachetime;
                if ($smarty->is_cached('apache/'.$this->func.'.tpl',$_SERVER['REQUEST_URI'])) {
                        return 1;
                } else {
                        return 0;
                }

        }
}


//
// Contents of smarty.php
//
<?php
/*
 *   Copyright (C) 2007  Peter Hoskin
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2 as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not,
 *                      - download it from http://codemonkey.criten.org/LICENSE.txt
 *                      - download it from http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 *                      - write to
 *                              Free Software Foundation, Inc.,
 *                              51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Created on Nov 28, 2007
 */

require_once('config.php');
require_once(SMARTY_PATH);

function smarty_setup() {
        $smarty = new Smarty();
        $smarty->template_dir = SMARTY_TEMPLATEDIR;
        $smarty->compile_dir = SMARTY_COMPILEDIR;
        $smarty->cache_dir = SMARTY_CACHEDIR;
        $smarty->config_dir = SMARTY_CONFIGDIR;

        return $smarty;
}
?>

//
// Contents of filesystem.php
//
<?php
/*
 *   Copyright (C) 2007  Peter Hoskin
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2 as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not,
 *                      - download it from http://codemonkey.criten.org/LICENSE.txt
 *                      - download it from http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 *                      - write to
 *                              Free Software Foundation, Inc.,
 *                              51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Created on Nov 29, 2007
 */


/*
 * size_readable($size, $retstring)
 *
 * Returns filesize in human readable format
 *
 * Modified and relicensed from:
 * http://aidanlister.com/repos/v/function.size_readable.php
 */
function size_readable ($size, $retstring = null) {
        $sizes = array('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
        if ($retstring === null) { $retstring = '%01.2f%s'; }
        $lastsizestring = end($sizes);
        foreach ($sizes as $sizestring) {
                if ($size < 1024) { break; }
                if ($sizestring != $lastsizestring) { $size /= 1024; }
        }
        if ($sizestring == $sizes[0]) { $retstring = '%01d'; }
        return sprintf($retstring, $size, $sizestring);
}
?>