1
0
Fork 0

Struggeling with relocating

Dieser Commit ist enthalten in:
DSB 2011-06-10 21:28:27 +00:00
Commit 89ea01c429
301 geänderte Dateien mit 59926 neuen und 0 gelöschten Zeilen

195
inc/classes/Log.php Normale Datei
Datei anzeigen

@ -0,0 +1,195 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @version SVN: $Rev$
* @author $Author$
* @lastmodified $Date$
*/
/**
* Log Class
*/
class Log
{
// Define constants
const PHP = 1;
const PERL = 2;
const PERL_COMPLETE = 3;
const ERROR = 4;
const PHP_FILE = 'work/log/mysqldump.log';
const PERL_FILE = 'work/log/mysqldump_perl.log';
const PERL_COMPLETE_FILE = 'work/log/mysqldump_perl.complete.log';
const ERROR_FILE = 'work/log/error.log';
/**
* Init file handles
*
* @return void
*/
public function __construct()
{
$this->handle = array();
$this->handle[self::PHP] = false;
$this->handle[self::PERL] = false;
$this->handle[self::PERL_COMPLETE] = false;
$this->handle[self::ERROR] = false;
}
/**
* Close all open file handles on destruct
*
* @return void
*/
public function __destruct()
{
if ($this->handle[self::PHP]) {
$this->_close(self::PHP);
}
if (is_resource($this->handle[self::PERL])) {
$this->_close(self::PERL);
}
if (is_resource($this->handle[self::PERL_COMPLETE])) {
$this->_close(self::PERL_COMPLETE);
}
if (is_resource($this->handle[self::ERROR])) {
$this->_close(self::ERROR);
}
}
/**
* Open file and store file handle
*
* @param string $file
*
* @return bool
*/
private function _getFileHandle($file)
{
$filename=$this->getLogfile($file);
if ($_SESSION['config']['logcompression'] == 1) {
$fileHandle = @gzopen($filename, 'a');
} else {
$fileHandle = @fopen($filename, 'ab');
}
if ($fileHandle) {
$this->handle[$file] = $fileHandle;
return $this->handle[$file];
} else {
return false;
}
}
/**
* Close a filehandle
*
* @param Loge $file The file to close
*/
private function _close($file)
{
$filename=$this->getLogfile($file);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if ($extension == 'gz') {
gzclose($this->handle[$file]);
} else {
fclose($this->handle[$file]);
}
}
/**
* Write to log file
*
* @param string $file
* @param string $message
*
* @return bool
*/
public function write($file, $message)
{
// TODO get $config-values from a config class and delete global
global $config;
if (!$this->handle[$file]) {
$this->handle[$file] = $this->_getFileHandle($file);
}
$message = strip_tags($message);
// we don't need linebreaks in the log
$search = array("\n","\r");
$replace = array ('','');
$message = str_replace($search, $replace, $message);
$logMessage = date('d.m.Y H:i:s') . ' '. $message . "\n";
$_SESSION['log']['actions'][] = $logMessage;
$filename = $this->getLogfile($file);
if (@filesize($filename) > $config['log_maxsize']) {
Log::delete($file);
}
//save to log file
if ($_SESSION['config']['logcompression'] == 1) {
$res = @gzwrite($this->handle[$file], $logMessage);
} else {
$res = @fwrite($this->handle[$file], $logMessage);
}
return $res;
}
/**
* Get log file name
*
* @param const $file
*
* @return string Filename of logfile
*/
public function getLogfile($file)
{
switch ($file)
{
case Log::PHP:
$filename = self::PHP_FILE;
break;
case Log::PERL:
$filename = self::PERL_FILE;
break;
case Log::PERL_COMPLETE:
$filename = self::PERL_COMPLETE_FILE;
break;
case Log::ERROR:
$filename = self::ERROR_FILE;
break;
default:
echo "Unknown $file: ".$file;
return;
}
if ($_SESSION['config']['logcompression'] == 1) {
$filename .= '.gz';
}
return $filename;
}
/**
* Delete log file and recreates it.
*
* @param string $file Filename
*
* @return void
*/
public function delete($file)
{
$filename=Log::getLogfile($file);
@unlink($filename);
if ($file == Log::PHP) {
// re-create main log
if (substr($filename, -3) == '.gz') {
$log = date('d.m.Y H:i:s') . " Log created.\n";
if ($_SESSION['config']['logcompression'] == 1) {
$fp = @gzopen($filename, "wb");
@gzwrite($fp, $log);
@chmod($file . '.gz', 0777);
$this->handle[$file]=$fp;
} else {
$fp = @fopen($filename, "wb");
@fwrite($fp, $log);
@chmod($file, 0777);
$this->handle[$file]=$fp;
}
}
}
}
}

Datei anzeigen

