///////////////////////////////////////////////
//PHP functions for Randomly Ever After
//begun in the Summer of 2001 --
//greatly expanded in the Fall and Winter of 2003
//Many of these have origins in old ASP functions
//for Vodkatea and Bathtubgirl
//though some are original or I developed them in
//the PHP world in places like CaSanctuary.org
//or Jonbowermaster.com
////////////////////////////////////////////////
function thisfilename()
{
//returns the name of the file we are on, with no path
$arrfilename=split("/",$GLOBALS["PHP_SELF"]);
$strfilename=$arrfilename[count($arrfilename)-1];
return($strfilename);
}
function datecodetourl($datecode)
{
//given a six character code of the form YYMMDD, it returns the relative URL of its location in the file system
$out="";
$upper=substr($datecode, 0, 4);
$extension="php";
$daycode=substr($datecode, 4, 2);
$monthcode=intval(substr($datecode, 2, 2));
$yearcode=substr($datecode, 0, 2);
$arrMusingsMonths=array("", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
//echo $datecode . "
" ;
if ($datecode>"961130" and $datecode<"980923") //conventional musings entry
{
//echo("!!");
$extension="htm";
$monthstring=$arrMusingsMonths[$monthcode];
$out="../musings/" . $monthstring . $yearcode . "/" . $daycode;
}
elseif ($datecode<"961201" and $datecode<"980923" and $datecode >todaydatecode()) //all-in-one musings entries
{
if ($datecode>"960831" and $datecode<"961001") //fucked up september case
{
$out="../musings/sept96.htm#" . $daycode;
$extension="";
}
else if($datecode>"960730")
{
$monthstring=$arrMusingsMonths[$monthcode];
$extension="";
$out="../musings/" . $monthstring . "96.htm#" . $daycode;
}
}
else //randomly every after, either php or htm ending
{
$out = $upper . "/" . $datecode . "." ;
}
if (file_exists($out . "htm")) //i'll use whichever ending is available, or none.
{
$out=$out . "htm";
}
else if (file_exists($out . "php"))
{
$out=$out. "php";
}
else if (file_exists($out ))
{
$out=$out;
}
else //return nothing if the file doesn't exist. we do not create entry files in this system; those are uploaded
{
$out="";
}
return($out);
}
function monthofRanArticleList($datecode)
{
//give me an unformatted list of linked numbers - not a true calendar
$monthpart=substr($datecode,0,4);
$out= filereadcache("m" . $monthpart, calculateexpiration($datecode));
//$out="";
if ($out=="")
{
$count=0;
$poo=0;
//first day of next month later followed by one day earlier
//$datecode=firstofnextmonth($datecode);
//$datecode=earlierday($datecode);
$datecode=$monthpart."01";
$datearray=getdate(makedate($datecode));
$month=$datearray["month"];
$year=$datearray["year"];
$out=$out . "" . $month . " " . $year . "
";
while (substr($datecode,0,4)==$monthpart and $poo<70)
{
$url=datecodetourl($datecode);
$day=substr($datecode, 4,2);
//echo($url . "
");
if (file_exists($url) and strpos($url, "#")<1)
{
$handle=fopen ($url, "r");
$content=fread($handle, filesize($url));
fclose($handle);
$title=parseRanTitle($content);
if (substr($title, 0, 1)!="*") //do not show asterisk-preceded titles
{
$out=$out . $day . ": " . $title . "" . chr(13);
$description=parseRanDescription($content);
$out=$out . " - " . $description . "
" . chr(13);
}
$count++;
}
$datecode=laterday($datecode);
$poo++;
}
filewritecache("m".$monthpart, $out);
}
return($out);
}
function recentRanArticleList($intNumber, $intNumberOfDescriptions)
{
//$intNumber recent ran articles, the first $intNumberOfDescriptions having descriptions too
$todaydatecode=todaydatecode();
$count=0;
$poo=0;
$out="";
while ($count<$intNumber && $poo<70)
{
$url=datecodetourl($todaydatecode);
if (file_exists($url))
{
$handle=fopen ($url, "r");
$content=fread($handle, filesize($url));
fclose($handle);
$title=parseRanTitle($content);
if (substr($title, 0, 1)!="*") //do not show asterisk-preceded titles
{
$out=$out . "" . $title . "";
if ($intNumberOfDescriptions>0)
{
$description=parseRanDescription($content);
$out=$out . " - " . $description . "
";
$intNumberOfDescriptions--;
}
else
{
$out=$out . "
";
}
}
$count++;
}
$todaydatecode=earlierday($todaydatecode);
$poo++;
}
return($out);
}
function parseContent($strIn)
{
if (strpos($strIn, "$" . "articletitle=" . chr(34))>0)
{
$content=parseBetween($strIn, "<" . "?include(\"../header.php\")" . "?" . ">", "<" . "?include(\"../footer.php\")?" . ">");
}
else
{
$content=parseBetween($strIn, "
", "
"); //if (strlen($strIn)<$pos1) //{ // $pos1=0; //} $pos2 = strpos($strIn, $strEnd, $pos1); //echo "
=" . $pos1 . " " . $pos2 . "=
"; $found=substr($strIn, $pos1, ($pos2-$pos1)); return($found); } function yearlinks($datecode) { //the months in a year, old method (still used in thin month sliver) $datearray=getdate(makedate($datecode)); $year=$datearray["year"]; $earlieryear=$year-1; $lateryear=$year+1; $extension=decideextension($earlieryear); $target=""; $base="days.php?"; $thisbase="years.php"; if ($extension=="htm") { $earlierurl=$earlieryear . "." . $extension; } else { $earlierurl= $thisbase . "?" . substr($earlieryear,2,2) . "0101"; } $laterurl=$thisbase . "?" . substr($lateryear,2,2) . "0101"; $startdate= substr($year,2,2) . "0101"; $out="
"; $out=$out . ""; if (substr($earlieryear, 2, 2) . "0101">"950801" or substr($earlieryear, 2, 2) . "0101" < todaydatecode()) { $out=$out . "" . $earlieryear. " | "; } else { $out=$out . $earlieryear. " | "; } if (todaydatecode() > substr($lateryear, 2, 2) . "0101" or substr($lateryear, 2, 2) . "0101">"950801") { $out=$out . "" . $lateryear . ""; } else { $out=$out . $lateryear ; } $out=$out . ""; return($out); } function dyearlinks($datecode) { //a better function for showing a full year of month links, with proper noun abstracts. $expiration=calculateexpiration($datecode); $datearray=getdate(makedate($datecode)); $year=$datearray["year"]; $out=filereadcache("y" . $year, $expiration); if ($out=="") { $earlieryear=$year-1; $lateryear=$year+1; $extension="php"; $target=""; $base="months.php?"; $thisbase="dyears.php"; $content=""; $earlierurl= $thisbase . "?" . substr($earlieryear,2,2) . "0101"; $laterurl=$thisbase . "?" . substr($lateryear,2,2) . "0101"; $startdate= substr($year,2,2) . "0101"; $out="
" . $year . "
"; $t=2; while ($t<14) { $datearray=getdate(makedate($startdate)); $month=str_pad($datearray["mon"], 2, '0', STR_PAD_LEFT); $monthname=$datearray["month"]; $extension=decideextension($startdate); $monthpart=substr($year,2,2) . $month; if ($monthpart . "01"<=todaydatecode() or $monthpart . "01">"960730" ) { $url="cache/m" . $monthpart . ".htm"; if (file_exists($url)) { $handle=fopen ($url, "r"); $content=fread($handle, filesize($url)); fclose($handle); $content =ProperNounList($content); } else { $content =""; } if (inList("9607 9608 9609 9610 9611", $monthpart)) { $target=" target=_top"; $link=datecodetourl($monthpart ."01"); } else { $link=$base . $monthpart ."01"; } $out=$out . "" . $monthname. " - " . $content . ""; } else { $out=$out . "". $monthname. "
"; } $startdate= substr($year,2,2) . str_pad($t, 2, '0', STR_PAD_LEFT) . "01"; $t++; } $out=$out . "
";
$out=$out . "";
if (substr($earlieryear, 2, 2) . "0101">"950801" or substr($earlieryear, 2, 2) . "0101" < todaydatecode())
{
$out=$out . "" . $earlieryear. " | ";
}
else
{
$out=$out . $earlieryear. " | ";
}
if (todaydatecode() > substr($lateryear, 2, 2) . "0101" or substr($lateryear, 2, 2) . "0101">"950801")
{
$out=$out . "" . $lateryear . "";
}
else
{
$out=$out . $lateryear ;
}
$out=$out . "
index of years";
$out=$out . "";
filewritecache("y" . $year, $out);
}
return($out);
}
function allyears()
{
//give me a list of all years my journal has existed, with a sampling of important proper nouns
$out=filereadcache("allyears", 600);
if ($out=="")
{
$out="
complete archive
"; $datearray=getdate(time()); $thisyear=$datearray["year"]; for($year=1996; $year<=$thisyear; $year++) { $yearpart=substr($year, 2, 2); $link="dyears.php?" . $yearpart . "0101"; $url="cache/y" . $year . ".htm"; if (file_exists($url)) { $handle=fopen ($url, "r"); $content=fread($handle, filesize($url)); fclose($handle); } $content =ProperNounList($content); if (strlen($content)>350) { $content =filterwordsize($content , 8, 18); } $out=$out . "" . $year. " - " . $content . "
";
}
filewritecache("allyears", $out);
}
return($out);
}
function filterwordsize($strIn, $intMin, $inMax)
{
//filters words in $strIn not between $intMin and $intMax in size
$strOut="";
$arrThis=explode(" ",$strIn);
foreach($arrThis as $this)
{
//echo $this . "-";
if (strlen($this)<$inMax and strlen($this)>$intMin)
{
$strOut=$strOut . " " . $this;
}
}
return($strOut);
}
function latestdateurl()
//give me the latest entry
{
$datecode=todaydatecode();
$out="";
while ($out=="")
{
$monthcode=monthcode($datecode);
$dir="" . $monthcode;
if (is_dir($dir))
{
$dh = opendir($dir);
while (false !== ($filename = readdir($dh)))
{
$files[] = $filename;
}
rsort($files);
foreach ($files as $file)
{
list($name, $extension )= split("\.", $file);
if (strlen($name)==6)
{
if (ereg("^[0-9]+$",$name))
{
if (makedate($name))
{
if ($name>$out)
{
$out=$name;
}
}
}
}
}
}
$datecode=earliermonthcode($datecode) . "01";
}
return($monthcode . "/" . $out . ".php");
}
function monthdescription($datecode )
{
//what a month heading looks like, with links
$datearray=getdate(makedate($datecode));
$month=$datearray["month"];
$year=$datearray["year"];
$extension=decideextension($datecode);
//$extension="php";
if ($extension=="htm")
{
$out=$month . " " . $year . "";
}
else
{
$out=$month . " " . $year . "";
}
return($out);
}
function monthnav($datecode )
{
//nav for simple undescriptive month link frames
$earliermonth=datecodetomonthdescription(earliermonthcode($datecode) . "01", 2);
$latermonth=datecodetomonthdescription(latermonthcode($datecode) . "01", 2);
$latermonthcode=latermonthcode($datecode);
$out="" . $earliermonth . "
\n";
if ($latermonthcode."01" < todaydatecode())
{
$out=$out . "" . $latermonth . "";
}
return($out);
}
function descriptivemonthnav($datecode )
{
//a nav for the more interesting month link pages
$datearray=getdate(makedate($datecode));
$year=$datearray["year"];
$earliermonth=datecodetomonthdescription(earliermonthcode($datecode) . "01", 2);
$latermonth=datecodetomonthdescription(latermonthcode($datecode) . "01", 2);
$latermonthcode=latermonthcode($datecode);
$out="" . $earliermonth . " | \n";
if ($latermonthcode."01" < todaydatecode() or $latermonthcode."01">"960730")
{
$out=$out . "" . $latermonth . "";
}
else
{
$out=$out . $latermonth ;
}
$out=$out . "
" . $year . "";
return($out);
}
function datecodetomonthdescription($datecode, $yearlen)
{
$datearray=getdate(makedate($datecode));
$month=$datearray["month"];
$year=$datearray["year"];
if ($yearlen!=4)
{
$year=substr($year,2,2);
}
return($month . " " . $year);
}
function earliermonthurl($datecode)
{
$earliermonthcode=earliermonthcode($datecode);
$extension=decideextension($earliermonthcode . "01");
if ($extension!="htm")
{
$out="days.php?" . $earliermonthcode . "01";
}
else
{
$out=$earliermonthcode . "/days.htm";
}
return ($out);
}
function dearliermonthurl($datecode)
{
$earliermonthcode=earliermonthcode($datecode);
$extension=decideextension($earliermonthcode . "01");
$out="months.php?" . $earliermonthcode . "01";
return ($out);
}
function dlatermonthurl($datecode)
{
$latermonthcode=latermonthcode($datecode);
$extension=decideextension($latermonthcode . "01");
$out="months.php?" . $latermonthcode . "01";
return ($out);
}
function latermonthurl($datecode)
{
$latermonthcode=latermonthcode($datecode);
$extension=decideextension($latermonthcode . "01");
if ($extension="htm")
{
$out="days.php?" . $latermonthcode . "01";
}
else
{
$out=$latermonthcode . "/days.htm";
}
return ($out);
}
function earliermonthcode($datecode)
{
//returns a monthcode from the month preceding datecode
$firstofmonth=firstofmonth($datecode);
$firstofmonthdate=makedate($firstofmonth);
$mildlyearlierdatecode=makedatecode(adddaystodate($firstofmonthdate, -5));
$out=monthcode($mildlyearlierdatecode);
return ($out);
}
function latermonthcode($datecode)
{
//returns a monthcode from the month preceding datecode
$firstofmonth=firstofmonth($datecode);
$firstofmonthdate=makedate($firstofmonth);
$mildlylaterdatecode=makedatecode(adddaystodate($firstofmonthdate, 35));
$out=monthcode($mildlylaterdatecode);
return ($out);
}
function monthcode($datecode)
{
$out=substr($datecode,0,4);
return ($out);
}
function monthlinks($datecode)
{
//returns a list of links for the month up to $datecode
$firstofmonth=firstofmonth($datecode);
$newdatecode=$firstofmonth;
$out="";
$p=1;
$datecodetest=laterday($datecode);
$todayceiling=laterday(todaydatecode());
while ($datecodetest>$newdatecode and $p<40 and $newdatecode<$todayceiling)
{
$monthcode=monthcode($datecode);
$day=substr($newdatecode,4,2);
$extension=decideextension($datecode);
$path=$monthcode . "/" . $newdatecode . "." . $extension;
if (is_file($path) )
{
if ($datecode>"030700")
{
$strHref="d.php?datecode=" . $newdatecode ;
}
else
{
$strHref=$monthcode . "/" . $newdatecode . "." . $extension;
}
$out=$out . "" . $day . " " ;
}
else
{
$out=$out . " " . $day . " " ;
}
$newdatecode=laterday($newdatecode);
$p=$p+1;
}
return ($out);
}
function firstofmonth($datecode)
{
$year=substr($datecode,0,2);
$month=substr($datecode,2,2);
$out=$year . $month . "01";
return($out);
}
function todaydatecode()
{
$datearray=getdate();
$year=substr($datearray["year"],2,2);
$month=str_pad($datearray["mon"], 2, '0', STR_PAD_LEFT);
$day=str_pad($datearray["mday"], 2, '0', STR_PAD_LEFT);
$out=$year . $month . $day;
return($out);
}
function displayrandate($datecode)
{
$datearray=getdate(makedate($datecode));
$dayoftheweek=$datearray["weekday"];
$year=$datearray["year"];
$month=$datearray["month"];
$day=$datearray["mday"];
$monthpart=substr($datecode,0,4);
$out=$dayoftheweek . ", " . "" . $month . " " . $day . " " . $year . "";
return ($out);
}
function makedate($datecode)
//from a randomly ever after $datecode to a UNIX timestamp
{
$year=substr($datecode,0,2);
$month=substr($datecode,2,2);
$day=substr($datecode,4,2);
$newdate=strtotime($month . '/' . $day . '/' . $year);
return($newdate);
}
function decideextension($datecode)
//i have begun abandoning this hardcoding in much of the code
{
$extension="php";
if ($datecode<"030701" or $datecode>"980901")
{
$extension="htm";
}
return($extension);
}
function makeearlierdateurl($datecode)
{
$earlierdatecode=earlierday($datecode);
$earliermonthpart=substr($earlierdatecode,0,4);
$nowmonthpart=substr($datecode,0,4);
$extension=decideextension($earlierdatecode);
$allinoneurl="d.php";
$bwlIsD=(strpos($_SERVER['SCRIPT_FILENAME'], $allinoneurl)>0);
if ($earliermonthpart>"0307" and !$bwlIsD)
{
$out="../" . $allinoneurl . "?datecode=" . $earlierdatecode;
}
else if ($bwlIsD)
{
if ($earliermonthpart<"0307")
{
$out= $earliermonthpart . "/" . $earlierdatecode . "." . $extension;
}
else
{
$out=$allinoneurl . "?datecode=" . $earlierdatecode;
}
}
else if ($nowmonthpart!=$earliermonthpart)
{
$out="../" . $earliermonthpart . "/" . $earlierdatecode . "." . $extension;
}
else
{
$out=$earlierdatecode . "." . $extension;
}
return($out);
}
function makelaterdatelink($datecode)
{
$laterdateurl=makelaterdateurl($datecode);
$out=" | next ";
if ($laterdateurl!="")
{
$out=" | next";
}
return($out);
}
function makelaterdateurl($datecode)
{
$laterdatecode=laterday($datecode);
$latermonthpart=substr($laterdatecode,0,4);
$nowmonthpart=substr($datecode,0,4);
$allinoneurl="d.php";
$bwlIsD=(strpos($_SERVER['SCRIPT_FILENAME'], $allinoneurl)>0);
if ($latermonthpart>"0307" and !$bwlIsD)
{
$out="../" . $allinoneurl . "?datecode=" . $laterdatecode;
}
else if ($bwlIsD)
{
$out=$allinoneurl . "?datecode=" . $laterdatecode;
if (datecodetourl($laterdatecode)=="")
{
$out="";
}
}
else
{
if ($nowmonthpart!=$latermonthpart)
{
$out="../" . $latermonthpart . "/" . $laterdatecode . ".php";
}
else
{
$out=$laterdatecode . ".php";
}
if (!is_file($out))
{
$out="";
}
}
return($out);
}
function makedatecode($date)
//function written in the Summer of 2001, back when I was just learning PHP
//given a date, i want the datecode
{
$datecode=str_pad(substr(date("Y", ($date)),2,2), 2, '0', STR_PAD_LEFT) . str_pad(date("m", ($date)), 2, '0', STR_PAD_LEFT) . str_pad(date("d", ($date)), 2, '0', STR_PAD_LEFT) ;
return($datecode);
}
function adddaystodate($date, $amount)
//give me a date that is $amount days in the future (can take negative for the past)
{
$weird=$date;
$newdate = mktime (0,0,0,date("m", $weird), date("d", $weird)+$amount , date("Y", $weird));
return($newdate);
}
function addmonthstodate($date, $amount)
//add months to this $date
{
$weird=$date;
$newdate = mktime (0,0,0,date("m", $weird)+$amount, date("d", $weird), date("Y", $weird));
return($newdate);
}
function earlierday($datecode)
//give me the datecode preceding this one
{
$out=makedatecode(adddaystodate(makedate($datecode), -1));
return($out);
}
function laterday($datecode)
//give me the datecode for the one following this one
{
$out=makedatecode(adddaystodate(makedate($datecode), 1));
return($out);
}
function firstofnextmonth($datecode)
//i want to know what the datecode of the first of next month looks like
{
$out=makedatecode(addmonthstodate(makedate($datecode), 1));
$out=substr($out, 0, 4). "01";
return($out);
}
function possiblefolderappend($thisdatecode, $navdatecode)
//do i need a folder path with this item? if so, hit me yo!!
{
$thismonthcode=substr($thisdatecode, 0, 4);
$navmonthcode=substr($navdatecode, 0, 4);
if ($thismonthcode != $navmonthcode)
{
$out="../".$navmonthcode."/".$navdatecode;
}
else
{
$out=$navdatecode;
}
return($out);
}
function flash($bgcolor,$width, $height, $swffile)
//display a Flash object embed for both IE and Mozilla
{
$canflash=true;
$out="";
if ($canflash==true)
{
$out=$out . "" .chr(13);
}
else
{
$out="" . chr(13);
}
return($out);
}
function parseEntry($strIn)
//a placeholder of course
{
$strOut=$strIn;
return($strOut);
}
function defeed($strIn)
//no linefeeds in $strIn!
{
$strOut=$strIn;
$strOut=str_replace(chr(10), "", $strOut);
$strOut=str_replace(chr(13), "", $strOut);
return($strOut);
}
//i needed this because the Spies.com PHP parser is kinda oldschool
if (!function_exists("ob_get_clean")) {
function ob_get_clean() {
$ob_contents = ob_get_contents();
ob_end_clean();
return $ob_contents;
}
}
function stringprefix($strIn, $strPrefix)
//adds a prefix to quoted strings that look like URLs found within $strIn
{
$strEndingList="swf htm php jpg gif";
$intCursor=0;
$intEndCursor=1;
$bwlNotDone=true;
$strOut=$strIn;
$intCount=0;
while($intEndCursor>0 and $intCount<12000 and $bwlNotDone)
{
$intCursor=strpos($strIn, chr(34), $intCursor);
$intEndCursor=strpos($strIn, chr(34), $intCursor+1);
$strThisQuotedString=substr($strIn, $intCursor +1, $intEndCursor-$intCursor-1);
if ($intCursor)
{
if (substr($strThisQuotedString, strlen($strThisQuotedString)-4,1)==".")
{
if (inList($strEndingList, substr($strThisQuotedString, strlen($strThisQuotedString)-3, strlen($strThisQuotedString))))
{
if (substr($strThisQuotedString, 0 , strlen($strPrefix))!=$strPrefix)
{
$strOut=str_replace($strThisQuotedString, $strPrefix.$strThisQuotedString,$strOut);
}
}
}
}
else
{
$bwlNotDone=false;
}
$intCursor=$intEndCursor+1;
$intCount++;
}
return($strOut);
}
function tagHREFprefixer($strIn, $strTagList, $strParameterList, $strPrefix)
//scans through HTML in $strIn and appends $strPrefix to URL parameters in space-delimited $strParameterList list in tags named in space-delimited $strTagList
{
$intCursor=0;
$intEndCursor=1;
$intCount=0;
$bwlNotDone=true;
$strBadBegins="/ ! $";
$strOut=$strIn;
//$strOut=str_replace("", "--~", $strOut);
while($intEndCursor>0 and $intCount<12000 and $bwlNotDone)
{
$intCursor=strpos($strIn, "<", $intCursor);
$intEndCursor=strpos($strIn, ">", $intCursor);
if ($intCursor)
{
$strThisTag=substr($strIn, $intCursor +1, $intEndCursor-$intCursor-1);
$strFullTag=substr($strIn, $intCursor , 1+$intEndCursor-$intCursor);
//$excerpt=substr($strIn, $intCursor +1, strlen($strIn));
if (substr($strThisTag, 0, 1)=="?")
{
$strThisTagMod=substr($strThisTag, 1, strlen($strThisTag)-2);
if (substr($strThisTagMod, 0 , 7)!="include")
{
$strThisTagMod=stringprefix($strThisTagMod, $strPrefix);
ob_start();
eval($strThisTagMod . ";");
$strThis = ob_get_clean();
$strOut=str_replace($strFullTag, $strThis, $strOut, 1);
}
}
else if (!inList($strBadBegins, substr($strThisTag, 0, 1)))//not interested in end tags or comments
{
$arrThis=parameterarray($strThisTag);
if (count($arrThis)>1)
{
if (inList($strTagList, $arrThis[0]))
{
for($t=1; $t