Instant personal intranet: just add water!

Posted by andrew on August 19, 2005

UPDATE: Simon Brown of Safecoms has contacted me to point out a couple of — oh, let’s say gaping — security holes in my script that could allow access to any file on your system through my script, as well as the possibility to attack another computer. If you grabbed the script before noon October 21st, please replace the contents of markdown_file_results.php with what’s below. This code is now correct. Thanks again, Simon!

In the comments on the 43folders post about putting everything in one big text file (inspired by this posting in O’Reilly’s Developer Weblogs) the topic of Markdown and Textile came up quite a bit. This inspired me to finish a couple of really basic PHP scripts on which I’d been working. I’ve wanted to be able to sync all my little text notes amongst environments, but still provide a nice, clean presentation and allow search. The solution seems to be PHP Markdown and two simple scripts.

If you haven’t come across Markdown and Textile before, they’re simple markup languages that are completely human readable, and closely mimic the conventions people came up with for adding presentation to plain text Back In The Day. The syntax is very intuitive. If you’re ever building an intranet for non-technical sorts, this would be the way to get them to add markup. These are just two of the more popular lightweight markup languages; there are dozens. If another one suits you, you’ve probably got the technical chops to make these scripts work for your package of choice.

Before you can start, you’ll need PHP and a copy of PHP Markdown. If you don’t have a web server with PHP, Mac users can follow this tutorial to enable PHP on Tiger, and Windows users can grab PortableWebAp, an easy to install WAMP package.

Dump PHP Markdown in your webroot.

In your index file, add the following inside your BODY between UL tags:

<?php
$dir = "./";
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
      while (($file = readdir($dh)) !== false) {

     if (strpos($file, 'txt')) {
        echo "&lt;li&gt;&lt;a href=\"markdown_file_results.php
                ?theFile=$file\"&gt;$file&lt;/a&gt;&lt;/li&gt;";
     }

  }
  closedir($dh);

} } ?>

(That echo statement needs to be all on one line, but it breaks my site formating.)

This will create a bulleted list of all the files that contain TXT, with a link to each that runs it though the file you’re going to create next. Make a file called markdown_file_results.php that contains the usual HTML elements. Above the opening HTML tag, paste this code:

<?php
include_once "markdown.php";

$filename = $_GET[theFile];

if (preg_match('/[^A-Za-z0-9._-]/', $filename) ) { exit ('Invalid filename supplied'); }

$fp = fopen($filename, 'r'); $my_text = fread($fp, filesize($filename)); fclose($fp); $my_html = Markdown($my_text); ?> Then, inside the BODY, paste the following snippet:

<?php
echo($my_html);
?>

Put some text files in your directory, and load your index file in a web browser. You should see all your files listed. Click on one will create a valid HTML file from your source. Because you get properly marked-up HTML from this, you can then fancy everything up using CSS without affecting the readability of your plain text files one bit.

Trackbacks

Trackbacks are closed.

Comments

Comments are closed.