Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
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.

DB.class.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. class DB {
  3. //open a connection to the database. Make sure this is called
  4. //on every page that needs to use the database.
  5. public function connect($CONF) {
  6. $this->connection = mysql_connect($CONF['db_host'], $CONF['db_user'], $CONF['db_pass']);
  7. mysql_select_db($CONF['db_name']);
  8. return true;
  9. }
  10. //takes a mysql row set and returns an associative array, where the keys
  11. //in the array are the column names in the row set. If singleRow is set to
  12. //true, then it will return a single row instead of an array of rows.
  13. public function processRowSet($rowSet, $singleRow=false)
  14. {
  15. $resultArray = array();
  16. while ($row = mysql_fetch_assoc($rowSet))
  17. {
  18. array_push($resultArray, $row);
  19. }
  20. if($singleRow === true)
  21. return $resultArray[0];
  22. return $resultArray;
  23. }
  24. //Select rows from the database.
  25. //returns a full row or rows from $table using $where as the where clause.
  26. //return value is an associative array with column names as keys.
  27. public function select($table, $where, $where_data, $fields = "*") {
  28. //any amendments?
  29. $this->_query("select $fields FROM $table WHERE $where", $where_data);
  30. if (mysql_num_rows($this->dbresult) == 1)
  31. return $this->processRowSet($this->dbresult, true);
  32. return $this->processRowSet($this->dbresult);
  33. }
  34. //Select rows from the database.
  35. //returns a full row or rows from $table using $where as the where clause.
  36. //return value is an associative array with column names as keys.
  37. public function select_raw($table, $args, $args_data, $fields = "*") {
  38. //any amendments?
  39. $this->_query("select $fields FROM $table $args", $args_data);
  40. if (mysql_num_rows($this->dbresult) == 1)
  41. return $this->processRowSet($this->dbresult, true);
  42. return $this->processRowSet($this->dbresult);
  43. }
  44. //Updates a current row in the database.
  45. //takes an array of data, where the keys in the array are the column names
  46. //and the values are the data that will be inserted into those columns.
  47. //$table is the name of the table and $where is the sql where clause.
  48. public function update($data, $table, $where, $where_data) {
  49. foreach ($data as $column => $value) {
  50. $this->_query("UPDATE $table SET $column=? WHERE $where", $value, $where_data);
  51. }
  52. return true;
  53. }
  54. //Deletes row(s) in the database.
  55. //takes an array of data, where the keys in the array are the column names
  56. //and the values are the data that will be inserted into those columns.
  57. //$table is the name of the table and $where is the sql where clause.
  58. public function delete($table, $where, $where_data, $fields = "") {
  59. $this->_query("delete $fields FROM $table WHERE $where", $where_data);
  60. return true;
  61. }
  62. //Inserts a new row into the database.
  63. //takes an array of data, where the keys in the array are the column names
  64. //and the values are the data that will be inserted into those columns.
  65. //$table is the name of the table.
  66. public function insert($data, $table) {
  67. $columns = "";
  68. $placeholders = "";
  69. $values = array();
  70. foreach ($data as $column => $value) {
  71. $columns .= ($columns == "") ? "" : ", ";
  72. $columns .= $column;
  73. $placeholders .= ($placeholders == "") ? "" : ", ";
  74. $placeholders .= "?";
  75. array_push($values, $value);
  76. }
  77. $this->_query("insert into $table ($columns) values ($placeholders)", $values);
  78. //return the ID of the user in the database.
  79. return $this->_get_insert_id();
  80. }
  81. // How many pastes are in the database?
  82. function getPasteCount()
  83. {
  84. $this->_query('select count(*) as cnt from paste');
  85. return $this->_next_record() ? $this->_f('cnt') : 0;
  86. }
  87. // Delete oldest $deletecount pastes from the database.
  88. function trimPastes($deletecount)
  89. {
  90. // Build a one-shot statement to delete old pastes
  91. $sql='delete from paste where pid in (';
  92. $sep='';
  93. $this->_query("select * from paste order by posted asc limit $deletecount");
  94. while ($this->_next_record())
  95. {
  96. $sql.=$sep.$this->_f('pid');
  97. $sep=',';
  98. }
  99. $sql.=')';
  100. // Delete extra pastes.
  101. $this->_query($sql);
  102. }
  103. // Delete all expired pastes.
  104. function deleteExpiredPastes()
  105. {
  106. $this->_query("delete from paste where expires is not null and now() > expires");
  107. }
  108. // Add paste and return ID.
  109. function addPost($title,$format,$code,$parent_pid,$expiry_flag,$password,$user_id)
  110. {
  111. //figure out expiry time
  112. switch ($expiry_flag)
  113. {
  114. case 'd':
  115. $expires="DATE_ADD(NOW(), INTERVAL 1 DAY)";
  116. break;
  117. case 'f':
  118. $expires="NULL";
  119. break;
  120. default:
  121. $expires="DATE_ADD(NOW(), INTERVAL 1 MONTH)";
  122. break;
  123. }
  124. $this->_query('insert into paste (title, posted, format, code, parent_pid, expires, expiry_flag, password, user_id) '.
  125. "values (?, now(), ?, ?, ?, $expires, ?, ?, ?)",
  126. $title,$format,$code,$parent_pid,$expiry_flag,$password,$user_id);
  127. $id=$this->_get_insert_id();
  128. return $id;
  129. }
  130. // Return entire paste row for given ID.
  131. function getPaste($id)
  132. {
  133. $this->_query('select *,date_format(posted, \'%M %a %D %l:%i %p\') as postdate '.'from paste where pid=?', $id);
  134. if ($this->_next_record())
  135. return $this->row;
  136. else
  137. return false;
  138. }
  139. // Return summaries for $count posts ($count=0 means all)
  140. function getRecentPostSummary($count)
  141. {
  142. $limit=$count?"limit $count":"";
  143. $posts=array();
  144. $this->_query("select pid,title,unix_timestamp()-unix_timestamp(posted) as age, ".
  145. "date_format(posted, '%a %D %b %H:%i') as postdate ".
  146. "from paste ".
  147. "order by posted desc, pid desc $limit");
  148. while ($this->_next_record())
  149. {
  150. $posts[]=$this->row;
  151. }
  152. return $posts;
  153. }
  154. // Get follow up posts for a particular post
  155. function getFollowupPosts($pid, $limit=5)
  156. {
  157. //any amendments?
  158. $childposts=array();
  159. $this->_query("select pid,title,".
  160. "date_format(posted, '%a %D %b %H:%i') as postfmt ".
  161. "from paste where parent_pid=? ".
  162. "order by posted limit $limit", $pid);
  163. while ($this->_next_record())
  164. {
  165. $childposts[]=$this->row;
  166. }
  167. return $childposts;
  168. }
  169. // Save formatted code for displaying.
  170. function saveFormatting($pid, $codefmt, $codecss)
  171. {
  172. $this->_query("update paste set codefmt=?,codecss=? where pid=?",
  173. $codefmt, $codecss, $pid);
  174. }
  175. // Execute query - should be regarded as private to insulate the rest ofthe application from sql differences.
  176. function _query($sql)
  177. {
  178. // Been passed more parameters? do some smart replacement.
  179. if (func_num_args() > 1)
  180. {
  181. // Query contains ? placeholders, but it's possible the
  182. // replacement string have ? in too, so we replace them in
  183. // our sql with something more unique
  184. $q=md5(uniqid(rand(), true));
  185. $sql=str_replace('?', $q, $sql);
  186. $args=func_get_args();
  187. for ($i=1; $i<=count($args); $i++)
  188. {
  189. if(isset($args[$i])){
  190. if(is_array($args[$i]))
  191. {
  192. for ($x=0; $x<=count($args[$i]); $x++)
  193. {
  194. $sql=preg_replace("/$q/", "'".preg_quote(mysql_real_escape_string($args[$i][$x]))."'", $sql,1);
  195. }
  196. }
  197. else
  198. {
  199. $sql=preg_replace("/$q/", "'".preg_quote(mysql_real_escape_string($args[$i]))."'", $sql,1);
  200. }
  201. }
  202. }
  203. // We shouldn't have any $q left, but it will help debugging if we change them back!
  204. $sql=str_replace($q, '?', $sql);
  205. }
  206. $this->dbresult=mysql_query($sql, $this->connection);
  207. if (!$this->dbresult)
  208. {
  209. die("Query failure: ".mysql_error()."<br />$sql");
  210. }
  211. return $this->dbresult;
  212. }
  213. // get next record after executing _query.
  214. function _next_record()
  215. {
  216. $this->row=mysql_fetch_array($this->dbresult);
  217. return $this->row!=FALSE;
  218. }
  219. // Get result column $field.
  220. function _f($field)
  221. {
  222. return $this->row[$field];
  223. }
  224. // Get the last insertion ID.
  225. function _get_insert_id()
  226. {
  227. return mysql_insert_id($this->connection);
  228. }
  229. // Get last error.
  230. function get_db_error()
  231. {
  232. return mysql_last_error();
  233. }
  234. }
  235. ?>