147 lines
5.5 KiB
PHP
147 lines
5.5 KiB
PHP
|
<?php // lib.php :: Common functions used throughout the program.
|
||
|
|
||
|
$starttime = getmicrotime();
|
||
|
$numqueries = 0;
|
||
|
|
||
|
function opendb() { // Open database connection.
|
||
|
|
||
|
include('../config.php');
|
||
|
extract($dbsettings);
|
||
|
$link = mysql_connect($server, $user, $pass) or die(mysql_error());
|
||
|
mysql_select_db($name) or die(mysql_error());
|
||
|
return $link;
|
||
|
|
||
|
}
|
||
|
|
||
|
function doquery($query, $table) { // Something of a tiny little database abstraction layer.
|
||
|
|
||
|
include('../config.php');
|
||
|
global $numqueries;
|
||
|
$sqlquery = mysql_query(str_replace("{{table}}", $dbsettings["prefix"] . "_" . $table, $query)) or die(mysql_error());
|
||
|
$numqueries++;
|
||
|
return $sqlquery;
|
||
|
|
||
|
}
|
||
|
|
||
|
function gettemplate($templatename) { // SQL query for the template.
|
||
|
|
||
|
$filename = "../templates/" . $templatename . ".php";
|
||
|
include("$filename");
|
||
|
return $template;
|
||
|
|
||
|
}
|
||
|
|
||
|
function parsetemplate($template, $array) { // Replace template with proper content.
|
||
|
|
||
|
foreach($array as $a => $b) {
|
||
|
$template = str_replace("{{{$a}}}", $b, $template);
|
||
|
}
|
||
|
return $template;
|
||
|
|
||
|
}
|
||
|
|
||
|
function getmicrotime() { // Used for timing script operations.
|
||
|
|
||
|
list($usec, $sec) = explode(" ",microtime());
|
||
|
return ((float)$usec + (float)$sec);
|
||
|
|
||
|
}
|
||
|
|
||
|
function prettydate($uglydate) { // Change the MySQL date format (YYYY-MM-DD) into something friendlier.
|
||
|
|
||
|
return date("F j, Y", mktime(0,0,0,substr($uglydate, 5, 2),substr($uglydate, 8, 2),substr($uglydate, 0, 4)));
|
||
|
|
||
|
}
|
||
|
|
||
|
function is_email($email) { // Thanks to "mail(at)philipp-louis.de" from php.net!
|
||
|
|
||
|
return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
|
||
|
|
||
|
}
|
||
|
|
||
|
function my_htmlspecialchars($text) { // Thanks to "etymxris at yahoo dot com" from php.net!
|
||
|
|
||
|
$ALLOWABLE_TAGS = array("b", "i", "u", "p", "blockquote", "ol", "ul", "li");
|
||
|
static $PATTERNS = array();
|
||
|
static $REPLACEMENTS = array();
|
||
|
if (count($PATTERNS) == 0) {
|
||
|
foreach ($ALLOWABLE_TAGS as $tag) {
|
||
|
$PATTERNS[] = "/<$tag>/i";
|
||
|
$PATTERNS[] = "/<\/$tag>/i";
|
||
|
$REPLACEMENTS[] = "<$tag>";
|
||
|
$REPLACEMENTS[] = "</$tag>";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$result = str_replace(array(">", "<", "\"", "'"),
|
||
|
array(">", "<", """, "'"),
|
||
|
$text);
|
||
|
|
||
|
$result = preg_replace($PATTERNS, $REPLACEMENTS, $result);
|
||
|
|
||
|
return $result;
|
||
|
|
||
|
}
|
||
|
|
||
|
function checkcookies() {
|
||
|
|
||
|
include('../config.php');
|
||
|
|
||
|
$row = false;
|
||
|
|
||
|
if (isset($_COOKIE["dkgame"])) {
|
||
|
|
||
|
// COOKIE FORMAT:
|
||
|
// {ID} {USERNAME} {PASSWORDHASH} {REMEMBERME}
|
||
|
$theuser = explode(" ",$_COOKIE["dkgame"]);
|
||
|
$query = doquery("SELECT * FROM {{table}} WHERE username='$theuser[1]'", "users");
|
||
|
if (mysql_num_rows($query) != 1) { die("Invalid cookie data (Error 1). Please clear cookies and log in again."); }
|
||
|
$row = mysql_fetch_array($query);
|
||
|
if ($row["id"] != $theuser[0]) { die("Invalid cookie data (Error 2). Please clear cookies and log in again."); }
|
||
|
if (md5($row["password"] . "--" . $dbsettings["secretword"]) != $theuser[2]) { die("Invalid cookie data (Error 3). Please clear cookies and log in again."); }
|
||
|
|
||
|
// If we've gotten this far, cookie should be valid, so write a new one.
|
||
|
$newcookie = implode(" ",$theuser);
|
||
|
if ($theuser[3] == 1) { $expiretime = time()+31536000; } else { $expiretime = 0; }
|
||
|
setcookie ("dkgame", $newcookie, $expiretime, "/", "", 0);
|
||
|
$onlinequery = doquery("UPDATE {{table}} SET onlinetime=NOW() WHERE id='$theuser[0]' LIMIT 1", "users");
|
||
|
|
||
|
}
|
||
|
|
||
|
return $row;
|
||
|
|
||
|
}
|
||
|
|
||
|
function display($content, $title) { // Finalize page and output to browser.
|
||
|
|
||
|
include('../config.php');
|
||
|
global $numqueries, $userrow, $controlrow, $starttime;
|
||
|
if (!isset($controlrow)) {
|
||
|
$controlquery = doquery("SELECT * FROM {{table}} WHERE id='1' LIMIT 1", "control");
|
||
|
$controlrow = mysql_fetch_array($controlquery);
|
||
|
}
|
||
|
|
||
|
$template = gettemplate("admin");
|
||
|
|
||
|
// Make page tags for XHTML validation.
|
||
|
$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
|
||
|
. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">\n"
|
||
|
. "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
|
||
|
|
||
|
$finalarray = array(
|
||
|
"title"=>$title,
|
||
|
"content"=>$content,
|
||
|
"totaltime"=>round(getmicrotime() - $starttime, 4),
|
||
|
"numqueries"=>$numqueries,
|
||
|
"version"=>$version,
|
||
|
"build"=>$build);
|
||
|
$page = parsetemplate($template, $finalarray);
|
||
|
$page = $xml . $page;
|
||
|
|
||
|
if ($controlrow["compression"] == 1) { ob_start("ob_gzhandler"); }
|
||
|
echo $page;
|
||
|
die();
|
||
|
|
||
|
}
|
||
|
|
||
|
?>
|