@ -0,0 +1,249 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @version SVN: $rev: 1212 $
* @author $Author$
* @lastmodified $Date$
*/
/**
* DB-Factory
*
* @abstract
*/
abstract class MsdDbFactory
{
const FORCE_MYSQL = false; // for debugging
// define result set types
const ARRAY_NUMERIC = 0; // return resultset as numeric array
const ARRAY_ASSOC = 1; // return resultset as associative array
const ARRAY_OBJECT = 2; // return resultset as array of object
const SIMPLE = 3; // return result as boolean
/**
* Init database object
*
* @param string $server SQL-Server
* @param string $user SQL-User used to authenticate at server
* @param string $password SQL-User-Password usued to authenticate
* @param string $port [optional] Port to use
* @param string $socket [optional] Socket to use (only for *nix)
*
* @return void
*/
function __construct($server, $user, $password, $port = 3306, $socket ='')
{
$this->server = $server;
$this->user = $user;
$this->password = $password;
$this->port = $port;
$this->socket = $socket;
$this->connectionCharset = 'utf8';
$this->connectionHandle = false;
$this->dbSelected = '';
$this->tables = array();
$this->metaTables = array();
}
/**
* Create database adapter
*
* @param string $server
* @param string $user
* @param string $password
* @param string $port
* @param string $socket
*
* @return MsdDbFactory
*/
static function getAdapter($server, $user, $password, $port = 3306, $socket = '')
{
if (function_exists('mysqli_connect') && !self::FORCE_MYSQL) {
include_once ('./inc/classes/db/mysqli/MsdDbMysqli.php');
$db = new MsdDbMysqli($server, $user, $password, $port, $socket);
} else {
include_once ('./inc/classes/db/mysql/MsdDbMysql.php');
$db = new MsdDbMysql($server, $user, $password, $port, $socket);
}
return $db;
}
/**
* Establish a connection to SQL-Server.
*
* Doesn't need to be called directly because of lazy loading.
* Will be called automatically if any other method is called that
* needs a connection.
* Creates a connection to the database and stores the connection handle
* in $this->connection_handle.
* If $select_database is set, the database is selected for next queries.
* Returns true on success or false if connection couldn't be established.
*
* @param string $select_database
* @param string $connection_charset
*
* @return bool
* */
abstract public function dbConnect($selectDatabase = false);
/**
* Get selected database
*
* @return string
*/
abstract public function getSelectedDb();
/**
* Get version nr of sql server
*
* @return string
*/
abstract public function getServerInfo();
/**
* Get version nr of sql client
*
* @return string
*/
abstract public function getClientInfo();
/**
* Get all known character sets of this SQL-Server.
*
* @return array
*/
abstract public function getCharsets();
/**
* Set character set of the MySQL-connection.
*
* Trys to set the connection charset and returns it.
* Throw Exception on failure.
*
* @param string $charset
* @throws Exception
*
* @return string
*/
abstract public function setConnectionCharset($charset = 'utf8');
/**
* Get list of databases
*
* Gets list of all databases that the actual SQL-Server-User has access to
* and saves it in $this->databases.
* Returns true on success or false on error.
*
* @return bool
*/
abstract public function getDatabases();
/**
* Select the given database to use it as the target for following queries.
*
* Returns true if selection was succesfull otherwise false.
*
* @throws Exception
* @param string $database
*
* @return bool
*/
abstract public function selectDb($database);
/**
* Execute a SQL-Server-Query
*
* @param $query The query to execute
* @param $kind Type of result set
*
* @return array
*/
abstract public function query($query, $kind = self::ARRAY_OBJECT);
/**
* Get table list of given database(s)
*
* Stores them in $this->tables[database]
* When no database is given, all databases are scanned for tables.
* Returns table list for selected or for all databases.
*
* @param string|array $database
*
* @return array
*/
abstract public function getTables($database = false);
/**
* Gets extended table information for one or all tables
*
* @param string $table
* @param string $database
*
* @return array
*/
abstract public function getTableStatus($table = false, $database = false);
/**
* Returns the CREATE Statement of a table.
*
* @throws Exception
* @param string $table Get CREATE-Statement for this table
* @param string $database Database holding the table
*
* @return string Create statement
*/
abstract public function getTableCreate($table, $database = false);
/**
* Gets the full description of all columns of a table
*
* Saves list to $this->metaTables[$database][$table].
*
* @param string $table Table to read meta info from
* @param string $database Database holding the table
*
* @return array
*/
abstract public function getTableColumns($table, $database = false);
/**
* Gets the number of affected rows of the last query
*
* @return int
*/
abstract function getAffectedRows();
/**
* Escape a value for inserting it in query
*
* @param string $val
*
* @return string
*/
abstract function escape($val);
/**
* Optimize a table. Returns true on success or MySQL-Error.
*
* @param $table string Name of table
*
* @return string|bool Returned optimize message or false on error
*/
abstract function optimizeTable($table);
/**
* Handles a SQL-Error
*
* @param string $errmsg
* @param int $errno
* @throws MsdEception
*
* @return void
*/
public function sqlError($errmsg, $errno)
{
throw new Exception($errmsg, $errno);
}
}

Datei anzeigen

