<?
// ###################### Start cpheader #######################
function cpheader($headinsert="") {
global $gzipoutput,$nozip;

if ($gzipoutput and !headers_sent() and function_exists("ob_start") and function_exists("crc32") and function_exists("gzcompress") and !$nozip){

	ob_start();

}

?>
<html><head>
<meta content="text/html; charset=big5" http-equiv="Content-Type">
<meta http-equiv="MSThemeCompatible" content="Yes">
<link rel="stylesheet" href="../cp.css">
<?php
  echo $headinsert;
?>
</head>
<body leftmargin="10" topmargin="10" marginwidth="10" marginheight="10">
<?php

}

// ###################### Start cpfooter #######################
function cpfooter() {
global $gzipoutput,$nozip,$level,$HTTP_ACCEPT_ENCODING;

?>
</BODY></HTML>
<?php

	if ($gzipoutput and !headers_sent() and function_exists("ob_start") and function_exists("crc32") and function_exists("gzcompress") and !$nozip) {
    if (strpos(" ".$HTTP_ACCEPT_ENCODING,"x-gzip")) {
      $encoding = "x-gzip";
    }
    if (strpos(" ".$HTTP_ACCEPT_ENCODING,"gzip")) {
      $encoding = "gzip";
    }

    if ($encoding) {
	   	$text = ob_get_contents();
			ob_end_clean();

      header("Content-Encoding: $encoding");

      $size = strlen($text);
      $crc = crc32($text);

      $returntext = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
      $returntext .= substr(gzcompress($text,$level),0,-4);
      $returntext .= pack("V",$crc);
      $returntext .= pack("V",$size);

      echo $returntext;
      exit;
    }
  }
}

// ###################### Start makelinkcode #######################
function makelinkcode($text,$url,$newwin=0,$popup="") {
	$url=append_sid($url);
  return " <a href=\"$url\" class=\"lc\"".iif($newwin," target=\"_blank\"","").iif($popup!="","title=\"$popup\"","").">[$text]</a>";
}

// ###################### Start iif #######################
function iif ($expression,$returntrue,$returnfalse) {

  if ($expression==0) {
    return $returnfalse;
  } else {
    return $returntrue;
  }

}

// ###################### Start doformheader #######################
function doformheader ($phpscript,$action,$uploadform=0,$addtable=1,$name="name") {
// makes the standard form header, setting sctript to call and action to do
  global $session,$tableadded;
	$phpscript=append_sid($phpscript.".php");
  echo "<form action=\"$phpscript\" ".iif($uploadform,"ENCTYPE=\"multipart/form-data\" ","")." name=\"$name\" method=\"post\">\n<input type=\"hidden\" name=\"s\" value=\"$session[sessionhash]\"><input type=\"hidden\" name=\"action\" value=\"$action\">\n";

  if ($addtable==1) {
  	$tableadded = 1;
    echo "<br><table cellpadding='1' cellspacing='0' border='0' align='center' width='90%' class='tblborder'><tr><td>\n";
  	echo "<table cellpadding='4' cellspacing='0' border='0' width='100%'>\n";
  } else {
  	$tableadded = 0;
  }
}

// ###################### Start maketableheader #######################
function maketableheader ($title,$anchor="",$htmlise=1,$colspan=2) {
// makes a two-cell spanning bar with a named <A> and a title
// then reinitialises the bgcolor counter.
	global $bgcounter;
	echo "<tr class='tblhead'><td colspan='$colspan'><a name=\"$anchor\"><font size='1'><b><span class='tblhead'>".iif($htmlise,htmlspecialchars($title),$title)."</span></b></font></a></td></tr>";
	$bgcounter = 0;
}

// ###################### Start makeuploadcode #######################
function makeuploadcode ($title,$name,$maxfilesize=1000000) {
// makes code for an imput box: first column contains $title
// second column contains an input box of name
	echo "<tr class='".getrowbg()."' valign='top'>\n<td><p>$title</p></td>\n<td><p><INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$maxfilesize\"><input type=\"file\" name=\"$name\"></p></td>\n</tr>\n";
}

// ###################### Start getrowbg #######################
function getrowbg () {
// returns the current alternating class for <TR> rows in the CP.
	global $bgcounter;
	if ($bgcounter++%2==0) {
		return "firstalt";
	} else {
		return "secondalt";
	}
}

// ###################### Start makeinputcode #######################
function makeinputcode ($title,$name,$value="",$htmlise=1,$size=35) {
// makes code for an imput box: first column contains $title
// second column contains an input box of name, $name and value, $value. $value is "HTMLised"

	if ($htmlise) {
		$value=htmlspecialchars($value);
	}

  echo "<tr class='".getrowbg()."' valign='top'>\n<td><p>$title</p></td>\n<td><p><input type=\"text\" size=\"$size\" name=\"$name\" value=\"$value\"></p></td>\n</tr>\n";
}

