Advertisement
Guest User

Database.class.php

a guest
Mar 18th, 2021
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.40 KB | None | 0 0
  1. <?php
  2.  
  3. $root = (empty($_SERVER['DOCUMENT_ROOT']) ? getenv("DOCUMENT_ROOT") : $_SERVER['DOCUMENT_ROOT']);
  4. require_once $root.'/config.inc.php';
  5. class Database {
  6.  
  7. var $server = ""; //database server
  8. var $user = ""; //database login name
  9. var $pass = ""; //database login password
  10. var $database = ""; //database name
  11. var $pre = ""; //table prefix
  12. var $showerror = false;
  13. #######################
  14. //internal info
  15. var $error = "";
  16. var $errno = 0;
  17. //number of rows affected by SQL query
  18. var $affected_rows = 0;
  19. var $link_id = 0;
  20. var $query_id = 0;
  21.  
  22. #-#############################################
  23. # desc: constructor
  24.  
  25. function Database($database) {
  26. $root = (empty($_SERVER['DOCUMENT_ROOT']) ? getenv("DOCUMENT_ROOT") : $_SERVER['DOCUMENT_ROOT']);
  27. require_once $root.'/config.inc.php';
  28. if (isset($database) && ($database == "FORUMS")) {
  29. $this->server = DB_FORUMS_SERVER;
  30. $this->user = DB_FORUMS_USER;
  31. $this->pass = DB_FORUMS_PASS;
  32. $this->database = DB_FORUMS_DATABASE;
  33. $this->pre = DB_FORUMS_PREFIX;
  34. } elseif (isset($database) && $database == "LOGS") {
  35. $this->server = DB_LOGS_SERVER;
  36. $this->user = DB_LOGS_USER;
  37. $this->pass = DB_LOGS_PASS;
  38. $this->database = DB_LOGS_DATABASE;
  39. $this->pre = DB_LOGS_PREFIX;
  40. } elseif (isset($database) && $database == "MTA") {
  41. $this->server = DB_SERVER;
  42. $this->user = DB_USER;
  43. $this->pass = DB_PASS;
  44. $this->database = DB_DATABASE;
  45. $this->pre = DB_PREFIX;
  46. }
  47. $this->showerror = DB_SHOW_ERROR;
  48. }
  49.  
  50. #-#constructor()
  51. #-#############################################
  52. # desc: connect and select database using vars above
  53. # Param: $new_link can force connect() to open a new link, even if mysql_connect() was called before with the same parameters
  54.  
  55. function affected_rows() {
  56. return $this->affected_rows;
  57. }
  58.  
  59. function connect($new_link = false) {
  60. // var_dump($this->server);die;
  61. $this->link_id = mysqli_connect($this->server, $this->user, $this->pass, $this->database, $new_link);
  62.  
  63. if (!$this->link_id) {//open failed
  64. $this->oops("Could not connect to server: <b>$this->server</b>.");
  65. }
  66.  
  67. if (!@mysqli_select_db($this->database, $this->link_id)) {//no database
  68. $this->oops("Could not open database: <b>$this->database</b>.");
  69. }
  70.  
  71. // Deprecated but will get us rolling with mysql 5.7 for now.
  72.  
  73. $this->query("SET SESSION sql_mode = ''");
  74.  
  75. // unset the data so it can't be dumped
  76. $this->server = '';
  77. $this->user = '';
  78. $this->pass = '';
  79. $this->database = '';
  80. }
  81.  
  82. #-#connect()
  83. #-#############################################
  84. # desc: close the connection
  85.  
  86. public function close() {
  87. if (!@mysqli_close($this->link_id)) {
  88. $this->oops("Connection close failed.");
  89. }
  90. }
  91.  
  92. #-#close()
  93. #-#############################################
  94. # Desc: escapes characters to be mysql ready
  95. # Param: string
  96. # returns: string
  97.  
  98. public function escape($string) {
  99. if (get_magic_quotes_runtime())
  100. $string = stripslashes($string);
  101. return @mysqli_real_escape_string($string, $this->link_id);
  102. }
  103.  
  104. #-#escape()
  105. #-#############################################
  106. # Desc: executes SQL query to an open connection
  107. # Param: (MySQL query) to execute
  108. # returns: (query_id) for fetching results etc
  109.  
  110. function query($sql) {
  111. // do query
  112. $this->query_id = @mysqli_query($sql, $this->link_id);
  113.  
  114. if (!$this->query_id) {
  115. $this->oops("<b>MySQL Query fail:</b> $sql");
  116. return 0;
  117. }
  118.  
  119. $this->affected_rows = mysqli_affected_rows($this->link_id);
  120.  
  121. return $this->query_id;
  122. }
  123.  
  124. #-#query()
  125. #-#############################################
  126. # desc: fetches and returns results one line at a time
  127. # param: query_id for mysql run. if none specified, last used
  128. # return: (array) fetched record(s)
  129.  
  130. function fetch_array($query_id = -1) {
  131. // retrieve row
  132. if ($query_id != -1) {
  133. $this->query_id = $query_id;
  134. }
  135.  
  136. if (isset($this->query_id)) {
  137. $record = @mysqli_fetch_assoc($this->query_id);
  138. } else {
  139. $this->oops("Invalid query_id: <b>$this->query_id</b>. Records could not be fetched.");
  140. }
  141.  
  142. return $record;
  143. }
  144.  
  145. #-#fetch_array()
  146. #-#############################################
  147. # desc: returns all the results (not one row)
  148. # param: (MySQL query) the query to run on server
  149. # returns: assoc array of ALL fetched results
  150.  
  151. function fetch_all_array($sql) {
  152. $query_id = $this->query($sql);
  153. $out = array();
  154.  
  155. while ($row = $this->fetch_array($query_id)) {
  156. $out[] = $row;
  157. }
  158.  
  159. $this->free_result($query_id);
  160. return $out;
  161. }
  162.  
  163. #-#fetch_all_array()
  164. #-#############################################
  165. # desc: frees the resultset
  166. # param: query_id for mysql run. if none specified, last used
  167.  
  168. function free_result($query_id = -1) {
  169. if ($query_id != -1) {
  170. $this->query_id = $query_id;
  171. }
  172. if ($this->query_id != 0 && !@mysqli_free_result($this->query_id)) {
  173. $this->oops("Result ID: <b>$this->query_id</b> could not be freed.");
  174. }
  175. }
  176.  
  177. #-#free_result()
  178. #-#############################################
  179. # desc: does a query, fetches the first row only, frees resultset
  180. # param: (MySQL query) the query to run on server
  181. # returns: array of fetched results
  182.  
  183. function query_first($query_string) {
  184. $query_id = $this->query($query_string);
  185. $out = $this->fetch_array($query_id);
  186. $this->free_result($query_id);
  187. return $out;
  188. }
  189.  
  190. #-#query_first()
  191. #-#############################################
  192. # desc: does an update query with an array
  193. # param: table (no prefix), assoc array with data (doesn't need escaped), where condition
  194. # returns: (query_id) for fetching results etc
  195.  
  196. function query_update($table, $data, $where = '1') {
  197. $q = "UPDATE `" . $this->pre . $table . "` SET ";
  198.  
  199. foreach ($data as $key => $val) {
  200. if (strtolower($val) == 'null')
  201. $q.= "`$key` = NULL, ";
  202. elseif (strtolower($val) == 'now()')
  203. $q.= "`$key` = NOW(), ";
  204. elseif (preg_match("/^increment\((\-?\d+)\)$/i", $val, $m))
  205. $q.= "`$key` = `$key` + $m[1], ";
  206. else
  207. $q.= "`$key`='" . $this->escape($val) . "', ";
  208. }
  209.  
  210. $q = rtrim($q, ', ') . ' WHERE ' . $where . ';';
  211.  
  212. return $this->query($q);
  213. }
  214.  
  215. #-#query_update()
  216. #-#############################################
  217. # desc: does an insert query with an array
  218. # param: table (no prefix), assoc array with data
  219. # returns: id of inserted record, false if error
  220.  
  221. function query_insert($table, $data) {
  222. $q = "INSERT INTO `" . $this->pre . $table . "` ";
  223. $v = '';
  224. $n = '';
  225. foreach ($data as $key => $val) {
  226. $n.="`$key`, ";
  227. if (strtolower($val) == 'null')
  228. $v.="NULL, ";
  229. elseif (strtolower($val) == 'now()')
  230. $v.="NOW(), ";
  231. else
  232. $v.= "'" . $this->escape($val) . "', ";
  233. }
  234.  
  235. $q .= "(" . rtrim($n, ', ') . ") VALUES (" . rtrim($v, ', ') . ");";
  236. if ($this->query($q)) {
  237. //$this->free_result();
  238. return mysqli_insert_id($this->link_id);
  239. } else {
  240. return false;
  241. }
  242. }
  243.  
  244. #-#query_insert()
  245. #-#############################################
  246. # desc: throw an error message
  247. # param: [optional] any custom error to display
  248.  
  249. function oops($msg = '') {
  250. if ($this->showerror) {
  251. if ($this->link_id > 0 and $this->link_id != 5) {
  252. $this->error = mysqli_error($this->link_id);
  253. $this->errno = mysqli_errno($this->link_id);
  254. } else {
  255. $this->error = mysqli_error();
  256. $this->errno = mysqli_errno();
  257. }
  258. ?>
  259. <table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
  260. <tr><th colspan=2>Database Error</th></tr>
  261. <tr><td align="right" valign="top">Message:</td><td><?php echo $msg; ?></td></tr>
  262. <?php if (!empty($this->error)) echo '<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>' . $this->error . '</td></tr>'; ?>
  263. <tr><td align="right">Date:</td><td><?php echo date("l, F j, Y \a\\t g:i:s A"); ?></td></tr>
  264. <?php if (!empty($_SERVER['REQUEST_URI'])) echo '<tr><td align="right">Script:</td><td><a href="' . $_SERVER['REQUEST_URI'] . '">' . $_SERVER['REQUEST_URI'] . '</a></td></tr>'; ?>
  265. <?php if (!empty($_SERVER['HTTP_REFERER'])) echo '<tr><td align="right">Referer:</td><td><a href="' . $_SERVER['HTTP_REFERER'] . '">' . $_SERVER['HTTP_REFERER'] . '</a></td></tr>'; ?>
  266. </table>
  267. <?php
  268. }
  269. }
  270.  
  271. #-#oops()
  272. }
  273.  
  274. //CLASS Database
  275. ###################################################################################################
  276. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement