blob: 6190043766871ec16a83b0aa42488f4c1fd39723 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
Derek Jonesf4a4bd82011-10-20 12:18:42 -05003 * CodeIgniter
Derek Allard2067d1a2008-11-13 22:59:24 +00004 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev24276a32012-01-08 02:44:38 +02008 *
Andrey Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
Andrey Andreev24276a32012-01-08 02:44:38 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Database Utility Class
42 *
43 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050044 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020045 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000046 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020047abstract class CI_DB_utility {
Derek Allard2067d1a2008-11-13 22:59:24 +000048
Andrey Andreevae85eb42012-11-02 01:42:31 +020049 /**
50 * Database object
51 *
52 * @var object
53 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020054 protected $db;
Derek Allard2067d1a2008-11-13 22:59:24 +000055
Andrey Andreevae85eb42012-11-02 01:42:31 +020056 // --------------------------------------------------------------------
57
58 /**
Andrey Andreevc98e93a2012-11-02 02:04:59 +020059 * List databases statement
60 *
61 * @var string
62 */
Andrey Andreeva24e52e2012-11-02 03:54:12 +020063 protected $_list_databases = FALSE;
Andrey Andreevc98e93a2012-11-02 02:04:59 +020064
65 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +020066 * OPTIMIZE TABLE statement
67 *
Andrey Andreevc98e93a2012-11-02 02:04:59 +020068 * @var string
Andrey Andreevae85eb42012-11-02 01:42:31 +020069 */
Andrey Andreevb457a402012-04-09 16:11:56 +030070 protected $_optimize_table = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +020071
72 /**
73 * REPAIR TABLE statement
74 *
Andrey Andreevc98e93a2012-11-02 02:04:59 +020075 * @var string
Andrey Andreevae85eb42012-11-02 01:42:31 +020076 */
Andrey Andreevb457a402012-04-09 16:11:56 +030077 protected $_repair_table = FALSE;
78
Andrey Andreevae85eb42012-11-02 01:42:31 +020079 // --------------------------------------------------------------------
80
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030081 /**
Andrey Andreevc98e93a2012-11-02 02:04:59 +020082 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030083 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +020084 * @param object &$db Database object
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030085 * @return void
86 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020087 public function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +020089 $this->db =& $db;
Andrey Andreev90726b82015-01-20 12:39:22 +020090 log_message('info', 'Database Utility Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
92
93 // --------------------------------------------------------------------
94
95 /**
96 * List databases
97 *
Andrey Andreevb457a402012-04-09 16:11:56 +030098 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +000099 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200100 public function list_databases()
Barry Mienydd671972010-10-04 16:33:58 +0200101 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 // Is there a cached result?
Andrey Andreev5d281762012-06-11 22:05:40 +0300103 if (isset($this->db->data_cache['db_names']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Andrey Andreev5d281762012-06-11 22:05:40 +0300105 return $this->db->data_cache['db_names'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
Andrey Andreevb457a402012-04-09 16:11:56 +0300107 elseif ($this->_list_databases === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200109 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Andrey Andreev5d281762012-06-11 22:05:40 +0300112 $this->db->data_cache['db_names'] = array();
Andrey Andreevb457a402012-04-09 16:11:56 +0300113
114 $query = $this->db->query($this->_list_databases);
115 if ($query === FALSE)
116 {
Andrey Andreev5d281762012-06-11 22:05:40 +0300117 return $this->db->data_cache['db_names'];
Andrey Andreevb457a402012-04-09 16:11:56 +0300118 }
119
Andrey Andreev9e945762012-11-13 01:08:34 +0200120 for ($i = 0, $query = $query->result_array(), $c = count($query); $i < $c; $i++)
Andrey Andreevb457a402012-04-09 16:11:56 +0300121 {
Andrey Andreev9e945762012-11-13 01:08:34 +0200122 $this->db->data_cache['db_names'][] = current($query[$i]);
Andrey Andreevb457a402012-04-09 16:11:56 +0300123 }
124
Andrey Andreev5d281762012-06-11 22:05:40 +0300125 return $this->db->data_cache['db_names'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
127
128 // --------------------------------------------------------------------
129
130 /**
Derek Allarde7f03252010-02-04 16:47:01 +0000131 * Determine if a particular database exists
132 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200133 * @param string $database_name
Andrey Andreevb457a402012-04-09 16:11:56 +0300134 * @return bool
Derek Allarde7f03252010-02-04 16:47:01 +0000135 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200136 public function database_exists($database_name)
Derek Allard62396232010-02-04 17:45:47 +0000137 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300138 return in_array($database_name, $this->list_databases());
Derek Allarde7f03252010-02-04 16:47:01 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * Optimize Table
145 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200146 * @param string $table_name
Andrey Andreevb457a402012-04-09 16:11:56 +0300147 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200149 public function optimize_table($table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300151 if ($this->_optimize_table === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200153 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Andrey Andreevb457a402012-04-09 16:11:56 +0300156 $query = $this->db->query(sprintf($this->_optimize_table, $this->db->escape_identifiers($table_name)));
157 if ($query !== FALSE)
158 {
159 $query = $query->result_array();
Dimitar34cadee2012-11-24 13:13:07 +0200160 return current($query);
Andrey Andreevb457a402012-04-09 16:11:56 +0300161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Andrey Andreevb457a402012-04-09 16:11:56 +0300163 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Optimize Database
170 *
Andrey Andreevb457a402012-04-09 16:11:56 +0300171 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200173 public function optimize_database()
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300175 if ($this->_optimize_table === FALSE)
176 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200177 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Andrey Andreevb457a402012-04-09 16:11:56 +0300178 }
179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $result = array();
181 foreach ($this->db->list_tables() as $table_name)
182 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300183 $res = $this->db->query(sprintf($this->_optimize_table, $this->db->escape_identifiers($table_name)));
184 if (is_bool($res))
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300186 return $res;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // Build the result array...
Andrey Andreevb457a402012-04-09 16:11:56 +0300190 $res = $res->result_array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 $res = current($res);
192 $key = str_replace($this->db->database.'.', '', current($res));
193 $keys = array_keys($res);
194 unset($res[$keys[0]]);
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 $result[$key] = $res;
197 }
198
199 return $result;
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Repair Table
206 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200207 * @param string $table_name
Andrey Andreevb457a402012-04-09 16:11:56 +0300208 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200210 public function repair_table($table_name)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300212 if ($this->_repair_table === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200214 return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Andrey Andreevb457a402012-04-09 16:11:56 +0300217 $query = $this->db->query(sprintf($this->_repair_table, $this->db->escape_identifiers($table_name)));
218 if (is_bool($query))
219 {
220 return $query;
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Andrey Andreevb457a402012-04-09 16:11:56 +0300223 $query = $query->result_array();
224 return current($query);
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // --------------------------------------------------------------------
228
229 /**
230 * Generate CSV from a query result object
231 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200232 * @param object $query Query result object
233 * @param string $delim Delimiter (default: ,)
234 * @param string $newline Newline character (default: \n)
235 * @param string $enclosure Enclosure (default: ")
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @return string
237 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200238 public function csv_from_result($query, $delim = ',', $newline = "\n", $enclosure = '"')
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Derek Jones47874132009-02-10 17:32:15 +0000240 if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 show_error('You must submit a valid result object');
Barry Mienydd671972010-10-04 16:33:58 +0200243 }
244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // First generate the headings from the table column names
247 foreach ($query->list_fields() as $name)
248 {
249 $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Andrey Andreevb8cd5e62015-05-21 01:10:36 +0300252 $out = substr($out, 0, -strlen($delim)).$newline;
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // Next blast through the result array and build out the rows
Andrey Andreevd06acd82012-05-25 00:29:09 +0300255 while ($row = $query->unbuffered_row('array'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Ahmad Anbar6d6b3b22015-10-10 22:51:54 +0300257 $line = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 foreach ($row as $item)
259 {
Ahmad Anbar6d6b3b22015-10-10 22:51:54 +0300260 $line[] = $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
Ahmad Anbar6d6b3b22015-10-10 22:51:54 +0300262 $out .= implode($delim, $line).$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264
265 return $out;
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // --------------------------------------------------------------------
269
270 /**
271 * Generate XML data from a query result object
272 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200273 * @param object $query Query result object
274 * @param array $params Any preferences
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @return string
276 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200277 public function xml_from_result($query, $params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Pascal Kriete69b1fcc2009-08-24 16:42:52 +0000279 if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 show_error('You must submit a valid result object');
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // Set our default values
285 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
286 {
287 if ( ! isset($params[$key]))
288 {
289 $params[$key] = $val;
290 }
291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // Create variables for convenience
294 extract($params);
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 // Load the xml helper
Andrey Andreev119d8a72014-01-08 15:27:53 +0200297 get_instance()->load->helper('xml');
Derek Allard2067d1a2008-11-13 22:59:24 +0000298
299 // Generate the result
Andrey Andreevb457a402012-04-09 16:11:56 +0300300 $xml = '<'.$root.'>'.$newline;
Andrey Andreevd06acd82012-05-25 00:29:09 +0300301 while ($row = $query->unbuffered_row())
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300303 $xml .= $tab.'<'.$element.'>'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 foreach ($row as $key => $val)
305 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300306 $xml .= $tab.$tab.'<'.$key.'>'.xml_convert($val).'</'.$key.'>'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
Andrey Andreevb457a402012-04-09 16:11:56 +0300308 $xml .= $tab.'</'.$element.'>'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Andrey Andreevb457a402012-04-09 16:11:56 +0300311 return $xml.'</'.$root.'>'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Database Backup
318 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200319 * @param array $params
Gabriel Potkánycea5fb72015-02-04 08:22:06 +0100320 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200322 public function backup($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 // If the parameters have not been submitted as an
325 // array then we know that it is simply the table
326 // name, which is a valid short cut.
327 if (is_string($params))
328 {
329 $params = array('tables' => $params);
330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // Set up our default preferences
333 $prefs = array(
Andrey Andreev61c4d0a2012-12-20 16:29:29 +0200334 'tables' => array(),
335 'ignore' => array(),
336 'filename' => '',
337 'format' => 'gzip', // gzip, zip, txt
338 'add_drop' => TRUE,
339 'add_insert' => TRUE,
340 'newline' => "\n",
341 'foreign_key_checks' => TRUE
342 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000343
344 // Did the user submit any preferences? If so set them....
345 if (count($params) > 0)
346 {
347 foreach ($prefs as $key => $val)
348 {
349 if (isset($params[$key]))
350 {
351 $prefs[$key] = $params[$key];
352 }
353 }
354 }
355
Barry Mienydd671972010-10-04 16:33:58 +0200356 // Are we backing up a complete database or individual tables?
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // If no table names were submitted we'll fetch the entire table list
Andrey Andreev24276a32012-01-08 02:44:38 +0200358 if (count($prefs['tables']) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 $prefs['tables'] = $this->db->list_tables();
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // Validate the format
364 if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
365 {
366 $prefs['format'] = 'txt';
367 }
368
Andrey Andreev24276a32012-01-08 02:44:38 +0200369 // Is the encoder supported? If not, we'll either issue an
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // error or use plain text depending on the debug settings
Andrey Andreev58e1c002014-02-26 18:51:18 +0200371 if (($prefs['format'] === 'gzip' && ! function_exists('gzencode'))
372 OR ($prefs['format'] === 'zip' && ! function_exists('gzcompress')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 if ($this->db->db_debug)
375 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200376 return $this->db->display_error('db_unsupported_compression');
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 $prefs['format'] = 'txt';
380 }
381
Barry Mienydd671972010-10-04 16:33:58 +0200382 // Was a Zip file requested?
Andrey Andreev24276a32012-01-08 02:44:38 +0200383 if ($prefs['format'] === 'zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200385 // Set the filename if not provided (only needed with Zip files)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100386 if ($prefs['filename'] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200388 $prefs['filename'] = (count($prefs['tables']) === 1 ? $prefs['tables'] : $this->db->database)
389 .date('Y-m-d_H-i', time()).'.sql';
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200391 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200393 // If they included the .zip file extension we'll remove it
394 if (preg_match('|.+?\.zip$|', $prefs['filename']))
395 {
396 $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
397 }
398
399 // Tack on the ".sql" file extension if needed
400 if ( ! preg_match('|.+?\.sql$|', $prefs['filename']))
401 {
402 $prefs['filename'] .= '.sql';
403 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405
406 // Load the Zip class and output it
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 $CI =& get_instance();
408 $CI->load->library('zip');
Barry Mienydd671972010-10-04 16:33:58 +0200409 $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 return $CI->zip->get_zip();
411 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100412 elseif ($prefs['format'] === 'txt') // Was a text file requested?
Andrey Andreev24276a32012-01-08 02:44:38 +0200413 {
414 return $this->_backup($prefs);
415 }
416 elseif ($prefs['format'] === 'gzip') // Was a Gzip file requested?
417 {
418 return gzencode($this->_backup($prefs));
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Gabriel Potkány40bbd602015-02-04 14:06:47 +0100421 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423
424}