301 lines
9.8 KiB
PHP
301 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace BRSystems\Core\Library\cDbLog;
|
|
|
|
use BRSystems\Core\Library\cDb\cDb;
|
|
use BRSystems\Core\Library\cJSON\cJSON;
|
|
|
|
/**
|
|
* DB log Module
|
|
*
|
|
* @category DB log Module
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @since Version 2.0
|
|
*/
|
|
class cDbLogModule
|
|
{
|
|
public $cDb;
|
|
|
|
/**
|
|
* Class constructor
|
|
*/
|
|
public function __construct() {
|
|
|
|
$this->cDb = cDb::getInstance(false, false, false, false, false, 'DbLogger');
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get all tables from db table info to check & create missing
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @param array $tableNames - table names data
|
|
* @return array with data to check
|
|
* @version 1.1
|
|
*/
|
|
public function getTablesInfoByName($tableNames = array()){
|
|
|
|
if($tableNames && count($tableNames)){
|
|
$tableNames = implode("','", $tableNames);
|
|
|
|
$sSql = "
|
|
SELECT
|
|
table_name
|
|
FROM
|
|
information_schema.tables
|
|
WHERE
|
|
table_schema = '" . (defined('COMP_DB_NAME') ? COMP_DB_NAME : DB_NAME) . "'
|
|
AND table_name IN ('{$tableNames}')
|
|
ORDER BY
|
|
table_name
|
|
";
|
|
|
|
$this->cDb->query($sSql);
|
|
return $this->cDb->getKeyValBasedArray('table_name', 'table_name');
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Function to get all tables columns from db table info
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @param array $tableNames - table names data
|
|
* @return array with data to check
|
|
* @version 1.1
|
|
*/
|
|
public function getBaseTableColumnsByName($tableNames = array()){
|
|
|
|
if($tableNames && count($tableNames)){
|
|
$sBaseTableNames = implode("','", array_keys($tableNames));
|
|
|
|
|
|
$sSql = "
|
|
SELECT
|
|
table_name, column_name, column_key, extra
|
|
FROM
|
|
information_schema.columns
|
|
WHERE
|
|
table_schema = '" . (defined('COMP_DB_NAME') ? COMP_DB_NAME : DB_NAME) . "'
|
|
AND table_name IN ('{$sBaseTableNames}')
|
|
AND (column_key = 'PRI' OR LENGTH(extra))
|
|
ORDER BY
|
|
TABLE_NAME, ORDINAL_POSITION
|
|
";
|
|
|
|
$this->cDb->query($sSql);
|
|
return $this->cDb->getFieldBasedArrayFieldsAdv(array('table_name', 'column_name'), array('column_key', 'extra'));
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Function to copy db table for log reasons
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @param string $copyTable - table name to copy from
|
|
* @param string $newName - new table name to copy to
|
|
* @param array $vTableColumns - clone table columns from information_schema
|
|
* @return array with data to check
|
|
* @version 1.1
|
|
*/
|
|
public function createLogTableByName($copyTable, $newName, $vTableColumns){
|
|
|
|
if($copyTable && $newName){
|
|
|
|
$sSql = "CREATE TABLE {$newName} LIKE {$copyTable}";
|
|
$this->cDb->query($sSql);
|
|
|
|
// Loop each clone db table and reset primary & auto inc keys
|
|
if(!!$vTableColumns){
|
|
foreach($vTableColumns as $sKey=>$vData){
|
|
if($vData['extra'] == 'auto_increment')
|
|
$this->cDb->query("ALTER TABLE `{$newName}` MODIFY `{$sKey}` INT NOT NULL DEFAULT '0' FIRST");
|
|
if($vData['column_key'] == 'PRI')
|
|
$this->cDb->query("ALTER TABLE `{$newName}` DROP PRIMARY KEY ");
|
|
}
|
|
}
|
|
|
|
$this->cDb->query("ALTER TABLE `{$newName}` ADD `log_user_id` INT NOT NULL DEFAULT '0' FIRST");
|
|
$this->cDb->query("ALTER TABLE `{$newName}` ADD `log_date` INT NOT NULL DEFAULT '0' FIRST");
|
|
$this->cDb->query("ALTER TABLE `{$newName}` ADD `log_id` INT NOT NULL DEFAULT '0' FIRST");
|
|
$this->cDb->query("ALTER TABLE `{$newName}` ADD `pk` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST");
|
|
|
|
// Copy base table data
|
|
//$this->logData($newName, 0, $copyTable, false);
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Function to create company query log table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @return array with data to check
|
|
* @version 1.1
|
|
*/
|
|
public function createQueryLogTable(){
|
|
|
|
$sSql = "
|
|
CREATE TABLE IF NOT EXISTS `" . cDbLog::C_SQL_TABLE . "` (
|
|
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
|
`date` INT NOT NULL ,
|
|
`user_id` INT NOT NULL ,
|
|
`user_type` ENUM( '1', '2' ) NOT NULL DEFAULT '1' COMMENT '1-pub, 2-sys cms',
|
|
`op_type` VARCHAR( 30 ) NOT NULL DEFAULT '' COMMENT 'Operation type INSERT | UPDATE | SELECT | DELETE,LOGICAL_DELETE | LOGIN | ect.',
|
|
`affected_table` VARCHAR( 200 ) NOT NULL ,
|
|
`json_data` TEXT NOT NULL COMMENT 'Json additional data',
|
|
`s` FLOAT NOT NULL DEFAULT '0' COMMENT 'exec time s',
|
|
`executed_query` LONGTEXT NOT NULL
|
|
) ENGINE = MYISAM ;
|
|
";
|
|
|
|
return $this->cDb->query($sSql);
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to create company action log table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @return array with data to check
|
|
* @version 1.1
|
|
*/
|
|
public function createActionLogTable(){
|
|
|
|
$sSql = "
|
|
CREATE TABLE IF NOT EXISTS `" . cDbLog::C_ACTION_TABLE . "` (
|
|
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
|
`user_id` INT NOT NULL ,
|
|
`time` INT NOT NULL ,
|
|
`action` varchar(200) NOT NULL,
|
|
`value` varchar(4000) NOT NULL
|
|
) ENGINE = MYISAM ;
|
|
";
|
|
|
|
return $this->cDb->query($sSql);
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to insert log table record
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @param string $table - log table name
|
|
* @param array $data - data to insert
|
|
* @return int new inserted ID
|
|
* @version 1.1
|
|
*/
|
|
public function insLog($table, $data){
|
|
|
|
return $this->cDb->_queryInsert($table, $data, false, true);
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to insert log table records from base table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @param int $logId - log table log ID
|
|
* @param string $logTable - log table
|
|
* @param int $userId - user id to log
|
|
* @param string $table - log table name
|
|
* @param string $sWhere - copy condition for log table
|
|
* @internal param array $data - data to insert
|
|
* @return array with data to check
|
|
* @version 1.1
|
|
*/
|
|
public function logData($logId, $logTable, $userId, $table, $sWhere){
|
|
|
|
$date = time();
|
|
$sSql = "
|
|
INSERT INTO {$logTable}
|
|
SELECT 0, {$logId}, {$date}, {$userId}, tt.* FROM {$table} tt WHERE {$sWhere}
|
|
";
|
|
|
|
return $this->cDb->query($sSql);
|
|
}
|
|
|
|
/**
|
|
* Function to insert action into _log table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @param string $action - log action
|
|
* @param string|bool $data - data to insert
|
|
* @param int $userId - user id
|
|
* @param bool $pHTML - preserver html
|
|
* @return array with data to check
|
|
* @version 1.1
|
|
*/
|
|
public static function actionLog($action, $data = false, $userId = 0, $pHTML = false){
|
|
|
|
if($action){
|
|
$cDb = cDb::getInstance(false, false, false, false, false, 'DbLogger');
|
|
$data = $cDb->prepData($data);
|
|
|
|
$cDb->_queryInsert(cDbLog::C_ACTION_TABLE, array(
|
|
'time' => time(),
|
|
'user_id' => $userId,
|
|
'action' => $cDb->prepData($action),
|
|
'value' => (is_array($data) ? cJSON::encode($data) : $data)
|
|
), false, $pHTML);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Function to insert into BASE DB action into temp_action_log table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @param string $action - log action
|
|
* @param string|bool $data - data to insert
|
|
* @param int $userId - user id
|
|
* @param bool $pHTML - preserver html
|
|
* @return array with data to check
|
|
* @version 1.1
|
|
*/
|
|
public static function baseActionLog($action, $data = false, $userId = 0, $pHTML = false){
|
|
if($action){
|
|
$cDb = cDb::getInstance(DB_HOST, DB_USR, DB_PWD, DB_PORT, DB_NAME, 'DbLogger');
|
|
$data = $cDb->prepData($data);
|
|
|
|
$cDb->_queryInsert(cDbLog::C_BASE_ACTION_TABLE, array(
|
|
'time' => time(),
|
|
'user_id' => $userId,
|
|
'action' => $cDb->prepData($action),
|
|
'value' => (is_array($data) ? cJSON::encode($data) : $data)
|
|
), false, $pHTML);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Function to get log data by log id
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category DB log Module
|
|
* @param int $iID - log id
|
|
* @param string $sTableName - log table name
|
|
* @return array|bool - data from select
|
|
*/
|
|
public function getSingleLogDataById($iID, $sTableName){
|
|
|
|
if(!!$iID)
|
|
return $this->cDb->_querySelectRow($sTableName, false, array('log_id' => $iID));
|
|
return false;
|
|
|
|
}
|
|
}
|