PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/JACKED/MySQL.php

http://poordecisions.googlecode.com/
PHP | 286 lines | 210 code | 35 blank | 41 comment | 23 complexity | b7e03a5af8a99ef48423658834c94505 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. class MySQL extends JACKEDModule{
  3. const moduleName = 'MySQL';
  4. const moduleVersion = 2.0;
  5. const dependencies = '';
  6. const optionalDependencies = '';
  7. private $mysql_link = NULL;
  8. public function __destruct(){
  9. mysql_close($this->mysql_link);
  10. $this->mysql_link = NULL;
  11. }
  12. //LOOK ITS STUFF TO MAKEHAS WORKING
  13. private function isLinkOpen($link = NULL){
  14. $link = $link? $link : $this->mysql_link;
  15. return $this->mysql_link;
  16. }
  17. private function openLink(){
  18. $this->mysql_link = mysql_connect($this->config->db_host, $this->config->db_user, $this->config->db_pass);
  19. if($this->mysql_link){
  20. if(mysql_select_db($this->config->db_name)){
  21. return $this->mysql_link;
  22. }else{
  23. $this->isModuleEnabled = false;
  24. throw new Exception('Could not select the database.');
  25. }
  26. }else{
  27. $this->isModuleEnabled = false;
  28. throw new Exception('Could not connect to the database.');
  29. }
  30. }
  31. private function getLink(){
  32. if($this->isLinkOpen()){
  33. return $this->mysql_link;
  34. }else{
  35. return $this->openLink();
  36. }
  37. }
  38. /////////////////////////////
  39. //actual public mysql stuff//
  40. /////////////////////////////
  41. //maybe handle some better mysql sanitizing later or something
  42. //takes a value, returns a sanitized version of it for mysql
  43. public function sanitize($value, $link = NULL){
  44. $link = $link? $link : $this->getLink();
  45. return mysql_real_escape_string($value, $link);
  46. }
  47. //should probably make a generic paginator function:
  48. //paginator(howmany, page)
  49. ////return the LIMIT string
  50. public function paginator($howMany, $page){
  51. return " LIMIT " . ($howMany * ($page - 1)) . ", " . $howMany;
  52. }
  53. //takes an array of fields, checks against an array of allowed fields,
  54. ////returns a string of csv fields suitable for mysql SELECT
  55. public function getFieldString($fields, $allowedFields = false){
  56. $fieldschecked = array();
  57. if($allowedFields){
  58. if($fields){
  59. foreach($fields as $field){
  60. if(in_array($field, $allowedFields))
  61. $fieldschecked[] = '`' . $field . '`';
  62. }
  63. }
  64. }
  65. if(empty($fieldschecked))
  66. $fieldstring = "*";
  67. else
  68. $fieldstring = implode(", ", $fieldschecked);
  69. return $fieldstring;
  70. }
  71. //SELECT val FROM table WHERE cond
  72. //val is just one field, and you only get the first result
  73. ////default link can be overridden
  74. public function getVal($val, $table, $cond = null, $link = NULL){
  75. $link = $link? $link : $this->getLink();
  76. if(stripos($val, "function:") === 0){
  77. $val = substr($val, 9); //function: ends at 9, lol.
  78. $query = "SELECT " . $val . " FROM `" . $table . "`";
  79. }else
  80. $query = "SELECT `" . $val . "` FROM `" . $table . "`";
  81. if($cond)
  82. $query .= " WHERE " . $cond;
  83. JACKED::debug_dump($query);
  84. $result = mysql_query($query, $link);
  85. $row = mysql_fetch_array($result, MYSQL_NUM);
  86. if($result && mysql_num_rows($result) > 0)
  87. $final = stripslashes($row[0]);
  88. else
  89. $final = false;
  90. mysql_free_result($result);
  91. return $final;
  92. }
  93. //SELECT vals FROM table WHERE cond
  94. ////default link can be overridden
  95. public function getRowVals($vals, $table, $cond, $result_type = MYSQL_BOTH, $link = NULL){
  96. $link = $link? $link : $this->getLink();
  97. $query = "SELECT $vals FROM `" . $table . "` WHERE " . $cond;
  98. JACKED::debug_dump($query);
  99. $result = mysql_query($query, $link);
  100. $row = mysql_fetch_array($result, $result_type);
  101. if($result && mysql_num_rows($result) > 0)
  102. $final = array_map("stripslashes", $row);
  103. else
  104. $final = false;
  105. mysql_free_result($result);
  106. return $final;
  107. }
  108. //SELECT * FROM table WHERE cond
  109. ////default link can be overridden
  110. public function getRow($table, $cond, $result_type = MYSQL_BOTH, $link = NULL){
  111. $link = $link? $link : $this->getLink();
  112. $query = "SELECT * FROM `" . $table . "` WHERE " . $cond;
  113. JACKED::debug_dump($query);
  114. $result = mysql_query($query, $link);
  115. $row = mysql_fetch_array($result, $result_type);
  116. if($result && mysql_num_rows($result) > 0)
  117. $final = array_map("stripslashes", $row);
  118. else
  119. $final = false;
  120. mysql_free_result($result);
  121. return $final;
  122. }
  123. //SELECT vals FROM table WHERE cond
  124. //vals is an array of field names
  125. //returns an array of vals
  126. ////default link can be overridden
  127. public function getAllVals($vals, $table, $cond, $link = NULL){
  128. $link = $link? $link : $this->getLink();
  129. if(is_array($vals)){
  130. $query = "SELECT " . implode(",", $vals) . " FROM `" . $table . "` WHERE " . $cond;
  131. }else{
  132. $query = "SELECT * FROM `" . $table . "` WHERE " . $cond;
  133. }
  134. JACKED::debug_dump($query);
  135. $result = mysql_query($query, $link);
  136. if($result && mysql_num_rows($result) > 0){
  137. $final = array();
  138. if(is_array($vals)){
  139. while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  140. $newrow = array();
  141. foreach($vals as $fieldname){
  142. $newrow[$fieldname] = stripslashes($row[$fieldname]);
  143. }
  144. $final[] = $newrow;
  145. }
  146. }else{
  147. while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  148. $newrow = array();
  149. foreach($row as $fieldname => $value){
  150. $newrow[$fieldname] = stripslashes($row[$fieldname]);
  151. }
  152. $final[] = $newrow;
  153. }
  154. }
  155. }else
  156. $final = false;
  157. mysql_free_result($result);
  158. return $final;
  159. }
  160. //SELECT * FROM table WHERE cond
  161. ////default link can be overridden
  162. ////returns the result
  163. public function getResult($table, $cond, $link = NULL){
  164. $link = $link? $link : $this->getLink();
  165. $query = "SELECT * FROM `" . $table . "` WHERE " . $cond;
  166. JACKED::debug_dump($query);
  167. $result = mysql_query($query, $link);
  168. if($result && mysql_num_rows($result) > 0)
  169. $final = $result;
  170. else
  171. $final = false;
  172. mysql_free_result($result);
  173. return $final;
  174. }
  175. //make does do a query!
  176. public function query($query, $link = NULL){
  177. $link = $link? $link : $this->getLink();
  178. JACKED::debug_dump($query);
  179. $result = mysql_query($query, $link);
  180. if($result && mysql_num_rows($result) > 0)
  181. $final = $result;
  182. else
  183. $final = false;
  184. return $final;
  185. }
  186. //INSERT INTO table (fields) VALUES (values)
  187. ///$data is an associative array where $field=>$value
  188. ////default link can be overridden
  189. ////returns bool whether it worked
  190. public function insertValues($table, $data, $link = NULL){
  191. $link = $link? $link : $this->getLink();
  192. $fields = array();
  193. $values = array();
  194. foreach($data as $field => $value){
  195. $fields[] = $this->sanitize($field, $link);
  196. $values[] = $this->sanitize($value, $link);
  197. }
  198. $query = "INSERT INTO $table (`" . implode($fields, '`, `') . "`) VALUES ('" . implode($values, '\', \'') . "')";
  199. JACKED::debug_dump($query);
  200. $result = mysql_query($query, $link);
  201. $done = ($result)? mysql_insert_id($link) : $result;
  202. mysql_free_result($result);
  203. return $done;
  204. }
  205. //UPDATE table SET field1 = value1, ... fieldn = value1 WHERE cond
  206. ///$data is an associative array where $field=>$value
  207. ////default link can be overridden
  208. ////returns bool whether it worked
  209. public function update($table, $data, $cond, $link = NULL){
  210. $link = $link? $link : $this->getLink();
  211. $fields = array();
  212. $values = array();
  213. foreach($data as $field => $value){
  214. $pairs[] = "`" . $this->sanitize($field, $link) . "` = '" . $this->sanitize($value, $link) . "'";
  215. }
  216. $query = "UPDATE $table SET " . implode($pairs, ', ') . " WHERE " . $cond;
  217. JACKED::debug_dump($query);
  218. $result = mysql_query($query, $link);
  219. $done = $result;
  220. mysql_free_result($result);
  221. return $done;
  222. }
  223. //REPLACE INTO table (field1, ... fieldn) VALUES (value1, ... value1)
  224. ///$data is an associative array where $field=>$value
  225. ////default link can be overridden
  226. ////returns bool whether it worked
  227. public function replace($table, $data, $link = NULL){
  228. $link = $link? $link : $this->getLink();
  229. $fields = array();
  230. $values = array();
  231. foreach($data as $field => $value){
  232. $fields[] = $this->sanitize($field, $link);
  233. $values[] = $this->sanitize($value, $link);
  234. }
  235. $query = "REPLACE INTO $table (`" . implode($fields, '`, `') . "`) VALUES ('" . implode($values, '\', \'') . "')";
  236. JACKED::debug_dump($query);
  237. $result = mysql_query($query, $link);
  238. return $result;
  239. }
  240. //DELETE FROM table WHERE cond
  241. ////default link can be overridden
  242. ////returns bool whether it worked
  243. public function delete($table, $cond, $link = NULL){
  244. $link = $link? $link : $this->getLink();
  245. $query = 'DELETE FROM ' . $this->sanitize($table) . ' WHERE ' . $cond;
  246. JACKED::debug_dump($query);
  247. $result = mysql_query($query, $link);
  248. return $result;
  249. }
  250. }
  251. ?>