<?php
define("WIKI_PATH", (getenv('GCL_WIKI_URL') ?: "http://glitchcity.info/wiki") . "/");
define("LOCAL_WIKI_PATH", "http://localhost/wiki/");
define("WIKI_NAME","Glitch City Laboratories");
//include("../wiki/include.inc.php");
//define("MEDIAWIKI",1);
$wiki = WIKI_PATH;

global $refrainFromShowingSidebarHere;
$refrainFromShowingSidebarHere = array("admin","moderate","profile");

function wfUrlencode ( $s ) {
	$s = urlencode( $s );
	$s = preg_replace( '/%3[Aa]/', ':', $s );
	$s = preg_replace( '/%2[Ff]/', '/', $s );

	return $s;
}

function wNamespace($ns_id) {
	$ns = array(
		0 => "",
		1 => "Talk:",
		2 => "User:",
		3 => "User_talk:",
		4 => "Glitch_City_Laboratories:",
		5 => "Glitch_City_Laboratories_talk:",
		14 => "Category:",
		15 => "Category_talk:",
		6 => "Image:",
		7 => "Image_talk:",
		8 => "MediaWiki:",
		10 => "Template:",
		11 => "Template_talk:"
	);
	return $ns[$ns_id];
}

function UnixTime($mysql_timestamp){
    if (preg_match('/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)
        || preg_match('/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)) {
            $unix_time = mktime($pieces[4], $pieces[5], $pieces[6], $pieces[2], $pieces[3], $pieces[1]);
    } elseif (preg_match('/\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp)
        || preg_match('/\d{2}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp)
        || preg_match('/\d{4}\-\d{2}\-\d{2}/', $mysql_timestamp)
        || preg_match('/\d{2}\-\d{2}\-\d{2}/', $mysql_timestamp)) {
            $unix_time = strtotime($mysql_timestamp);
    } elseif (preg_match('/(\d{4})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)
        || preg_match('/(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)) {
            $unix_time = mktime(0, 0, 0, $pieces[2], $pieces[3], $pieces[1]);
    }
  return $unix_time;
}

function rmBOM($string) {
    if(substr($string, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
        $string=substr($string, 3);
    }
    return $string;
}

function wik2($str) {
	$terms = array("mainpage-description"=>"Main Page",
                                   "mainpage"=>"Main_Page",
				   "portal"=>"Community portal",
				   "portal-url"=>WIKI_NAME.":Community_Portal",
				   "currentevents"=>"Current events",
				   "currentevents-url"=>"Current_events",
				   "recentchanges"=>"Recent changes",
				   "recentchanges-url"=>"Special:Recentchanges",
				   "randompage"=>"Random page",
				   "randompage-url"=>"Special:Random",
				   "sitesupport"=>"Donations",
				   "sitesupport-url"=>WIKI_NAME.":Site_support",
				   "navigation"=>"Navigation",
				   "help"=>"Help",
				   "helppage"=>"Help:Contents");
	if(isset($terms[$str])) return $terms[$str];
	else return $str;
}

function template_submenu() {
global $wiki, $context, $ID_MEMBER, $theme, $settings, $user_info, $refrainFromShowingSidebarHere;

//$hideMenus = array("SEARCH","TOOLBOX","LANGUAGES");

if(in_array($_GET['action'],$refrainFromShowingSidebarHere))
{
	return;
}

echo '<td valign="top" width="223">';

$wiki = WIKI_PATH;
// This template refers to the wiki to build the sub menu.
$wdata = explode("\n",rmBOM(file_get_contents(LOCAL_WIKI_PATH."MediaWiki:Sidebar?action=raw")));
//$wdata = explode("\n",rmBOM(get_page_contents("MediaWiki:Sidebar")));
// Cycle through the data...
$inBlock = false;

$blocks = array();
$currentBlock = "";

foreach($wdata As $line)
{
	//$line = preg_replace('/[^(\x20-\x7F)]*/','', $line);
       $line = trim($line);

	if(substr($line,0,2) == "* ")
	{
		if($inBlock)
		{
			$blocks[$title] = $currentBlock;
			$currentBlock = "";
		}
		$inBlock = true;
		$title = wik2(trim(substr($line,2)));
	}
	elseif(substr($line,0,2) == "**")
	{
		$link = trim(substr($line,2));
		$bits = explode("|",$link);
		$url = wik2($bits[0]);
		$pageTitle = wik2($bits[1]);

		if(substr($url,0,7) != "http://" &&
			 substr($url,0,8) != "https://" &&
			 substr($url,0,2) != "//")
		{
			$url = WIKI_PATH."$url";
		}

		if(!empty($bits[2]))
		{
			$currentBlock .= "<a href='$url'><img src='http://elsanctum.co.cc/" . Image::imageUrl($bits[2]) . "' alt='$pageTitle' title='$pageTitle'></a><br />\n";
		}
		else
		{
			$currentBlock .= "<a href='$url'>$pageTitle</a><br />\n";
		}
	}
}

$blocks[$title] = $currentBlock;

foreach($blocks as $title => $block)
{
	if(!empty($block))
	{
		echo '<table cellspacing="1" cellpadding="4" border="0" width="100%" align="center" class="bordercolor" >
				<tr>
				<td class="catbg"><strong>'.$title.'</strong></td>
				</tr>
				<tr>
				<td class="windowbg2">'.$block.'</td></tr></table><br />';
	}
}

echo <<<FOO
<table cellspacing="1" cellpadding="4" border="0" width="100%" align="center" class="bordercolor" >
<tr>
<td class="catbg"><strong>Search Wiki</strong></td>
</tr>
<tr>
<td class="windowbg2">			<form action="{$wiki}Special:Search" id="searchform"><div>
				<input id="searchInput" name="search" type="text" title="Search InfamyWiki [f]" accesskey="f" value="" /><br />
				<input type='submit' name="go" class="searchButton" id="searchGoButton"	value="Go" />&nbsp;
				<input type='submit' name="fulltext" class="searchButton" id="mw-searchButton" value="Search" />
			</div></form>
	</td></tr></table><br />
		<table cellspacing="1" cellpadding="4" border="0" width="100%" align="center" class="bordercolor" >
<tr>
<td class="catbg"><strong>Search Forums</strong></td>
</tr>
<tr>
<td class="windowbg2">	<form action="index.php?action=search2" method="post" style="margin: 0;">
						<input type="text" name="search" value="" /><br />
						<input type="submit" name="submit" value="Search" style="width: 11ex;" />
						<input type="hidden" name="advanced" value="0" />
					</form>
	</td></tr></table>
FOO;

echo '</td><td>&nbsp;</td>';
}

?>
