<?
echo "<!--
PurpleSlurple Copyright © 2002 by Matthew A. Schneider. PurpleSlurple code is licensed under the Open Software License version 1.1.
***************************************************************
* PurpleSlurple(TM) was created by Matthew A. Schneider       *
* and was inspired by Purple, Augment, and others.            *
* It was created ostensibly for the purpose of                *
* facilitating my communication with Eric S. Raymond          *
* regarding edits to his \"How to Become a Hacker\" document. *
* I'm not kidding. You can't make this stuff up!              *
***************************************************************
-->"
;

// Location of this file (if running on your server, edit to point to your ps.php file)
$file_location = "http://purpleslurple.net/ps.php";

//if (!ereg('^[^./][^/]*$', $theurl))
//     die('bad filename'); //die, do not process

// check for target URL, if none present PS form
if (!($theurl))
{
  echo
"<title>PurpleSlurple</title>";
  echo
"<h2>Welcome to PurpleSlurple &#153;</h2>";
  echo
"<h3>Granular Addressability in HTML Documents - ON THE FLY</h3>";
  echo
"<p><b>\"Slurp up a Web page, spit back Purple numbers\"</b></p><hr>";
  echo
"<p>If you are not familiar with Purple numbers you may want to read Eugene Eric Kim's &ldquo;<a href='http://www.eekim.com/software/purple/purple.html'>An Introduction to Purple</a>&rdquo;. See also Eric Armstrong's comments on <a href='$file_location?theurl=http://www.treelight.com/software/collaboration/whatsWrongWithEmail.html#purp587'>granular addressability</a></p>";
  echo
"<p>Want one-click Purple numbers? Right-click on this link, <a href=\"javascript:location.href='$file_location?theurl='+document.location.href;\">PurpleSlurple Bookmarklet</a>, and bookmark it, or drag and drop this bookmark onto your browser's personal toolbar. Now when you are viewing a page on which you would like Purple numbers just click the bookmarklet. (Javascript must be enabled).</p><hr>";
  echo
"<p>Enter the URL of the page to which you would like to apply Purple numbers.</p>";
  echo
"<form method='get' action='ps.php'><input type='text' name='theurl' size='30'> (e.g., http://www.somedomain.com/somepage.html)<br><input type='submit' value='Submit'></form>";
  echo
"<hr><p>PurpleSlurple &#153; was created by <a href='mailto:matsch@sasites.com'>Matthew A. Schneider</a></p>";
  exit;
}

// check for http-based url (thanks Jonathan Cheyer)
if (!ereg('http://', $theurl))
     die(
'PurpleSlurple only supports http-based urls'); //die, do not process

// display PurpleSlurple header/disclaimer
echo "This page was generated by <a href='http://www.purpleslurple.net/'>PurpleSlurple</a>&#153;. The original page can be found <a href='$theurl'>here</a>.";

// expand and collapse links
echo "<br><a href='$file_location?collapse=no&theurl=$theurl'>expand</a>&nbsp;|&nbsp<a href='$file_location?collapse=yes&theurl=$theurl'>collapse</a><hr>";

// set base to ensure relative links work
// Thanks to http://marc.theaimsgroup.com/?l=php-general&m=95597547227951&w=2  Duh!
echo "<base href='$theurl'>";

// PurpleSlurple "Proxy" stuff goes here (or somewhere) someday

// collapse outline (hiding elements)
if ($collapse == "yes") {
echo
"<style>p{display:none}li{display:none}</style>";
}

// get the web page into an array, loop through array, find <h>, <p> and <li> tags, apply Purple numbers, echo back to browser
$fcontents = file ($theurl);
while (list (
$line_num, $line) = each ($fcontents)) {
    
$pattern = "<p[^>]*>|<h[1-6][^>]*>|<li[^nk>]*>";
    
$replacement = "\\0(<a href='$file_location?theurl=$theurl#purp$line_num' name='purp$line_num'><font color='purple'>$line_num</font></a>) ";
    
$purple_contents = eregi_replace($pattern, $replacement, $line);
    echo
$purple_contents;
}

// display footer
echo "<hr><a href='http://www.purpleslurple.net/'>PurpleSlurple</a>&#153; was created by <a href='mailto:matsch@sasites.com'>Matthew A. Schneider</a><br>";

// insert spacer image to facilitate scrolling to bottom links
echo '<img src="pixelshim.png" width="1" height="700" alt="pixelshim to facilitate scrolling">';
?>