Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

many improvements #6

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
oups missing functions
William waisse committed Mar 4, 2012
commit 68f2fc0ee1950c25f2bd120556632e656c906b66
102 changes: 102 additions & 0 deletions includes/requiredFunctions.php
Original file line number Diff line number Diff line change
@@ -132,4 +132,106 @@ function antiXss($input) {
//strip HTML tags from input data
return htmlentities(strip_tags($input), ENT_QUOTES);
}

function sqlerr($file = '', $line = '')
{
print("<table border=0 bgcolor=blue align=left cellspacing=0 cellpadding=10 style='background: blue'>" .
"<tr><td class=embedded><font color=white><h1>SQL Error</h1>\n" .
"<b>" . mysql_error() . ($file != '' && $line != '' ? "<p>in $file, line $line</p>" : "") . "</b></font></td></tr></table>");
die;
}

$_current_lock = null;

function islocked($name) {
$result = mysql_query("SELECT locked FROM locks WHERE name ='$name' and locked=1 LIMIT 1");
if (!$result || mysql_numrows($result) == 0)
return false;
return true;
}

function unlock() {
global $_current_lock;
mysql_query("UNLOCK TABLES");
$sql = "UPDATE locks SET locked = 0 WHERE name = '" . mysql_real_escape_string($_current_lock) . "'";
mysql_query($sql);
}

function lock($name) {
global $_current_lock;
mysql_query("LOCK TABLES locks WRITE");
$q = mysql_query("SELECT locked FROM locks WHERE name = '" . mysql_real_escape_string($name) . "'");

$lock = mysql_fetch_object($q);
if ($lock === false) {
mysql_query("INSERT INTO locks (name, locked) VALUES ('".mysql_real_escape_string($name)."', 1)");
} elseif ($lock->locked) {
echo("Lock already held, exiting. (".$name.")");
mysql_query("UNLOCK TABLES");
exit();
return;
} else {
mysql_query("UPDATE locks SET locked = 1 WHERE name = '" . mysql_real_escape_string($name) . "'");
}

//mysql_query("UNLOCK TABLES");
$_current_lock = $name;
register_shutdown_function('unlock');
}

function ScriptIsRunLocally() {
if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != "127.0.0.1") {
echo "This script can only be run locally.";
exit;
}
}

//Cache functions

# Gets key / value pair into memcache ... called by mysql_query_cache()
function getCache($key) {
global $memcache;
return ($memcache) ? $memcache->get($key) : false;
}

# Puts key / value pair into memcache ... called by mysql_query_cache()
function setCache($key, $object, $timeout = 600) {
global $memcache;
return ($memcache) ? $memcache->set($key, $object, $timeout) : false;
}

function removeCache($key) {
global $memcache;
$memcache->delete($key);
}

function removeSqlCache($key) {
global $memcache;
$memcache->delete(md5("mysql_query".$key));
}

# Caching version of mysql_query()
function mysql_query_cache($sql, $timeout = 600) {
if($objResultset = unserialize(getCache(md5("mysql_query".$sql)))) {
return $objResultset;
}
$objResultSet = mysql_query($sql);
$objarray = Array();
while ($row = mysql_fetch_object($objResultSet)) {
$objarray[] = $row;
}
setCache(md5("mysql_query".$sql), serialize($objarray), $timeout);
return $objarray;
}

function GetCachedBitcoinDifficulty() {
global $bitcoinController;
$difficulty = 0;
if (!($difficulty = getCache("bitcoinDifficulty"))) {
$difficulty = $bitcoinController->query("getdifficulty");
setCache("bitcoinDifficulty", $difficulty, 60);
}
return $difficulty;
}

?>