Home | History | Annotate | Download | only in contrib
      1 <?php
      2 
      3 /**
      4  * Another GeSHi example script
      5  *
      6  * Configure your Apache server with 'AcceptPathInfo true' and something like
      7  * 'Alias /viewmysource /var/www/geshi/contrib/aliased.php'. Don't forget
      8  * to protect this alias as necessary.
      9  *
     10  * Usage - visit /viewmysource/file.name.ext to see that file with syntax
     11  * highlighting, where "viewmysource" is the name of the alias you set up.
     12  * You can use this without an alias too, just by visiting
     13  * aliased.php/file.name.ext.
     14  *
     15  * @author  Ross Golder <ross (at) golder.org>
     16  * @version $Id: aliased.php 785 2006-07-19 10:09:45Z oracleshinoda $
     17  */
     18 
     19 // Your config here
     20 define("SOURCE_ROOT", "/var/www/your/source/root/");
     21 
     22 // Assume you've put geshi in the include_path already
     23 require_once("geshi.php");
     24 
     25 // Get path info
     26 $path = SOURCE_ROOT.$_SERVER['PATH_INFO'];
     27 
     28 // Check for dickheads trying to use '../' to get to sensitive areas
     29 $base_path_len = strlen(SOURCE_ROOT);
     30 $real_path = realpath($path);
     31 if(strncmp($real_path, SOURCE_ROOT, $base_path_len)) {
     32     exit("Stop that.");
     33 }
     34 
     35 // Check file exists
     36 if(!file_exists($path)) {
     37     exit("File not found ($path).");
     38 }
     39 
     40 // Gather contents
     41 $contents = file_get_contents($path);
     42 
     43 // Prepare GeSHi instance
     44 $geshi =& new GeSHi($contents, "PHP");
     45 $geshi->set_header_type(GESHI_HEADER_PRE);
     46 $geshi->enable_classes();
     47 $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 10);
     48 $geshi->set_overall_style('color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0;', true);
     49 $geshi->set_line_style('font: normal normal 95% \'Courier New\', Courier, monospace; color: #003030;', 'font-weight: bold; color: #006060;', true);
     50 $geshi->set_code_style('color: #000020;', 'color: #000020;');
     51 $geshi->set_link_styles(GESHI_LINK, 'color: #000060;');
     52 $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;');
     53 $geshi->set_header_content('Source code viewer');
     54 $geshi->set_header_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-bottom: 1px solid #d0d0d0; padding: 2px;');
     55 $geshi->set_footer_content('Parsed in <TIME> seconds,  using GeSHi <VERSION>');
     56 $geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;');
     57 
     58 ?>
     59 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     60      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     61 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     62 <head>
     63     <title>Source code viewer - <?php echo $path; ?></title>
     64     <style type="text/css">
     65     <!--
     66     <?php
     67         // Output the stylesheet. Note it doesn't output the <style> tag
     68     echo $geshi->get_stylesheet();
     69     ?>
     70     html {
     71         background-color: #f0f0f0;
     72     }
     73     body {
     74         font-family: Verdana, Arial, sans-serif;
     75         margin: 10px;
     76         border: 2px solid #e0e0e0;
     77         background-color: #fcfcfc;
     78         padding: 5px;
     79     }
     80     h2 {
     81         margin: .1em 0 .2em .5em;
     82         border-bottom: 1px solid #b0b0b0;
     83         color: #b0b0b0;
     84         font-weight: normal;
     85         font-size: 150%;
     86     }
     87     h3 {
     88         margin: .1em 0 .2em .5em;
     89         color: #b0b0b0;
     90         font-weight: normal;
     91         font-size: 120%;
     92     }
     93     #footer {
     94         text-align: center;
     95         font-size: 80%;
     96         color: #a9a9a9;
     97     }
     98     #footer a {
     99         color: #9999ff;
    100     }
    101     textarea {
    102         border: 1px solid #b0b0b0;
    103         font-size: 90%;
    104         color: #333;
    105         margin-left: 20px;
    106     }
    107     select, input {
    108         margin-left: 20px;
    109     }
    110     p {
    111         font-size: 90%;
    112         margin-left: .5em;
    113     }
    114     -->
    115     </style>
    116 </head>
    117 <body>
    118 <?php
    119 // The fun part :)
    120 echo $geshi->parse_code();
    121 ?>
    122 <hr/>
    123 </body>
    124 </html>
    125