Web UI for ARGUS server
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.

128 lines
3.5 KiB

6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. class MySQLi_DB{
  3. var $DBHost;
  4. var $DBName;
  5. var $DBUser;
  6. var $DBPassword;
  7. var $DBLink;
  8. var $CachePath;
  9. var $isConnected=false;
  10. var $isCached=false;
  11. function MySQLi_DB( $Host,$Name,$User,$Password){
  12. $this->DBHost=$Host;
  13. $this->DBName=$Name;
  14. $this->DBUser=$User;
  15. $this->DBPassword=$Password;
  16. $this->DBLink = new mysqli( $this->DBHost, $this->DBUser, $this->DBPassword, $this->DBName );
  17. if( $this->DBLink->connect_error ){
  18. $this->isConnected=false;
  19. echo 'Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
  20. } else {
  21. //mysql_query("SET NAMES 'UTF8'",$this->DBLink);
  22. if (!$this->DBLink->set_charset("utf8")) {
  23. printf("Error loading character set utf8: %s\n", $mysqli->error);
  24. } else {
  25. //printf("Current character set: %s\n", $mysqli->character_set_name());
  26. };
  27. $this->isConnected=true;
  28. };
  29. }
  30. function CleanUpCache(){
  31. if ($DD=@opendir($this->CachePath)){
  32. while($FN=readdir($DD)){
  33. $CF=$this->CachePath.'/'.$FN;
  34. if (is_file($CF)){
  35. if ( strstr($FN,'dbchache_') && (time()-filemtime($CF))>360){
  36. @unlink($CF);
  37. };
  38. };
  39. };
  40. closedir($DD);
  41. } else $this->isCached=false;
  42. }
  43. function ResetCache(){
  44. if ($DD=@opendir($this->CachePath)){
  45. while($FN=readdir($DD)){
  46. $CF=$this->CachePath.'/'.$FN;
  47. if (is_file($CF)){
  48. if ( strstr($FN,'dbchache_')){
  49. @unlink($CF);
  50. };
  51. };
  52. };
  53. closedir($DD);
  54. } else $this->isCached=false;
  55. }
  56. function SetCacheDir($Path){
  57. $this->CachePath = $Path;
  58. $this->isCached = is_dir($this->CachePath) && is_writable($this->CachePath);
  59. if ($this->isCached) $this->CleanUpCache();
  60. }
  61. function escape( $Str ){
  62. if($this->isConnected){
  63. return $this->DBLink->real_escape_string( $Str );
  64. };
  65. return '';
  66. }
  67. function Query($QS,$ChacheIfAveable=true){
  68. if($this->isConnected){
  69. if ($this->isCached && $ChacheIfAveable){
  70. $QMD5=md5($QS);
  71. $Prefetch=$this->CachePath.'/dbchache_'.$QMD5;
  72. if ( is_file($Prefetch)){
  73. if ( (time()- filemtime($Prefetch))<360){
  74. $FData=file_get_contents($Prefetch);
  75. return unserialize($FData);
  76. } else {
  77. $result = mysqli_query( $this->DBLink, $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
  78. $RetVal=array();
  79. while($array=mysqli_fetch_assoc($result)) $RetVal[]=$array;
  80. $FData=serialize($RetVal);
  81. $FD=fopen($Prefetch,'w');
  82. fwrite($FD,$FData);
  83. fclose($FD);
  84. return $RetVal;
  85. }
  86. } else {
  87. $result = mysqli_query( $this->DBLink, $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
  88. $RetVal=array();
  89. while($array=mysqli_fetch_assoc($result)) $RetVal[]=$array;
  90. $FData = serialize($RetVal);
  91. $FD=fopen($Prefetch,'w');
  92. fwrite($FD,$FData);
  93. fclose($FD);
  94. return $RetVal;
  95. }
  96. } else {
  97. $result = mysqli_query( $this->DBLink, $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
  98. $RetVal=array();
  99. if ($result !== false && is_a( $result, 'mysqli_result' ) ){
  100. while($array=mysqli_fetch_assoc($result)) $RetVal[]=$array;
  101. };
  102. return $RetVal;
  103. };
  104. } else {
  105. echo "No Connection to DB<br>";
  106. };
  107. }
  108. function getLastInsertID(){
  109. if($this->isConnected){
  110. return $this->DBLink->insert_id;
  111. };
  112. return -1;
  113. }
  114. function __destruct(){
  115. if( $this->isConnected ){
  116. $this->DBLink->close();
  117. };
  118. }
  119. }
  120. ?>