@ -0,0 +1,439 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage MsdDbFactory
* @version SVN: $rev: 1205 $
* @author $Author$
* @lastmodified $Date$
*/
/**
* Capsules all database related actions.
* Uses some magic getters to allow lazy connecting.
*
*/
class MsdDbMysql extends MsdDbFactory
{
/**
* Get all known character sets of this SQL-Server.
*
* @return array
*/
public function getCharsets()
{
if (isset($this->charsets)) return $this->charsets;
if (false === $this->connectionHandle) $this->dbConnect();
$result = $this->query('SHOW CHARACTER SET', self::ARRAY_ASSOC);
$this->charsets = array();
foreach ($result as $r) {
$this->charsets[$r['Charset']] = $r;
}
@ksort($this->charsets);
return $this->charsets;
}
/**
* Establish a connection to MySQL.
*
* Creates a connection to the database and stores the connection handle in
* $this->connectionHandle.
* If $select_database is set, the database is selected for further queries.
* Returns true on success or false if connection couldn't be established.
*
* @param string $select_database
* @param string $connectionCharset
*
* @return bool
**/
public function dbConnect($selectDatabase = false)
{
if (is_resource($this->connectionHandle)) return true;
$connectionString = $this->server . ':' . $this->port;
if ($this->socket > '') $connectionString .= ':' . $this->socket;
if (false === $this->connectionHandle) {
$this->connectionHandle = @mysql_connect(
$connectionString,
$this->user, $this->password
);
if (false === $this->connectionHandle) {
return '(' . mysql_errno() . ') ' . mysql_error();
}
}
$this->setConnectionCharset($this->connectionCharset);
if (false === $selectDatabase && $this->dbSelected > '') {
$selectDatabase = $this->dbSelected;
}
if ($selectDatabase) {
try
{
$this->selectDb($selectDatabase, $this->connectionHandle);
$this->dbSelected = $selectDatabase;
return true;
} catch (Exception $e) {
$this->sqlError(
mysql_error($this->connectionHandle),
mysql_errno($this->connectionHandle)
);
return false;
}
}
return true;
}
/**
* Get version nr of sql server
*
* @return string
*/
public function getServerInfo()
{
if (false === $this->connectionHandle) {
$this->dbConnect();
}
$version = mysql_get_server_info($this->connectionHandle);
return $version;
}
/**
* Get version nr of sql client
*
* @return string
*/
public function getClientInfo()
{
if (false === $this->connectionHandle) $this->dbConnect();
$version = mysql_get_client_info();
return $version;
}
/**
* Set character set of the MySQL-connection.
*
* Trys to set the connection charset and returns it.
* Throw Exception on failure.
*
* @param string $charset
* @throws Exception
*
* @return string
*/
public function setConnectionCharset($charset = 'utf8')
{
if (false === $this->connectionHandle) $this->dbConnect();
if (function_exists('mysql_set_charset')
&& @mysql_set_charset($charset, $this->connectionHandle)) {
$this->connectionCharset = $charset;
return $this->connectionCharset;
} else {
$this->query('SET NAMES \'' . $charset . '\'', self::SIMPLE);
$this->connectionCharset = $charset;
return $charset;
}
}
/**
* Get list of databases
*
* Gets list of all databases that the actual MySQL-User has access to
* and saves it in $this->databases.
* Returns true on success or false on error.
*
* @return array List of found database names
*/
public function getDatabases()
{
if (false === $this->connectionHandle) $this->dbConnect();
$this->databases = array();
$databases = @mysql_list_dbs($this->connectionHandle);
if (is_resource($databases)) {
WHILE ($row = mysql_fetch_array($databases, MYSQL_ASSOC)) {
$this->databases[] = $row['Database'];
}
} else {
//mysql_list_dbs seems not to be allowed for this user
// try to get list via "SHOW DATABASES"
$res = $this->query('SHOW DATABASES', self::ARRAY_ASSOC);
foreach ($res as $r) {
$this->databases[] = $r['Database'];
}
}
sort($this->databases);
return $this->databases;
}
/**
* Select the given database to use it as the target for following queries
*
* Returns true if selection was succesfull otherwise false.
*
* @throws Exception
* @param string $database
*
* @return bool
*/
public function selectDb($database)
{
if (!is_resource($this->connectionHandle)) {
$this->dbConnect();
}
$res=@mysql_select_db($database, $this->connectionHandle);
if ($res===false) {
return mysql_error();
} else {
$this->dbSelected = $database;
return true;
}
}
/**
* Get selected database
*
* @see inc/classes/db/MsdDbFactory#getSelectedDb()
*
* @return string
*/
public function getSelectedDb()
{
return $this->dbSelected;
}
/**
* Execute a query
*
* @param $query The query to execute
* @param $kind Type of result set
*
* @return array
*/
public function query($query, $kind = self::ARRAY_OBJECT)
{
if (false === $this->connectionHandle) {
$this->dbConnect();
}
$res = @mysql_query($query, $this->connectionHandle);
if (false === $res) {
$this->sqlError(
mysql_error($this->connectionHandle),
mysql_errno($this->connectionHandle)
);
}
if ($kind === self::SIMPLE || is_bool($res)) {
return $res;
}
$ret = array();
if ($kind === self::ARRAY_OBJECT) {
WHILE ($row = mysql_fetch_object($res)) {
$ret[] = $row;
}
} elseif ($kind === self::ARRAY_NUMERIC) {
WHILE ($row = mysql_fetch_array($res, MYSQL_NUM)) {
$ret[] = $row;
}
} elseif ($kind === self::ARRAY_ASSOC) {
WHILE ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
$ret[] = $row;
}
}
return $ret;
}
/**
* Get table list of given database(s)
*
* Stores them in $this->tables[database]
* When no database is given, all databases are scanned for tables.
* Returns table list for selected or for all databases.
*
* @param string|array $database
*
* @return array
*/
public function getTables($database = false)
{
if (false === $this->connectionHandle) $this->dbConnect();
if (!isset($this->tables)) $this->tables = array();
if ($database !== false) {
//list tables of selected database
if (is_array($database)) {
$databases = $database;
} else {
$databases = array();
$databases[0] = $database;
}
} else {
//list tables for all databases
$this->getDatabases();
$databases = $this->databases;
}
// get tablenames inside each database
foreach ($databases as $db) {
$this->tables[$db] = array();
$sql = 'SHOW TABLES FROM `' . $db . '`';
$res = $this->query($sql, self::ARRAY_NUMERIC);
foreach ($res as $val) {
$this->tables[$db][] = $val[0];
}
}
return is_string($database) ? $this->tables[$database] : $this->tables;
}
/**
* Gets extended table information for one or all tables
*
* @param string $table
* @param string $database
*
* @return array
*/
public function getTableStatus($table = false, $database = false)
{
if ($database !== false && $database != $this->dbSelected) {
$this->selectDb($database);
} elseif (!$this->_mysqli instanceof mysqli) {
$this->selectDb($this->dbSelected);
}
if (!isset($this->tableinfo)) {
$this->tableinfo = array();
}
if (!isset($this->tableinfo[$this->dbSelected])) {
$this->tableinfo[$this->dbSelected] = array();
}
$sql = 'SHOW TABLE STATUS';
if ($table !== false) {
$sql .= ' LIKE \'' . $table . '\'';
}
$res = $this->query($sql, self::ARRAY_ASSOC);
if (is_array($res)) {
foreach ($res as $r) {
$tablename = $r['Name'];
unset($r['Name']);
$this->tableinfo[$this->dbSelected][$tablename] = $r;
}
} elseif ($res === false) {
$this->sqlError(
mysql_error($this->connectionHandle),
mysql_errno($this->connectionHandle)
);
}
if ($table !== false) {
if (isset($this->tableinfo[$this->dbSelected][$table])) {
return $this->tableinfo[$this->dbSelected][$table];
}
}
return $this->tableinfo[$this->dbSelected];
}
/**
* Returns the CREATE-Statement for the given table or false on error.
*
* @throws Exception
* @param string $table
* @param string $database
*
* @return string
*/
public function getTableCreate($table, $database = false)
{
if (false === $database) $database = $this->dbSelected;
if ($database != $this->dbSelected) {
if (false === $this->selectDB($database)) {
$this->sqlError(
mysql_error($this->connectionHandle),
mysql_errno($this->connectionHandle)
);
}
}
if (!is_resource($this->connectionHandle)) {
$this->dbConnect($this->dbSelected);
}
$sql = 'SHOW CREATE TABLE `' . $table . '`';
$res = $this->query($sql, self::ARRAY_ASSOC);
if (isset($res[0]['Create Table'])) {
return $res[0]['Create Table'];
} else {
$this->sqlError(
mysql_error($this->connectionHandle),
mysql_errno($this->connectionHandle)
);
}
}
/**
* Gets the full description of all columns of a table
*
* Saves it to $this->metaTables[$database][$table].
*
* @param string $table
* @param string $database
* @return array
*/
public function getTableColumns($table, $database = false)
{
if (false === $database) $database = $this->dbSelected;
if ($database != $this->dbSelected) {
if (false === $this->selectDB($database)) {
$this->sqlError(
mysql_error($this->connectionHandle),
mysql_errno($this->connectionHandle)
);
}
}
if (!is_resource($this->connectionHandle)) {
$this->dbConnect($this->dbSelected);
}
$sql = 'SHOW FULL COLUMNS FROM `' . $table . '`';
$res = $this->query($sql, self::ARRAY_ASSOC);
if (!isset($this->metaTables[$database])) {
$this->metaTables[$database] = array();
}
if ($res) {
$this->metaTables[$database][$table] = array();
foreach ($res as $r) {
$this->metaTables[$database][$table][$r['Field']] = $r;
}
}
return $this->metaTables[$database][$table];
}
/**
* Gets the number of affected rows for the last query
*
* @see inc/classes/db/MsdDbFactory#affectedRows()
*
* @return int
*/
public function getAffectedRows()
{
return mysql_affected_rows($this->connectionHandle);
}
/**
* Escape a value to use it in a query
*
* @see inc/classes/db/MsdDbFactory#escape($val)
* @param mixed $val The value to escape
* @return mixed
*/
public function escape($val)
{
return mysql_real_escape_string($val, $this->connectionHandle);
}
/**
* Optimize a table. Returns true on success or MySQL-Error.
*
* @param string $table Name of table
*
* @return string|bool Returned optimize message or false on error
*/
function optimizeTable($table)
{
$sql = 'OPTIMIZE TABLE `' . $this->dbSelected . '`.`' . $table . '`';
$res = $this->query($sql, MsdDbFactory::ARRAY_ASSOC);
if (isset($res[0]['Msg_text'])) {
return $res[0];
} else {
return false;
}
}
}

