<?php

// Capture error and info messages in these variables, rendered in adminmenu() as a box below the menu bar.
$error='';
$info='';

// write from doctype, html, head, body
function adminhead($title = '') {
	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Megatokyo Administrative Dashboard<?php if( $title ) echo " &#8212; $title"; ?></title>
<link rel="stylesheet" href="<?php echo ADMIN_PATH; ?>/wp-admin.css" type="text/css" />
<link type="application/rss+xml" rel="alternate" title="Admin Log" href="<?php printf('%s%s/%s/adminlog.xml', SITE_HOST, SITE_PATH, SITE_ADMIN) ?>" />
<link type="application/rss+xml" rel="alternate" title="Scratchpad" href="<?php printf('%s%s/%s/scratchpad.xml', SITE_HOST, SITE_PATH, SITE_ADMIN) ?>" />
</head>
<body>
	<?php
}

// structure of the top level menubar
//'manage-statusbox.php' => 'Update Statusbox',
$menus = array(
	'index.php' => 'Dashboard',
	'post-comic.php?next=yes' => 'Post Next Comic',
	'post-comic.php' => 'Post Comic',
	'post-rant.php' => 'Write Rant',
	'post-twitter.php' => 'Update Feeds',
	'post-page.php' => 'Add page',
	'manage-comics.php' => 'Manage',
	'users.php' => 'Users'
	);

// structure of the second level menubar
$submenu['manage-comics.php'] = array(
	'manage-comics.php' => 'Comics',
	'manage-rants.php' => 'Rants' ,
	'manage-pages.php' => 'Pages',
	'manage-types.php' => 'Types',
	'manage-twitter-users.php' => 'Twitter Users',
	'manage-metatypes.php' => 'Metatypes',
	'swap-comics.php' => 'Swap Comics',
	'character-twitter.php' => 'Character Twitters',
	'view-adminlog.php' => 'View Admin Log'
	);

