Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. class SQLite_DB{
  3. var $DBFILE;
  4. var $DBFLAGS;
  5. var $DBENCRYPT;
  6. var $DBLink;
  7. var $CachePath;
  8. var $isConnected=false;
  9. var $isCached=false;
  10. var $DebugLog = '';
  11. function __construct( $DBFILE, $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $encryptionKey = "" ){
  12. $this->DBFILE=$DBFILE;
  13. $this->DBFLAGS=$flags;
  14. $this->DBENCRYPT=$encryptionKey;
  15. try{
  16. $this->DBLink = new SQLite3( $this->DBFILE, $this->DBFLAGS, $this->DBENCRYPT );
  17. $this->DBLink->busyTimeout(5000);
  18. // WAL mode has better control over concurrency.
  19. // Source: https://www.sqlite.org/wal.html
  20. $this->DBLink->exec('PRAGMA journal_mode = wal;');
  21. $this->isConnected = true;
  22. }catch( Exception $e ){
  23. echo $e->getMessage();
  24. };
  25. }
  26. function CleanUpCache(){
  27. if ($DD=@opendir($this->CachePath)){
  28. while($FN=readdir($DD)){
  29. $CF=$this->CachePath.'/'.$FN;
  30. if (is_file($CF)){
  31. if ( strstr($FN,'dbchache_') && (time()-filemtime($CF))>360){
  32. @unlink($CF);
  33. };
  34. };
  35. };
  36. closedir($DD);
  37. } else $this->isCached=false;
  38. }
  39. function ResetCache(){
  40. if ($DD=@opendir($this->CachePath)){
  41. while($FN=readdir($DD)){
  42. $CF=$this->CachePath.'/'.$FN;
  43. if (is_file($CF)){
  44. if ( strstr($FN,'dbchache_')){
  45. @unlink($CF);
  46. };
  47. };
  48. };
  49. closedir($DD);
  50. } else $this->isCached=false;
  51. }
  52. function SetCacheDir($Path){
  53. $this->CachePath = $Path;
  54. $this->isCached = is_dir($this->CachePath) && is_writable($this->CachePath);
  55. if ($this->isCached) $this->CleanUpCache();
  56. }
  57. function escape( $Str ){
  58. if( $this->isConnected ){
  59. return $this->DBLink->escapeString( $Str );
  60. };
  61. return '';
  62. }
  63. function Query($QS,$ChacheIfAveable=true){
  64. if($this->isConnected){
  65. $QS = trim( $QS );
  66. if( $this->isCached && $ChacheIfAveable ){
  67. $QMD5=md5($QS);
  68. $Prefetch=$this->CachePath.'/dbchache_'.$QMD5;
  69. if ( is_file($Prefetch)){
  70. if ( (time()- filemtime($Prefetch))<360){
  71. $FData=file_get_contents($Prefetch);
  72. $this->DebugLog .="[{$QS}] as (Cahed Data),\n";
  73. return unserialize($FData);
  74. } else {
  75. $result = $this->DBLink->query( $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
  76. $RetVal=array();
  77. if( ( stripos( $QS, 'insert' ) === false ) && ( stripos( $QS, 'update' ) === false ) && $result !== false && is_a( $result, 'SQLite3Result' ) ){
  78. while( $array = $result->fetchArray( SQLITE3_ASSOC )) $RetVal[]=$array;
  79. $result->finalize();
  80. };
  81. $FData=serialize($RetVal);
  82. $FD=fopen($Prefetch,'w');
  83. fwrite($FD,$FData);
  84. fclose($FD);
  85. $this->DebugLog .="[{$QS}] as (".$this->DBLink->lastInsertRowID()."),\n";
  86. return $RetVal;
  87. }
  88. } else {
  89. $result = $this->DBLink->query( $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
  90. $RetVal=array();
  91. if( ( stripos( $QS, 'insert' ) === false ) && ( stripos( $QS, 'update' ) === false ) && $result !== false && is_a( $result, 'SQLite3Result' ) ){
  92. while( $array = $result->fetchArray( SQLITE3_ASSOC )) $RetVal[]=$array;
  93. $result->finalize();
  94. };
  95. $FData = serialize($RetVal);
  96. $FD=fopen($Prefetch,'w');
  97. fwrite($FD,$FData);
  98. fclose($FD);
  99. $this->DebugLog .="[{$QS}] as (".$this->DBLink->lastInsertRowID()."),\n";
  100. return $RetVal;
  101. }
  102. } else {
  103. $result = $this->DBLink->query( $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
  104. $RetVal=array();
  105. if( ( stripos( $QS, 'insert' ) === false ) && ( stripos( $QS, 'update' ) === false ) && $result !== false && is_a( $result, 'SQLite3Result' ) ){
  106. while( $array = $result->fetchArray( SQLITE3_ASSOC )) $RetVal[]=$array;
  107. $result->finalize();
  108. };
  109. $this->DebugLog .="[{$QS}] as (".$this->DBLink->lastInsertRowID()."),\n";
  110. return $RetVal;
  111. };
  112. } else {
  113. echo "No Connection to DB<br>";
  114. };
  115. }
  116. function getLastInsertID(){
  117. if($this->isConnected){
  118. return $this->DBLink->lastInsertRowID();
  119. };
  120. return -1;
  121. }
  122. function getDebugLog(){
  123. return $this->DebugLog;
  124. }
  125. function __destruct(){
  126. if( $this->isConnected ){
  127. $this->DBLink->close();
  128. unset( $this->DBLink );
  129. };
  130. }
  131. }
  132. ?>