Datei anzeigen

@ -0,0 +1,413 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage MsdDbFactory
* @version SVN: $rev: 1208 $
* @author $Author$
* @lastmodified $Date$
*/
/**
* Capsules all database related actions.
* Uses some magic getters to allow lazy connecting.
*
*/
class MsdDbMysqli extends MsdDbFactory
{
/**
* @var resource
*/
private $_mysqli = null;
/**
* Get known charsets of MySQL-Server
*
* @return array
*/
public function getCharsets()
{
if (isset($this->charsets)) return $this->charsets;
if (!$this->_mysqli instanceof mysqli) $this->dbConnect();
$result = $this->query('SHOW CHARACTER SET', self::ARRAY_ASSOC);
$this->charsets = array();
foreach ($result as $r) {
$this->charsets[$r['Charset']] = $r;
}
@ksort($this->charsets);
return $this->charsets;
}
/**
* Establish a connection to MySQL.
*
* Create a connection to MySQL and store the connection handle in
* $this->connectionHandle.
* If $select_database is set, the database is selected for further queries.
* Returns true on success or false if connection couldn't be established.
*
* @param string $select_database
* @param string $connectionCharset
* @return boolean
**/
public function dbConnect($selectDatabase = false)
{
if ($this->_mysqli instanceof mysqli) {
return true;
}
$this->_mysqli = @new mysqli(
$this->server,
$this->user,
$this->password,
$this->dbSelected,
$this->port,
$this->socket
);
if ($this->_mysqli->connect_errno) {
$error = $this->_mysqli->connect_error;
$errno = $this->_mysqli->connect_errno;
$this->_mysqli = null;
return '(' . $errno . ') ' . $error;
}
$this->setConnectionCharset($this->connectionCharset);
if (false === $selectDatabase && $this->dbSelected > '') {
$selectDatabase = $this->dbSelected;
}
if ($selectDatabase) {
if ($this->selectDb($selectDatabase)) {
$this->dbSelected = $selectDatabase;
return true;
} else {
$this->sqlError(mysqli_error(mysqli_error(), mysqli_errno()));
}
}
return true;
}
/**
* Get version nr of sql server
*
* @return string
*/
public function getServerInfo()
{
if (!$this->_mysqli instanceof mysqli) {
$this->dbConnect();
}
return $this->_mysqli->server_info;
}
/**
* Get version nr of sql client
*
* @return string
*/
public function getClientInfo()
{
if (!$this->_mysqli instanceof mysqli) $this->dbConnect();
return $this->_mysqli->client_info;
}
/**
* Set character set of the MySQL-connection.
*
* Trys to set the connection charset and returns it.
* Throw Exception on failure.
*
* @param string $charset
* @throws Exception
*
* @return string
*/
public function setConnectionCharset($charset = 'utf8')
{
if (!$this->_mysqli instanceof mysqli) {
$this->dbConnect();
}
if (!@$this->_mysqli->set_charset($charset)) {
$this->sqlError(
$charset . ' ' . $this->_mysqli->error,
$this->_mysqli->errno
);
}
$this->connectionCharset = $this->_mysqli->character_set_name();
return $this->connectionCharset;
}
/**
* Get list of databases
*
* Gets list of all databases that the actual MySQL-User has access to
* and saves it in $this->databases.
* Returns true on success or false on error.
*
* @return boolean
*/
public function getDatabases()
{
if (!$this->_mysqli instanceof mysqli) $this->dbConnect();
$res = $this->query('SHOW DATABASES', self::ARRAY_ASSOC);
$this->databases = array();
foreach ($res as $r) {
$this->databases[] = $r['Database'];
}
sort($this->databases);
return $this->databases;
}
/**
* Select the given database to use it as the target for following queries.
*
* Returns true if selection was succesfull otherwise false.
*
* @throws Exception
* @param string $database Database to select
*
* @return boolean
*/
public function selectDb($database)
{
if (!$this->_mysqli instanceof mysqli) {
$this->dbConnect();
}
$res=@$this->_mysqli->select_db($database);
if ($res===false) {
return $this->_mysqli->error;
} else {
$this->dbSelected = $database;
return true;
}
}
/**
* Get selected database
*
* @see inc/classes/db/MsdDbFactory#getSelectedDb()
* @return string
*/
public function getSelectedDb()
{
return $this->dbSelected;
}
/**
* Execute a MySQL-Query
*
* @throws Exception
* @param string $query The query to execute
* @param const $kind Type of result set
*
* @return array
*/
public function query($query, $kind = self::ARRAY_OBJECT)
{
if (!$this->_mysqli instanceof mysqli) {
$this->dbConnect($this->dbSelected);
}
//echo "<br>Mysqli: executing query: " . $query;
$result = $this->_mysqli->query($query);
if (false === $result) {
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
}
if (!$result instanceof mysqli_result || $kind === self::SIMPLE) {
return $result;
}
$ret = array();
if ($kind === self::ARRAY_OBJECT) {
WHILE ($row = $result->fetch_object()) {
$ret[] = $row;
}
} elseif ($kind === self::ARRAY_NUMERIC) {
WHILE ($row = $result->fetch_array(MYSQLI_NUM)) {
$ret[] = $row;
}
} elseif ($kind === self::ARRAY_ASSOC) {
WHILE ($row = $result->fetch_assoc()) {
$ret[] = $row;
}
}
if ($result instanceof mysqli) {
$result->close();
}
return $ret;
}
/**
* Get table list of given database(s)
*
* Stores them in $this->tables[database]
* When no database is given, all databases are scanned for tables.
* Returns table list for selected or for all databases.
*
* @param string|array $database
* @return array
*/
public function getTables($database = false)
{
if (!$this->_mysqli instanceof mysqli) {
$this->dbConnect($this->dbSelected);
}
if (!isset($this->tables)) {
$this->tables = array();
}
if ($database !== false) {
//only list tables of selected database
if (is_array($database)) {
$databases = $database;
} else {
$databases = array();
$databases[0] = $database;
}
} else {
//list tables for all databases
$databases = $this->databases;
}
// get tablenames for each database
foreach ($databases as $db) {
$this->tables[$db] = array();
$sql = 'SHOW TABLES FROM `' . $db . '`';
$res = $this->query($sql, self::ARRAY_NUMERIC);
foreach ($res as $val) {
$this->tables[$db][] = $val[0];
}
}
return is_string($database) ? $this->tables[$database] : $this->tables;
}
/**
* Gets extended table information for one or all tables
*
* @param string $table
* @param string $database
* @return array
*/
public function getTableStatus($table = false, $database = false)
{
if ($database !== false && $database != $this->dbSelected) {
$this->selectDb($database);
}
if (!$this->_mysqli instanceof mysqli) {
$this->selectDb($this->dbSelected);
}
if (!isset($this->tableinfo)) {
$this->tableinfo = array();
}
if (!isset($this->tableinfo[$this->dbSelected])) {
$this->tableinfo[$this->dbSelected] = array();
}
$sql = 'SHOW TABLE STATUS';
if ($table !== false) {
$sql .= ' LIKE \'' . $table . '\'';
}
$res = $this->query($sql, self::ARRAY_ASSOC);
if (is_array($res)) {
foreach ($res as $r) {
$tablename = $r['Name'];
unset($r['Name']);
$this->tableinfo[$this->dbSelected][$tablename] = $r;
}
} elseif ($res === false) {
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
}
if ($table !== false) {
if (isset($this->tableinfo[$this->dbSelected][$table])) {
return $this->tableinfo[$this->dbSelected][$table];
}
}
return $this->tableinfo[$this->dbSelected];
}
/**
* Returns the CREATE Statement of a table.
*
* @throws Exception
* @param string $table
* @param string $database
*
* @return string
*/
public function getTableCreate($table, $database = false)
{
if (false === $database) {
$database = $this->dbSelected;
}
if ($database != $this->dbSelected) {
if (false === $this->selectDB($database)) {
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
}
}
if (!$this->_mysqli instanceof mysqli) {
$this->dbConnect($this->dbSelected);
}
$sql = 'SHOW CREATE TABLE `' . $table . '`';
$res = $this->query($sql, self::ARRAY_ASSOC);
if (isset($res[0]['Create Table'])) {
return $res[0]['Create Table'];
} else {
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
}
}
/**
* Gets the full description of all columns of a table
*
* Saves it to $this->metaTables[$database][$table].
*
* @param string $table
* @param string $database
*
* @return array
*/
public function getTableColumns($table, $database = false)
{
if (!$this->_mysqli instanceof mysqli) {
$this->dbConnect($this->dbSelected);
}
if (false === $database) {
$database = $this->dbSelected;
}
if ($database != $this->dbSelected) {
if (false === $this->selectDB($database)) {
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
}
}
$sql = 'SHOW FULL FIELDS FROM `' . $table . '`';
$res = $this->query($sql, self::ARRAY_ASSOC);
if (!isset($this->metaTables[$database])) $this->metaTables[$database] = array();
if (is_array($res)) {
$this->metaTables[$database][$table] = array();
foreach ($res as $r) {
$this->metaTables[$database][$table][$r['Field']] = $r;
}
}
return $this->metaTables[$database][$table];
}
/**
* Gets the number of affected rows for the last query
*
* @see inc/classes/db/MsdDbFactory#affectedRows()
* @return integer
*/
public function getAffectedRows()
{
return $this->_mysqli->affected_rows;
}
/**
* Escape a value to use it in a query
*
* @see inc/classes/db/MsdDbFactory#escape($val)
* @param mixed $val The value to escape
*
* @return mixed
*/
public function escape($val)
{
return $this->_mysqli->real_escape_string($val);
}
/**
* Optimize a table. Returns true on success or MySQL-Error.
*
* @param string $table Name of table
*
* @return boolean or string true on success or mysql_error() on failure
*/
function optimizeTable($table)
{
$sql = 'OPTIMIZE TABLE `' . $this->dbSelected . '`.`' . $table . '`';
$res = $this->query($sql, MsdDbFactory::ARRAY_ASSOC);
if (isset($res[0]['Msg_text'])) {
return $res[0];
} else {
return false;
}
}
}

