<?php
	define('PAGE_NAME', '[Simpub CMS]');	// Title of the page
	define('PAGE_AUTHOR', 'Simpub CMS');	// Author of the page (used in footer)

	define('HOME_PAGE', 'blog');    		// Entry page (and link for logo)
	define('CACHE_POSTS_TTL', 3600);		// Time to cache the posts list (1h)
	define('POSTS_BASE', "/b/");    		// Root directory of the posts (relative)
	define('POSTS_ROOT', $_SERVER['DOCUMENT_ROOT'] . POSTS_BASE); // Root directory of the posts (absolute)
	define('POSTS_PAGE_SIZE', 10);  		// Numbers of posts on a page

	define('POSTS_DIR_SEP', ".");			// Directory separator (/) in post names
	define('POSTS_MAX_FILENAME', 30);		// Maximum length of a post name (including suffix)
	define('POSTS_TAG_SEARCH', 30);   		// Maximum length for the tag search argument

	$menuItems = array(
		array("label" => "Page A",	"link" => "?p=00-template",			"highlight" => "related-subpages,00-template"),
		array("label" => "Page B",  	"link" => "?p=404",		"highlight" => "404"),
		array("label" => "Repository",	"link" => "https://git.notsyncing.net:8080",	"highlight" => ""),
		array("label" => "Contact",		"link" => "?p=00-template",			"highlight" => "")
	);
	
	$blogDirs = array("2017");


	/*
	 * This function will include an image as figure.
	 * returns  : Number of the figure for later referencing.
	 * $title   : Image title, will be prefixed with Figure n.
	 * $path    : If $path starts with http or / it will be
	 *            treated as an absolute path and no downscaled preview will be rendered.
	 *            Other paths will be prefixed with /b/img.
	 * $preview : When set to "", no preview will be generated. Otherwise the specified
	 *            path will be used.
	 * $link    : Link to follow when clicking the image. Default is the full resolution image.
	 */
	$counters['figure'] = 1;
	function includeGraphics($title, $path, $preview="+", $style="", $link="+") {
		global $blogentry;
		global $counters;
		
		$title = "Figure " . $counters['figure'] . ". $title";

		// TODO: External image
		if (substr($path, 0, 4) == "http" || $path[0] == "/") {
			$preview="";
			$spath=$path;
		}
		else {
			$spath = POSTS_BASE . "img/" . $path;
			$fpath = $_SERVER['DOCUMENT_ROOT'] . $spath;
		}

		if ($preview == "+") {
			$previewPath = POSTS_BASE . "pimg/" . str_replace("/", "_", $path);
			$fpreviewPath = $_SERVER['DOCUMENT_ROOT'] . $previewPath;
			if (!file_exists($fpreviewPath)) {
				preg_match("/^[A-Za-z_\.\-0-9\/]*$/", $fpath, $matches); // SANITIZED!
				if (!$matches) {
					echo "<p>SERVER ERROR: Image not valid!</p>";
					return ;
				}
				$page=substr($page,0,12); // SECURITY: Max 12 chars
				exec("gm convert '$fpath' -resize 640 '$fpreviewPath';");
			}
		}
		elseif ($preview == "") {
			$previewPath = $spath;
		}
		else {
			$previewPath = $preview;
		}

		if ($link == "+")
			$link = $spath;

		echo '<figure>
				<a href="' . $link . '">
					<img style="' . $style . '" title="' . $title . '" style="margin: 0px;padding: 0px;" src="' . $previewPath . '">
				</a>
				<figcaption>' . $title . '</figcaption>
			  </figure>';

		return $counters['figure']++;
	}


	/*
	 * This function will include a video.
	 * returns  : Number of the video for later referencing.
	 * $url     : URL of the video. Autodetects YouTube. Otherwise HTML5 video.
	 * $title   : Display "Video n. $title" below the video.
	 * $width   : Width of the player
	 */
	$counters['video'] = 1;
	function includeVideo($url, $title="", $width=640) {
		global $counters;
		// TODO: HTML5 video
		echo '<videoframe><div class="videoframe" style="width:' . $width . 'px;height:' . $width*9/16 . 'px">';
		echo '<iframe width="100%" height="100%" src="' . $url . '" frameborder="0" allowfullscreen></iframe>';
		echo '</div></videoframe>';
		if ($title != "") {
			echo '<vidcaption>Video ' . $counters['video'] . ". $title</vidcaption>";
		}
		return $counters['video']++;
	}

	
	/*
	 * This function will add a reference
	 * returns  : Number of the source for later referencing
	 * $url     : URL of the source, if its a number it will not create a new entry.
	 */
	$meta['sources'] = array();
	$counters['sources'] = 1;
	function addSource($url) {
		global $counters;
		global $meta;
		if (is_numeric($url)) {
			echo "<a href=\"#src$url\">[" . $url . "]</a>";
			return $url;
		}
		else {
			// Check whether source already exists
			$i = 1;
			foreach ($meta['sources'] as $v) {
				if ($v == $url) {
					echo "<a href=\"#src$i\">[" . $i . "]</a>";
					return $i;
				}
				$i++;
			}
			// If not found, then add
			$meta['sources'][$counters['sources']] = $url;
			echo "<a href=\"#src$i\">[" . $counters['sources'] . "]</a>";
			return $counters['sources']++;
		}
	}
?>