Source code for /Users/Shared/www/vhosts/top-frog/public_html/script_src/source.php

  1.  <?php
  2.   /**
  3.   * PHPS source file viewer
  4.   *
  5.   * @author: Shawn Parker, http://www.top-frog.com
  6.   * http://www.top-frog.com/archives/2006/11/22/show_php_source_without_duplicating_files
  7.   * This file has been modified slightly since the article was written
  8.   *
  9.   * @requires: http://pear.php.net/package/Text_Highlighter/docs
  10.   *
  11.   * @example: put this file and an htaccess file with the information outlined below in a
  12.   * dedicated directory. Add in any other files that you'd like to view the source for and
  13.   * call them with an extra s in the name. So, for example myscript.php can be viewed at myscript.phps
  14.   *
  15.   * .htaccess requirement
  16.   *
  17.   * include the following items in the .htaccess file in
  18.   * the directory you'd like to use
  19.  
  20.   Options FollowSymLinks
  21.  
  22.   RewriteEngine on
  23.   RewriteRule (.*) source.php
  24.  
  25.   *
  26.   */
  27.  
  28.   $types = array(
  29.   'js' => 'JAVASCRIPT',
  30.   'php' => 'PHP',
  31.   'phps' => 'PHP',
  32.   'css' => 'CSS',
  33.   'xml' => 'XML',
  34.   'sql' => 'SQL',
  35.   'py' => 'PYTHON',
  36.   'htm' => 'HTML',
  37.   'html' => 'HTML',
  38.   'shtml' => 'HTML',
  39.   'rb' => 'RUBY',
  40.   'pl' => 'PERL',
  41.   'sh' => 'SQL' // not a great solution, but it looks the best
  42.   );
  43.  
  44.   $filename = str_replace('phps','php',array_pop(explode('/',$_SERVER['REQUEST_URI'])));
  45.   $file = dirname(realpath(__FILE__)).($filename != 'source.php' ? '/files/' : '/').$filename;
  46.   if(is_file($file)) {
  47.   require_once('Text/Highlighter.php');
  48.   $th =& Text_Highlighter::factory($types[pathinfo($file,PATHINFO_EXTENSION)],array('numbers' => HL_NUMBERS_LI));
  49.   $src = $th->highlight(file_get_contents($file));
  50.   }
  51.   else {
  52.   $src = '<p>I could not find <i>'.$file.'</i> in the allowed directory structure. '.
  53.   'Please check your request and try again. If you followed a link here then please '.
  54.   'contact that system administrator and inform him/her of the bad link.</p>';
  55.   $file = 'File not found';
  56.   }
  57.  
  58.  ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  59.   "http://www.w3.org/TR/html4/strict.dtd">
  60.  <html lang="en">
  61.   <head>
  62.   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  63.   <title>Source for: <?php echo $file; ?></title>
  64.   <link rel="stylesheet" type="text/css" href="highlight.css" />
  65.   <style type="text/css" media="all">
  66.   body {
  67.   margin: 30px;
  68.   }
  69.   h1, #footer {
  70.   background: #ddd;
  71.   }
  72.   h1 {
  73.   font-family: arial, helvetica, sans-serif;
  74.   font-size: 18px;
  75.   border-bottom: 1px solid gray;
  76.   padding: 10px;
  77.   }
  78.   #footer{
  79.   padding: 5px;
  80.   border-top: 1px solid gray;
  81.   font-size: 11px;
  82.   margin-top: 15px;
  83.   }
  84.   #footer p {
  85.   margin: 0;
  86.   padding: 0;
  87.   }
  88.   ol.hl-main {
  89.   background: #efefef;
  90.   }
  91.   ol.hl-main li {
  92.   white-space: pre;
  93.   font: 10px/1.3 sans-serif;
  94.   background: white;
  95.   margin-bottom: 1px;
  96.   padding: 1px;
  97.   }
  98.   ol.hl-main li span {
  99.   font: 12px/1.3 courier,"courier new", monospaced;
  100.   }
  101.   </style>
  102.   </head>
  103.   <body>
  104.   <h1>Source code for <?php echo $file; ?></h1>
  105.   <div id="source">
  106.  <?php echo $src ?>
  107.   </div>
  108.   <div id="footer"><p>Page delivered by: <a href="source.phps">PHPS source file viewer</a>.</p></div>
  109.   </body>
  110.  </html>