51
inc/classes/helper/File.php Normale Datei
Datei anzeigen

@ -0,0 +1,51 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage Helper
* @version SVN: $rev: 1207 $
* @author $Author$
* @lastmodified $Date$
*/
/**
* File-Helper Class
*
* Class offers some methods for file handling
*/
class File
{
/**
* Get CHMOD of a file
*
* @param string $file The file to get chmod for
*
* @return int
*/
public static function getChmod($file)
{
clearstatcache();
return @substr(decoct(fileperms($file)), -3);
}
/**
* Detects if file or directory is writable and trys to chmod it.
*
* Returns if file or directory is writable after chmodding.
*
* @param string $path
* @param string $chmod
*
* @return bool
*/
public static function isWritable($path, $chmod)
{
if (!is_writable($path)) {
@chmod($path, $chmod);
}
return is_writable($path);
}
}

218
inc/classes/helper/Html.php Normale Datei
Datei anzeigen

@ -0,0 +1,218 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @version SVN: $rev: 1207 $
* @author $Author$
* @lastmodified $Date$
*/
/**
* HTML-Helper Class
*
* Class has some static methods for building HTML-output
*/
class Html
{
/**
* Build HTML option list from array.
*
* @param array $arr Array $array[key]=value
* @param string $selected Selected key
* @param string $valuePattern Pattern to format value output
*
* @return string HTML option list
*/
public static function getOptionlist($arr, $selected, $valuePattern = false)
{
$r = '';
foreach ($arr as $key => $val) {
$r .= '<option value="' . $key . '"';
$r .= Html::getSelected($key, $selected) . '>';
if ($valuePattern) $r .= sprintf($valuePattern, $val);
else $r .= $val;
$r .= '</option>' . "\n";
}
return $r;
}
/**
* Get HTML output for attribute "checked"
*
* @param string $val The current value
* @param string $checked The value for the element if it should be checked
* @return string Html attribute "checked" or empty string
*/
public static function getChecked($val, $checked)
{
return $val == $checked ? ' checked="checked"' : '';
}
/**
* Get HTML output for attribute "disable"
*
* @param string $val The current value
* @param string $disabled The value for the element if disabled
*
* @return string Html attribute "disabled" or empty string
*/
public static function getDisabled($val, $disabled)
{
return $val == $disabled ? ' disabled="disabled"' : '';
}
/**
* Get HTML output for attribute "selected"
*
* @param string $val The current value
* @param string $selected The value for the element if selected
*
* @return string Html attribute "selected" or empty string
*/
public static function getSelected($val, $selected)
{
return $val == $selected ? ' selected="selected"' : '';
}
/**
* Convert HTML br's to new lines.
*
* @param string $str The string to convert
* @return string Converted string
*/
public static function br2nl($str)
{
$search = array('<br>', '<br />', '<br/>');
$replace = array("\n", "\n", "\n");
return str_replace($search, $replace, $str);
}
/**
* Escape quotes and/or slashes and double quotes.
*
* Used for escaping strings in JS-alerts and config-files.
*
* @param string $string String to escape
* @param boolean $escapeSlashes Escape slashes and double quotes
*
* @return string Escaped string
*/
public static function getJsQuote($string, $escapeSlashes = false)
{
if ($escapeSlashes) {
$string = str_replace('/', '\/', $string);
$string = str_replace('"', '\"', $string);
}
$string = str_replace("\n", '\n', $string);
return str_replace("'", '\\\'', $string);
}
/**
* Replace quotes with their Html entity.
*
* Used for outputing values in HTML attributes without breaking HTML
*
* @param string $string String with quotes
*
* @return string String with replaced quotes
*/
public static function replaceQuotes($string)
{
$string = str_replace("\n", '\n', $string);
return str_replace('"', '&quot;', $string);
}
/**
* Escape special chars according to Perl syntax.
*
* @param string $text The string to escape
*
* @return string Escaped string
*/
public static function escapeSpecialchars($string)
{
$search = array('@', '$', '\\\\', '"');
$replace = array('\@', '\$', '\\', '\"');
return str_replace($search, $replace, $string);
}
/**
* Remove added slashes recursively.
*
* @param mixed $values Array or string to remove added slashes from
*
* @return mixed Array or String with recursively removed slashes
*/
public static function stripslashesDeep($values)
{
if (is_array($values)) {
$values = array_map('Html::stripslashesDeep', $values);
} else {
$values = stripslashes($values);
}
return $values;
}
/**
* Remove whitespaces before and after string or array.
*
* @param mixed $values Array or string to remove whitespaces from
*
* @return mixed Recursively trimmed array or string
*/
public static function trimDeep($values)
{
if (is_array($values)) {
$values = array_map('Html::trimDeep', $values);
} else {
$values = trim($values);
}
return $values;
}
/**
* Build HTML-Message-String for success messages
*
* @param string $text Message to print
*
* @return string Message surrounded by HTML-Container
*/
public static function getOkMsg($text)
{
$html= sprintf('<span class="ok">%s</span>', $text);
$html .= '<span style="float:right">';
$html .= '<img src="./css/msd/icons/ok.gif" title="" alt="">';
$html .= '</span><br />';
return $html;
}
/**
* Build HTML-Message-String for error messages
*
* @param string $text Message to print
*
* @return string Message surrounded by HTML-Container
*/
public static function getErrorMsg($text)
{
$html= sprintf('<span class="error">%s</span>', $text);
$html .= '<span style="float:right">';
$html .= '<img src="./css/msd/icons/notok.gif" title="" alt="">';
$html .= '</span><br />';
return $html;
}
/**
* Create IMG-Tag
*
* @param string $pic Filename of picture
* @param string $title Title for the picture (will alsobe used for alt-tag)
*
* @return string Built img-tag
*/
public static function getIcon($pic, $title = '')
{
//TODO get value from a config class and get rid of global
global $config;
$img = '<img src="%s%s" alt="%s" title="%s" />';
return sprintf($img, $config['files']['iconpath'], $pic, $title, $title);
}
}

