From f2f3c6b839c3575a53dffab899e89a06b1d09e36 Mon Sep 17 00:00:00 2001 From: rylon Date: Fri, 28 Nov 2014 00:46:37 +0100 Subject: [PATCH] cron_log2sqlite, sqlite connection(proto, working for purpose) --- cron/cron_log2sqlite.php | 70 +++++++++++++++++++++++++++ cron/cronjob.php | 3 +- db/connection/Connection.php | 5 ++ db/connection/ConnectionSQLite.php | 57 ++++++++++++++++++++++ db/dbinfo/DBInfoSQLite.php | 16 +++++++ db/result/ResultSQLite.php | 76 ++++++++++++++++++++++++++++++ dbd/qq/SYS_LOG_MONTH.php | 11 +++++ dbd/qq/SYS_LOG_MONTH_DEL.php | 11 +++++ dbd/qq/SYS_LOG_OLDEST.php | 11 +++++ 9 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 cron/cron_log2sqlite.php create mode 100644 db/connection/ConnectionSQLite.php create mode 100644 db/dbinfo/DBInfoSQLite.php create mode 100644 db/result/ResultSQLite.php create mode 100644 dbd/qq/SYS_LOG_MONTH.php create mode 100644 dbd/qq/SYS_LOG_MONTH_DEL.php create mode 100644 dbd/qq/SYS_LOG_OLDEST.php diff --git a/cron/cron_log2sqlite.php b/cron/cron_log2sqlite.php new file mode 100644 index 0000000..d626edf --- /dev/null +++ b/cron/cron_log2sqlite.php @@ -0,0 +1,70 @@ + abort + if( $oldest['year'] >= $now_year && + $oldest['month'] >= $now_month){ + return cronstatus::CRON_STATUS_SUCCESFULLY;} + + $filename = '/home/web/webdir/mojotrollz/mojotrollz/files/log/'.$oldest['year'].'.'.$oldest['month'].'.db'; + //extract whole month to file + $con = new \SYSTEM\DB\Connection(new \SYSTEM\DB\DBInfoSQLite($filename,0666)); + + //create table + $con->query('CREATE TABLE IF NOT EXISTS `system_log` ('. + ' `ID` INT(10) NOT NULL,'. + ' `class` TEXT NOT NULL,'. + ' `message` TEXT NOT NULL,'. + ' `code` INT(11) NOT NULL,'. + ' `file` TEXT NOT NULL,'. + ' `line` INT(11) NOT NULL,'. + ' `trace` TEXT NOT NULL,'. + ' `ip` TEXT NOT NULL,'. + ' `querytime` DOUBLE NOT NULL,'. + ' `time` DATETIME NOT NULL,'. + ' `server_name` CHAR(255) NOT NULL,'. + ' `server_port` INT(10) NOT NULL,'. + ' `request_uri` CHAR(255) NOT NULL,'. + ' `post` TEXT NOT NULL,'. + ' `http_referer` CHAR(255) NULL DEFAULT NULL,'. + ' `http_user_agent` TEXT NOT NULL,'. + ' `user` INT(11) NULL DEFAULT NULL,'. + ' `thrown` BIT(1) NOT NULL,'. + ' PRIMARY KEY (`ID`)'.');'); + + //write data + $con->exec('begin transaction'); + $res = \SYSTEM\DBD\SYS_LOG_MONTH::QQ(array($oldest['month'],$oldest['year'])); + $i = 0; + while($row = $res->next()){ + set_time_limit(30); + $i++; + if(!$con->exec('INSERT OR IGNORE INTO '.\SYSTEM\DBD\system_log::NAME_MYS. + '(`ID`, `class`, `message`, `code`, `file`, `line`, `trace`, `ip`, `querytime`, `time`,'. + ' `server_name`, `server_port`, `request_uri`, `post`,'. + ' `http_referer`, `http_user_agent`, `user`, `thrown`)'. + 'VALUES ('.$row['ID'].', \''.\SQLite3::escapeString($row['class']).'\', \''.\SQLite3::escapeString($row['message']).'\', '. + $row['code'].', \''.\SQLite3::escapeString($row['file']).'\', '.$row['line'].', \''.\SQLite3::escapeString($row['trace']).'\', \''. + $row['ip'].'\', '.$row['querytime'].', \''.$row['time'].'\', \''. + \SQLite3::escapeString($row['server_name']).'\', '.$row['server_port'].', \''.\SQLite3::escapeString($row['request_uri']).'\', \''.\SQLite3::escapeString($row['post']).'\', \''. + \SQLite3::escapeString($row['http_referer']).'\', \''.\SQLite3::escapeString($row['http_user_agent']).'\', '.$row['user'].','.true.');')){ + new \SYSTEM\LOG\ERROR('failed to insert into log archiev'); + return cronstatus::CRON_STATUS_FAIL; + } + } + if(!$con->exec('end transaction')){ + new \SYSTEM\LOG\ERROR('failed to insert into log archiev'); + return cronstatus::CRON_STATUS_FAIL;}; + + //delete from database + if(!\SYSTEM\DBD\SYS_LOG_MONTH_DEL::QI(array($oldest['month'],$oldest['year']))){ + new \SYSTEM\LOG\ERROR('failed to delete log entries'); + return cronstatus::CRON_STATUS_FAIL;} + + return cronstatus::CRON_STATUS_SUCCESFULLY; + } +} \ No newline at end of file diff --git a/cron/cronjob.php b/cron/cronjob.php index b0c58f0..da27e93 100644 --- a/cron/cronjob.php +++ b/cron/cronjob.php @@ -1,7 +1,6 @@ connection = new \SYSTEM\DB\ConnectionMYS($dbinfo); } else if ($dbinfo instanceof \SYSTEM\DB\DBInfoAMQP){ $this->connection = new \SYSTEM\DB\ConnectionAMQP($dbinfo); + } else if ($dbinfo instanceof \SYSTEM\DB\DBInfoSQLite){ + $this->connection = new \SYSTEM\DB\ConnectionSQLite($dbinfo); } else { throw new \Exception('Could not understand Database Settings. Check ur Database Settings');} } @@ -39,4 +41,7 @@ class Connection extends ConnectionAbstr{ //Query connected Database public function query($query){ return $this->connection->query($query);} + + public function exec($query){ + return $this->connection->exec($query);} } \ No newline at end of file diff --git a/db/connection/ConnectionSQLite.php b/db/connection/ConnectionSQLite.php new file mode 100644 index 0000000..8a45dc9 --- /dev/null +++ b/db/connection/ConnectionSQLite.php @@ -0,0 +1,57 @@ +connection = new \SQLite3($dbinfo->m_database); + if(!$this->connection){ + throw new \Exception('Could not connect to Database. Check ur Database Settings: '.$error);} + } + + public function __destruct(){ + $this->close();} + + public function prepare($stmtName, $stmt, $values){ + $prepStmt = \sqlite_prepare($this->connection, $stmt,$error); + if(!$prepStmt){ + throw new \SYSTEM\LOG\ERROR('Prepared Statement prepare fail: '. $error);} + + $types = ''; + $binds = array($prepStmt,null); + for($i =0; $i < \count($values);$i++){ + $types .= self::getPrepareValueType($values[$i]); + $binds[] = &$values[$i];} + $binds[1] = $types; + \call_user_func_array('sqlite_stmt_bind_param', $binds); //you need 2 append the parameters - thats the right way to do that. + + if(!sqlite_stmt_execute($prepStmt,$error)){ + throw new \SYSTEM\LOG\ERROR("Could not execute prepare statement: ". $error);} + + return new ResultSQLite($prepStmt,$this); + } + + public function close(){ + return $this->connection->close();} + + public function query($query){ + $result = $this->connection->query($query); + if(!$result){ + throw new \SYSTEM\LOG\ERROR('Could not query Database. Check ur Query Syntax or required Rights: '.$this->connection->lastErrorMsg());} + if($result === TRUE){ + return TRUE;} + + return new ResultSQLite($result); + } + + public function exec($query){ + $result = false; + + $result = $this->connection->exec($query); + + return $result; + } +} \ No newline at end of file diff --git a/db/dbinfo/DBInfoSQLite.php b/db/dbinfo/DBInfoSQLite.php new file mode 100644 index 0000000..3861dac --- /dev/null +++ b/db/dbinfo/DBInfoSQLite.php @@ -0,0 +1,16 @@ +m_database = $database; + $this->m_user = $permissions; + $this->m_password = $password; + $this->m_host = $host; + $this->m_port = $port; + + if( $this->m_database == null || + $this->m_user == null){ + throw new \Exception("DBInfo not correct, database, permissions are null");} + } +} \ No newline at end of file diff --git a/db/result/ResultSQLite.php b/db/result/ResultSQLite.php new file mode 100644 index 0000000..2bd659e --- /dev/null +++ b/db/result/ResultSQLite.php @@ -0,0 +1,76 @@ +res = $res;} + + public function __destruct(){ + $this->close();} + + public function close(){ + $this->res->finalize();} + + public function count(){ + throw new Exception("Problem SQLite"); + return mysqli_num_rows($this->res);} + /* + * if ($res->numColumns() && $res->columnType(0) != SQLITE3_NULL) { + // have rows +} else { + // zero rows +} + */ + + public function affectedRows(){ + return mysqli_affected_rows($this->res);} + + public function next($object = false, $result_type = SQLITE3_BOTH){ + if($object){ + throw new Exception("Problem SQLite"); + } else { + $this->current = $this->res->fetchArray($result_type); + } + return $this->current; + } + + /* + * function fetchObject($sqlite3result, $objectType = NULL) { + $array = $sqlite3result->fetchArray(); + + if(is_null($objectType)) { + $object = new stdClass(); + } else { + // does not call this class' constructor + $object = unserialize(sprintf('O:%d:"%s":0:{}', strlen($objectType), $objectType)); + } + + $reflector = new ReflectionObject($object); + for($i = 0; $i < $sqlite3result->numColumns(); $i++) { + $name = $sqlite3result->columnName($i); + $value = $array[$name]; + + try { + $attribute = $reflector->getProperty($name); + + $attribute->setAccessible(TRUE); + $attribute->setValue($object, $value); + } catch (ReflectionException $e) { + $object->$name = $value; + } + } + + return $object; +} + */ + + public function seek($row_number){ + throw new Exception("Problem SQLite"); + return mysqli_data_seek($this->res,$row_number);} +} \ No newline at end of file diff --git a/dbd/qq/SYS_LOG_MONTH.php b/dbd/qq/SYS_LOG_MONTH.php new file mode 100644 index 0000000..5a1101f --- /dev/null +++ b/dbd/qq/SYS_LOG_MONTH.php @@ -0,0 +1,11 @@ +