function adminmenu($current='') {
	global $menus, $submenu, $error, $info;

	if( $current ) {
		$curS = $curT = $current;
	} else {
		$curS = $curT = basename( $_SERVER["PHP_SELF"] );
	}


	foreach( $submenu as $l=>$L ) {
		foreach( $L as $k=>$v ) {
			if( $k == $curS ) {
				$curT = $l;
			}
		}
	}
	$current_sub_menu = false;

	?>
	<div id="wphead">
	<h1>Megatokyo Site Administration <span>(<a href="<?php echo SITE_HOST . SITE_PATH; ?>">View site &raquo;</a>)</span></h1>
	</div>
	<div id="user_info"><p>Howdy, <strong><?php getCurrentUser(); ?></strong>. [<a href="<?php echo ADMIN_PATH;
		?>/login.php?action=logout" title="Log out of this account">Sign Out</a>] </p></div>


	<ul id="adminmenu">
	<?php
	foreach ( $menus as $url=>$desc ) {
		printf ( "		<li><a href=\"%s/%s\"%s>%s</a></li>\n", ADMIN_PATH, $url, ( $curT == $url ? ' class="current"' : '' ), htmlentities($desc) );
		if( $curT == $url && isset($submenu[$url]) ) {
			$current_sub_menu = $submenu[$url];
		}
	}
	?></ul><?php

	if( $current_sub_menu ) {
		?><ul id="submenu"><?php
		foreach( $current_sub_menu as $url => $desc ) {
			printf ( "		 <li><a href=\"%s/%s\"%s>%s</a></li>\n", ADMIN_PATH, $url, ( $curS == $url ? ' class="current"' : '' ), htmlentities($desc) );
		}
		?></ul><?php
	} else {
		?><div id="minisub"></div><?php
	}
	?>

	<div class="clear"></div>

	<?php
	if( $_GET['deleted'] && $_GET['deleted'] == 'success' ) $info.='<p>Deleted successfully.</p>';
	if( $_GET['saved'] && $_GET['saved'] == 'success' ) $info.='<p>Changes saved successfully.</p>';

	if( $error ) echo "<div class=\"error fade\">$error</div>";
	if( $info ) echo "<div class=\"updated fade\">$info</div>";
	?>
	<div class="wrap">
	<?php
}

function adminfooter($copy = true) {
	?>
	</div>
	<?php if( $copy ) { ?>
		<div id="footer">
		<p class="logo">Style based on <a href="http://wordpress.org/">Wordpress</a>. Powered by Taiyaki.</p>
		</div>
	<?php } ?>
	</body>
	</html>
	<?php
}

function pagination( $page, $total ) {
	?><p class="pagenav"><?php
	if( 1 < $page ) {
	 ?><a class="prev" href="?page=<?php echo $page-1; ?>">&laquo; Previous Page</a> <?php
	}
	for( $page_num = 1; $page_num <= $total; $page_num++ ) {
	 if ( $page == $page_num ) {
		echo "<span>$page_num</span>";
	 } else {
		if ( $page_num < 4 || ( $page_num >= $page - 3 && $page_num <= $page + 3 ) || $page_num > $total - 3 ) {
			?><a class="page-numbers" href="?page=<?php echo $page_num; ?>"><?php echo $page_num; ?></a><?php
			$in=true;
		} elseif( $in==true ) {
			echo ' ... ';
			$in=false;
		}
	 }
	}
	if( $page < $total ) {
	 ?> <a class="next" href="?page=<?php echo $page+1; ?>">Next Page &raquo;</a><?php
	}
	?></p><?php
}

set_error_handler('handle_error');

function handle_error($errno, $errstr, $errfile, $errline, $errcontext) {
	switch ($errno)	{
		case E_USER_WARNING:
		case E_USER_NOTICE:
		case E_WARNING:
		case E_NOTICE:
		case E_CORE_WARNING:
		case E_COMPILE_WARNING:
			break;
		case E_USER_ERROR:
		case E_ERROR:
		case E_PARSE:
		case E_CORE_ERROR:
		case E_COMPILE_ERROR:

			header('Content-Type: text/html; charset=utf-8');

			if (eregi('^(sql)$', $errstr)) {
				$errstr = "SQL Error " . mysqli_errno() . ': ' . mysqli_error();
			}

			$message = "Error#$errno: $errstr";
			//logthis( $message, "in $errfile at line $errline" );
			mtdie( $message, 'Critical Site Error' );
		}
}

function logthis($str,$extra='') {
	error_log( $str, 0 );
	if( $extra ) error_log( "Megatokyo Site Error\r\n$str\r\n$extra", 1, SITE_CONTACT );
}

function mtdie($message,$title='') {
	header('Content-Type: text/html; charset=utf-8');

	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><?php echo $title?$title:'Megatokyo Administration Editor'; ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<style media="screen" type="text/css">
		html {
			background: #eee;
		}
		body {
			background: #fff;
			color: #000;
			font-family: Georgia, "Times New Roman", Times, serif;
			margin-left: 25%;
			margin-right: 25%;
			padding: .2em 2em;
		}
		h1 {
			color: #006;
			font-dize: 18px;
			font-weight: lighter;
		}
				h2 {
			font-size: 16px;
		}
		p, li, dt {
			line-height: 140%;
			padding-bottom: 2px;
		}
		ul, ol {
			padding: 5px 5px 5px 20px;
		}
		#logo {
			margin-bottom: 2em;
		}
		-->
	</style>
</head>
<body>
	<h1 id="Logo">Megatokyo Admin</h1>
		<?php if($title) echo "<h2>$title</h2>\n"; ?>
	<p><?php echo $message; ?></p>
</body>
</html>
<?php
	die();

}

function numeric_entities($string){
    $mapping = array();
    foreach (get_html_translation_table(HTML_ENTITIES, ENT_QUOTES) as $char => $entity){
        $mapping[$entity] = '&#' . ord($char) . ';';
    }
    return str_replace(array_keys($mapping), $mapping, $string);
}

function utfentities($string)
{
	return htmlentities($string, ENT_COMPAT, 'UTF-8');
}

?>
