Merge branch 'master' of git://github.com/Froxlor/Froxlor

This commit is contained in:
BNoiZe
2013-10-30 17:23:37 +01:00

View File

@@ -38,18 +38,29 @@ class Database {
*/ */
private static $_link = null ; private static $_link = null ;
/**
* indicator whether to use root-connection or not
*/
private static $_needroot = false;
/**
* indicator which database-server we're on (not really used)
*/
private static $_dbserver = 0;
/** /**
* Wrapper for PDOStatement::execute so we can catch the PDOException * Wrapper for PDOStatement::execute so we can catch the PDOException
* and display the error nicely on the panel * and display the error nicely on the panel
* *
* @param PDOStatement $stmt * @param PDOStatement $stmt
* @param array $params (optional) * @param array $params (optional)
* @param bool $showerror suppress errordisplay (default true)
*/ */
public static function pexecute(&$stmt, $params = null) { public static function pexecute(&$stmt, $params = null, $showerror = true) {
try { try {
$stmt->execute($params); $stmt->execute($params);
} catch (PDOException $e) { } catch (PDOException $e) {
self::_showerror($e); self::_showerror($e, $showerror);
} }
} }
@@ -58,10 +69,36 @@ class Database {
* *
* @return int * @return int
*/ */
public static function num_rows($stmt) { public static function num_rows() {
return Database::query("SELECT FOUND_ROWS()")->fetchColumn(); return Database::query("SELECT FOUND_ROWS()")->fetchColumn();
} }
/**
* enabled the usage of a root-connection to the database
* Note: must be called *before* any prepare/query/etc.
* and should be called again with 'false'-parameter to resume
* the 'normal' database-connection
*
* @param bool $needroot
* @param int $dbserver optional
*/
public static function needRoot($needroot = false, $dbserver = 0) {
// force re-connecting to the db with corresponding user
// and set the $dbserver (mostly to 0 = default)
self::_setServer($dbserver);
self::$_needroot = $needroot;
}
/**
* set the database-server (relevant for root-connection)
*
* @param int $dbserver
*/
private static function _setServer($dbserver = 0) {
self::$_dbserver = $dbserver;
self::$_link = null;
}
/** /**
* let's us interact with the PDO-Object by using static * let's us interact with the PDO-Object by using static
* call like "Database::function()" * call like "Database::function()"
@@ -72,7 +109,7 @@ class Database {
* @return mixed * @return mixed
*/ */
public static function __callStatic($name, $args) { public static function __callStatic($name, $args) {
$callback = array(self::getDB(), $name); $callback = array(self::getDB(self::$_needroot), $name);
$result = null; $result = null;
try { try {
$result = call_user_func_array($callback, $args ); $result = call_user_func_array($callback, $args );
@@ -118,9 +155,9 @@ class Database {
// either root or unprivileged user // either root or unprivileged user
if ($root) { if ($root) {
$user = $sql_root[0]['user']; $user = $sql_root[self::$_dbserver]['user'];
$password = $sql_root[0]['password']; $password = $sql_root[self::$_dbserver]['password'];
$host = $sql_root[0]['host']; $host = $sql_root[self::$_dbserver]['host'];
} else { } else {
$user = $sql["user"]; $user = $sql["user"];
$password = $sql["password"]; $password = $sql["password"];
@@ -166,14 +203,15 @@ class Database {
* display a nice error if it occurs and log everything * display a nice error if it occurs and log everything
* *
* @param PDOException $error * @param PDOException $error
* @param bool $showerror if set to false, the error will be logged but we go on
*/ */
private static function _showerror($error) { private static function _showerror($error, $showerror = true) {
global $theme; global $theme;
/** /**
* log to a file, so we can actually ask people for the error * log to a file, so we can actually ask people for the error
* (no one seems to find the stuff in the syslog) * (no one seems to find the stuff in the syslog)
*/ */
$sl_dir = makeCorrectDir(FROXLOR_INSTALL_DIR."/logs/"); $sl_dir = makeCorrectDir(FROXLOR_INSTALL_DIR."/logs/");
if (!file_exists($sl_dir)) { if (!file_exists($sl_dir)) {
@mkdir($sl_dir, 0755); @mkdir($sl_dir, 0755);
@@ -183,15 +221,17 @@ class Database {
@fwrite($sqllog, date('d.m.Y H:i', time())." --- ".str_replace("\n", " ", $error->getMessage())."\n"); @fwrite($sqllog, date('d.m.Y H:i', time())." --- ".str_replace("\n", " ", $error->getMessage())."\n");
@fclose($sqllog); @fclose($sqllog);
if (!isset($_SERVER['SHELL']) || (isset($_SERVER['SHELL']) && $_SERVER['SHELL'] == '')) { if ($showerror) {
// if we're not on the shell, output a nicer error-message if (!isset($_SERVER['SHELL']) || (isset($_SERVER['SHELL']) && $_SERVER['SHELL'] == '')) {
$err_hint = file_get_contents(dirname($sl_dir).'/templates/'.$theme.'/misc/dberrornice.tpl'); // if we're not on the shell, output a nicer error-message
// replace values $err_hint = file_get_contents(dirname($sl_dir).'/templates/'.$theme.'/misc/dberrornice.tpl');
$err_hint = str_replace("<TEXT>", $error->getMessage(), $err_hint); // replace values
$err_hint = str_replace("<DEBUG>", $error->getTraceAsString(), $err_hint); $err_hint = str_replace("<TEXT>", $error->getMessage(), $err_hint);
// show $err_hint = str_replace("<DEBUG>", $error->getTraceAsString(), $err_hint);
die($err_hint); // show
die($err_hint);
}
die("We are sorry, but a MySQL - error occurred. The administrator may find more information in in the sql-error.log in the logs/ directory");
} }
die("We are sorry, but a MySQL - error occurred. The administrator may find more information in in the sql-error.log in the logs/ directory");
} }
} }