<?php
if (strstr($_SERVER["PHP_SELF"], "/core/"))  die ("You can't access this file directly...");

if( !isset($cfg["core"]["theme"]) ) $cfg["core"]["theme"] = "bg";
require( "themes/{$cfg["core"]["theme"]}/th_functions.php" );

function draw_header_tag()
{
	print "<head>\n<title>";
	draw_page_title();
	print "</title>\n";
	theme_draw_style();
	draw_header_code();
	print "</head>\n";
}

function draw_page_title()
{
    global $page_name, $cfg;
    $txt = $cfg["core"]["title"];
    if( isset($page_name) ) $txt .= " - ".$page_name;
    print $txt;
}

function draw_header_code()
{
	global $lang, $cfg;
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset={$lang["CHARSET"]}\">\n"
		."<meta http-equiv=\"Generator\" content=\"phpWebThings 0.6\">\n"
		."<meta name=\"Keywords\" content=\"{$cfg["core"]["keywords"]}\">\n"
		."<meta name=\"Description\" content=\"{$cfg["core"]["description"]}\">\n";
}

function draw_copyright()
{
    global $cfg, $time_start, $lang, $qtde_qry;
    echo "&nbsp;This website was created with phpWebThings.";
    if( $cfg["core"]["page_processed"] )
printf( "<br><span class=\"pgprocessed\">&nbsp;&nbsp;{$lang["PAGE_PROCESSED"]} - $qtde_qry queries<br><br></span>",(getmicrotime() - $time_start));
else echo "&nbsp;";
}

function theme_draw_style()
{
	global $modules, $config, $cfg;
	
    print "<link href=\"themes/{$cfg["core"]["theme"]}/style.css\" rel=\"stylesheet\" type=\"text/css\">\n";
    print "<link href=\"themes/{$cfg["core"]["theme"]}/forms.css\" rel=\"stylesheet\" type=\"text/css\">\n";
    print "<link href=\"themes/{$cfg["core"]["theme"]}/lists.css\" rel=\"stylesheet\" type=\"text/css\">\n";
	reset($modules);
	while( $mod = each($modules) ) {
		if( $mod[1] ) {
			if( isset($config["stylecss"][$mod[0]]) && $config["stylecss"][$mod[0]] )
			{
				print "<link href=\"themes/{$cfg["core"]["theme"]}/{$mod[0]}.css\" rel=\"stylesheet\" type=\"text/css\">\n";
			}
		}
	}
}

function draw_pending_actions()
{
	if( !$_SESSION["wt"]["logged"] ) return;
	global $pending_actions;
	if( count($pending_actions) == 0 ) return;
	theme_draw_leftbox_open( "Pending actions" );
	print "<table border=0 cellpadding=1 cellspacing=0>\n";
	reset($pending_actions);
	while( $item = each($pending_actions) )
	{
		print "<tr><td><a href=\"{$item[1]["link"]}\">{$item[1]["title"]}</a></td><td align=\"right\">{$item[1]["qty"]}</td></tr>\n";
	}
	print "</table>";
	theme_draw_leftbox_close();
}

function draw_sideboxes($side)
{
	global $index_page, $config;
	$stmt = "select * from {$config["prefix"]}_sideboxes where side='$side'";
	if(!$index_page) $stmt .= " AND onlyindex=0";
	$stmt .= " order by pos";
	$ret = db_query($stmt);
	print db_error();
	while($row=db_fetch_array($ret))
	{
		if(isset($row["type"])) $type=$row["type"];
		if( $type != "A" ) {
			if( $type == "L" ) {
				if( !$_SESSION["wt"]["logged"] ) continue;
			} else if(  $type == "N" ) {
				if( $_SESSION["wt"]["logged"] ) continue;
			}
		}
		if( !empty($row["function"]) )
		{
			$row["function"]();
			continue;
		}
		if( $side == "left" ) theme_draw_leftbox_open( $row["title"] );
		else theme_draw_rightbox_open( $row["title"] );
		if( strlen($row["file"]) > 0 && $row["file"] != "none" && $row["file"] != "index"  )
		{
			include_once( "sideboxes/{$row["file"]}.php" );
		}
		else
		{
			echo $row["content"];
		}
		if( $side == "left" ) theme_draw_leftbox_close( $row["title"] );
		else theme_draw_rightbox_close( $row["title"] );
	}
	db_free_result($ret);
}
?>
