/** * 记录操作数据库日志 * * @param string $sql */ public static function logDbOprate($sql) { //如果禁用日志则不记录 by terry at Wed Aug 18 10:02:05 CST 2010 if (self::$_disabledAddLog) { return true; } $sql = trim($sql); if (!$sql) { return false; } //生成消息序列 by jute $enableSync = Watt_Config::getCfgInFile('EnableSync', 'sync/sync.conf.php'); if ($enableSync == '1') { Watt_Sync_MessageListManage::createDbMsgList($sql); } $tableName = ''; $logName = ''; $msg = ''; //忽略列表优先于允许列表 $ignoreTalbenameList = array('tpm_rizhi' => 'rz_id', 'tpm_xiangmu_rizhi' => 'rz_id', 'tpm_ziyuan_rizhi' => 'rz_id', 'tpm_rizhi_fangwen' => 'rz_id'); $toLogTablenameList = array('tpm_dingdan' => 'dd_id', 'tpm_shengchandingdan' => 'sd_id', 'tpm_xiangmu' => 'xm_id', 'tpm_renwu' => 'rw_id', 'tpm_gaojian' => 'gj_id', 'tpm_yonghukuozhan' => 'yh_id', 'tpm_yonghu' => 'yh_id', 'tpm_kehu_yonghu' => 'yh_id'); //因为之前已经trim了,所以 === 0 if (stripos($sql, 'SELECT') === 0) { /** * debug模式同时记录select便于调试 * @author terry * @version 0.1.0 * Tue Oct 21 09:33:14 CST 2008 */ /* if( defined('DEBUG') && DEBUG ){ $logName = self::DB_OPRATE_TYPE_SELECT; $msg = $sql; }else{ */ //不记录Select return false; //$sql = str_replace( array("\n","\r") , array(" ", "" ) , $sql ); /* } */ } elseif (stripos($sql, 'UPDATE') === 0) { /* /update[\s]+(\w+)[\s]+set(.*)where[\s]+(.*)/ ^^table name ^^col ^^cond */ if (preg_match("/update[\\s]+(\\w+)[\\s]+set(.*)where[\\s]+(.*)/i", $sql, $matchs)) { $tableName = $matchs[1]; $cols = $matchs[2]; $cond = $matchs[3]; $logName = self::DB_OPRATE_TYPE_PREFIX . '-' . $tableName . '-' . self::DB_OPRATE_TYPE_UPDATE; $msg = $cols . self::DB_OPRATE_MSG_SPLITER . $cond; } } elseif (stripos($sql, 'INSERT') === 0) { /* /insert into[\s]+(\w+)[\s]+\((.*)\)[\s]+values[\s]+\((.*)\)/ */ if (preg_match('/insert into[\\s]+(\\w+)[\\s]+\\((.*)\\)[\\s]+values[\\s]+\\((.*)\\)/i', $sql, $matchs)) { $tableName = $matchs[1]; $cols = $matchs[2]; $values = $matchs[3]; $logName = self::DB_OPRATE_TYPE_PREFIX . '-' . $tableName . '-' . self::DB_OPRATE_TYPE_INSERT; $msg = $cols . self::DB_OPRATE_MSG_SPLITER . $values; } } elseif (stripos($sql, 'DELETE') === 0) { /* /insert into[\s]+(\w+)[\s]+\((.*)\)[\s]+values[\s]+\((.*)\)/ */ if (preg_match('/DELETE FROM[\\s]+(\\w+)[\\s]+WHERE(.*)/i', $sql, $matchs)) { $tableName = $matchs[1]; $cond = $matchs[2]; $logName = self::DB_OPRATE_TYPE_PREFIX . '-' . $tableName . '-' . self::DB_OPRATE_TYPE_DELETE; $msg = $cond; } } else { /** * 对未解析的SQL 也记录在案 * @author terry * @version 0.1.0 * Wed Sep 26 13:38:08 CST 2007 */ $logName = self::DB_OPRATE_TYPE_UNKNOWN; $msg = $sql; } //strtolower( $tableName ) != 'tpm_rizhi' if ($msg && $logName && !key_exists(strtolower($tableName), $ignoreTalbenameList) && key_exists(strtolower($tableName), $toLogTablenameList) && stripos($sql, 'UPDATE tpm_yonghu SET YH_ZAIXIANSHIJIAN') === false) { //self::addLog( "stripos".stripos( $sql, 'UPDATE tpm_yonghu SET YH_ZAIXIANSHIJIAN' ) ); /** * 更新用户状态的不记录在数据库,因为频率太高,太常规了 * @author terry * @version 0.1.0 * Sat Sep 29 19:41:27 CST 2007 */ // print"<pre>Terry :";var_dump( $logName );print"</pre>"; // print"<pre>Terry :";print ( $msg );print"</pre>"; // exit(); self::addLog("Execute [ {$sql} ]" . self::DB_OPRATE_MSG_SPLITER . $msg, self::LEVEL_SYSTEM, $logName); return true; } else { /** * 忽略列表中的数据表操作亦将不被写入文件Log * @author terry * @version 0.1.0 * Wed Sep 05 09:46:56 CST 2007 */ if (!key_exists(strtolower($tableName), $ignoreTalbenameList)) { self::addLog("Execute [ {$sql} ]", Watt_Log::LEVEL_DEBUG, $logName); } return false; } }