Guest User

Untitled

a guest
May 24th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.38 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4. * Null DBW v6 (PHP5 Only)
  5. */
  6.  
  7. abstract class DB //abstract classes cannot be instantiated, must extend
  8. {
  9. /**
  10. * database connection vars
  11. * i need to make it so that these are called from the $GLOBALS array
  12. */
  13. const dbhost = 'localhost'; //database host, eg: localhost
  14. const dbuser = 'root'; //database user, eg: root
  15. const dbpass = 'ishkurisagaywad'; //database pass, eg: changeme
  16. const dbname = 'demo'; //database name, eg: wp_db1
  17.  
  18. /**
  19. * global connection variables
  20. */
  21. private static $dblink = false;
  22. private static $dbconnected = false;
  23.  
  24. /**
  25. * class construct, must be extended and called by parent::
  26. */
  27. public function __construct()
  28. {
  29. try {
  30. self::Connect();
  31. } catch(Exception $e) {
  32. die('Caught Exception: '.$e->getMessage());
  33. }
  34. }
  35.  
  36. /**
  37. * class destruct, do not call directly
  38. */
  39. public function __destruct()
  40. {
  41. try {
  42. self::Disconnect();
  43. } catch(Exception $e) {
  44. die('Caught Exception: '.$e->getMessage());
  45. }
  46. }
  47.  
  48. /**
  49. * connect method, used for instantiating database connections
  50. */
  51. private static function Connect()
  52. {
  53. if(!self::$dbconnected)
  54. {
  55. if((self::$dblink = @mysql_connect(self::dbhost, self::dbuser, self::dbpass)) === false)
  56. throw new Exception('Failed to connect to database server, incorrect connection variables.');
  57. if((@mysql_select_db(self::dbname, self::$dblink)) === false)
  58. throw new Exception('Failed to select database, unknown database name.');
  59. if((@mysql_query()) === false)
  60. throw new Exception('Impossible to use UTF-8 encoding with current database.');
  61. self::$dbconnected = true;
  62. }
  63. }
  64.  
  65. /**
  66. * disconnect method, destroys database connection
  67. */
  68. private static function Disconnect()
  69. {
  70. if(self::$dbconnected === false)
  71. throw new Exception('No database connection present, cannot disconnect.');
  72. @mysql_close(self::$dblink);
  73. }
  74.  
  75. /**
  76. * extendable insert method
  77. */
  78. protected function insert_bdd()
  79. {
  80. $table = eval('return '.get_class($this).'::$_table;');
  81. if(!$table)
  82. throw new Exception('Don\'t know what table to use for '.get_class($this));
  83.  
  84. $fields = eval('return '.get_class($this).'::$_fields;');
  85. if(!$fields)
  86. throw new Exception('Don\'t know what fields describe '.get_class($this));
  87. $tb_fields = array_fill_keys($fields, 1);
  88.  
  89. $txt_fields = '';
  90. $txt_values = '';
  91. $i = 0;
  92.  
  93. foreach($this as $key => $value)
  94. {
  95. if(isset($tb_fields[$key]))
  96. {
  97. if($i > 0)
  98. {
  99. $txt_fields .= ',';
  100. $txt_values .= ',';
  101. }
  102. $txt_fields .= $key;
  103.  
  104. $c = self::quote_smart($value);
  105. if(is_numeric($value))
  106. $txt_values .= $c;
  107. elseif(is_null($value))
  108. $txt_values .= 'NULL';
  109. else
  110. $txt_values .= "'$c'";
  111.  
  112. $i++;
  113. }
  114. }
  115.  
  116. $sql = 'INSERT INTO '.$table.'('.$txt_fields.') VALUES ('.$txt_values.');';
  117. if(@mysql_query($sql, self::$dblink) === false)
  118. throw new Exception('Insertion error:<br />'.$sql.'<br />'.@mysql_error(self::$dblink));
  119. return @mysql_insert_id();
  120. }
  121.  
  122. /**
  123. * extendable update method
  124. */
  125. protected function update_bdd()
  126. {
  127. $table = eval('return '.get_class($this).'::$_table;');
  128. if(!$table)
  129. throw new Exception('Don\'t know what table to use for '.get_class($this));
  130.  
  131. $fields = eval('return '.get_class($this).'::$_fields;');
  132. if(!$fields)
  133. throw new Exception('Don\'t know what fields describe '.get_class($this));
  134. $tb_fields = array_fill_keys($fields, 1);
  135.  
  136. $txt_query = 'UPDATE '.$table;
  137. $i = 0;
  138.  
  139. foreach($this as $key => $value)
  140. {
  141. if(isset($tb_fields[$key]))
  142. {
  143. if($i == 0)
  144. $txt_query .= ' SET ';
  145. else
  146. $txt_query .= ',';
  147.  
  148. $txt_value = self::quote_smart($value);
  149. if(is_null($value))
  150. $txt_query .= "$key = NULL";
  151. else
  152. $txt_query .= "$key = '$txt_value'";
  153. $i++;
  154. }
  155. }
  156.  
  157. $i = 0;
  158. $primaryKey = eval('return '.get_class($this).'::$_primaryKey;');
  159.  
  160. foreach($primaryKey as $key)
  161. {
  162. if($i == 0)
  163. $txt_query .= ' WHERE ';
  164. else
  165. $txt_query .= ' AND ';
  166.  
  167. $c = self::quote_smart($this->$key); // __get()
  168. $txt_query .= "$key = '$c'";
  169. }
  170.  
  171. $txt_query .= ';';
  172. if(@mysql_query($txt_query, self::$dblink) === false)
  173. throw new Exception('Update error:<br />'.$txt_query.'<br />'.@mysql_error(self::$dblink));
  174. if(@mysql_affected_rows(self::$dblink) == 0)
  175. throw new Exception('Update error:<br />'.$txt_query.'<br />0 records affected');
  176. }
  177.  
  178. /**
  179. * extendable delete method
  180. */
  181. protected function delete_bdd()
  182. {
  183. $table = eval('return '.get_class($this).'::$_table;');
  184. if(!$table)
  185. throw new Exception('Don\'t know what table to use for '.get_class($this));
  186.  
  187. $primaryKey = eval('return '.get_class($this).'::$_primaryKey;');
  188. if(!$primaryKey)
  189. throw new Exception('Don\'t know what are primary key fields for '.get_class($this));
  190.  
  191. $t = array();
  192. foreach($primaryKey as $key => $value)
  193. $t[$value] = $this->$value;
  194.  
  195. self::deleteDirectly($table, $primaryKey, $t);
  196. }
  197.  
  198. /**
  199. * initialize an object based on its primary key
  200. */
  201. protected function init_by_primaryKey($Pk)
  202. {
  203. $table = eval('return '.get_class($this).'::$_table;');
  204. if(!$table)
  205. throw new Exception('Don\'t know what table to use for '.get_class($this));
  206.  
  207. $Pkfields = eval('return '.get_class($this).'::$_primaryKey;');
  208. if(!$Pkfields)
  209. throw new Exception('Don\'t know what are primary key fields for '.get_class($this));
  210.  
  211. $Pkfields = array_flip($Pkfields);
  212. if(count(array_intersect_key($Pk, $Pkfields)) != count($Pkfields))
  213. throw new Exception('Primary key fields does not match those of table '.$table);
  214.  
  215. $sql = 'SELECT * FROM '.$table;
  216.  
  217. $i = 0;
  218. foreach($Pk as $key => $value)
  219. {
  220. if($i == 0)
  221. $sql .= ' WHERE ';
  222. else
  223. $sql .= ' AND ';
  224.  
  225. $c = self::quote_smart($value);
  226. $sql .= "$key = '$c'";
  227. }
  228.  
  229. $result = @mysql_query($sql);
  230. if(!$result)
  231. throw new Exception('Invalid request to init by primary key');
  232. if($d = @mysql_fetch_object($result))
  233. {
  234. foreach(get_object_vars($d) as $var => $value)
  235. $this->$var = $value;
  236. }
  237. else
  238. throw new Exception('No record for this primary key');
  239. }
  240.  
  241. /**
  242. * retrieve primary key from database
  243. */
  244. protected function getPrimaryKey()
  245. {
  246. if(!self::$dbconnected)
  247. self::connect();
  248.  
  249. $table = eval('return '.get_class($this).'::$_table;');
  250. $keys = array();
  251.  
  252. $result = @mysql_query('SHOW KEYS FROM '.$table, self::$dblink);
  253. if(!$result)
  254. throw new Exception('Impossible to get primary key(s) of table '.$table);
  255. while($row = @mysql_fetch_assoc($result))
  256. {
  257. if ($row['Key_name'] == 'PRIMARY')
  258. $keys[$row['Seq_in_index'] - 1] = $row['Column_name'];
  259. }
  260.  
  261. return $keys;
  262. }
  263.  
  264. /**
  265. * retrieve fieldnames from database
  266. */
  267. protected function getFields()
  268. {
  269. if(!self::$dbconnected)
  270. self::connect();
  271.  
  272. $table = eval('return '.get_class($this).'::$_table;');
  273. $tb = array();
  274.  
  275. $result = @mysql_query('SHOW COLUMNS FROM '.$table, self::$dblink);
  276. if(!$result)
  277. throw new Exception('Impossible to get information about table '.$table);
  278. while($row = @mysql_fetch_assoc($result))
  279. $tb[] = $row['Field'];
  280.  
  281. return $tb;
  282. }
  283.  
  284. /**
  285. * directly delete an entry
  286. */
  287. protected static function deleteDirectly($table, $Pkfields, $Pk)
  288. {
  289. if(!self::$dbconnected)
  290. self::connect();
  291.  
  292. $Pkfields = array_flip($Pkfields);
  293. if(count(array_intersect_key($Pk, $Pkfields)) != count($Pkfields))
  294. throw new Exception('Primary key fields does not match those of table '.$table);
  295.  
  296. $txt_query = 'DELETE FROM '.$table;
  297. $i = 0;
  298.  
  299. foreach($Pk as $key => $value)
  300. {
  301. if($i == 0)
  302. $txt_query .= ' WHERE ';
  303. else
  304. $txt_query .= ' AND ';
  305.  
  306. $c = self::quote_smart($value);
  307. $txt_query .= "$key='$c'";
  308. }
  309.  
  310. $txt_query .= ';';
  311. if(@mysql_query($txt_query, self::$dblink) === false)
  312. throw new Exception('Delete error:<br />'.$txt_query.'<br />'.@mysql_error(self::$dblink));
  313. if(@mysql_affected_rows(self::$dblink) == 0) // Primary Key does not match any record => exception
  314. throw new Exception('Delete error:<br />'.$txt_query.'<br />0 records affected');
  315. }
  316.  
  317. /**
  318. * retrieve all rows from database
  319. */
  320. protected static function getAll($class, $table)
  321. {
  322. if(!self::$dbconnected)
  323. self::connect();
  324.  
  325. $etu = array();
  326.  
  327. if(($result = @mysql_query('SELECT * FROM '.$table)) === false)
  328. throw new Exception('Impossible to retrieve all items of '.$table);
  329. while($d = @mysql_fetch_object($result))
  330. {
  331. $e = new $class;
  332. foreach(get_object_vars($d) as $var => $value)
  333. $e->$var = $value; // call __set
  334. $etu[] = $e;
  335. }
  336.  
  337. return $etu;
  338. }
  339.  
  340. /**
  341. * $page = page::getWhere(array('PageID' => 125'));
  342. */
  343. protected static function getWhere($class, $table, $where = array())
  344. {
  345. if(!self::$dbconnected)
  346. self::connect();
  347. $etu = array();
  348. //process WHERE statement
  349. $txt_query = 'SELECT * FROM '.$table;
  350. $i = 0;
  351. foreach($where as $key => $value)
  352. {
  353. if($i == 0)
  354. $txt_query .= ' WHERE ';
  355. else
  356. $txt_query .= ' AND ';
  357.  
  358. $txt_value = self::quote_smart($value);
  359. if(is_numeric($value))
  360. $txt_query .= "$key = $value";
  361. elseif(is_null($value))
  362. $txt_query .= "$key = NULL";
  363. else
  364. $txt_query .= "$key = '$txt_value'";
  365. $i++;
  366. }
  367.  
  368. $txt_query .= ';';
  369. if(($result = @mysql_query($txt_query)) === false)
  370. throw new Exception('Impossible to retrieve desired items of '.$table);
  371. while($d = @mysql_fetch_object($result))
  372. {
  373. $e = new $class;
  374. foreach(get_object_vars($d) as $var => $value)
  375. $e->$var = $value;
  376. $etu[] = $e;
  377. }
  378. return $etu;
  379. }
  380.  
  381. /**
  382. * retrieve row count from database
  383. */
  384. protected static function getCount($table)
  385. {
  386. if(!self::$dbconnected)
  387. self::connect();
  388.  
  389. $result = @mysql_query('SELECT COUNT(*) AS nb FROM '.$table);
  390. if(!$result)
  391. throw new Exception('Impossible to count items of '.$table);
  392. $row = @mysql_fetch_assoc($result);
  393. return $row['nb'];
  394. }
  395.  
  396. /**
  397. * quote database queries
  398. */
  399. public static function quote_smart($value)
  400. {
  401. if(get_magic_quotes_gpc())
  402. $value = stripslashes($value);
  403.  
  404. if(!is_numeric($value))
  405. $value = @mysql_real_escape_string($value);
  406.  
  407. return $value;
  408. }
  409. }
  410. ?>
Add Comment
Please, Sign In to add comment