PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ecms/libraries/ecms_1.0.6/classes/database/MySQL.class.php

https://gitlab.com/Elron_MacBong/ecms
PHP | 229 lines | 129 code | 55 blank | 45 comment | 38 complexity | c6636373cea0248fb86a670621fb5dc6 MD5 | raw file
  1. <?php
  2. if(!defined('eCMS')) die('Hacking attempt...');
  3. class MySQL {
  4. private $eCMS; // Holds the eCMS-class.
  5. private $dbSettings; // Holds the database-settings.
  6. private $hostname; // Holds the ip/hostname.
  7. private $username; // Holds the username.
  8. private $password; // Holds the password.
  9. private $database; // Holds the database-name.
  10. private $port; // Holds the port.
  11. private $connected = false; // Connected? ;)
  12. private $connection; // Holds the connect as self.
  13. private $sql_query; // Holds the sql-query.
  14. private $query_counter; // Count the queries.
  15. private $result; // Holds the query-result.
  16. public $resource; // Holds the public resource.
  17. // The constructor.
  18. # @param int $id
  19. #
  20. # @return bool
  21. public function __construct($eCMS, $settings, $id = DB_DEFAULT_01) {
  22. $this->eCMS = $eCMS;
  23. $this->dbSettings = $settings;
  24. if(is_int($id)) {
  25. $this->hostname = $this->dbSettings[$id]['host'];
  26. $this->username = $this->dbSettings[$id]['user'];
  27. $this->password = $this->dbSettings[$id]['pass'];
  28. $this->database = $this->dbSettings[$id]['db'];
  29. $this->port = $this->dbSettings[$id]['port'];
  30. $this->connection = @mysql_connect($this->hostname.':'.$this->port, $this->username, $this->password, false, 0);
  31. if(is_resource($this->connection)) {
  32. if(@mysql_select_db($this->database, $this->connection) === false) $this->error("DB.SELECT", mysql_error());
  33. else $this->connected = true;
  34. return true;
  35. }
  36. if(!is_resource($this->connection)) {
  37. #$this->log->setLog('Can\'t connect to database.', 'error');
  38. $this->error('CONN.OPEN', mysql_error());
  39. return false;
  40. }
  41. }
  42. return false;
  43. }
  44. // Returns the count affected rows. (mysql_affected_rows)
  45. # @return int
  46. public function getAffectedRows() {
  47. if($this->connected === true) return mysql_affected_rows($this->connection);
  48. else $this->error('CONN.NO', NULL);
  49. }
  50. // Returns the encoding. (mysql_client_encoding)
  51. # @return string
  52. public function getClientEncoding() {
  53. if($this->connected === true) return mysql_client_encoding($this->connection);
  54. else $this->error('CONN.NO', NULL);
  55. }
  56. // Handles database creation. (mysql_create_db)
  57. # @param string $database
  58. #
  59. # @return bool
  60. public function createDatabase($database) {}
  61. // Handles database dropping. (mysql_drop_db)
  62. # @param string $database
  63. #
  64. # @return bool
  65. public function dropDatabase($database) {}
  66. // mysql_fetch_array
  67. # @param string $query
  68. # @param int $type
  69. #
  70. # @return array
  71. public function getArray($query, $type = MYSQL_BOTH) {
  72. if($this->query($query) === true && is_resource($this->result)) {
  73. $count = @mysql_num_rows($this->result);
  74. $array = array();
  75. for($i = 0; $i < $count; $i++) {
  76. $array[$i] = @mysql_fetch_array($this->result, $type);
  77. }
  78. return $array;
  79. } else return false;
  80. }
  81. // Execute query. (mysql_query)
  82. # @param string $sql
  83. #
  84. # @return bool
  85. public function query($sql) {
  86. if(is_string($sql)) {
  87. $this->sql_query = $sql;
  88. $this->query_counter++;
  89. $this->result = @mysql_query($this->sql_query, $this->connection);
  90. if($this->result === false) { $this->error('QUERY.FAILED', NULL); return false; }
  91. else return true;
  92. } else return false;
  93. }
  94. // Returns the query counter.
  95. # @return int
  96. public function getDbQueries() {
  97. if($this->connected === true) return $this->query_counter;
  98. else $this->error('CONN.NO', NULL);
  99. }
  100. // Makes full log-string.
  101. # @return string
  102. public function drawDbQueries() {
  103. if($this->connected === true) {
  104. if($this->getDbQueries == 1) $text = "Script executed with 1 database-request.";
  105. else $text = "Script executed with ".$this->DBQueries." database-requests.";
  106. $this->log->setLog($text, 'debug');
  107. return $text;
  108. } else $this->error('CONN.NO', NULL);
  109. }
  110. // Secure a string for query using. (mysql_real_escape_string)
  111. # @param string $unescapedString
  112. #
  113. # @return string
  114. public function secureString($unescapedString) {
  115. if(!is_string($unescapedString)) $this->eCMS->dieFunctionCall('secureString', 'unescapedString', gettype($unescapedString), 'string');
  116. if(is_string($unescapedString)) {
  117. $escapedString = '';
  118. $temp = $unescapedString;
  119. $temp = mysql_real_escape_string($temp);
  120. $escapedString = $temp;
  121. return $escapedString;
  122. } else return false;
  123. }
  124. // Secure a string for query using. (mysql_real_escape_string)
  125. # @param int $unescapedString
  126. #
  127. # @returnstring
  128. public function secureInt($unescapedString) {
  129. if(!is_int($unescapedString)) $this->eCMS->dieFunctionCall('secureInt', 'unescapedString', gettype($unescapedString), 'integer');
  130. if(is_int($unescapedString)) {
  131. $escapedString = '';
  132. $temp = $unescapedString;
  133. $temp = mysql_real_escape_string($temp);
  134. $escapedString = $temp;
  135. return $escapedString;
  136. } else return false;
  137. }
  138. // Error-Handler
  139. # @param string $error_type
  140. # @param string $error
  141. private function error($error_type, $error = NULL) {
  142. if(is_string($error_type)) {
  143. switch($error_type) {
  144. case 'CONN.OPEN':
  145. $text = "Error while opening database-connection.\r\n".$error;
  146. break;
  147. case 'CONN.CLOSE':
  148. $text = "Error while closing database-connection.\r\n".$error;
  149. break;
  150. case 'CONN.NO':
  151. $text = "Try to execute database-command, but database isn\'t connected!";
  152. break;
  153. case 'DB.SELECT':
  154. $text = "Error while selecting the database (Database exists?).";
  155. break;
  156. case 'QUERY.FAILED':
  157. $text = "Error while query:\r\n".$this->sql_query;
  158. break;
  159. default:
  160. $text = "Database-Error:\r\n".$error_type;
  161. break;
  162. }
  163. if($this->connected === true) $text .= "\r\nDatabase-Error (MySQL):\r\nError No.: ".@mysql_errno($this->connection)."\r\nError Description: ".@mysql_error($this->connection);
  164. #$this->log->setLog($text, 'error');
  165. echo $text;
  166. } else return false;
  167. }
  168. // The destructor.
  169. # @return void
  170. public function __destruct() {
  171. #$this->db->__destruct();
  172. }
  173. }
  174. ?>