87
inc/classes/helper/Sql.php Normale Datei
Datei anzeigen

@ -0,0 +1,87 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage Helper
* @version SVN: $rev: 1207 $
* @author $Author$
* @lastmodified $Date$
*/
/**
* Sql-Helper Class
*
* Class offers some methods to wrap some common Sql-commands
*/
class Sql
{
/**
* Optimize a table and write returned message to log file.
*
* Returns true on success or MySQL-Error.
*
* @param MsdDbFactory $dbo Database object
* @param string $table Name of table
*
* @return bool
*/
public static function optimizeTable(MsdDbFactory $dbo, $table)
{
global $lang, $log;
$res = $dbo->optimizeTable($table);
if (false !== $res) {
$success=array('status', 'info','warning','note');
if (in_array($res['Msg_type'], $success)) {
$logMsg = $lang['L_OPTIMIZE'].' `'.$dbo->dbSelected. '`.`';
$logMsg .= $table.'`: '.$res['Msg_text'];
$log->write(Log::PHP, $logMsg);
return true;
} else {
$logMsg = sprintf($lang['L_OPTIMIZE_TABLE_ERR'], $table);
writeToErrorLog($dbo->dbSelected, $logMsg, $res['msg_text'], 0);
return false;
}
} else {
$logMsg = sprintf($lang['L_OPTIMIZE_TABLE_ERR'], $table);
writeToErrorLog($dbo->dbSelecte, $logMsg, $res['msg_text'], 0);
return false;
}
}
/**
* Creates a INSERT INTO-string
*
* "INSERT INTO (`field1`,`field2`,..)"-Command for the given table
* by looking up the meta-information. Table must exists.
* Note: Only used when restoring a backup not from MySQLDumper containing
* extended inserts.
*
* @param MsdDbFactory Database object
* @param string $table Name of table to analyze
*
* @return string $insert The name of the table extracted from the Query
**/
public static function getInsertSyntax(MsdDbFactory $dbo, $table)
{
$insert = '';
$columns=$dbo->getTableColumns($table);
$fields=array_keys($columns);
$insert = 'INSERT INTO `' . $table . '` (`';
$insert .=implode('`,`', $fields) . '`)';
return $insert;
}
/**
* Disables keys for given table
*
* @param MsdDbFactory $dbo Database object
* @param string $table Name of table
*
* @return bool
*/
public static function disableKeys(MsdDbFactory $dbo, $table)
{
$query = '/*!40000 ALTER TABLE `' . $table . '` DISABLE KEYS */';
return $dbo->query($query, MsdDbFactory::SIMPLE);
}
}

Datei anzeigen

@ -0,0 +1,37 @@
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @version SVN: $rev: $
* @author $Author$
* @lastmodified $Date$
*/
/**
* String-Helper Class
*
* Class has some static methods to modify String-output
*/
class String
{
/**
* Format the given number to better readable number
*
* @param float|int $number Number to format
* @param int $precision Selected format precision
*
* @return string Formatted number
*/
public static function formatNumber($number, $precision = 0)
{
$formattedNumber = number_format(
(float) $number,
(int) $precision,
',',
'.'
);
return $formattedNumber;
}
}