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.

149 lines
4.2 KiB

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