<?php
// Build <head> html with included
// CSS, etc
function build_head() {
	echo "<head>";

	$content = <<<EOF
	<title>World Wide Witzeman</title>
	EOF;

	echo $content;
	//Import css
	echo "<style>";
	echo file_get_contents("css/fonts.css");
	echo file_get_contents("css/colors.css");
	echo file_get_contents("css/appearance.css");
	echo "</style>";

	echo "</head>";	
}

// Get content from file at path $path
// $recnum keeps memory of numbers of recursion.
// We need this ability to wrap the final string
// in <div> elements.
function get_content($path, $recnum = 0) {
	$extracontent = "";
	$output = "";
	// Get contents of the file
	$fcontent = file_get_contents($path);

	// If the file has an <!--include [path]--> comment,
	// then call this function on that path too.
	$includestart = strpos($fcontent, "<!--");
	
	// First, check if a comment exists
	if ($includestart) {
		// Remove <!--
		$includeend = strpos($fcontent, "-->") + 2;
		$includelen = $includeend - $includestart + 1;
		$include_str = substr($fcontent, $includestart, $includelen);

		// Then, check if the string has "include"
		$includestart = strpos($include_str, "include");
		if ($includestart) {
			// If it does, get the file to include
			// and put it in $include_str
			$include_str = substr(
				$include_str, 
				$includestart + 7, // +7 removes "include"
				strlen($includestart) - 4); // -4 removes -->
			$include_str = trim($include_str);
		}
		// Call the function on this string as path,
		// increment recursion to ensure we don't get lost
		$extracontent = get_content($include_str, $recnum + 1);
	}	

	$output = $fcontent . $extracontent . $output;
	if ($recnum == 0) {
		$output = "<div class='content'>" . $output . "</div>";
	}
	return $output;	
}

// Remove filename endings from any string which is a file path.
function clean_fn($string) {
	// To do this, we are going to move upwards from the
	// bottom of the string, looking for '.' and '/' characters.
	// When we find a '.' char, we then check if there is any '/'
	// placed after it. If this is true, we are dealing with a folder
	// that has a '.' in its name, which we do not want to alter.
	// If it is not true, we can remove all characters after the '.'
	// and then check the next '.' up the string.
	
	$exitcond = FALSE;
	$index = -1;
	$index_lastper = -1;
	$index_lastslash = 1;
	$final_index = 0;

	// Iterate over the string, looking for slashes and periods.
	while (abs($index) < strlen($string) && !$exitcond) {
		// If we find a period, save the index.
		if ($string[$index] == '.') {
			$index_lastper = $index;
		}
		// If we find a slash, save the index.
		else if ($string[$index] == '/') {
			$index_lastslash = $index;
			$exitcond = TRUE;
		}

		// Compare the indices. If there is no
		// slash before the period, that's our
		// final index.
		if ($index_lastper > $index_lastslash) {
			$final_index = $index_lastper;	
		}
		$index--;
	}
	if (abs($final_index) > 0) {
		$output = substr($string, 0, $final_index);
//$output = $output . " | " . $index_lastper . " | " . $index_lastslash;
	}
	else {
		$output = $string;
	}
	return $output;
}

// Quick function to build MOTD with optional delimiter
function build_motd($strings, $delimiter = "\t") {
	$motd = $strings[0];
	for ($i = 1; $i < sizeof($strings); $i++) {
		$motd = $motd . $delimiter . $strings[$i];
	}
	return $motd;
}
?>
