/** * 记录操作数据库日志 * * @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; } }
public function ready3() { while (true) { $msg = socket_accept($this->sock); // 接受一个SOCKET if (!$msg) { echo "socket_accept() failed:" . socket_strerror($msg) . "\n"; break; } $dd = parent::read(); socket_write($msg, $dd, strlen($dd)); $dd = trim(socket_read($msg, 1024)); ob_start(); print "<pre>\r\n"; print_r($dd); print "</pre>\r\n"; $fp = fopen('zzzzzzzzzzzzzzzzzzzzzzzz.txt', 'wb'); fwrite($fp, ob_get_contents()); fclose($fp); ob_end_clean(); $dd = unserialize($dd); $result = Watt_Sync_MessageListManage::createAllMsgList($dd, 'outside'); if ($result == '1') { $writer = '1'; } else { $writer = '0'; } socket_write($msg, $writer, strlen($writer)); socket_close($msg); $command = strtoupper("quit"); if ($command == "QUIT") { break; } } parent::close(); }
/** * socket输出接口 * 将内部数据同步到外部服务器 */ public static function soutput() { echo microtime() . "<br>"; $loger = new Watt_Log_File('syncsocket'); //访问权限判断 if (!Watt_Sync_MessageListManage::interfaceAuth('InitiativeOutput')) { $loger->log('没有访问socket输出接口的权限'); exit; } //判断是否锁定 if (Watt_Sync_MessageListManage::isLock('soutput')) { //如果锁定 $loger->log('socket输出接口已锁定'); exit; } //锁定接口 Watt_Sync_MessageListManage::AddLock('soutput'); //------------外部unknown中的数据处理 start---------------// //------------外部unknown中的数据处理 end---------------// //------------外部消息序列的处理 start---------------// //获取所有的消息序列 $msgListArray = Watt_Sync_MessageListManage::getAllMessageList(); $msgList_execute = array(); $num_del = Watt_Sync_MessageListManage::getNumOnceDel(); //一次删除消息序列的个数 if (is_array($msgListArray) && count($msgListArray)) { foreach ($msgListArray as $key => $val) { //--------------------- 数据过滤器 start---------------------// //根据创建消息序列的服务器类型获取过滤器 $filterNameList = Watt_Sync_MessageListManage::getMsgListFilter($val['syncServerType']); //调用过滤器过滤数据 if (is_array($filterNameList) && count($filterNameList)) { foreach ($filterNameList as $filterName) { $aFilter = Watt_Sync_Filter::filterFactory($filterName); if ($aFilter) { $val = $aFilter->filter($val); } } } //如果字段为空,删除消息序列 if (($val['operate'] == 'UPDATE' || $val['operate'] == 'INSERT') && !count($val['cols']) || !count($val)) { //删除序列 Watt_Sync_MessageListManage::delMessageList($key); continue; } //--------------------- 数据过滤器 end---------------------// if (count($msgList_execute) < $num_del - 1) { $msgList_execute[$key] = $val; } else { //满足个数 $msgList_execute[$key] = $val; $msgSignArr = array_keys($msgList_execute); $data_str = ''; $data = array(); $data = array('msgList' => $msgList_execute, 'msgListDir' => 'unknown', 'validatePassword' => Watt_Sync_MessageListManage::getValidatePassword()); $data_str = urlencode(serialize($data)); //汇总文件内容 $msglistfilesize = strlen($data_str); //汇总文件大小 //生成本地消息序列汇总文件 $re = Watt_Sync_MessageListManage::createdMsgListFileSync($data_str); if ($re != '1') { $loger->log('socket输出接口创建内部消息序列汇总文件失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } $dd_chunk = str_split($data_str, 1000); $count_arr = count($dd_chunk); //总块数 foreach ($dd_chunk as $key => $val) { //循环传输数据 $order_str = "send_" . ($key + 1) . "_" . $count_arr; $count = strlen($order_str); $order_str = str_split($order_str, 1); $temp_arr = array_fill($count, 20 - $count, ' '); $order_str_arr = array_merge($order_str, $temp_arr); $order_str = ''; foreach ($order_str_arr as $k => $v) { $order_str .= $v; } $data = $order_str . $val; $result = Watt_Sync_SyncSocket::syncInterface('send', $data); if ($result != '1') { $loger->log('socket输出接口传送消息序列汇总文件失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } } //获取外部消息序列汇总文件的大小,比较内外汇总文件 //删除内部消息序列汇总文件 $result = Watt_Sync_MessageListManage::delFile('', 'msgListfile.msg'); if ($result != '1') { $loger->log('socket输入接口删除本地sync目录消息序列汇总文件失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } //解析外部消息序列汇总文件成消息序列 $msgsignstr = Watt_Sync_SyncSocket::syncInterface('execmsgfile', ''); if ($result == '0') { $loger->log('socket输出接口解析外部消息序列汇总文件失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } //删除内部消息序列 $msgsign = unserialize(urldecode($msgsignstr)); if (is_array($msgsign) && count($msgsign)) { $re = Watt_Sync_MessageListManage::delMessageList($msgsign); if ($re != '1') { $loger->log('socket输出接口删除内部消息序列失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } } else { $loger->log('socket输出接口内部消息序列数据错误'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } //移动外部消息序列到outsite目录 $result = Watt_Sync_SyncSocket::syncInterface('movemsglistoutsite', $msgsign); if ($result != '1') { $loger->log('socket输出接口移动外部消息序列到outsite目录失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } //执行外部outsite目录下的消息序列 /* $result = Watt_Sync_SyncSocket::syncInterface('execmsglist',''); if ($result!='1'){ $loger->log( 'socket执行外部outsite目录下的消息序列' ); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; }*/ // exit; $msgList_execute = array(); } } //--- 未满个数end //$msgList_execute[$key] = $val; $msgSignArr = array_keys($msgList_execute); $data_str = ''; $data = array(); $data = array('msgList' => $msgList_execute, 'msgListDir' => 'unknown', 'validatePassword' => Watt_Sync_MessageListManage::getValidatePassword()); $data_str = urlencode(serialize($data)); //汇总文件内容 $msglistfilesize = strlen($data_str); //汇总文件大小 //生成本地消息序列汇总文件 $re = Watt_Sync_MessageListManage::createdMsgListFileSync($data_str); if ($re != '1') { $loger->log('socket输出接口创建内部消息序列汇总文件失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } $dd_chunk = str_split($data_str, 1000); $count_arr = count($dd_chunk); //总块数 foreach ($dd_chunk as $key => $val) { //循环传输数据 $order_str = "send_" . ($key + 1) . "_" . $count_arr; $count = strlen($order_str); $order_str = str_split($order_str, 1); $temp_arr = array_fill($count, 20 - $count, ' '); $order_str_arr = array_merge($order_str, $temp_arr); $order_str = ''; foreach ($order_str_arr as $k => $v) { $order_str .= $v; } $data = $order_str . $val; $result = Watt_Sync_SyncSocket::syncInterface('send', $data); if ($result != '1') { $loger->log('socket输出接口传送消息序列汇总文件失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } } //获取外部消息序列汇总文件的大小,比较内外汇总文件 //删除内部消息序列汇总文件 $result = Watt_Sync_MessageListManage::delFile('', 'msgListfile.msg'); if ($result != '1') { $loger->log('socket输入接口删除本地sync目录消息序列汇总文件失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } //解析外部消息序列汇总文件成消息序列 $msgsignstr = Watt_Sync_SyncSocket::syncInterface('execmsgfile', ''); if ($result == '0') { $loger->log('socket输出接口解析外部消息序列汇总文件失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } //删除内部消息序列 $msgsign = unserialize(urldecode($msgsignstr)); if (is_array($msgsign) && count($msgsign)) { $re = Watt_Sync_MessageListManage::delMessageList($msgsign); if ($re != '1') { $loger->log('socket输出接口删除内部消息序列失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } } else { $loger->log('socket输出接口内部消息序列数据错误'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } //移动外部消息序列到outsite目录 $result = Watt_Sync_SyncSocket::syncInterface('movemsglistoutsite', $msgsign); if ($result != '1') { $loger->log('socket输出接口移动外部消息序列到outsite目录失败'); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; } //执行外部outsite目录下的消息序列 /* $result = Watt_Sync_SyncSocket::syncInterface('execmsglist',''); if ($result!='1'){ $loger->log( 'socket执行外部outsite目录下的消息序列' ); //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); exit; }*/ // exit; //---未满个数 end } //------------外部消息序列的处理 end---------------// //解除锁定 Watt_Sync_MessageListManage::DelLock('soutput'); //$end_time = time(); echo '<br>' . microtime(); }
/** * 读取指定个数的消息序列生成文件 * * 返回值:成功返回文件大小,失败返回false */ public static function createLocalMsgListFile() { $config = self::_getConfig(); $count = $config['numOnceDel']; $msglist = self::getAllMessageList('', $count); $data = array('msgList' => $msglist, 'msgListDir' => 'unknown', 'validatePassword' => Watt_Sync_MessageListManage::getValidatePassword()); $data = urlencode(serialize($data)); //写文件,目录sync下 $filename = $config['TpmMsgListDir'] . 'msgListfile.msg'; if (!is_file($filename)) { //不存在 $fp = fopen($filename, 'wb'); fwrite($fp, $data); fclose($fp); if (is_file($filename)) { return strlen($data); } else { return false; } } else { //存在 } }