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.

133 lines
4.0 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->isConnected = true;
  18. }catch( Exception $e ){
  19. echo $e->getMessage();
  20. };
  21. }
  22. function CleanUpCache(){
  23. if ($DD=@opendir($this->CachePath)){
  24. while($FN=readdir($DD)){
  25. $CF=$this->CachePath.'/'.$FN;
  26. if (is_file($CF)){
  27. if ( strstr($FN,'dbchache_') && (time()-filemtime($CF))>360){
  28. @unlink($CF);
  29. };
  30. };
  31. };
  32. closedir($DD);
  33. } else $this->isCached=false;
  34. }
  35. function ResetCache(){
  36. if ($DD=@opendir($this->CachePath)){
  37. while($FN=readdir($DD)){
  38. $CF=$this->CachePath.'/'.$FN;
  39. if (is_file($CF)){
  40. if ( strstr($FN,'dbchache_')){
  41. @unlink($CF);
  42. };
  43. };
  44. };
  45. closedir($DD);
  46. } else $this->isCached=false;
  47. }
  48. function SetCacheDir($Path){
  49. $this->CachePath = $Path;
  50. $this->isCached = is_dir($this->CachePath) && is_writable($this->CachePath);
  51. if ($this->isCached) $this->CleanUpCache();
  52. }
  53. function escape( $Str ){
  54. if( $this->isConnected ){
  55. return $this->DBLink->escapeString( $Str );
  56. };
  57. return '';
  58. }
  59. function Query($QS,$ChacheIfAveable=true){
  60. if($this->isConnected){
  61. $QS = trim( $QS );
  62. if( $this->isCached && $ChacheIfAveable ){
  63. $QMD5=md5($QS);
  64. $Prefetch=$this->CachePath.'/dbchache_'.$QMD5;
  65. if ( is_file($Prefetch)){
  66. if ( (time()- filemtime($Prefetch))<360){
  67. $FData=file_get_contents($Prefetch);
  68. $this->DebugLog .="[{$QS}] as (Cahed Data),\n";
  69. return unserialize($FData);
  70. } else {
  71. $result = $this->DBLink->query( $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
  72. $RetVal=array();
  73. if( ( stripos( $QS, 'insert' ) === false ) && ( stripos( $QS, 'update' ) === false ) && $result !== false && is_a( $result, 'SQLite3Result' ) ){
  74. while( $array = $result->fetchArray( SQLITE3_ASSOC )) $RetVal[]=$array;
  75. $result->finalize();
  76. };
  77. $FData=serialize($RetVal);
  78. $FD=fopen($Prefetch,'w');
  79. fwrite($FD,$FData);
  80. fclose($FD);
  81. $this->DebugLog .="[{$QS}] as (".$this->DBLink->lastInsertRowID()."),\n";
  82. return $RetVal;
  83. }
  84. } else {
  85. $result = $this->DBLink->query( $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
  86. $RetVal=array();
  87. if( ( stripos( $QS, 'insert' ) === false ) && ( stripos( $QS, 'update' ) === false ) && $result !== false && is_a( $result, 'SQLite3Result' ) ){
  88. while( $array = $result->fetchArray( SQLITE3_ASSOC )) $RetVal[]=$array;
  89. $result->finalize();
  90. };
  91. $FData = serialize($RetVal);
  92. $FD=fopen($Prefetch,'w');
  93. fwrite($FD,$FData);
  94. fclose($FD);
  95. $this->DebugLog .="[{$QS}] as (".$this->DBLink->lastInsertRowID()."),\n";
  96. return $RetVal;
  97. }
  98. } else {
  99. $result = $this->DBLink->query( $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
  100. $RetVal=array();
  101. if( ( stripos( $QS, 'insert' ) === false ) && ( stripos( $QS, 'update' ) === false ) && $result !== false && is_a( $result, 'SQLite3Result' ) ){
  102. while( $array = $result->fetchArray( SQLITE3_ASSOC )) $RetVal[]=$array;
  103. $result->finalize();
  104. };
  105. $this->DebugLog .="[{$QS}] as (".$this->DBLink->lastInsertRowID()."),\n";
  106. return $RetVal;
  107. };
  108. } else {
  109. echo "No Connection to DB<br>";
  110. };
  111. }
  112. function getLastInsertID(){
  113. if($this->isConnected){
  114. return $this->DBLink->lastInsertRowID();
  115. };
  116. return -1;
  117. }
  118. function getDebugLog(){
  119. return $this->DebugLog;
  120. }
  121. function __destruct(){
  122. if( $this->isConnected ){
  123. $this->DBLink->close();
  124. };
  125. }
  126. }
  127. ?>