<?php

/////////////////////// Functions and includes used on whoopis.com /////////////////

function uptime () {
	$uptime_array = explode(" ", exec("cat /proc/uptime"));
	$seconds = round($uptime_array[0], 0);
	$minutes = $seconds / 60;
	$hours = $minutes / 60;
	$days = floor($hours / 24);
	$hours = floor($hours - ($days * 24));
	$minutes = floor($minutes - ($days * 24 * 60) - ($hours * 60));
	$seconds = floor($seconds - ($days * 24 * 60 * 60) - ($hours * 60 * 60) - ($minutes * 60));
	$uptime_array = array($days, $hours, $minutes, $seconds);
	
	
	if ($uptime_array[0] == 0) {
		if ($uptime_array[1] == 0) {
			if ($uptime_array[2] == 0) {
				print("Uptime: " . $uptime_array[3] . " second(s)");
			}
			else {
				print("Uptime: " . $uptime_array[2] . " minute(s)");
			}
		}
		else {
			print("Uptime: " . $uptime_array[1] . " hour(s)");
		}
	}
	else {
		print("Uptime: " . $uptime_array[0] . " day(s)");
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////

function lastmod() {
	// Highlight in red the files which have been added or updated in the
	// last week.
	// Requires one argument, $filename
	// Second, optional argument = path to $filename

	$args = func_get_args();
	$filename = $args[0];

	if ( func_num_args() > 1 ) {  // If we got optional args, then assign them
		$home = $args[1];
	}
	else {  // default
		$home = "/var/websites/whoopis.com/howtos";
	}

	// Grab the mod date of the file, then make it a unix timestamp
	// (= total seconds since the epoch) so  that we can do date
	// arithmetic easily.
	$moddate = date("F d Y H:i:s", filemtime("$home/$filename"));
	$moddatestamp = strtotime($moddate);

	// 7 days times 60 seconds times 60 minutes times 24 hours = a week,
	// in seconds
	$weekduration = 7*(60*60*24);

	// Without a second param, date() returns the date/time right now
	$rightnow = date("F d Y H:i:s");

	// Everything in seconds
	$weekago = strtotime($rightnow) - $weekduration;

	if ( $moddatestamp >= $weekago ) { // counter-intuitive; bigger timestamps are more recent
			echo " <i><font color=\"#ff0000\">Last modified: $moddate</font></i>";
	}

	else {
			echo " <i>Last modified: $moddate </i>";
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////

function searchmod() {

// Display search block and lastmod time

// Takes $bgcolor, $path, and $pagename as arguments.
// $path and $pagename are optional, defaults will be used
// if this function is called without them. They are only
// needed if the calling page is not under webroot (e.g. ~/public_html dirs)

$args = func_get_args();
$bgcolor = $args[0];

if ( $bgcolor === "white" ) {  // we have a white background
	$logocolor = "white";
	$bgcolorvalue = "#ffffff";
	$fontcolor = "#000000";
}

else {	// we have a black background
	$logocolor = "black";  
	$bgcolorvalue = "#000000";
	$fontcolor = "#ffffff";
}

// Start output, sub in variables where they matter

echo '<p>
<table border="0">
	<tr>
		<td>
		<!-- SiteSearch Google -->
<form method="get" action="http://www.google.com/custom" target="google_window">
<table bgcolor="' . $bgcolorvalue .'" cellspacing="0" border="0">
<tr><td nowrap="nowrap" valign="top" align="left" height="32">
<a href="http://www.google.com/">
<img SRC="http://www.whoopis.com/Logo_25_' . $logocolor . '.gif" border="0" ALT="Google" align="middle"></a>
</td>
<td nowrap="nowrap">
<input type="hidden" name="domains" value="whoopis.com;westwingtranscripts.com;stearns.org"></input>
<input type="text" name="q" size="31" maxlength="255" value=""></input>
<input type="submit" name="sa" value="Search"></input>
</td></tr>
<tr>
<td>&nbsp;</td>
<td nowrap="nowrap">
<table>
<tr>
<td>
<input type="radio" name="sitesearch" value="whoopis.com" checked="checked"></input>
<font size="-1" color="' . $fontcolor . '">whoopis.com</font>
</td>
<td>
<input type="radio" name="sitesearch" value=""></input>
<font size="-1" color="' . $fontcolor . '">Web</font>
</td>
</tr>
<tr>
<td>
<input type="radio" name="sitesearch" value="ists.dartmouth.edu"></input>
<font size="-1" color="' . $fontcolor . '">ists.dartmouth.edu</font>
</td>
<td>
<input type="radio" name="sitesearch" value="stearns.org"></input>
<font size="-1" color="' . $fontcolor . '">stearns.org</font>
</td>
</tr>
</table>
<input type="hidden" name="client" value="pub-7808487654982593"></input>
<input type="hidden" name="forid" value="1"></input>
<input type="hidden" name="ie" value="ISO-8859-1"></input>
<input type="hidden" name="oe" value="ISO-8859-1"></input>
<input type="hidden" name="cof" VALUE="L:http://www.whoopis.com/whoopbanner.gif;AH:left;S:http://www.whoopis.com;AWFID:7a05d053a8bbf751;"></input>
<input type="hidden" name="hl" value="en"></input>

</td></tr></table>
</form>
<!-- SiteSearch Google -->
		</td>
	</tr>
	<tr>
		<td>';

// end of search part

if ( func_num_args() > 1 ) {  // If we got optional args, then assign them
	$path = $args[1];
	$home = $path . "/";
	$pagename = $args[2];
	$self = $pagename;
}
else {  // default
	$home = "/var/websites/whoopis.com";
	$self = $_SERVER['PHP_SELF'];  // name of the page calling this
}

// No slash here, _SERVER[PHP_SELF] comes with a slash already, 
// and we do the same in the other case to match
$filename = "$home$self";

if ( file_exists($filename) ) {

	// outputs e.g.  somefile.txt was last modified: December 29 2002 22:16:23.
    echo '<font color="' . $fontcolor . '" size="2">This page was last modified on<br>' . date ("F d Y H:i:s.", filemtime($filename)) . '</font>';

}

// finish table
echo '</td>
	</tr>
</table>
<p>';

}

//////////////////////////////////////////////////////////////////////////////////

function printLink2man($theQuery) {

	echo '<br><font size="1">See the Unix man pages for '.$theQuery.' <a href="http://www.whoopis.com/howtos/info/'.$theQuery.'" target="new">here.</a></font>';

}

//////////////////////////////////////////////////////////////////////////////////

function printEurogeek() {

	$imageDir = "eurogeek_imgs";

        //////

        $baseurl = "http://www.whoopis.com/$imageDir";
        $imagePath = "/var/websites/whoopis.com/$imageDir";

        $handle = opendir($imagePath);

        while ( false !== ($imageName = readdir($handle)) ) {
                if ($imageName[0] != ".") {
                	$imgs[] .= $imageName;
		}
	}

        closedir($handle);

	// Shuffle the array
	shuffle($imgs);
	
	// NOTE: More efficient would be to just grab two random indices
	// from the array BUT they could be identical. This guarantees
	// that the two images will always be different. Plus it's what
	// I thought of first, and it worked.  ;) 

	if (func_num_args() > 0) { // if we got an arg, we need to format differently for the man page thing.
		echo '<img src="'.$baseurl.'/'.$imgs[0].'" border="0" valign="middle"><big><a href="http://www.cafepress.com/eurogeek/" target="new">Get Euro-style oval stickers for Geeks!</a></big><img src="'.$baseurl.'/'.$imgs[1].'" border="0" valign="middle">';	
	}

	else {	
		echo '<center>
<a href="http://www.cafepress.com/eurogeek/" target="new"><img src="'.$baseurl.'/'.$imgs[0].'" border="0" valign="middle"><big>Get Euro-style oval stickers for Geeks!</big><img src="'.$baseurl.'/'.$imgs[1].'" border="0" valign="middle"></a>
</center>';
	}
}

///////////////////////////////////////////////////////////////////////////////////

function printPicassomac() {

        $imageDir = "picassomac_imgs";
        
        //////

        $baseurl = "http://www.whoopis.com/$imageDir";
        $imagePath = "/var/websites/whoopis.com/$imageDir";

	$handle = opendir($imagePath);
	
	echo '<center><h3>Get <a href=http://www.cafepress.com/compupics,picassomac>"Picasso" Mac t-shirts, etc. HERE!</h3></a><p>(scroll down to find the color one, or just click <a href="http://www.cafepress.com/compupics,picassomac.19541363">here</a> to jump right to it.)<p><a href="http://www.cafepress.com/compupics,picassomac">';

	while (false !== ($imageName = readdir($handle))) {
		if ($imageName[0] != ".") {
			echo "<img src=\"$baseurl/$imageName\" border=\"0\" height=\"120\">";
		}
	}

	closedir($handle);

	echo "</a>\n</center>";
}

//////////////////////////////////////////////////////////////////////////////////

// this is the standard white four-across ad.

$googleAd = '
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-7808487654982593";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_channel ="5824086615";
google_ad_type = "text";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_url = "666666";
google_color_text = "333333";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
';

// links in black

$googleAdBlack = '
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-7808487654982593";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_channel ="5824086615";
google_color_border = "ffffff";
google_color_bg = "FFFFFF";
google_color_url = "000000";
google_color_link = "000000";
google_color_text = "000000";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
';

// black background, white links

$googleAdDark = '
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-7808487654982593";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_channel ="5824086615";
google_color_border = "000000";
google_color_bg = "000000";
google_color_url = "666666";
google_color_text = "333333";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
';

?>