// ###################### Start doformfooter #######################
function doformfooter($submitname="°e¥X",$resetname="­«³]",$colspan=2,$goback="") {
// closes the standard form table and makes a new one containing centred submit and reset buttons
  global $tableadded;

  echo iif($tableadded==1,"<tr id='submitrow'>\n<td colspan='$colspan' align='center'>","<p><center>");
  echo "<p id='submitrow'><input type=\"submit\" value=\"   $submitname   \" accesskey=\"s\">\n";
  if ($resetname!="") {
  	echo "<input type=\"reset\" value=\"   $resetname   \">\n";
  }
  if ($goback!="") {
  	echo "<input type=\"button\" value=\"   $goback   \" onclick=\"history.back(1)\">\n";
  }
  echo iif($tableadded==1,"</p></td>\n</tr>\n</table>\n</td>\n</tr>\n</table>\n","</p></center>\n");
  echo "</form>\n";
}

// ####################### add these functions if they aren't defined #######
if (!function_exists("is_uploaded_file")) {
  function is_uploaded_file($filename) {
    if (function_exists('ini_get')) {
      $utdval = ini_get('upload_tmp_dir');
    } else {
      $utdval = get_cfg_var('upload_tmp_dir');
    }

    if (!$tmp_file = str_replace('\\', '/', $utdval)) {
        $tmp_file = dirname(str_replace('\\', '/', tempnam('', '')));
    }
    $tmp_file .= '/' . basename($filename);

    // remove any trailing slashes and standardize directory seperator to /
    return (ereg_replace('/+', '/', $tmp_file) == str_replace('\\', '/', $filename));
  }
}

if (!function_exists('move_uploaded_file')) {
  function move_uploaded_file($filename, $destination) {
     if (is_uploaded_file($filename))  {
       if (copy($filename,$destination)) {
         return true;
       } else {
         return false;
       }
     } else {
       return false;
     }
   }
}

// ###################### Start makehiddencode #######################
function makehiddencode ($name,$value="",$htmlise=1) {
// makes code for an imput box: first column contains $title
// second column contains an input box of name, $name and value, $value. $value is "HTMLised"

  if ($htmlise) {
    $value=htmlspecialchars($value);
  }
  echo "<input type=\"hidden\" name=\"$name\" value=\"$value\">\n";
}

// ###################### Start makedescription #######################
function makedescription($text,$htmlise=0) {
// makes a two-cell <tr> for text descriptions
	echo "<tr class='".getrowbg()."' valign='top'><td colspan='2'>".iif($htmlise==0,$text,htmlspecialchars($text))."</td></tr>\n";
}

// ###################### Start gettemplate #######################
function gettemplate($templatename,$escape=1,$gethtmlcomments=1) {
  // gets a template from the db or from the local cache
  global $templatecache,$db,$templatesetid,$addtemplatename;

  if (isset($templatecache[$templatename])) {
    $template=$templatecache[$templatename];
  } else {
	$result = $db->sql_query("SELECT template FROM template WHERE title='".addslashes($templatename)."' AND (templatesetid=-1 OR templatesetid=" . intval($templatesetid). ") ORDER BY templatesetid DESC LIMIT 1");
	$gettemp = $db->sql_fetchrow($result);
    $template=$gettemp[template];
    $templatecache[$templatename]=$template;
 }

  if ($escape==1) {
    $template=addslashes($template);
    $template=str_replace("\\'","'",$template);
  }
  if ($gethtmlcomments and $addtemplatename) {
    return "<!-- BEGIN TEMPLATE: $templatename -->\n$template\n<!-- END TEMPLATE: $templatename -->";
  }

  return $template;
}

// ###################### Start dooutput #######################
function dooutput($vartext,$sendheader=1) {

  global $pagestarttime,$query_count,$showqueries,$querytime,$DB_site;

  if ($showqueries) {
    $pageendtime=microtime();

    $starttime=explode(" ",$pagestarttime);
    $endtime=explode(" ",$pageendtime);

    $totaltime=$endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];

    $vartext.="<!-- Page generated in $totaltime seconds with $query_count queries -->";
  }

  if (!$showqueries) {
    echo dovars($vartext,$sendheader);
	flush();
  } else {
    $output=dovars($vartext,$sendheader);
    echo "\n<b>Page generated in $totaltime seconds with $query_count queries,\nspending $querytime doing MySQL queries and ".($totaltime-$querytime)." doing PHP things.</b></pre>";
    flush();
  }
}

// ###################### Start dovars #######################
function dovars($newtext, $sendheader = 1)
{
	// parses replacement vars

	global $PHP_SELF, $DB_site, $replacementsetid, $gzipoutput, $gziplevel, $newpmmsg;
	//global $PHP_SELF, $DB_site, $replacementsetid, $gzipoutput, $gziplevel, $newpmmsg,$HTTP_COOKIE_VARS,$big5;
	static $vars;

	if (connection_status())
	{
		exit;
	}

	if ($gzipoutput and !headers_sent())
	{
		$newtext = gzipoutput($newtext, $gziplevel);
	}

	if ($sendheader)
	{
		@header("Content-Length: " . strlen($newtext));
	}
	return $newtext;
}

?>