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

/inc/classes/class_db_mysql.php

http://lansuite.googlecode.com/
PHP | 266 lines | 192 code | 54 blank | 20 comment | 77 complexity | 09c00ef2ed667135e648793ca571fd94 MD5 | raw file
Possible License(s): LGPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. class db {
  3. var $mysqli = 0;
  4. var $link_id = 0;
  5. var $query_id = 0;
  6. var $record = array();
  7. var $print_sql_error;
  8. var $success = false;
  9. var $count_query = 0;
  10. var $errors = '';
  11. var $errorsFound = 0;
  12. var $connectfailure = 0; //0= no error, 1=connection error, 2=database error
  13. var $QueryArgs = array();
  14. // Construktor
  15. function db() {
  16. if (extension_loaded("mysqli")) $this->mysqli = 1;
  17. elseif (!extension_loaded("mysql")) echo HTML_FONT_ERROR . t('Das MySQL-PHP Modul ist nicht geladen. Bitte f?ge die mysql.so Erweiterung zur php.ini hinzu und restarte den Webserver neu. Lansuite wird abgebrochen') . HTML_FONT_END;
  18. }
  19. #### Internal only ####
  20. function print_error($msg, $query_string_with_error) {
  21. global $func, $config, $auth;
  22. $error = t('SQL-Failure. Database respondet: <b>%1</b><br /><br />Query: <br /><i>%2</i>', $msg, $query_string_with_error);
  23. $this->errors .= $error . '<br />';
  24. $this->errorsFound = 1;
  25. // Need to use mysql_querys here, to prevent loops!!
  26. $query = 'INSERT INTO '. $config['database']['prefix'] .'log SET date = NOW(), userid = '. (int)$auth['userid'] .', type = 3, description = "'. strip_tags($error) .'", sort_tag = "SQL-Fehler"';
  27. if ($this->mysqli) mysqli_query($this->link_id, $query);
  28. else mysql_query($query, $this->link_id);
  29. $this->count_query++;
  30. }
  31. function escape($match) {
  32. $CurrentArg = array_shift($this->QueryArgs);
  33. if ($match[0] == '%int%') return (int)$CurrentArg;
  34. elseif ($match[0] == '%string%') {
  35. $CurrentArg = stripslashes($CurrentArg);
  36. if ($this->mysqli) return "'". mysqli_real_escape_string($this->link_id, (string)$CurrentArg) ."'";
  37. else return "'". mysql_real_escape_string((string)$CurrentArg, $this->link_id) ."'";
  38. } elseif ($match[0] == '%plain%') return $CurrentArg;
  39. }
  40. #### Connection related ####
  41. function connect($save = false) {
  42. global $config;
  43. $server = $config['database']['server'];
  44. $user = $config['database']['user'];
  45. $pass = $config['database']['passwd'];
  46. $database = $config['database']['database'];
  47. // Try to connect
  48. if ($this->mysqli) $this->link_id = @mysqli_connect($server, $user, $pass);
  49. else $this->link_id = @mysql_connect($server, $user, $pass);
  50. if (!$this->link_id) {
  51. if ($save) {
  52. $this->connectfailure = 1;
  53. $this->success = false;
  54. return false;
  55. } else {
  56. echo HTML_FONT_ERROR . t('Die Verbindung zur Datenbank ist fehlgeschlagen. Lansuite wird abgebrochen') . HTML_FONT_END;
  57. exit();
  58. }
  59. // Try to select DB
  60. } else {
  61. if ($this->mysqli) $ret = mysqli_select_db($this->link_id, $database);
  62. else $ret = mysql_select_db($database, $this->link_id);
  63. if (!$ret) {
  64. if ($save) {
  65. $this->connectfailure = 2;
  66. $this->success = false;
  67. return false;
  68. } else {
  69. echo HTML_FONT_ERROR . t("Die Datenbank '%1' konnte nicht ausgew?hlt werden. Lansuite wird abgebrochen", $database) . HTML_FONT_END;
  70. exit();
  71. }
  72. }
  73. }
  74. if ($this->mysqli) @mysqli_query($this->link_id, "/*!40101 SET NAMES utf8_general_ci */;");
  75. else @mysql_query("/*!40101 SET NAMES utf8_general_ci */;", $this->link_id);
  76. $this->success = true;
  77. $this->connectfailure = 0;
  78. return true;
  79. }
  80. function set_charset()
  81. {
  82. if ($this->mysqli) @mysqli_query($this->link_id, "/*!40101 SET NAMES utf8_general_ci */;");
  83. else @mysql_query("/*!40101 SET NAMES utf8_general_ci */;", $this->link_id);
  84. }
  85. function get_host_info() {
  86. if ($this->mysqli) return @mysqli_get_host_info($this->link_id);
  87. else return @mysql_get_host_info($this->link_id);
  88. }
  89. function disconnect() {
  90. if ($this->mysqli) mysqli_close($this->link_id);
  91. else mysql_close($this->link_id);
  92. }
  93. #### Queries ####
  94. /**
  95. * If the second parameter is an array, the function uses the array as value list.
  96. * @return unknown_type
  97. */
  98. function qry() {
  99. global $config, $debug;
  100. $this->QueryArgs = func_get_args();
  101. if (is_array($this->QueryArgs[0])) $this->QueryArgs = $this->QueryArgs[0]; // Arguments could be passed als multiple ones, or a single array
  102. $query = array_shift($this->QueryArgs);
  103. #$this->QueryArgs = str_replace('%prefix%', $config['database']['prefix'], $this->QueryArgs);
  104. //if (is_array($this->QueryArgs[0])) $this->QueryArgs = $this->QueryArgs[0];
  105. $query = str_replace('%prefix%', $config['database']['prefix'], $query);
  106. $query = preg_replace_callback('#(%string%|%int%|%plain%)#sUi', array(&$this, 'escape'), $query);
  107. // TODO: Don't replace %prefix% within quotes!
  108. if (isset($debug)) $debug->query_start($query);
  109. if ($this->mysqli) {
  110. $this->query_id = mysqli_query($this->link_id, $query);
  111. $this->sql_error = mysqli_error($this->link_id);
  112. } else {
  113. $this->query_id = mysql_query($query, $this->link_id);
  114. $this->sql_error = mysql_error($this->link_id);
  115. }
  116. if (!$this->query_id) $this->print_error($this->sql_error, $query);
  117. $this->count_query++;
  118. if (isset($debug)) $debug->query_stop($this->sql_error);
  119. $this->QueryArgs = array();
  120. return $this->query_id;
  121. }
  122. function fetch_array($query_id = -1, $save = 1) {
  123. global $func;
  124. if ($query_id != -1) $this->query_id = $query_id;
  125. if ($this->mysqli) $this->record = @mysqli_fetch_array($this->query_id);
  126. else $this->record = @mysql_fetch_array($this->query_id);
  127. if ($save and $this->record) foreach ($this->record as $key => $value) $this->record[$key] = $func->NoHTML($value);
  128. return $this->record;
  129. }
  130. function num_rows($query_id =- 1) {
  131. if ($query_id != -1) $this->query_id=$query_id;
  132. if ($this->mysqli) return @mysqli_num_rows($this->query_id);
  133. else return @mysql_num_rows($this->query_id);
  134. }
  135. function get_affected_rows($query_id =- 1) {
  136. if ($query_id != -1) $this->query_id=$query_id;
  137. if ($this->mysqli) return @mysqli_affected_rows($this->link_id);
  138. else return @mysql_affected_rows($this->link_id);
  139. }
  140. function insert_id($query_id =- 1) {
  141. if ($query_id != -1) $this->query_id=$query_id;
  142. if ($this->mysqli) return @mysqli_insert_id($this->link_id);
  143. return @mysql_insert_id($this->link_id);
  144. }
  145. function num_fields($query_id =- 1) {
  146. if ($query_id != -1) $this->query_id=$query_id;
  147. if ($this->mysqli) return mysqli_num_fields($this->query_id);
  148. else return mysql_num_fields($this->query_id);
  149. }
  150. function field_name($pos, $query_id =- 1) {
  151. if ($query_id !=- 1) $this->query_id=$query_id;
  152. if ($this->mysqli) {
  153. $finfo = mysqli_fetch_field_direct($this->query_id, $pos);
  154. return $finfo->name;
  155. } else return mysql_field_name($this->query_id, $pos);
  156. }
  157. function free_result($query_id = -1) {
  158. if ($query_id != -1) $this->query_id = $query_id;
  159. if ($this->mysqli) return @mysqli_free_result($this->query_id);
  160. else return @mysql_free_result($this->query_id);
  161. }
  162. #### Special ####
  163. /**
  164. * If the second parameter is an array, the function uses the array as value list.
  165. * @return unknown_type
  166. */
  167. function qry_first() {
  168. $this->qry($args = func_get_args());
  169. $row = $this->fetch_array();
  170. $this->free_result();
  171. return $row;
  172. }
  173. function qry_first_rows() {
  174. $this->qry($args = func_get_args());
  175. $row = $this->fetch_array();
  176. $row['number'] = $this->num_rows(); // fieldname "number" is reserved
  177. $this->free_result();
  178. return $row;
  179. }
  180. #### Misc ####
  181. function client_info() {
  182. if ($this->mysqli) {
  183. if (function_exists('mysqli_get_client_info')) return mysqli_get_client_info();
  184. else return false;
  185. } else {
  186. if (function_exists('mysql_get_client_info')) return mysql_get_client_info();
  187. else return false;
  188. }
  189. }
  190. function DisplayErrors() {
  191. global $cfg, $func;
  192. if ($cfg['show_mysql_errors'] and $this->errors) {
  193. $func->error($this->errors);
  194. $this->errors = '';
  195. }
  196. }
  197. function field_exist($table, $field) {
  198. $fields = mysql_list_fields($this->database, $table);
  199. if ($this->mysqli) $columns = mysqli_num_fields($fields);
  200. else $columns = mysql_num_fields($fields);
  201. $found = 0;
  202. for ($i = 0; $i < $columns; $i++) {
  203. if ($this->mysqli) {
  204. if (trim($field) == trim(mysqli_field_name($fields, $i))) $found = 1;
  205. } else {
  206. if (trim($field) == trim(mysql_field_name($fields, $i))) $found = 1;
  207. }
  208. }
  209. return $found;
  210. }
  211. }
  212. ?>