PageRenderTime 70ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/application/core/Database - orig.php

https://gitlab.com/maxauvy/XG_Project_v3
PHP | 338 lines | 217 code | 46 blank | 75 comment | 27 complexity | 157475d36c57e1b18b316f58362dc43c MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * @package XG Project
  4. * @copyright Copyright (c) 2008 - 2015
  5. * @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0
  6. * @since Version 3.0.0
  7. */
  8. if ( ! defined ( 'INSIDE' ) ) { die ( header ( 'location:../../' ) ) ; }
  9. require ( XGP_ROOT . 'application/config/config.php' );
  10. class Database
  11. {
  12. public $_last_query;
  13. private $_connection;
  14. private $_magic_quotes_active;
  15. private $_real_escape_string_exists;
  16. private $_debug;
  17. /**
  18. * __construct()
  19. */
  20. public function __construct()
  21. {
  22. global $debug;
  23. $this->_debug = $debug;
  24. $this->open_connection();
  25. $this->_magic_quotes_active = get_magic_quotes_gpc();
  26. $this->_real_escape_string_exists = function_exists ( "mysql_real_escape_string" );
  27. }
  28. /**
  29. * open_connection();
  30. */
  31. public function open_connection()
  32. {
  33. if ( defined ( 'DB_HOST' ) && defined ( 'DB_USER' ) && defined ( 'DB_PASS' ) && defined ( 'DB_NAME' ) )
  34. {
  35. if ( !$this->try_connection() )
  36. {
  37. if ( !defined ( 'IN_INSTALL' ) )
  38. {
  39. die ( $this->_debug->error ( 'Database connection failed: ' . mysql_error() , "SQL Error" ) );
  40. }
  41. else
  42. {
  43. return FALSE;
  44. }
  45. }
  46. else
  47. {
  48. if ( !$this->try_database() )
  49. {
  50. if ( !defined ( 'IN_INSTALL' ) )
  51. {
  52. die ( $this->_debug->error ( 'Database selection failed: ' . mysql_error() , "SQL Error" ) );
  53. }
  54. else
  55. {
  56. return FALSE;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * open_connection();
  64. */
  65. public function try_connection()
  66. {
  67. $this->_connection = @mysql_connect ( DB_HOST, DB_USER , DB_PASS );
  68. return $this->_connection;
  69. }
  70. /**
  71. * try_database();
  72. */
  73. public function try_database()
  74. {
  75. $db_select = @mysql_select_db ( DB_NAME , $this->_connection );
  76. if ( $db_select )
  77. {
  78. return TRUE;
  79. }
  80. else
  81. {
  82. return FALSE;
  83. }
  84. }
  85. /**
  86. * close_connection();
  87. */
  88. public function close_connection()
  89. {
  90. if ( is_resource ( $this->_connection ) OR is_object ( $this->_connection ) )
  91. {
  92. mysql_close ( $this->_connection );
  93. unset ( $this->_connection );
  94. }
  95. }
  96. /**
  97. * query();
  98. */
  99. public function query ( $sql = FALSE )
  100. {
  101. if ( $sql != FALSE )
  102. {
  103. $this->_last_query = $sql;
  104. $result = @mysql_query ( $sql , $this->_connection );
  105. $this->_confirm_query ( $result );
  106. return $result;
  107. }
  108. return FALSE;
  109. }
  110. /**
  111. * query_fetch();
  112. */
  113. public function query_fetch ( $sql )
  114. {
  115. if ( $sql != FALSE )
  116. {
  117. $this->_last_query = $sql;
  118. $result = @mysql_query ( $sql , $this->_connection );
  119. $this->_confirm_query ( $result );
  120. return $this->fetch_array ( $result );
  121. }
  122. return FALSE;
  123. }
  124. /**
  125. * escape_value();
  126. */
  127. public function escape_value ( $value )
  128. {
  129. if( $this->_real_escape_string_exists )
  130. {
  131. // PHP v4.3.0 or higher
  132. // undo any magic quote effects so mysql_real_escape_string can do the work
  133. if( $this->_magic_quotes_active )
  134. {
  135. $value = stripslashes( $value );
  136. }
  137. $value = mysql_real_escape_string( $value );
  138. }
  139. else
  140. {
  141. // before PHP v4.3.0
  142. // if magic quotes aren't already on then add slashes manually
  143. if( !$this->_magic_quotes_active )
  144. {
  145. $value = addslashes ( $value );
  146. }
  147. // if magic quotes are active, then the slashes already exist
  148. }
  149. return $value;
  150. }
  151. /**
  152. * fetch_array();
  153. */
  154. public function fetch_array ( $result_set )
  155. {
  156. return mysql_fetch_array ( $result_set );
  157. }
  158. /**
  159. * fetch_assoc();
  160. */
  161. public function fetch_assoc ( $result_set )
  162. {
  163. return mysql_fetch_assoc ( $result_set );
  164. }
  165. /**
  166. * fetch_row();
  167. */
  168. public function fetch_row ( $result_set )
  169. {
  170. return mysql_fetch_row ( $result_set );
  171. }
  172. /**
  173. * num_rows();
  174. */
  175. public function num_rows ( $result_set )
  176. {
  177. return mysql_num_rows ( $result_set );
  178. }
  179. /**
  180. * num_fields();
  181. */
  182. public function num_fields ( $result_set )
  183. {
  184. return mysql_num_fields ( $result_set );
  185. }
  186. /**
  187. * insert_id();
  188. */
  189. public function insert_id()
  190. {
  191. // get the last id inserted over the current db connection
  192. return mysql_insert_id ( $this->_connection );
  193. }
  194. /**
  195. * affected_rows();
  196. */
  197. public function affected_rows()
  198. {
  199. return mysql_affected_rows ( $this->_connection );
  200. }
  201. /**
  202. * server_info();
  203. */
  204. public function server_info()
  205. {
  206. return mysql_get_server_info ( $this->_connection );
  207. }
  208. /**
  209. * free_resul();
  210. */
  211. public function free_result ( $result )
  212. {
  213. return mysql_free_result ( $result );
  214. }
  215. /**
  216. * _confirm_query();
  217. */
  218. private function _confirm_query ( $result )
  219. {
  220. if ( !$result )
  221. {
  222. $output = "Database query failed: " . mysql_error();
  223. // uncomment below line when you want to debug your last query
  224. $output .= " Last SQL Query: " . $this->_last_query;
  225. die ( $this->_debug->error ( $output , "SQL Error" ) );
  226. }
  227. // DEBUG LOG
  228. $this->_debug->add ( $this->_last_query );
  229. }
  230. /**
  231. * backup_db();
  232. */
  233. public function backup_db ( $tables = '*' )
  234. {
  235. // GET ALL THE TABLES
  236. if ( $tables == '*' )
  237. {
  238. $tables = array();
  239. $result = $this->query ( 'SHOW TABLES' );
  240. while ( $row = $this->fetch_row ( $result ) )
  241. {
  242. $tables[] = $row[0];
  243. }
  244. }
  245. else
  246. {
  247. $tables = is_array ( $tables ) ? $tables : explode ( ',' , $tables );
  248. }
  249. $return = '';
  250. //CYCLE TROUGHT
  251. foreach ( $tables as $table )
  252. {
  253. $result = $this->query ( 'SELECT * FROM ' . $table );
  254. $num_fields = $this->num_fields ( $result );
  255. $return .= 'DROP TABLE ' . $table . ';';
  256. $row2 = $this->fetch_row ( $this->query ( 'SHOW CREATE TABLE ' . $table ) );
  257. $return .= "\n\n".$row2[1].";\n\n";
  258. for ( $i = 0 ; $i < $num_fields ; $i++ )
  259. {
  260. while ( $row = $this->fetch_row ( $result ) )
  261. {
  262. $return.= 'INSERT INTO '.$table.' VALUES(';
  263. for ( $j = 0 ; $j < $num_fields ; $j++ )
  264. {
  265. $row[$j] = addslashes ( $row[$j] );
  266. $row[$j] = str_replace ( "\n" , "\\n" , $row[$j] );
  267. if ( isset ( $row[$j] ) )
  268. {
  269. $return .= '"'.$row[$j].'"' ;
  270. }
  271. else
  272. {
  273. $return .= '""';
  274. }
  275. if ( $j < ( $num_fields - 1 ) )
  276. {
  277. $return .= ',';
  278. }
  279. }
  280. $return.= ");\n";
  281. }
  282. }
  283. $return.="\n\n\n";
  284. }
  285. // SAVE FILE
  286. $handle = fopen ( XGP_ROOT . BACKUP_PATH . 'db-backup-' . date ( 'Ymd' ) . '-' . time() . '-' . ( sha1 ( implode ( ',' , $tables ) ) ) . '.sql' , 'w+' );
  287. $writed = fwrite ( $handle , $return );
  288. fclose ( $handle );
  289. return $writed;
  290. }
  291. }
  292. /* end of Database.php */