Refactored DB from global to singleton
This commit is contained in:
@@ -63,13 +63,11 @@ class CrashReport
|
||||
}
|
||||
function getTotal($filter)
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$where = $filter->getWhere();
|
||||
$q = "select count(id) as total from reports $where";
|
||||
if (false !== $cached = Memc::getq($q)) return $cached;
|
||||
|
||||
if (!$res = $DB->query($q) OR !$row = $DB->fetchRow($res))
|
||||
if (!$res = DBH::$db->query($q) OR !$row = DBH::$db->fetchRow($res))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -82,21 +80,19 @@ class CrashReport
|
||||
|
||||
function getReports($filter, $fields = "id, reported, client_version, client_channel, os, gpu, grid, region")
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$ret = array();
|
||||
$q = "select $fields from reports " . $filter->getWhere() . kl_str_sql(" order by id desc limit !i offset !i", $filter->limit, $filter->offset);
|
||||
if (false !== $cached = Memc::getq($q)) return $cached;
|
||||
|
||||
if (!$res = $DB->query($q))
|
||||
if (!$res = DBH::$db->query($q))
|
||||
{
|
||||
return $ret;
|
||||
}
|
||||
|
||||
while ($row = $DB->fetchRow($res))
|
||||
while ($row = DBH::$db->fetchRow($res))
|
||||
{
|
||||
$r = new CrashReport;
|
||||
$DB->loadFromDbRow($r, $res, $row);
|
||||
DBH::$db->loadFromDbRow($r, $res, $row);
|
||||
$ret[] = $r;
|
||||
}
|
||||
|
||||
@@ -106,18 +102,16 @@ class CrashReport
|
||||
|
||||
function getReport($id)
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$ret = array();
|
||||
if (!$res = $DB->query(kl_str_sql("select * from reports where id=!i", $id)))
|
||||
if (!$res = DBH::$db->query(kl_str_sql("select * from reports where id=!i", $id)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($row = $DB->fetchRow($res))
|
||||
if ($row = DBH::$db->fetchRow($res))
|
||||
{
|
||||
$r = new CrashReport;
|
||||
$DB->loadFromDbRow($r, $res, $row);
|
||||
DBH::$db->loadFromDbRow($r, $res, $row);
|
||||
$r->parseStackTrace($r->raw_stacktrace);
|
||||
return $r;
|
||||
}
|
||||
@@ -127,13 +121,11 @@ class CrashReport
|
||||
|
||||
function delete()
|
||||
{
|
||||
global $DB;
|
||||
$DB->query(kl_str_sql("delete from reports where id=!i", $this->id));
|
||||
DBH::$db->query(kl_str_sql("delete from reports where id=!i", $this->id));
|
||||
}
|
||||
|
||||
function save()
|
||||
{
|
||||
global $DB;
|
||||
$this->delete();
|
||||
$q = kl_str_sql("insert into reports (
|
||||
id,
|
||||
@@ -171,7 +163,7 @@ class CrashReport
|
||||
$this->crash_address,
|
||||
$this->crash_thread,
|
||||
$this->raw_stacktrace);
|
||||
if ($res = $DB->query($q))
|
||||
if ($res = DBH::$db->query($q))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<?php
|
||||
class DBH
|
||||
{
|
||||
|
||||
public
|
||||
$db_name,
|
||||
$db_pass,
|
||||
@@ -9,6 +8,9 @@ class DBH
|
||||
$db_host,
|
||||
$dbh,
|
||||
$last_error = "";
|
||||
|
||||
// instance
|
||||
static $db = null;
|
||||
|
||||
function log($line)
|
||||
{
|
||||
@@ -31,7 +33,16 @@ class DBH
|
||||
|
||||
@fwrite($f, "[".date('Y-m-d H:i')."] ".$line."\n");
|
||||
}
|
||||
|
||||
|
||||
function getInstance()
|
||||
{
|
||||
if (self::$db === null)
|
||||
{
|
||||
self::$db = new DBH;
|
||||
}
|
||||
|
||||
return self::$db;
|
||||
}
|
||||
|
||||
function connect($db_name, $db_host, $db_user, $db_pass)
|
||||
{
|
||||
|
||||
@@ -38,12 +38,10 @@ class Option
|
||||
*/
|
||||
public static function init()
|
||||
{
|
||||
global $DB;
|
||||
|
||||
if(!$result = $DB->query("SELECT * FROM options")) {
|
||||
if(!$result = DBH::$db->query("SELECT * FROM options")) {
|
||||
return false;
|
||||
} else {
|
||||
while ($row = $DB->fetchRow($result)) {
|
||||
while ($row = DBH::$db->fetchRow($result)) {
|
||||
self::$optionArray[$row['name']] = $row['value'];
|
||||
}
|
||||
return true;
|
||||
@@ -59,9 +57,8 @@ class Option
|
||||
*/
|
||||
public static function update($name, $value)
|
||||
{
|
||||
global $DB;
|
||||
$result = $DB->query(kl_str_sql("DELETE FROM options WHERE name=!s", $name));
|
||||
$result = $DB->query(kl_str_sql("INSERT INTO options (name, value) VALUES(!s,!s)", $name,$value ));
|
||||
$result = DBH::$db->query(kl_str_sql("DELETE FROM options WHERE name=!s", $name));
|
||||
$result = DBH::$db->query(kl_str_sql("INSERT INTO options (name, value) VALUES(!s,!s)", $name,$value ));
|
||||
self::$optionArray[$name] = $value;
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -37,14 +37,13 @@ class ReportParser
|
||||
|
||||
function parse($id)
|
||||
{
|
||||
global $DB;
|
||||
$q = kl_str_sql("select * from raw_reports where report_id=!i", $id);
|
||||
if (!$res = $DB->query($q) OR !$row = $DB->fetchRow($res))
|
||||
if (!$res = DBH::$db->query($q) OR !$row = DBH::$db->fetchRow($res))
|
||||
{
|
||||
return array();
|
||||
}
|
||||
$data = new stdClass;
|
||||
$DB->loadFromDbRow($data, $res, $row);
|
||||
DBH::$db->loadFromDbRow($data, $res, $row);
|
||||
$data->report = llsd_decode($data->raw_data);
|
||||
$data->report["reported"] = $data->reported;
|
||||
unset($data->raw_data);
|
||||
@@ -62,11 +61,10 @@ class ReportParser
|
||||
|
||||
function setProcessed($id, $status)
|
||||
{
|
||||
global $DB;
|
||||
$ret = array();
|
||||
$q = kl_str_sql("update raw_reports set processed=!i where report_id=!i", $status, $id);
|
||||
|
||||
if ($res = $DB->query($q))
|
||||
if ($res = DBH::$db->query($q))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -78,16 +76,15 @@ class ReportParser
|
||||
|
||||
function getUnprocessedIDs()
|
||||
{
|
||||
global $DB;
|
||||
$ret = array();
|
||||
$q = kl_str_sql("select report_id from raw_reports where processed=!i", 0);
|
||||
|
||||
if (!$res = $DB->query($q))
|
||||
if (!$res = DBH::$db->query($q))
|
||||
{
|
||||
return $ret;
|
||||
}
|
||||
|
||||
while ($row = $DB->fetchRow($res))
|
||||
while ($row = DBH::$db->fetchRow($res))
|
||||
{
|
||||
$ret[] = (int)$row["report_id"];
|
||||
}
|
||||
|
||||
@@ -81,19 +81,17 @@ class SearchFilter
|
||||
|
||||
function getVersions()
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$ret = array();
|
||||
$where = $this->chan ? kl_str_sql("where client_channel=!s", $this->chan) : '';
|
||||
$q = "select distinct client_version from reports $where order by client_version desc";
|
||||
if (false !== $cached = Memc::getq($q)) return $cached;
|
||||
|
||||
if (!$res = $DB->query($q))
|
||||
if (!$res = DBH::$db->query($q))
|
||||
{
|
||||
return $ret;
|
||||
}
|
||||
|
||||
while ($row = $DB->fetchRow($res))
|
||||
while ($row = DBH::$db->fetchRow($res))
|
||||
{
|
||||
$ret[] = $row["client_version"];
|
||||
}
|
||||
@@ -104,18 +102,16 @@ class SearchFilter
|
||||
|
||||
function getGrids()
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$ret = array();
|
||||
$q = "select distinct grid from reports order by grid asc";
|
||||
if (false !== $cached = Memc::getq($q)) return $cached;
|
||||
|
||||
if (!$res = $DB->query($q))
|
||||
if (!$res = DBH::$db->query($q))
|
||||
{
|
||||
return $ret;
|
||||
}
|
||||
|
||||
while ($row = $DB->fetchRow($res))
|
||||
while ($row = DBH::$db->fetchRow($res))
|
||||
{
|
||||
$ret[] = $row["grid"];
|
||||
}
|
||||
@@ -155,7 +151,7 @@ class SearchFilter
|
||||
|
||||
<div class="filterelem">
|
||||
Version<br/>
|
||||
<select class="ui-widget-content" name="version" onchange="this.form.submit();" style="width: 100px; margin-top: 3px;">
|
||||
<select class="ui-widget-content" name="version" onchange="this.form.submit();" style="width: 100px; margin-top: 4px;">
|
||||
<option value="" <?php echo !$this->version ? 'selected="selected"' : '' ?>>All</option>
|
||||
<?php
|
||||
for($i = 0; $i < count($ver); $i++)
|
||||
@@ -179,7 +175,7 @@ for($i = 0; $i < count($ver); $i++)
|
||||
|
||||
<div class="filterelem">
|
||||
Grid<br/>
|
||||
<select class="ui-widget-content" name="grid" onchange="this.form.submit();" style="width: 200px; margin-top: 3px;">
|
||||
<select class="ui-widget-content" name="grid" onchange="this.form.submit();" style="width: 200px; margin-top: 4px;">
|
||||
<option value="" <?php echo !$this->grid ? 'selected="selected"' : '' ?>>All</option>
|
||||
<?php
|
||||
for($i = 0; $i < count($grids); $i++)
|
||||
|
||||
@@ -66,12 +66,10 @@ class Session
|
||||
|
||||
function add()
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$this->sid = md5(uniqid(rand()));
|
||||
$this->expires=time() + $this->timeout;
|
||||
|
||||
$DB->query(kl_str_sql("DELETE from session where sid=!s", $this->sid));
|
||||
DBH::$db->query(kl_str_sql("DELETE from session where sid=!s", $this->sid));
|
||||
$q = kl_str_sql("INSERT into session (sid, user_id, authenticated, expires, persist) ".
|
||||
"values (!s, !i, !i, !t, !s)",
|
||||
$this->sid,
|
||||
@@ -81,7 +79,7 @@ class Session
|
||||
$this->ser_persist
|
||||
);
|
||||
|
||||
if ($DB->query($q)) {
|
||||
if (DBH::$db->query($q)) {
|
||||
setcookie($this->cookie, $this->sid, NULL, '/' . REL_DIR);
|
||||
return true;
|
||||
} else {
|
||||
@@ -91,12 +89,10 @@ class Session
|
||||
|
||||
function update()
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$q = kl_str_sql('UPDATE session SET user_id=!i, authenticated=!i, expires=!t, persist=!s WHERE sid=!s',
|
||||
$this->user->user_id, $this->authenticated, $this->expires, $this->ser_persist, $this->sid);
|
||||
|
||||
if (($res = $DB->query($q)) && $DB->affectedRows()) {
|
||||
if (($res = DBH::$db->query($q)) && DBH::$db->affectedRows()) {
|
||||
return true;
|
||||
} else {
|
||||
return $this->add();
|
||||
@@ -105,8 +101,6 @@ class Session
|
||||
|
||||
function remove($sid = false)
|
||||
{
|
||||
global $DB;
|
||||
|
||||
if (!$sid) {
|
||||
$sid = $this->sid;
|
||||
}
|
||||
@@ -116,13 +110,12 @@ class Session
|
||||
$this->user = new User;
|
||||
$this->persist = NULL;
|
||||
$this->ser_persist = NULL;
|
||||
$DB->query(kl_str_sql("DELETE from session where sid=!s", $sid));
|
||||
DBH::$db->query(kl_str_sql("DELETE from session where sid=!s", $sid));
|
||||
setcookie($this->cookie, '', 0, '/' . REL_DIR);
|
||||
}
|
||||
|
||||
function check()
|
||||
{
|
||||
global $DB;
|
||||
if (isset($_GET[$this->cookie])) {
|
||||
$this->sid = $_GET[$this->cookie];
|
||||
} else {
|
||||
@@ -143,8 +136,8 @@ class Session
|
||||
}
|
||||
$error = false;
|
||||
} else {
|
||||
$res = $DB->query(kl_str_sql('SELECT * from session where sid=!s', $this->sid));
|
||||
if ($res AND $row = $DB->fetchRow($res)) {
|
||||
$res = DBH::$db->query(kl_str_sql('SELECT * from session where sid=!s', $this->sid));
|
||||
if ($res AND $row = DBH::$db->fetchRow($res)) {
|
||||
$this->authenticated = (int)$row['authenticated'];
|
||||
$this->expires = strtotime($row['expires']);
|
||||
$this->ser_persist = $row['persist'];
|
||||
|
||||
@@ -24,7 +24,6 @@ class User
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
global $DB;
|
||||
$query = kl_str_sql('INSERT INTO users(
|
||||
name,
|
||||
email,
|
||||
@@ -41,17 +40,16 @@ class User
|
||||
$this->is_allowed
|
||||
);
|
||||
|
||||
if (!$res = $DB->query($query)) {
|
||||
if (!$res = DBH::$db->query($query)) {
|
||||
return false;
|
||||
} else {
|
||||
$this->user_id = $DB->insertID();
|
||||
$this->user_id = DBH::$db->insertID();
|
||||
return $this->user_id;
|
||||
}
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
global $DB;
|
||||
$query = kl_str_sql('
|
||||
UPDATE users SET
|
||||
name=!s,
|
||||
@@ -70,7 +68,7 @@ class User
|
||||
$this->user_id
|
||||
);
|
||||
//echo $query;
|
||||
if (!$DB->query($query)) {
|
||||
if (!DBH::$db->query($query)) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
@@ -85,19 +83,17 @@ class User
|
||||
*/
|
||||
public static function get($id)
|
||||
{
|
||||
global $DB;
|
||||
|
||||
if (is_null($id)) {
|
||||
return new User();
|
||||
}
|
||||
|
||||
$query = kl_str_sql("SELECT * FROM users WHERE user_id=!i",$id);
|
||||
|
||||
if(!$res = $DB->query($query) OR !$row = $DB->fetchRow($res)) {
|
||||
if(!$res = DBH::$db->query($query) OR !$row = DBH::$db->fetchRow($res)) {
|
||||
return false;
|
||||
} else {
|
||||
$user = new User();
|
||||
$DB->loadFromDbRow($user, $res, $row);
|
||||
DBH::$db->loadFromDbRow($user, $res, $row);
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -124,15 +120,13 @@ class User
|
||||
*/
|
||||
public static function getByLogin($username)
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$query = kl_str_sql('SELECT * FROM users WHERE login=!s', $username);
|
||||
|
||||
if(!$res = $DB->query($query) OR !$row = $DB->fetchRow($res)) {
|
||||
if(!$res = DBH::$db->query($query) OR !$row = DBH::$db->fetchRow($res)) {
|
||||
return false;
|
||||
} else {
|
||||
$user = new User();
|
||||
$DB->loadFromDbRow($user, $res, $row);
|
||||
DBH::$db->loadFromDbRow($user, $res, $row);
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -145,14 +139,12 @@ class User
|
||||
*/
|
||||
public static function getByEmail($email)
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$query = kl_str_sql('SELECT * FROM users WHERE cust_id!=1 AND email=!s', $email);
|
||||
if (!$res = $DB->query($query) OR !$row = $DB->fetchRow($res)) {
|
||||
if (!$res = DBH::$db->query($query) OR !$row = DBH::$db->fetchRow($res)) {
|
||||
return false;
|
||||
} else {
|
||||
$user = new User();
|
||||
$DB->loadFromDbRow($user, $res, $row);
|
||||
DBH::$db->loadFromDbRow($user, $res, $row);
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -164,16 +156,14 @@ class User
|
||||
*/
|
||||
public static function getAll()
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$query = kl_str_sql('SELECT * FROM users ORDER BY is_admin DESC, is_allowed DESC, user_id ASC');
|
||||
if(!$res = $DB->query($query)) {
|
||||
if(!$res = DBH::$db->query($query)) {
|
||||
return false;
|
||||
} else {
|
||||
$retval=array();
|
||||
while($row = $DB->fetchRow($res)) {
|
||||
while($row = DBH::$db->fetchRow($res)) {
|
||||
$tmp = new User();
|
||||
$DB->loadFromDbRow($tmp, $res, $row);
|
||||
DBH::$db->loadFromDbRow($tmp, $res, $row);
|
||||
$retval[] = $tmp;
|
||||
}
|
||||
return $retval;
|
||||
@@ -181,10 +171,9 @@ class User
|
||||
}
|
||||
|
||||
function delete(){
|
||||
global $DB;
|
||||
$query=kl_str_sql("DELETE FROM users WHERE user_id=!i",$this->user_id);
|
||||
//echo $query;
|
||||
if(!$res=$DB->query($query)){
|
||||
if(!$res=DBH::$db->query($query)){
|
||||
return false;
|
||||
}
|
||||
else{
|
||||
|
||||
@@ -57,14 +57,14 @@ if (!defined('IMG_ROOT')) {
|
||||
}
|
||||
|
||||
|
||||
$DB = new DBH();
|
||||
$DB = DBH::getInstance();
|
||||
|
||||
$DB_NAME = "singucrash";
|
||||
$DB_USER = 'singucrash';
|
||||
$DB_PASS = '-*-secrit-*-';
|
||||
$DB_HOST = 'localhost';
|
||||
|
||||
if (!$DB->connect($DB_NAME, $DB_HOST, $DB_USER, $DB_PASS)) {
|
||||
if (!DBH::$db->connect($DB_NAME, $DB_HOST, $DB_USER, $DB_PASS)) {
|
||||
echo "System is down for mantainence. Please try again later.";
|
||||
die();
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ $report = file_get_contents("php://input");
|
||||
if (strlen($report))
|
||||
{
|
||||
$query = kl_str_sql('insert into raw_reports(raw_data) values (!s)', $report);
|
||||
if ($res = $DB->query($query))
|
||||
if ($res = DBH::$db->query($query))
|
||||
{
|
||||
$report_id = $DB->insertID();
|
||||
$report_id = DBH::$db->insertID();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user