PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/DB_utility.php

https://gitlab.com/ablu/invertika-backup-web
PHP | 389 lines | 195 code | 68 blank | 126 comment | 31 complexity | ce1f630add89bfd8bd7800a5a73ed620 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Code Igniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Database Utility Class
  18. *
  19. * @category Database
  20. * @author ExpressionEngine Dev Team
  21. * @link http://codeigniter.com/user_guide/database/
  22. */
  23. class CI_DB_utility extends CI_DB_forge {
  24. var $db;
  25. var $data_cache = array();
  26. /**
  27. * Constructor
  28. *
  29. * Grabs the CI super object instance so we can access it.
  30. *
  31. */
  32. function CI_DB_utility()
  33. {
  34. // Assign the main database object to $this->db
  35. $CI =& get_instance();
  36. $this->db =& $CI->db;
  37. log_message('debug', "Database Utility Class Initialized");
  38. }
  39. // --------------------------------------------------------------------
  40. /**
  41. * List databases
  42. *
  43. * @access public
  44. * @return bool
  45. */
  46. function list_databases()
  47. {
  48. // Is there a cached result?
  49. if (isset($this->data_cache['db_names']))
  50. {
  51. return $this->data_cache['db_names'];
  52. }
  53. $query = $this->db->query($this->_list_databases());
  54. $dbs = array();
  55. if ($query->num_rows() > 0)
  56. {
  57. foreach ($query->result_array() as $row)
  58. {
  59. $dbs[] = current($row);
  60. }
  61. }
  62. $this->data_cache['db_names'] = $dbs;
  63. return $this->data_cache['db_names'];
  64. }
  65. // --------------------------------------------------------------------
  66. /**
  67. * Optimize Table
  68. *
  69. * @access public
  70. * @param string the table name
  71. * @return bool
  72. */
  73. function optimize_table($table_name)
  74. {
  75. $sql = $this->_optimize_table($table_name);
  76. if (is_bool($sql))
  77. {
  78. show_error('db_must_use_set');
  79. }
  80. $query = $this->db->query($sql);
  81. $res = $query->result_array();
  82. // Note: Due to a bug in current() that affects some versions
  83. // of PHP we can not pass function call directly into it
  84. return current($res);
  85. }
  86. // --------------------------------------------------------------------
  87. /**
  88. * Optimize Database
  89. *
  90. * @access public
  91. * @return array
  92. */
  93. function optimize_database()
  94. {
  95. $result = array();
  96. foreach ($this->db->list_tables() as $table_name)
  97. {
  98. $sql = $this->_optimize_table($table_name);
  99. if (is_bool($sql))
  100. {
  101. return $sql;
  102. }
  103. $query = $this->db->query($sql);
  104. // Build the result array...
  105. // Note: Due to a bug in current() that affects some versions
  106. // of PHP we can not pass function call directly into it
  107. $res = $query->result_array();
  108. $res = current($res);
  109. $key = str_replace($this->db->database.'.', '', current($res));
  110. $keys = array_keys($res);
  111. unset($res[$keys[0]]);
  112. $result[$key] = $res;
  113. }
  114. return $result;
  115. }
  116. // --------------------------------------------------------------------
  117. /**
  118. * Repair Table
  119. *
  120. * @access public
  121. * @param string the table name
  122. * @return bool
  123. */
  124. function repair_table($table_name)
  125. {
  126. $sql = $this->_repair_table($table_name);
  127. if (is_bool($sql))
  128. {
  129. return $sql;
  130. }
  131. $query = $this->db->query($sql);
  132. // Note: Due to a bug in current() that affects some versions
  133. // of PHP we can not pass function call directly into it
  134. $res = $query->result_array();
  135. return current($res);
  136. }
  137. // --------------------------------------------------------------------
  138. /**
  139. * Generate CSV from a query result object
  140. *
  141. * @access public
  142. * @param object The query result object
  143. * @param string The delimiter - comma by default
  144. * @param string The newline character - \n by default
  145. * @param string The enclosure - double quote by default
  146. * @return string
  147. */
  148. function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
  149. {
  150. if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
  151. {
  152. show_error('You must submit a valid result object');
  153. }
  154. $out = '';
  155. // First generate the headings from the table column names
  156. foreach ($query->list_fields() as $name)
  157. {
  158. $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
  159. }
  160. $out = rtrim($out);
  161. $out .= $newline;
  162. // Next blast through the result array and build out the rows
  163. foreach ($query->result_array() as $row)
  164. {
  165. foreach ($row as $item)
  166. {
  167. $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
  168. }
  169. $out = rtrim($out);
  170. $out .= $newline;
  171. }
  172. return $out;
  173. }
  174. // --------------------------------------------------------------------
  175. /**
  176. * Generate XML data from a query result object
  177. *
  178. * @access public
  179. * @param object The query result object
  180. * @param array Any preferences
  181. * @return string
  182. */
  183. function xml_from_result($query, $params = array())
  184. {
  185. if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
  186. {
  187. show_error('You must submit a valid result object');
  188. }
  189. // Set our default values
  190. foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
  191. {
  192. if ( ! isset($params[$key]))
  193. {
  194. $params[$key] = $val;
  195. }
  196. }
  197. // Create variables for convenience
  198. extract($params);
  199. // Load the xml helper
  200. $CI =& get_instance();
  201. $CI->load->helper('xml');
  202. // Generate the result
  203. $xml = "<{$root}>".$newline;
  204. foreach ($query->result_array() as $row)
  205. {
  206. $xml .= $tab."<{$element}>".$newline;
  207. foreach ($row as $key => $val)
  208. {
  209. $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
  210. }
  211. $xml .= $tab."</{$element}>".$newline;
  212. }
  213. $xml .= "</$root>".$newline;
  214. return $xml;
  215. }
  216. // --------------------------------------------------------------------
  217. /**
  218. * Database Backup
  219. *
  220. * @access public
  221. * @return void
  222. */
  223. function backup($params = array())
  224. {
  225. // If the parameters have not been submitted as an
  226. // array then we know that it is simply the table
  227. // name, which is a valid short cut.
  228. if (is_string($params))
  229. {
  230. $params = array('tables' => $params);
  231. }
  232. // ------------------------------------------------------
  233. // Set up our default preferences
  234. $prefs = array(
  235. 'tables' => array(),
  236. 'ignore' => array(),
  237. 'filename' => '',
  238. 'format' => 'gzip', // gzip, zip, txt
  239. 'add_drop' => TRUE,
  240. 'add_insert' => TRUE,
  241. 'newline' => "\n"
  242. );
  243. // Did the user submit any preferences? If so set them....
  244. if (count($params) > 0)
  245. {
  246. foreach ($prefs as $key => $val)
  247. {
  248. if (isset($params[$key]))
  249. {
  250. $prefs[$key] = $params[$key];
  251. }
  252. }
  253. }
  254. // ------------------------------------------------------
  255. // Are we backing up a complete database or individual tables?
  256. // If no table names were submitted we'll fetch the entire table list
  257. if (count($prefs['tables']) == 0)
  258. {
  259. $prefs['tables'] = $this->db->list_tables();
  260. }
  261. // ------------------------------------------------------
  262. // Validate the format
  263. if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
  264. {
  265. $prefs['format'] = 'txt';
  266. }
  267. // ------------------------------------------------------
  268. // Is the encoder supported? If not, we'll either issue an
  269. // error or use plain text depending on the debug settings
  270. if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
  271. OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
  272. {
  273. if ($this->db->db_debug)
  274. {
  275. return $this->db->display_error('db_unsuported_compression');
  276. }
  277. $prefs['format'] = 'txt';
  278. }
  279. // ------------------------------------------------------
  280. // Set the filename if not provided - Only needed with Zip files
  281. if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
  282. {
  283. $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
  284. $prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
  285. }
  286. // ------------------------------------------------------
  287. // Was a Gzip file requested?
  288. if ($prefs['format'] == 'gzip')
  289. {
  290. return gzencode($this->_backup($prefs));
  291. }
  292. // ------------------------------------------------------
  293. // Was a text file requested?
  294. if ($prefs['format'] == 'txt')
  295. {
  296. return $this->_backup($prefs);
  297. }
  298. // ------------------------------------------------------
  299. // Was a Zip file requested?
  300. if ($prefs['format'] == 'zip')
  301. {
  302. // If they included the .zip file extension we'll remove it
  303. if (preg_match("|.+?\.zip$|", $prefs['filename']))
  304. {
  305. $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
  306. }
  307. // Tack on the ".sql" file extension if needed
  308. if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
  309. {
  310. $prefs['filename'] .= '.sql';
  311. }
  312. // Load the Zip class and output it
  313. $CI =& get_instance();
  314. $CI->load->library('zip');
  315. $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
  316. return $CI->zip->get_zip();
  317. }
  318. }
  319. }
  320. /* End of file DB_utility.php */
  321. /* Location: ./system/database/DB_utility.php */