sourceview.php

Denna kod är public domain. Om ni hittar fel eller vill ändra något i koden blir jag jätteglad om ni skickar dessa ändringar till jesper [at] fantasi [punkt] se.


<?php
  $title="Sourceview -- ".$_GET["file"];
  $currentFile=__FILE__;
  include("header.php");
  if ($_GET["file"] != "")
  {
     $badRequest = 0;

     if ($_GET["file"][0] == '/')
        $badRequest = 1;
     else
     {
        $nr = 0;
        for ($i = 0; $i < strlen($_GET["file"]); $i++)
        {
           if ($_GET["file"][$i] == '.')
              $nr++;
           else if ($_GET["file"][$i] < '-')
           {
              $nr++;
              $_GET["file"][$i] = '-';
           }
        }
     }

     if ($nr != 1)
        $badRequest = 1;

     if ($badRequest == 0)
     {
        echo "<h1>".basename($_GET["file"])."</h1>";
        echo "<p class=\"maincontent\">Denna kod &auml;r public domain. ";
        echo "Om ni hittar fel eller vill &auml;ndra n&aring;got i koden blir ";
        echo "jag j&auml;tteglad om ni skickar dessa &auml;ndringar till ";
        echo "jesper [at] fantasi [punkt] se.";
        echo "</p><hr>";
        $file = fopen($_GET["file"],"r");
        if ($file != null)
        {
           echo "<pre class=\"codebox\">";
           while (!feof($file))
           {
              $ch = fgetc($file);
              switch ($ch)
              {
                 case '<': echo "&lt;"; break;
                 case '>': echo "&gt;"; break;
                 case '&': echo "&amp;"; break;
                 default: echo $ch;
              }
           }
           fclose($file);
           echo "</pre>";
        }
        else
           echo "<p class=\"maincontent\">Unable to open file.</p>";
     }
     else
        echo "<p class=\"maincontent\">I'd prefer not to display that page.</p>";
  }
  else
     echo "<p class=\"maincontent\">No file to open...</p>";
  include("footer.php");
?>