File size: 2,836 Bytes
36ddefa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
<?php
function cleanupDb()
{
if (isset($db)) {
$db->close();
$db = null;
}
}
function abortDb($errormsg)
{
header('HTTP/1.0 500 Error');
echo "<h1>500 Error</h1>";
echo $errormsg;
cleanupDb();
exit;
}
function initDbIfNeeded()
{
global $db, $dbhost, $dbuserid, $dbpassword, $dbname, $dbport;
$pos = strpos($dbname, '?');
if ($pos) {
$dbname = substr($dbname, 0, $pos);
}
if (!$db) {
$db = mysqli_connect('p:' . $dbhost, $dbuserid, $dbpassword, $dbname, $dbport);
if (mysqli_connect_errno()) {
abortDb("Error opening database");
}
}
}
function getStandaloneFileByServerId($fname, $sid)
{
global $db, $dbprefix;
initDbIfNeeded();
$stmt = $db->prepare('SELECT Content from ' . $dbprefix . 'StandaloneFiles WHERE FileName=? AND ServerID=?');
$stmt->bind_param('si', $fname, $sid);
$res = $stmt->execute();
$stmt->store_result();
$stmt->bind_result($content);
if ($stmt->fetch()) {
$rslt = $content;
} else {
$rslt = null;
}
$stmt->close();
return $rslt;
}
function getStandaloneFile($fname)
{
global $serverid;
if (!isset($serverid)) {
$serverid = 0;
if (isset($_REQUEST['serverid'])) {
$serverid = $_REQUEST['serverid'];
}
}
return getStandaloneFileByServerId($fname, $serverid);
}
function updateStandaloneFileByServerId($fname, $sid, $content)
{
global $db, $dbprefix;
initDbIfNeeded();
$stmt = $db->prepare('UPDATE ' . $dbprefix . 'StandaloneFiles SET Content=? WHERE FileName=? AND ServerID=?');
$stmt->bind_param('ssi', $content, $fname, $sid);
$res = $stmt->execute();
$stmt->close();
if (!$res) {
$res = insertStandaloneFileByServerId($fname, $sid, $content);
}
return $res;
}
function updateStandaloneFile($fname, $content)
{
global $serverid;
if (!isset($serverid)) {
$serverid = 0;
if (isset($_REQUEST['serverid'])) {
$serverid = $_REQUEST['serverid'];
}
}
return updateStandaloneFileByServerId($fname, $serverid, $content);
}
function insertStandaloneFileByServerId($fname, $sid, $content)
{
global $db, $dbprefix;
initDbIfNeeded();
$stmt = $db->prepare('INSERT INTO ' . $dbprefix . 'StandaloneFiles (Content,FileName,ServerID) VALUES (?,?,?);');
$stmt->bind_param('ssi', $content, $fname, $sid);
$res = $stmt->execute();
$stmt->close();
return $res;
}
function insertStandaloneFile($fname, $content)
{
global $serverid;
if (!isset($serverid)) {
$serverid = 0;
if (isset($_REQUEST['serverid'])) {
$serverid = $_REQUEST['serverid'];
}
}
return insertStandaloneFileByServerId($fname, $serverid, $content);
}
|