blob: b2dc6da43ce405a1e9ea350851e9041f25c7cc7f [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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 Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * @link http://codeigniter.com/user_guide/database/
46 */
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 Andreev24276a32012-01-08 02:44:38 +020090 log_message('debug', '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 Andreevd327c792013-03-01 16:28:58 +0200252 $out = substr(rtrim($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 {
257 foreach ($row as $item)
258 {
Barry Mienydd671972010-10-04 16:33:58 +0200259 $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
Andrey Andreevd327c792013-03-01 16:28:58 +0200261 $out = substr(rtrim($out), 0, -strlen($delim)).$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
263
264 return $out;
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // --------------------------------------------------------------------
268
269 /**
270 * Generate XML data from a query result object
271 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200272 * @param object $query Query result object
273 * @param array $params Any preferences
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @return string
275 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200276 public function xml_from_result($query, $params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Pascal Kriete69b1fcc2009-08-24 16:42:52 +0000278 if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 show_error('You must submit a valid result object');
281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // Set our default values
284 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
285 {
286 if ( ! isset($params[$key]))
287 {
288 $params[$key] = $val;
289 }
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 // Create variables for convenience
293 extract($params);
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // Load the xml helper
Andrey Andreev119d8a72014-01-08 15:27:53 +0200296 get_instance()->load->helper('xml');
Derek Allard2067d1a2008-11-13 22:59:24 +0000297
298 // Generate the result
Andrey Andreevb457a402012-04-09 16:11:56 +0300299 $xml = '<'.$root.'>'.$newline;
Andrey Andreevd06acd82012-05-25 00:29:09 +0300300 while ($row = $query->unbuffered_row())
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300302 $xml .= $tab.'<'.$element.'>'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 foreach ($row as $key => $val)
304 {
Andrey Andreevb457a402012-04-09 16:11:56 +0300305 $xml .= $tab.$tab.'<'.$key.'>'.xml_convert($val).'</'.$key.'>'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
Andrey Andreevb457a402012-04-09 16:11:56 +0300307 $xml .= $tab.'</'.$element.'>'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Andrey Andreevb457a402012-04-09 16:11:56 +0300310 return $xml.'</'.$root.'>'.$newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Database Backup
317 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200318 * @param array $params
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @return void
320 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200321 public function backup($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 // If the parameters have not been submitted as an
324 // array then we know that it is simply the table
325 // name, which is a valid short cut.
326 if (is_string($params))
327 {
328 $params = array('tables' => $params);
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // Set up our default preferences
332 $prefs = array(
Andrey Andreev61c4d0a2012-12-20 16:29:29 +0200333 'tables' => array(),
334 'ignore' => array(),
335 'filename' => '',
336 'format' => 'gzip', // gzip, zip, txt
337 'add_drop' => TRUE,
338 'add_insert' => TRUE,
339 'newline' => "\n",
340 'foreign_key_checks' => TRUE
341 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000342
343 // Did the user submit any preferences? If so set them....
344 if (count($params) > 0)
345 {
346 foreach ($prefs as $key => $val)
347 {
348 if (isset($params[$key]))
349 {
350 $prefs[$key] = $params[$key];
351 }
352 }
353 }
354
Barry Mienydd671972010-10-04 16:33:58 +0200355 // Are we backing up a complete database or individual tables?
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // If no table names were submitted we'll fetch the entire table list
Andrey Andreev24276a32012-01-08 02:44:38 +0200357 if (count($prefs['tables']) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 $prefs['tables'] = $this->db->list_tables();
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Validate the format
363 if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
364 {
365 $prefs['format'] = 'txt';
366 }
367
Andrey Andreev24276a32012-01-08 02:44:38 +0200368 // Is the encoder supported? If not, we'll either issue an
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // error or use plain text depending on the debug settings
Andrey Andreev58e1c002014-02-26 18:51:18 +0200370 if (($prefs['format'] === 'gzip' && ! function_exists('gzencode'))
371 OR ($prefs['format'] === 'zip' && ! function_exists('gzcompress')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 if ($this->db->db_debug)
374 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200375 return $this->db->display_error('db_unsupported_compression');
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 $prefs['format'] = 'txt';
379 }
380
Barry Mienydd671972010-10-04 16:33:58 +0200381 // Was a Zip file requested?
Andrey Andreev24276a32012-01-08 02:44:38 +0200382 if ($prefs['format'] === 'zip')
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200384 // Set the filename if not provided (only needed with Zip files)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100385 if ($prefs['filename'] === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200387 $prefs['filename'] = (count($prefs['tables']) === 1 ? $prefs['tables'] : $this->db->database)
388 .date('Y-m-d_H-i', time()).'.sql';
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200390 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200392 // If they included the .zip file extension we'll remove it
393 if (preg_match('|.+?\.zip$|', $prefs['filename']))
394 {
395 $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
396 }
397
398 // Tack on the ".sql" file extension if needed
399 if ( ! preg_match('|.+?\.sql$|', $prefs['filename']))
400 {
401 $prefs['filename'] .= '.sql';
402 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404
405 // Load the Zip class and output it
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 $CI =& get_instance();
407 $CI->load->library('zip');
Barry Mienydd671972010-10-04 16:33:58 +0200408 $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 return $CI->zip->get_zip();
410 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100411 elseif ($prefs['format'] === 'txt') // Was a text file requested?
Andrey Andreev24276a32012-01-08 02:44:38 +0200412 {
413 return $this->_backup($prefs);
414 }
415 elseif ($prefs['format'] === 'gzip') // Was a Gzip file requested?
416 {
417 return gzencode($this->_backup($prefs));
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Andrey Andreev24276a32012-01-08 02:44:38 +0200420 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422
423}
424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425/* End of file DB_utility.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400426/* Location: ./system/database/DB_utility.php */