includes/acp/acp_database.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acp/acp_database.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,2462 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package acp
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* @package acp
    1.24 +*/
    1.25 +class acp_database
    1.26 +{
    1.27 +	var $u_action;
    1.28 +
    1.29 +	function main($id, $mode)
    1.30 +	{
    1.31 +		global $cache, $db, $user, $auth, $template, $table_prefix;
    1.32 +		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
    1.33 +
    1.34 +		$user->add_lang('acp/database');
    1.35 +
    1.36 +		$this->tpl_name = 'acp_database';
    1.37 +		$this->page_title = 'ACP_DATABASE';
    1.38 +
    1.39 +		$action	= request_var('action', '');
    1.40 +		$submit = (isset($_POST['submit'])) ? true : false;
    1.41 +
    1.42 +		$template->assign_vars(array(
    1.43 +			'MODE'	=> $mode
    1.44 +		));
    1.45 +
    1.46 +		switch ($mode)
    1.47 +		{
    1.48 +			case 'backup':
    1.49 +
    1.50 +				$this->page_title = 'ACP_BACKUP';
    1.51 +
    1.52 +				switch ($action)
    1.53 +				{
    1.54 +					case 'download':
    1.55 +						$type	= request_var('type', '');
    1.56 +						$table	= request_var('table', array(''));
    1.57 +						$format	= request_var('method', '');
    1.58 +						$where	= request_var('where', '');
    1.59 +
    1.60 +						if (!sizeof($table))
    1.61 +						{
    1.62 +							trigger_error($user->lang['TABLE_SELECT_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING);
    1.63 +						}
    1.64 +
    1.65 +						$store = $download = $structure = $schema_data = false;
    1.66 +
    1.67 +						if ($where == 'store_and_download' || $where == 'store')
    1.68 +						{
    1.69 +							$store = true;
    1.70 +						}
    1.71 +
    1.72 +						if ($where == 'store_and_download' || $where == 'download')
    1.73 +						{
    1.74 +							$download = true;
    1.75 +						}
    1.76 +
    1.77 +						if ($type == 'full' || $type == 'structure')
    1.78 +						{
    1.79 +							$structure = true;
    1.80 +						}
    1.81 +
    1.82 +						if ($type == 'full' || $type == 'data')
    1.83 +						{
    1.84 +							$schema_data = true;
    1.85 +						}
    1.86 +
    1.87 +						@set_time_limit(1200);
    1.88 +						@set_time_limit(0);
    1.89 +
    1.90 +						$time = time();
    1.91 +
    1.92 +						$filename = 'backup_' . $time . '_' . unique_id();
    1.93 +						switch ($db->sql_layer)
    1.94 +						{
    1.95 +							case 'mysqli':
    1.96 +							case 'mysql4':
    1.97 +							case 'mysql':
    1.98 +								$extractor = new mysql_extractor($download, $store, $format, $filename, $time);
    1.99 +							break;
   1.100 +
   1.101 +							case 'sqlite':
   1.102 +								$extractor = new sqlite_extractor($download, $store, $format, $filename, $time);
   1.103 +							break;
   1.104 +
   1.105 +							case 'postgres':
   1.106 +								$extractor = new postgres_extractor($download, $store, $format, $filename, $time);
   1.107 +							break;
   1.108 +
   1.109 +							case 'oracle':
   1.110 +								$extractor = new oracle_extractor($download, $store, $format, $filename, $time);
   1.111 +							break;
   1.112 +
   1.113 +							case 'mssql':
   1.114 +							case 'mssql_odbc':
   1.115 +							case 'mssqlnative':
   1.116 +								$extractor = new mssql_extractor($download, $store, $format, $filename, $time);
   1.117 +							break;
   1.118 +
   1.119 +							case 'firebird':
   1.120 +								$extractor = new firebird_extractor($download, $store, $format, $filename, $time);
   1.121 +							break;
   1.122 +						}
   1.123 +
   1.124 +						$extractor->write_start($table_prefix);
   1.125 +
   1.126 +						foreach ($table as $table_name)
   1.127 +						{
   1.128 +							// Get the table structure
   1.129 +							if ($structure)
   1.130 +							{
   1.131 +								$extractor->write_table($table_name);
   1.132 +							}
   1.133 +							else
   1.134 +							{
   1.135 +								// We might wanna empty out all that junk :D
   1.136 +								switch ($db->sql_layer)
   1.137 +								{
   1.138 +									case 'sqlite':
   1.139 +									case 'firebird':
   1.140 +										$extractor->flush('DELETE FROM ' . $table_name . ";\n");
   1.141 +									break;
   1.142 +
   1.143 +									case 'mssql':
   1.144 +									case 'mssql_odbc':
   1.145 +									case 'mssqlnative':
   1.146 +										$extractor->flush('TRUNCATE TABLE ' . $table_name . "GO\n");
   1.147 +									break;
   1.148 +
   1.149 +									case 'oracle':
   1.150 +										$extractor->flush('TRUNCATE TABLE ' . $table_name . "/\n");
   1.151 +									break;
   1.152 +
   1.153 +									default:
   1.154 +										$extractor->flush('TRUNCATE TABLE ' . $table_name . ";\n");
   1.155 +									break;
   1.156 +								}
   1.157 +							}
   1.158 +
   1.159 +							// Data
   1.160 +							if ($schema_data)
   1.161 +							{
   1.162 +								$extractor->write_data($table_name);
   1.163 +							}
   1.164 +						}
   1.165 +
   1.166 +						$extractor->write_end();
   1.167 +
   1.168 +						add_log('admin', 'LOG_DB_BACKUP');
   1.169 +
   1.170 +						if ($download == true)
   1.171 +						{
   1.172 +							exit;
   1.173 +						}
   1.174 +
   1.175 +						trigger_error($user->lang['BACKUP_SUCCESS'] . adm_back_link($this->u_action));
   1.176 +					break;
   1.177 +
   1.178 +					default:
   1.179 +						include($phpbb_root_path . 'includes/functions_install.' . $phpEx);
   1.180 +						$tables = get_tables($db);
   1.181 +						asort($tables);
   1.182 +						foreach ($tables as $table_name)
   1.183 +						{
   1.184 +							if (strlen($table_prefix) === 0 || stripos($table_name, $table_prefix) === 0)
   1.185 +							{
   1.186 +								$template->assign_block_vars('tables', array(
   1.187 +									'TABLE'	=> $table_name
   1.188 +								));
   1.189 +							}
   1.190 +						}
   1.191 +						unset($tables);
   1.192 +
   1.193 +						$template->assign_vars(array(
   1.194 +							'U_ACTION'	=> $this->u_action . '&amp;action=download'
   1.195 +						));
   1.196 +
   1.197 +						$available_methods = array('gzip' => 'zlib', 'bzip2' => 'bz2');
   1.198 +
   1.199 +						foreach ($available_methods as $type => $module)
   1.200 +						{
   1.201 +							if (!@extension_loaded($module))
   1.202 +							{
   1.203 +								continue;
   1.204 +							}
   1.205 +
   1.206 +							$template->assign_block_vars('methods', array(
   1.207 +								'TYPE'	=> $type
   1.208 +							));
   1.209 +						}
   1.210 +
   1.211 +						$template->assign_block_vars('methods', array(
   1.212 +							'TYPE'	=> 'text'
   1.213 +						));
   1.214 +					break;
   1.215 +				}
   1.216 +			break;
   1.217 +
   1.218 +			case 'restore':
   1.219 +
   1.220 +				$this->page_title = 'ACP_RESTORE';
   1.221 +
   1.222 +				switch ($action)
   1.223 +				{
   1.224 +					case 'submit':
   1.225 +						$delete = request_var('delete', '');
   1.226 +						$file = request_var('file', '');
   1.227 +						$download = request_var('download', '');
   1.228 +
   1.229 +						if (!preg_match('#^backup_\d{10,}_[a-z\d]{16}\.(sql(?:\.(?:gz|bz2))?)$#', $file, $matches))
   1.230 +						{
   1.231 +							trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.232 +						}
   1.233 +
   1.234 +						$file_name = $phpbb_root_path . 'store/' . $matches[0];
   1.235 +
   1.236 +						if (!file_exists($file_name) || !is_readable($file_name))
   1.237 +						{
   1.238 +							trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.239 +						}
   1.240 +
   1.241 +						if ($delete)
   1.242 +						{
   1.243 +							if (confirm_box(true))
   1.244 +							{
   1.245 +								unlink($file_name);
   1.246 +								add_log('admin', 'LOG_DB_DELETE');
   1.247 +								trigger_error($user->lang['BACKUP_DELETE'] . adm_back_link($this->u_action));
   1.248 +							}
   1.249 +							else
   1.250 +							{
   1.251 +								confirm_box(false, $user->lang['DELETE_SELECTED_BACKUP'], build_hidden_fields(array('delete' => $delete, 'file' => $file)));
   1.252 +							}
   1.253 +						}
   1.254 +						else if ($download || confirm_box(true))
   1.255 +						{
   1.256 +							if ($download)
   1.257 +							{
   1.258 +								$name = $matches[0];
   1.259 +
   1.260 +								switch ($matches[1])
   1.261 +								{
   1.262 +									case 'sql':
   1.263 +										$mimetype = 'text/x-sql';
   1.264 +									break;
   1.265 +									case 'sql.bz2':
   1.266 +										$mimetype = 'application/x-bzip2';
   1.267 +									break;
   1.268 +									case 'sql.gz':
   1.269 +										$mimetype = 'application/x-gzip';
   1.270 +									break;
   1.271 +								}
   1.272 +
   1.273 +								header('Pragma: no-cache');
   1.274 +								header("Content-Type: $mimetype; name=\"$name\"");
   1.275 +								header("Content-disposition: attachment; filename=$name");
   1.276 +
   1.277 +								@set_time_limit(0);
   1.278 +
   1.279 +								$fp = @fopen($file_name, 'rb');
   1.280 +
   1.281 +								if ($fp !== false)
   1.282 +								{
   1.283 +									while (!feof($fp))
   1.284 +									{
   1.285 +										echo fread($fp, 8192);
   1.286 +									}
   1.287 +									fclose($fp);
   1.288 +								}
   1.289 +
   1.290 +								flush();
   1.291 +								exit;
   1.292 +							}
   1.293 +
   1.294 +							switch ($matches[1])
   1.295 +							{
   1.296 +								case 'sql':
   1.297 +									$fp = fopen($file_name, 'rb');
   1.298 +									$read = 'fread';
   1.299 +									$seek = 'fseek';
   1.300 +									$eof = 'feof';
   1.301 +									$close = 'fclose';
   1.302 +									$fgetd = 'fgetd';
   1.303 +								break;
   1.304 +
   1.305 +								case 'sql.bz2':
   1.306 +									$fp = bzopen($file_name, 'r');
   1.307 +									$read = 'bzread';
   1.308 +									$seek = '';
   1.309 +									$eof = 'feof';
   1.310 +									$close = 'bzclose';
   1.311 +									$fgetd = 'fgetd_seekless';
   1.312 +								break;
   1.313 +
   1.314 +								case 'sql.gz':
   1.315 +									$fp = gzopen($file_name, 'rb');
   1.316 +									$read = 'gzread';
   1.317 +									$seek = 'gzseek';
   1.318 +									$eof = 'gzeof';
   1.319 +									$close = 'gzclose';
   1.320 +									$fgetd = 'fgetd';
   1.321 +								break;
   1.322 +							}
   1.323 +
   1.324 +							switch ($db->sql_layer)
   1.325 +							{
   1.326 +								case 'mysql':
   1.327 +								case 'mysql4':
   1.328 +								case 'mysqli':
   1.329 +								case 'sqlite':
   1.330 +									while (($sql = $fgetd($fp, ";\n", $read, $seek, $eof)) !== false)
   1.331 +									{
   1.332 +										$db->sql_query($sql);
   1.333 +									}
   1.334 +								break;
   1.335 +
   1.336 +								case 'firebird':
   1.337 +									$delim = ";\n";
   1.338 +									while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false)
   1.339 +									{
   1.340 +										$query = trim($sql);
   1.341 +										if (substr($query, 0, 8) === 'SET TERM')
   1.342 +										{
   1.343 +											$delim = $query[9] . "\n";
   1.344 +											continue;
   1.345 +										}
   1.346 +										$db->sql_query($query);
   1.347 +									}
   1.348 +								break;
   1.349 +
   1.350 +								case 'postgres':
   1.351 +									$delim = ";\n";
   1.352 +									while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false)
   1.353 +									{
   1.354 +										$query = trim($sql);
   1.355 +
   1.356 +										if (substr($query, 0, 13) == 'CREATE DOMAIN')
   1.357 +										{
   1.358 +											list(, , $domain) = explode(' ', $query);
   1.359 +											$sql = "SELECT domain_name
   1.360 +												FROM information_schema.domains
   1.361 +												WHERE domain_name = '$domain';";
   1.362 +											$result = $db->sql_query($sql);
   1.363 +											if (!$db->sql_fetchrow($result))
   1.364 +											{
   1.365 +												$db->sql_query($query);
   1.366 +											}
   1.367 +											$db->sql_freeresult($result);
   1.368 +										}
   1.369 +										else
   1.370 +										{
   1.371 +											$db->sql_query($query);
   1.372 +										}
   1.373 +
   1.374 +										if (substr($query, 0, 4) == 'COPY')
   1.375 +										{
   1.376 +											while (($sub = $fgetd($fp, "\n", $read, $seek, $eof)) !== '\.')
   1.377 +											{
   1.378 +												if ($sub === false)
   1.379 +												{
   1.380 +													trigger_error($user->lang['RESTORE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.381 +												}
   1.382 +												pg_put_line($db->db_connect_id, $sub . "\n");
   1.383 +											}
   1.384 +											pg_put_line($db->db_connect_id, "\\.\n");
   1.385 +											pg_end_copy($db->db_connect_id);
   1.386 +										}
   1.387 +									}
   1.388 +								break;
   1.389 +
   1.390 +								case 'oracle':
   1.391 +									while (($sql = $fgetd($fp, "/\n", $read, $seek, $eof)) !== false)
   1.392 +									{
   1.393 +										$db->sql_query($sql);
   1.394 +									}
   1.395 +								break;
   1.396 +
   1.397 +								case 'mssql':
   1.398 +								case 'mssql_odbc':
   1.399 +								case 'mssqlnative':
   1.400 +									while (($sql = $fgetd($fp, "GO\n", $read, $seek, $eof)) !== false)
   1.401 +									{
   1.402 +										$db->sql_query($sql);
   1.403 +									}
   1.404 +								break;
   1.405 +							}
   1.406 +
   1.407 +							$close($fp);
   1.408 +
   1.409 +							// Purge the cache due to updated data
   1.410 +							$cache->purge();
   1.411 +
   1.412 +							add_log('admin', 'LOG_DB_RESTORE');
   1.413 +							trigger_error($user->lang['RESTORE_SUCCESS'] . adm_back_link($this->u_action));
   1.414 +							break;
   1.415 +						}
   1.416 +						else if (!$download)
   1.417 +						{
   1.418 +							confirm_box(false, $user->lang['RESTORE_SELECTED_BACKUP'], build_hidden_fields(array('file' => $file)));
   1.419 +						}
   1.420 +
   1.421 +					default:
   1.422 +						$methods = array('sql');
   1.423 +						$available_methods = array('sql.gz' => 'zlib', 'sql.bz2' => 'bz2');
   1.424 +
   1.425 +						foreach ($available_methods as $type => $module)
   1.426 +						{
   1.427 +							if (!@extension_loaded($module))
   1.428 +							{
   1.429 +								continue;
   1.430 +							}
   1.431 +							$methods[] = $type;
   1.432 +						}
   1.433 +
   1.434 +						$dir = $phpbb_root_path . 'store/';
   1.435 +						$dh = @opendir($dir);
   1.436 +
   1.437 +						$backup_files = array();
   1.438 +
   1.439 +						if ($dh)
   1.440 +						{
   1.441 +							while (($file = readdir($dh)) !== false)
   1.442 +							{
   1.443 +								if (preg_match('#^backup_(\d{10,})_[a-z\d]{16}\.(sql(?:\.(?:gz|bz2))?)$#', $file, $matches))
   1.444 +								{
   1.445 +									if (in_array($matches[2], $methods))
   1.446 +									{
   1.447 +										$backup_files[(int) $matches[1]] = $file;
   1.448 +									}
   1.449 +								}
   1.450 +							}
   1.451 +							closedir($dh);
   1.452 +						}
   1.453 +
   1.454 +						if (!empty($backup_files))
   1.455 +						{
   1.456 +							krsort($backup_files);
   1.457 +
   1.458 +							foreach ($backup_files as $name => $file)
   1.459 +							{
   1.460 +								$template->assign_block_vars('files', array(
   1.461 +									'FILE'		=> $file,
   1.462 +									'NAME'		=> $user->format_date($name, 'd-m-Y H:i:s', true),
   1.463 +									'SUPPORTED'	=> true,
   1.464 +								));
   1.465 +							}
   1.466 +						}
   1.467 +
   1.468 +						$template->assign_vars(array(
   1.469 +							'U_ACTION'	=> $this->u_action . '&amp;action=submit'
   1.470 +						));
   1.471 +					break;
   1.472 +				}
   1.473 +			break;
   1.474 +		}
   1.475 +	}
   1.476 +}
   1.477 +
   1.478 +/**
   1.479 +* @package acp
   1.480 +*/
   1.481 +class base_extractor
   1.482 +{
   1.483 +	var $fh;
   1.484 +	var $fp;
   1.485 +	var $write;
   1.486 +	var $close;
   1.487 +	var $store;
   1.488 +	var $download;
   1.489 +	var $time;
   1.490 +	var $format;
   1.491 +	var $run_comp = false;
   1.492 +
   1.493 +	function base_extractor($download = false, $store = false, $format, $filename, $time)
   1.494 +	{
   1.495 +		$this->download = $download;
   1.496 +		$this->store = $store;
   1.497 +		$this->time = $time;
   1.498 +		$this->format = $format;
   1.499 +
   1.500 +		switch ($format)
   1.501 +		{
   1.502 +			case 'text':
   1.503 +				$ext = '.sql';
   1.504 +				$open = 'fopen';
   1.505 +				$this->write = 'fwrite';
   1.506 +				$this->close = 'fclose';
   1.507 +				$mimetype = 'text/x-sql';
   1.508 +			break;
   1.509 +			case 'bzip2':
   1.510 +				$ext = '.sql.bz2';
   1.511 +				$open = 'bzopen';
   1.512 +				$this->write = 'bzwrite';
   1.513 +				$this->close = 'bzclose';
   1.514 +				$mimetype = 'application/x-bzip2';
   1.515 +			break;
   1.516 +			case 'gzip':
   1.517 +				$ext = '.sql.gz';
   1.518 +				$open = 'gzopen';
   1.519 +				$this->write = 'gzwrite';
   1.520 +				$this->close = 'gzclose';
   1.521 +				$mimetype = 'application/x-gzip';
   1.522 +			break;
   1.523 +		}
   1.524 +
   1.525 +		if ($download == true)
   1.526 +		{
   1.527 +			$name = $filename . $ext;
   1.528 +			header('Pragma: no-cache');
   1.529 +			header("Content-Type: $mimetype; name=\"$name\"");
   1.530 +			header("Content-disposition: attachment; filename=$name");
   1.531 +
   1.532 +			switch ($format)
   1.533 +			{
   1.534 +				case 'bzip2':
   1.535 +					ob_start();
   1.536 +				break;
   1.537 +
   1.538 +				case 'gzip':
   1.539 +					if ((isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) && strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'msie') === false)
   1.540 +					{
   1.541 +						ob_start('ob_gzhandler');
   1.542 +					}
   1.543 +					else
   1.544 +					{
   1.545 +						$this->run_comp = true;
   1.546 +					}
   1.547 +				break;
   1.548 +			}
   1.549 +		}
   1.550 +
   1.551 +		if ($store == true)
   1.552 +		{
   1.553 +			global $phpbb_root_path;
   1.554 +			$file = $phpbb_root_path . 'store/' . $filename . $ext;
   1.555 +
   1.556 +			$this->fp = $open($file, 'w');
   1.557 +
   1.558 +			if (!$this->fp)
   1.559 +			{
   1.560 +				trigger_error('FILE_WRITE_FAIL', E_USER_ERROR);
   1.561 +			}
   1.562 +		}
   1.563 +	}
   1.564 +
   1.565 +	function write_end()
   1.566 +	{
   1.567 +		static $close;
   1.568 +
   1.569 +		if ($this->store)
   1.570 +		{
   1.571 +			if ($close === null)
   1.572 +			{
   1.573 +				$close = $this->close;
   1.574 +			}
   1.575 +			$close($this->fp);
   1.576 +		}
   1.577 +
   1.578 +		// bzip2 must be written all the way at the end
   1.579 +		if ($this->download && $this->format === 'bzip2')
   1.580 +		{
   1.581 +			$c = ob_get_clean();
   1.582 +			echo bzcompress($c);
   1.583 +		}
   1.584 +	}
   1.585 +
   1.586 +	function flush($data)
   1.587 +	{
   1.588 +		static $write;
   1.589 +		if ($this->store === true)
   1.590 +		{
   1.591 +			if ($write === null)
   1.592 +			{
   1.593 +				$write = $this->write;
   1.594 +			}
   1.595 +			$write($this->fp, $data);
   1.596 +		}
   1.597 +
   1.598 +		if ($this->download === true)
   1.599 +		{
   1.600 +			if ($this->format === 'bzip2' || $this->format === 'text' || ($this->format === 'gzip' && !$this->run_comp))
   1.601 +			{
   1.602 +				echo $data;
   1.603 +			}
   1.604 +
   1.605 +			// we can write the gzip data as soon as we get it
   1.606 +			if ($this->format === 'gzip')
   1.607 +			{
   1.608 +				if ($this->run_comp)
   1.609 +				{
   1.610 +					echo gzencode($data);
   1.611 +				}
   1.612 +				else
   1.613 +				{
   1.614 +					ob_flush();
   1.615 +					flush();
   1.616 +				}
   1.617 +			}
   1.618 +		}
   1.619 +	}
   1.620 +}
   1.621 +
   1.622 +/**
   1.623 +* @package acp
   1.624 +*/
   1.625 +class mysql_extractor extends base_extractor
   1.626 +{
   1.627 +	function write_start($table_prefix)
   1.628 +	{
   1.629 +		$sql_data = "#\n";
   1.630 +		$sql_data .= "# phpBB Backup Script\n";
   1.631 +		$sql_data .= "# Dump of tables for $table_prefix\n";
   1.632 +		$sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
   1.633 +		$sql_data .= "#\n";
   1.634 +		$this->flush($sql_data);
   1.635 +	}
   1.636 +
   1.637 +	function write_table($table_name)
   1.638 +	{
   1.639 +		global $db;
   1.640 +		static $new_extract;
   1.641 +
   1.642 +		if ($new_extract === null)
   1.643 +		{
   1.644 +			if ($db->sql_layer === 'mysqli' || version_compare($db->sql_server_info(true), '3.23.20', '>='))
   1.645 +			{
   1.646 +				$new_extract = true;
   1.647 +			}
   1.648 +			else
   1.649 +			{
   1.650 +				$new_extract = false;
   1.651 +			}
   1.652 +		}
   1.653 +
   1.654 +		if ($new_extract)
   1.655 +		{
   1.656 +			$this->new_write_table($table_name);
   1.657 +		}
   1.658 +		else
   1.659 +		{
   1.660 +			$this->old_write_table($table_name);
   1.661 +		}
   1.662 +	}
   1.663 +
   1.664 +	function write_data($table_name)
   1.665 +	{
   1.666 +		global $db;
   1.667 +		if ($db->sql_layer === 'mysqli')
   1.668 +		{
   1.669 +			$this->write_data_mysqli($table_name);
   1.670 +		}
   1.671 +		else
   1.672 +		{
   1.673 +			$this->write_data_mysql($table_name);
   1.674 +		}
   1.675 +	}
   1.676 +
   1.677 +	function write_data_mysqli($table_name)
   1.678 +	{
   1.679 +		global $db;
   1.680 +		$sql = "SELECT *
   1.681 +			FROM $table_name";
   1.682 +		$result = mysqli_query($db->db_connect_id, $sql, MYSQLI_USE_RESULT);
   1.683 +		if ($result != false)
   1.684 +		{
   1.685 +			$fields_cnt = mysqli_num_fields($result);
   1.686 +
   1.687 +			// Get field information
   1.688 +			$field = mysqli_fetch_fields($result);
   1.689 +			$field_set = array();
   1.690 +
   1.691 +			for ($j = 0; $j < $fields_cnt; $j++)
   1.692 +			{
   1.693 +				$field_set[] = $field[$j]->name;
   1.694 +			}
   1.695 +
   1.696 +			$search			= array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
   1.697 +			$replace		= array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
   1.698 +			$fields			= implode(', ', $field_set);
   1.699 +			$sql_data		= 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
   1.700 +			$first_set		= true;
   1.701 +			$query_len		= 0;
   1.702 +			$max_len		= get_usable_memory();
   1.703 +
   1.704 +			while ($row = mysqli_fetch_row($result))
   1.705 +			{
   1.706 +				$values	= array();
   1.707 +				if ($first_set)
   1.708 +				{
   1.709 +					$query = $sql_data . '(';
   1.710 +				}
   1.711 +				else
   1.712 +				{
   1.713 +					$query  .= ',(';
   1.714 +				}
   1.715 +
   1.716 +				for ($j = 0; $j < $fields_cnt; $j++)
   1.717 +				{
   1.718 +					if (!isset($row[$j]) || is_null($row[$j]))
   1.719 +					{
   1.720 +						$values[$j] = 'NULL';
   1.721 +					}
   1.722 +					else if (($field[$j]->flags & 32768) && !($field[$j]->flags & 1024))
   1.723 +					{
   1.724 +						$values[$j] = $row[$j];
   1.725 +					}
   1.726 +					else
   1.727 +					{
   1.728 +						$values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
   1.729 +					}
   1.730 +				}
   1.731 +				$query .= implode(', ', $values) . ')';
   1.732 +
   1.733 +				$query_len += strlen($query);
   1.734 +				if ($query_len > $max_len)
   1.735 +				{
   1.736 +					$this->flush($query . ";\n\n");
   1.737 +					$query = '';
   1.738 +					$query_len = 0;
   1.739 +					$first_set = true;
   1.740 +				}
   1.741 +				else
   1.742 +				{
   1.743 +					$first_set = false;
   1.744 +				}
   1.745 +			}
   1.746 +			mysqli_free_result($result);
   1.747 +
   1.748 +			// check to make sure we have nothing left to flush
   1.749 +			if (!$first_set && $query)
   1.750 +			{
   1.751 +				$this->flush($query . ";\n\n");
   1.752 +			}
   1.753 +		}
   1.754 +	}
   1.755 +
   1.756 +	function write_data_mysql($table_name)
   1.757 +	{
   1.758 +		global $db;
   1.759 +		$sql = "SELECT *
   1.760 +			FROM $table_name";
   1.761 +		$result = mysql_unbuffered_query($sql, $db->db_connect_id);
   1.762 +
   1.763 +		if ($result != false)
   1.764 +		{
   1.765 +			$fields_cnt = mysql_num_fields($result);
   1.766 +
   1.767 +			// Get field information
   1.768 +			$field = array();
   1.769 +			for ($i = 0; $i < $fields_cnt; $i++)
   1.770 +			{
   1.771 +				$field[] = mysql_fetch_field($result, $i);
   1.772 +			}
   1.773 +			$field_set = array();
   1.774 +
   1.775 +			for ($j = 0; $j < $fields_cnt; $j++)
   1.776 +			{
   1.777 +				$field_set[] = $field[$j]->name;
   1.778 +			}
   1.779 +
   1.780 +			$search			= array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
   1.781 +			$replace		= array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
   1.782 +			$fields			= implode(', ', $field_set);
   1.783 +			$sql_data		= 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
   1.784 +			$first_set		= true;
   1.785 +			$query_len		= 0;
   1.786 +			$max_len		= get_usable_memory();
   1.787 +
   1.788 +			while ($row = mysql_fetch_row($result))
   1.789 +			{
   1.790 +				$values = array();
   1.791 +				if ($first_set)
   1.792 +				{
   1.793 +					$query = $sql_data . '(';
   1.794 +				}
   1.795 +				else
   1.796 +				{
   1.797 +					$query  .= ',(';
   1.798 +				}
   1.799 +
   1.800 +				for ($j = 0; $j < $fields_cnt; $j++)
   1.801 +				{
   1.802 +					if (!isset($row[$j]) || is_null($row[$j]))
   1.803 +					{
   1.804 +						$values[$j] = 'NULL';
   1.805 +					}
   1.806 +					else if ($field[$j]->numeric && ($field[$j]->type !== 'timestamp'))
   1.807 +					{
   1.808 +						$values[$j] = $row[$j];
   1.809 +					}
   1.810 +					else
   1.811 +					{
   1.812 +						$values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
   1.813 +					}
   1.814 +				}
   1.815 +				$query .= implode(', ', $values) . ')';
   1.816 +
   1.817 +				$query_len += strlen($query);
   1.818 +				if ($query_len > $max_len)
   1.819 +				{
   1.820 +					$this->flush($query . ";\n\n");
   1.821 +					$query = '';
   1.822 +					$query_len = 0;
   1.823 +					$first_set = true;
   1.824 +				}
   1.825 +				else
   1.826 +				{
   1.827 +					$first_set = false;
   1.828 +				}
   1.829 +			}
   1.830 +			mysql_free_result($result);
   1.831 +
   1.832 +			// check to make sure we have nothing left to flush
   1.833 +			if (!$first_set && $query)
   1.834 +			{
   1.835 +				$this->flush($query . ";\n\n");
   1.836 +			}
   1.837 +		}
   1.838 +	}
   1.839 +
   1.840 +	function new_write_table($table_name)
   1.841 +	{
   1.842 +		global $db;
   1.843 +
   1.844 +		$sql = 'SHOW CREATE TABLE ' . $table_name;
   1.845 +		$result = $db->sql_query($sql);
   1.846 +		$row = $db->sql_fetchrow($result);
   1.847 +
   1.848 +		$sql_data = '# Table: ' . $table_name . "\n";
   1.849 +		$sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
   1.850 +		$this->flush($sql_data . $row['Create Table'] . ";\n\n");
   1.851 +
   1.852 +		$db->sql_freeresult($result);
   1.853 +	}
   1.854 +
   1.855 +	function old_write_table($table_name)
   1.856 +	{
   1.857 +		global $db;
   1.858 +
   1.859 +		$sql_data = '# Table: ' . $table_name . "\n";
   1.860 +		$sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
   1.861 +		$sql_data .= "CREATE TABLE $table_name(\n";
   1.862 +		$rows = array();
   1.863 +
   1.864 +		$sql = "SHOW FIELDS
   1.865 +			FROM $table_name";
   1.866 +		$result = $db->sql_query($sql);
   1.867 +
   1.868 +		while ($row = $db->sql_fetchrow($result))
   1.869 +		{
   1.870 +			$line = '   ' . $row['Field'] . ' ' . $row['Type'];
   1.871 +
   1.872 +			if (!is_null($row['Default']))
   1.873 +			{
   1.874 +				$line .= " DEFAULT '{$row['Default']}'";
   1.875 +			}
   1.876 +
   1.877 +			if ($row['Null'] != 'YES')
   1.878 +			{
   1.879 +				$line .= ' NOT NULL';
   1.880 +			}
   1.881 +
   1.882 +			if ($row['Extra'] != '')
   1.883 +			{
   1.884 +				$line .= ' ' . $row['Extra'];
   1.885 +			}
   1.886 +
   1.887 +			$rows[] = $line;
   1.888 +		}
   1.889 +		$db->sql_freeresult($result);
   1.890 +
   1.891 +		$sql = "SHOW KEYS
   1.892 +			FROM $table_name";
   1.893 +
   1.894 +		$result = $db->sql_query($sql);
   1.895 +
   1.896 +		$index = array();
   1.897 +		while ($row = $db->sql_fetchrow($result))
   1.898 +		{
   1.899 +			$kname = $row['Key_name'];
   1.900 +
   1.901 +			if ($kname != 'PRIMARY')
   1.902 +			{
   1.903 +				if ($row['Non_unique'] == 0)
   1.904 +				{
   1.905 +					$kname = "UNIQUE|$kname";
   1.906 +				}
   1.907 +			}
   1.908 +
   1.909 +			if ($row['Sub_part'])
   1.910 +			{
   1.911 +				$row['Column_name'] .= '(' . $row['Sub_part'] . ')';
   1.912 +			}
   1.913 +			$index[$kname][] = $row['Column_name'];
   1.914 +		}
   1.915 +		$db->sql_freeresult($result);
   1.916 +
   1.917 +		foreach ($index as $key => $columns)
   1.918 +		{
   1.919 +			$line = '   ';
   1.920 +
   1.921 +			if ($key == 'PRIMARY')
   1.922 +			{
   1.923 +				$line .= 'PRIMARY KEY (' . implode(', ', $columns) . ')';
   1.924 +			}
   1.925 +			else if (strpos($key, 'UNIQUE') === 0)
   1.926 +			{
   1.927 +				$line .= 'UNIQUE ' . substr($key, 7) . ' (' . implode(', ', $columns) . ')';
   1.928 +			}
   1.929 +			else if (strpos($key, 'FULLTEXT') === 0)
   1.930 +			{
   1.931 +				$line .= 'FULLTEXT ' . substr($key, 9) . ' (' . implode(', ', $columns) . ')';
   1.932 +			}
   1.933 +			else
   1.934 +			{
   1.935 +				$line .= "KEY $key (" . implode(', ', $columns) . ')';
   1.936 +			}
   1.937 +
   1.938 +			$rows[] = $line;
   1.939 +		}
   1.940 +
   1.941 +		$sql_data .= implode(",\n", $rows);
   1.942 +		$sql_data .= "\n);\n\n";
   1.943 +
   1.944 +		$this->flush($sql_data);
   1.945 +	}
   1.946 +}
   1.947 +
   1.948 +/**
   1.949 +* @package acp
   1.950 +*/
   1.951 +class sqlite_extractor extends base_extractor
   1.952 +{
   1.953 +	function write_start($prefix)
   1.954 +	{
   1.955 +		$sql_data = "--\n";
   1.956 +		$sql_data .= "-- phpBB Backup Script\n";
   1.957 +		$sql_data .= "-- Dump of tables for $prefix\n";
   1.958 +		$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
   1.959 +		$sql_data .= "--\n";
   1.960 +		$sql_data .= "BEGIN TRANSACTION;\n";
   1.961 +		$this->flush($sql_data);
   1.962 +	}
   1.963 +
   1.964 +	function write_table($table_name)
   1.965 +	{
   1.966 +		global $db;
   1.967 +		$sql_data = '-- Table: ' . $table_name . "\n";
   1.968 +		$sql_data .= "DROP TABLE $table_name;\n";
   1.969 +
   1.970 +		$sql = "SELECT sql
   1.971 +			FROM sqlite_master
   1.972 +			WHERE type = 'table'
   1.973 +				AND name = '" . $db->sql_escape($table_name) . "'
   1.974 +			ORDER BY type DESC, name;";
   1.975 +		$result = $db->sql_query($sql);
   1.976 +		$row = $db->sql_fetchrow($result);
   1.977 +		$db->sql_freeresult($result);
   1.978 +
   1.979 +		// Create Table
   1.980 +		$sql_data .= $row['sql'] . ";\n";
   1.981 +
   1.982 +		$result = $db->sql_query("PRAGMA index_list('" . $db->sql_escape($table_name) . "');");
   1.983 +
   1.984 +		$ar = array();
   1.985 +		while ($row = $db->sql_fetchrow($result))
   1.986 +		{
   1.987 +			$ar[] = $row;
   1.988 +		}
   1.989 +		$db->sql_freeresult($result);
   1.990 +
   1.991 +		foreach ($ar as $value)
   1.992 +		{
   1.993 +			if (strpos($value['name'], 'autoindex') !== false)
   1.994 +			{
   1.995 +				continue;
   1.996 +			}
   1.997 +
   1.998 +			$result = $db->sql_query("PRAGMA index_info('" . $db->sql_escape($value['name']) . "');");
   1.999 +
  1.1000 +			$fields = array();
  1.1001 +			while ($row = $db->sql_fetchrow($result))
  1.1002 +			{
  1.1003 +				$fields[] = $row['name'];
  1.1004 +			}
  1.1005 +			$db->sql_freeresult($result);
  1.1006 +
  1.1007 +			$sql_data .= 'CREATE ' . ($value['unique'] ? 'UNIQUE ' : '') . 'INDEX ' . $value['name'] . ' on ' . $table_name . ' (' . implode(', ', $fields) . ");\n";
  1.1008 +		}
  1.1009 +
  1.1010 +		$this->flush($sql_data . "\n");
  1.1011 +	}
  1.1012 +
  1.1013 +	function write_data($table_name)
  1.1014 +	{
  1.1015 +		global $db;
  1.1016 +		static $proper;
  1.1017 +
  1.1018 +		if (is_null($proper))
  1.1019 +		{
  1.1020 +			$proper = version_compare(PHP_VERSION, '5.1.3', '>=');
  1.1021 +		}
  1.1022 +
  1.1023 +		if ($proper)
  1.1024 +		{
  1.1025 +			$col_types = sqlite_fetch_column_types($db->db_connect_id, $table_name);
  1.1026 +		}
  1.1027 +		else
  1.1028 +		{
  1.1029 +			$sql = "SELECT sql
  1.1030 +				FROM sqlite_master
  1.1031 +				WHERE type = 'table'
  1.1032 +					AND name = '" . $table_name . "'";
  1.1033 +			$table_data = sqlite_single_query($db->db_connect_id, $sql);
  1.1034 +			$table_data = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', '', $table_data);
  1.1035 +			$table_data = trim($table_data);
  1.1036 +
  1.1037 +			preg_match('#\((.*)\)#s', $table_data, $matches);
  1.1038 +
  1.1039 +			$table_cols = explode(',', trim($matches[1]));
  1.1040 +			foreach ($table_cols as $declaration)
  1.1041 +			{
  1.1042 +				$entities = preg_split('#\s+#', trim($declaration));
  1.1043 +				$column_name = preg_replace('/"?([^"]+)"?/', '\1', $entities[0]);
  1.1044 +
  1.1045 +				// Hit a primary key, those are not what we need :D
  1.1046 +				if (empty($entities[1]) || (strtolower($entities[0]) === 'primary' && strtolower($entities[1]) === 'key'))
  1.1047 +				{
  1.1048 +					continue;
  1.1049 +				}
  1.1050 +				$col_types[$column_name] = $entities[1];
  1.1051 +			}
  1.1052 +		}
  1.1053 +
  1.1054 +		$sql = "SELECT *
  1.1055 +			FROM $table_name";
  1.1056 +		$result = sqlite_unbuffered_query($db->db_connect_id, $sql);
  1.1057 +		$rows = sqlite_fetch_all($result, SQLITE_ASSOC);
  1.1058 +		$sql_insert = 'INSERT INTO ' . $table_name . ' (' . implode(', ', array_keys($col_types)) . ') VALUES (';
  1.1059 +		foreach ($rows as $row)
  1.1060 +		{
  1.1061 +			foreach ($row as $column_name => $column_data)
  1.1062 +			{
  1.1063 +				if (is_null($column_data))
  1.1064 +				{
  1.1065 +					$row[$column_name] = 'NULL';
  1.1066 +				}
  1.1067 +				else if ($column_data == '')
  1.1068 +				{
  1.1069 +					$row[$column_name] = "''";
  1.1070 +				}
  1.1071 +				else if (strpos($col_types[$column_name], 'text') !== false || strpos($col_types[$column_name], 'char') !== false || strpos($col_types[$column_name], 'blob') !== false)
  1.1072 +				{
  1.1073 +					$row[$column_name] = sanitize_data_generic(str_replace("'", "''", $column_data));
  1.1074 +				}
  1.1075 +			}
  1.1076 +			$this->flush($sql_insert . implode(', ', $row) . ");\n");
  1.1077 +		}
  1.1078 +	}
  1.1079 +
  1.1080 +	function write_end()
  1.1081 +	{
  1.1082 +		$this->flush("COMMIT;\n");
  1.1083 +		parent::write_end();
  1.1084 +	}
  1.1085 +}
  1.1086 +
  1.1087 +/**
  1.1088 +* @package acp
  1.1089 +*/
  1.1090 +class postgres_extractor extends base_extractor
  1.1091 +{
  1.1092 +	function write_start($prefix)
  1.1093 +	{
  1.1094 +		$sql_data = "--\n";
  1.1095 +		$sql_data .= "-- phpBB Backup Script\n";
  1.1096 +		$sql_data .= "-- Dump of tables for $prefix\n";
  1.1097 +		$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
  1.1098 +		$sql_data .= "--\n";
  1.1099 +		$sql_data .= "BEGIN TRANSACTION;\n";
  1.1100 +		$this->flush($sql_data);
  1.1101 +	}
  1.1102 +
  1.1103 +	function write_table($table_name)
  1.1104 +	{
  1.1105 +		global $db;
  1.1106 +		static $domains_created = array();
  1.1107 +
  1.1108 +		$sql = "SELECT a.domain_name, a.data_type, a.character_maximum_length, a.domain_default
  1.1109 +			FROM INFORMATION_SCHEMA.domains a, INFORMATION_SCHEMA.column_domain_usage b
  1.1110 +			WHERE a.domain_name = b.domain_name
  1.1111 +				AND b.table_name = '{$table_name}'";
  1.1112 +		$result = $db->sql_query($sql);
  1.1113 +		while ($row = $db->sql_fetchrow($result))
  1.1114 +		{
  1.1115 +			if (empty($domains_created[$row['domain_name']]))
  1.1116 +			{
  1.1117 +				$domains_created[$row['domain_name']] = true;
  1.1118 +				//$sql_data = "DROP DOMAIN {$row['domain_name']};\n";
  1.1119 +				$sql_data = "CREATE DOMAIN {$row['domain_name']} as {$row['data_type']}";
  1.1120 +				if (!empty($row['character_maximum_length']))
  1.1121 +				{
  1.1122 +					$sql_data .= '(' . $row['character_maximum_length'] . ')';
  1.1123 +				}
  1.1124 +				$sql_data .= ' NOT NULL';
  1.1125 +				if (!empty($row['domain_default']))
  1.1126 +				{
  1.1127 +					$sql_data .= ' DEFAULT ' . $row['domain_default'];
  1.1128 +				}
  1.1129 +				$this->flush($sql_data . ";\n");
  1.1130 +			}
  1.1131 +		}
  1.1132 +
  1.1133 +		$sql_data = '-- Table: ' . $table_name . "\n";
  1.1134 +		$sql_data .= "DROP TABLE $table_name;\n";
  1.1135 +		// PGSQL does not "tightly" bind sequences and tables, we must guess...
  1.1136 +		$sql = "SELECT relname
  1.1137 +			FROM pg_class
  1.1138 +			WHERE relkind = 'S'
  1.1139 +				AND relname = '{$table_name}_seq'";
  1.1140 +		$result = $db->sql_query($sql);
  1.1141 +		// We don't even care about storing the results. We already know the answer if we get rows back.
  1.1142 +		if ($db->sql_fetchrow($result))
  1.1143 +		{
  1.1144 +			$sql_data .= "DROP SEQUENCE {$table_name}_seq;\n";
  1.1145 +			$sql_data .= "CREATE SEQUENCE {$table_name}_seq;\n";
  1.1146 +		}
  1.1147 +		$db->sql_freeresult($result);
  1.1148 +
  1.1149 +		$field_query = "SELECT a.attnum, a.attname as field, t.typname as type, a.attlen as length, a.atttypmod as lengthvar, a.attnotnull as notnull
  1.1150 +			FROM pg_class c, pg_attribute a, pg_type t
  1.1151 +			WHERE c.relname = '" . $db->sql_escape($table_name) . "'
  1.1152 +				AND a.attnum > 0
  1.1153 +				AND a.attrelid = c.oid
  1.1154 +				AND a.atttypid = t.oid
  1.1155 +			ORDER BY a.attnum";
  1.1156 +		$result = $db->sql_query($field_query);
  1.1157 +
  1.1158 +		$sql_data .= "CREATE TABLE $table_name(\n";
  1.1159 +		$lines = array();
  1.1160 +		while ($row = $db->sql_fetchrow($result))
  1.1161 +		{
  1.1162 +			// Get the data from the table
  1.1163 +			$sql_get_default = "SELECT pg_get_expr(d.adbin, d.adrelid) as rowdefault
  1.1164 +				FROM pg_attrdef d, pg_class c
  1.1165 +				WHERE (c.relname = '" . $db->sql_escape($table_name) . "')
  1.1166 +					AND (c.oid = d.adrelid)
  1.1167 +					AND d.adnum = " . $row['attnum'];
  1.1168 +			$def_res = $db->sql_query($sql_get_default);
  1.1169 +			$def_row = $db->sql_fetchrow($def_res);
  1.1170 +			$db->sql_freeresult($def_res);
  1.1171 +
  1.1172 +			if (empty($def_row))
  1.1173 +			{
  1.1174 +				unset($row['rowdefault']);
  1.1175 +			}
  1.1176 +			else
  1.1177 +			{
  1.1178 +				$row['rowdefault'] = $def_row['rowdefault'];
  1.1179 +			}
  1.1180 +
  1.1181 +			if ($row['type'] == 'bpchar')
  1.1182 +			{
  1.1183 +				// Internally stored as bpchar, but isn't accepted in a CREATE TABLE statement.
  1.1184 +				$row['type'] = 'char';
  1.1185 +			}
  1.1186 +
  1.1187 +			$line = '  ' . $row['field'] . ' ' . $row['type'];
  1.1188 +
  1.1189 +			if (strpos($row['type'], 'char') !== false)
  1.1190 +			{
  1.1191 +				if ($row['lengthvar'] > 0)
  1.1192 +				{
  1.1193 +					$line .= '(' . ($row['lengthvar'] - 4) . ')';
  1.1194 +				}
  1.1195 +			}
  1.1196 +
  1.1197 +			if (strpos($row['type'], 'numeric') !== false)
  1.1198 +			{
  1.1199 +				$line .= '(';
  1.1200 +				$line .= sprintf("%s,%s", (($row['lengthvar'] >> 16) & 0xffff), (($row['lengthvar'] - 4) & 0xffff));
  1.1201 +				$line .= ')';
  1.1202 +			}
  1.1203 +
  1.1204 +			if (isset($row['rowdefault']))
  1.1205 +			{
  1.1206 +				$line .= ' DEFAULT ' . $row['rowdefault'];
  1.1207 +			}
  1.1208 +
  1.1209 +			if ($row['notnull'] == 't')
  1.1210 +			{
  1.1211 +				$line .= ' NOT NULL';
  1.1212 +			}
  1.1213 +
  1.1214 +			$lines[] = $line;
  1.1215 +		}
  1.1216 +		$db->sql_freeresult($result);
  1.1217 +
  1.1218 +
  1.1219 +		// Get the listing of primary keys.
  1.1220 +		$sql_pri_keys = "SELECT ic.relname as index_name, bc.relname as tab_name, ta.attname as column_name, i.indisunique as unique_key, i.indisprimary as primary_key
  1.1221 +			FROM pg_class bc, pg_class ic, pg_index i, pg_attribute ta, pg_attribute ia
  1.1222 +			WHERE (bc.oid = i.indrelid)
  1.1223 +				AND (ic.oid = i.indexrelid)
  1.1224 +				AND (ia.attrelid = i.indexrelid)
  1.1225 +				AND	(ta.attrelid = bc.oid)
  1.1226 +				AND (bc.relname = '" . $db->sql_escape($table_name) . "')
  1.1227 +				AND (ta.attrelid = i.indrelid)
  1.1228 +				AND (ta.attnum = i.indkey[ia.attnum-1])
  1.1229 +			ORDER BY index_name, tab_name, column_name";
  1.1230 +
  1.1231 +		$result = $db->sql_query($sql_pri_keys);
  1.1232 +
  1.1233 +		$index_create = $index_rows = $primary_key = array();
  1.1234 +
  1.1235 +		// We do this in two steps. It makes placing the comma easier
  1.1236 +		while ($row = $db->sql_fetchrow($result))
  1.1237 +		{
  1.1238 +			if ($row['primary_key'] == 't')
  1.1239 +			{
  1.1240 +				$primary_key[] = $row['column_name'];
  1.1241 +				$primary_key_name = $row['index_name'];
  1.1242 +			}
  1.1243 +			else
  1.1244 +			{
  1.1245 +				// We have to store this all this info because it is possible to have a multi-column key...
  1.1246 +				// we can loop through it again and build the statement
  1.1247 +				$index_rows[$row['index_name']]['table'] = $table_name;
  1.1248 +				$index_rows[$row['index_name']]['unique'] = ($row['unique_key'] == 't') ? true : false;
  1.1249 +				$index_rows[$row['index_name']]['column_names'][] = $row['column_name'];
  1.1250 +			}
  1.1251 +		}
  1.1252 +		$db->sql_freeresult($result);
  1.1253 +
  1.1254 +		if (!empty($index_rows))
  1.1255 +		{
  1.1256 +			foreach ($index_rows as $idx_name => $props)
  1.1257 +			{
  1.1258 +				$index_create[] = 'CREATE ' . ($props['unique'] ? 'UNIQUE ' : '') . "INDEX $idx_name ON $table_name (" . implode(', ', $props['column_names']) . ");";
  1.1259 +			}
  1.1260 +		}
  1.1261 +
  1.1262 +		if (!empty($primary_key))
  1.1263 +		{
  1.1264 +			$lines[] = "  CONSTRAINT $primary_key_name PRIMARY KEY (" . implode(', ', $primary_key) . ")";
  1.1265 +		}
  1.1266 +
  1.1267 +		// Generate constraint clauses for CHECK constraints
  1.1268 +		$sql_checks = "SELECT conname as index_name, consrc
  1.1269 +			FROM pg_constraint, pg_class bc
  1.1270 +			WHERE conrelid = bc.oid
  1.1271 +				AND bc.relname = '" . $db->sql_escape($table_name) . "'
  1.1272 +				AND NOT EXISTS (
  1.1273 +					SELECT *
  1.1274 +						FROM pg_constraint as c, pg_inherits as i
  1.1275 +						WHERE i.inhrelid = pg_constraint.conrelid
  1.1276 +							AND c.conname = pg_constraint.conname
  1.1277 +							AND c.consrc = pg_constraint.consrc
  1.1278 +							AND c.conrelid = i.inhparent
  1.1279 +				)";
  1.1280 +		$result = $db->sql_query($sql_checks);
  1.1281 +
  1.1282 +		// Add the constraints to the sql file.
  1.1283 +		while ($row = $db->sql_fetchrow($result))
  1.1284 +		{
  1.1285 +			if (!is_null($row['consrc']))
  1.1286 +			{
  1.1287 +				$lines[] = '  CONSTRAINT ' . $row['index_name'] . ' CHECK ' . $row['consrc'];
  1.1288 +			}
  1.1289 +		}
  1.1290 +		$db->sql_freeresult($result);
  1.1291 +
  1.1292 +		$sql_data .= implode(", \n", $lines);
  1.1293 +		$sql_data .= "\n);\n";
  1.1294 +
  1.1295 +		if (!empty($index_create))
  1.1296 +		{
  1.1297 +			$sql_data .= implode("\n", $index_create) . "\n\n";
  1.1298 +		}
  1.1299 +		$this->flush($sql_data);
  1.1300 +	}
  1.1301 +
  1.1302 +	function write_data($table_name)
  1.1303 +	{
  1.1304 +		global $db;
  1.1305 +		// Grab all of the data from current table.
  1.1306 +		$sql = "SELECT *
  1.1307 +			FROM $table_name";
  1.1308 +		$result = $db->sql_query($sql);
  1.1309 +
  1.1310 +		$i_num_fields = pg_num_fields($result);
  1.1311 +		$seq = '';
  1.1312 +
  1.1313 +		for ($i = 0; $i < $i_num_fields; $i++)
  1.1314 +		{
  1.1315 +			$ary_type[] = pg_field_type($result, $i);
  1.1316 +			$ary_name[] = pg_field_name($result, $i);
  1.1317 +
  1.1318 +
  1.1319 +			$sql = "SELECT pg_get_expr(d.adbin, d.adrelid) as rowdefault
  1.1320 +				FROM pg_attrdef d, pg_class c
  1.1321 +				WHERE (c.relname = '{$table_name}')
  1.1322 +					AND (c.oid = d.adrelid)
  1.1323 +					AND d.adnum = " . strval($i + 1);
  1.1324 +			$result2 = $db->sql_query($sql);
  1.1325 +			if ($row = $db->sql_fetchrow($result2))
  1.1326 +			{
  1.1327 +				// Determine if we must reset the sequences
  1.1328 +				if (strpos($row['rowdefault'], "nextval('") === 0)
  1.1329 +				{
  1.1330 +					$seq .= "SELECT SETVAL('{$table_name}_seq',(select case when max({$ary_name[$i]})>0 then max({$ary_name[$i]})+1 else 1 end FROM {$table_name}));\n";
  1.1331 +				}
  1.1332 +			}
  1.1333 +		}
  1.1334 +
  1.1335 +		$this->flush("COPY $table_name (" . implode(', ', $ary_name) . ') FROM stdin;' . "\n");
  1.1336 +		while ($row = $db->sql_fetchrow($result))
  1.1337 +		{
  1.1338 +			$schema_vals = array();
  1.1339 +
  1.1340 +			// Build the SQL statement to recreate the data.
  1.1341 +			for ($i = 0; $i < $i_num_fields; $i++)
  1.1342 +			{
  1.1343 +				$str_val = $row[$ary_name[$i]];
  1.1344 +
  1.1345 +				if (preg_match('#char|text|bool|bytea#i', $ary_type[$i]))
  1.1346 +				{
  1.1347 +					$str_val = str_replace(array("\n", "\t", "\r", "\b", "\f", "\v"), array('\n', '\t', '\r', '\b', '\f', '\v'), addslashes($str_val));
  1.1348 +					$str_empty = '';
  1.1349 +				}
  1.1350 +				else
  1.1351 +				{
  1.1352 +					$str_empty = '\N';
  1.1353 +				}
  1.1354 +
  1.1355 +				if (empty($str_val) && $str_val !== '0')
  1.1356 +				{
  1.1357 +					$str_val = $str_empty;
  1.1358 +				}
  1.1359 +
  1.1360 +				$schema_vals[] = $str_val;
  1.1361 +			}
  1.1362 +
  1.1363 +			// Take the ordered fields and their associated data and build it
  1.1364 +			// into a valid sql statement to recreate that field in the data.
  1.1365 +			$this->flush(implode("\t", $schema_vals) . "\n");
  1.1366 +		}
  1.1367 +		$db->sql_freeresult($result);
  1.1368 +		$this->flush("\\.\n");
  1.1369 +
  1.1370 +		// Write out the sequence statements
  1.1371 +		$this->flush($seq);
  1.1372 +	}
  1.1373 +
  1.1374 +	function write_end()
  1.1375 +	{
  1.1376 +		$this->flush("COMMIT;\n");
  1.1377 +		parent::write_end();
  1.1378 +	}
  1.1379 +}
  1.1380 +
  1.1381 +/**
  1.1382 +* @package acp
  1.1383 +*/
  1.1384 +class mssql_extractor extends base_extractor
  1.1385 +{
  1.1386 +	function write_end()
  1.1387 +	{
  1.1388 +		$this->flush("COMMIT\nGO\n");
  1.1389 +		parent::write_end();
  1.1390 +	}
  1.1391 +
  1.1392 +	function write_start($prefix)
  1.1393 +	{
  1.1394 +		$sql_data = "--\n";
  1.1395 +		$sql_data .= "-- phpBB Backup Script\n";
  1.1396 +		$sql_data .= "-- Dump of tables for $prefix\n";
  1.1397 +		$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
  1.1398 +		$sql_data .= "--\n";
  1.1399 +		$sql_data .= "BEGIN TRANSACTION\n";
  1.1400 +		$sql_data .= "GO\n";
  1.1401 +		$this->flush($sql_data);
  1.1402 +	}
  1.1403 +
  1.1404 +	function write_table($table_name)
  1.1405 +	{
  1.1406 +		global $db;
  1.1407 +		$sql_data = '-- Table: ' . $table_name . "\n";
  1.1408 +		$sql_data .= "IF OBJECT_ID(N'$table_name', N'U') IS NOT NULL\n";
  1.1409 +		$sql_data .= "DROP TABLE $table_name;\n";
  1.1410 +		$sql_data .= "GO\n";
  1.1411 +		$sql_data .= "\nCREATE TABLE [$table_name] (\n";
  1.1412 +		$rows = array();
  1.1413 +
  1.1414 +		$text_flag = false;
  1.1415 +
  1.1416 +		$sql = "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') as IS_IDENTITY
  1.1417 +			FROM INFORMATION_SCHEMA.COLUMNS
  1.1418 +			WHERE TABLE_NAME = '$table_name'";
  1.1419 +		$result = $db->sql_query($sql);
  1.1420 +
  1.1421 +		while ($row = $db->sql_fetchrow($result))
  1.1422 +		{
  1.1423 +			$line = "\t[{$row['COLUMN_NAME']}] [{$row['DATA_TYPE']}]";
  1.1424 +
  1.1425 +			if ($row['DATA_TYPE'] == 'text')
  1.1426 +			{
  1.1427 +				$text_flag = true;
  1.1428 +			}
  1.1429 +
  1.1430 +			if ($row['IS_IDENTITY'])
  1.1431 +			{
  1.1432 +				$line .= ' IDENTITY (1 , 1)';
  1.1433 +			}
  1.1434 +
  1.1435 +			if ($row['CHARACTER_MAXIMUM_LENGTH'] && $row['DATA_TYPE'] !== 'text')
  1.1436 +			{
  1.1437 +				$line .= ' (' . $row['CHARACTER_MAXIMUM_LENGTH'] . ')';
  1.1438 +			}
  1.1439 +
  1.1440 +			if ($row['IS_NULLABLE'] == 'YES')
  1.1441 +			{
  1.1442 +				$line .= ' NULL';
  1.1443 +			}
  1.1444 +			else
  1.1445 +			{
  1.1446 +				$line .= ' NOT NULL';
  1.1447 +			}
  1.1448 +
  1.1449 +			if ($row['COLUMN_DEFAULT'])
  1.1450 +			{
  1.1451 +				$line .= ' DEFAULT ' . $row['COLUMN_DEFAULT'];
  1.1452 +			}
  1.1453 +
  1.1454 +			$rows[] = $line;
  1.1455 +		}
  1.1456 +		$db->sql_freeresult($result);
  1.1457 +
  1.1458 +		$sql_data .= implode(",\n", $rows);
  1.1459 +		$sql_data .= "\n) ON [PRIMARY]";
  1.1460 +
  1.1461 +		if ($text_flag)
  1.1462 +		{
  1.1463 +			$sql_data .= " TEXTIMAGE_ON [PRIMARY]";
  1.1464 +		}
  1.1465 +
  1.1466 +		$sql_data .= "\nGO\n\n";
  1.1467 +		$rows = array();
  1.1468 +
  1.1469 +		$sql = "SELECT CONSTRAINT_NAME, COLUMN_NAME
  1.1470 +			FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
  1.1471 +			WHERE TABLE_NAME = '$table_name'";
  1.1472 +		$result = $db->sql_query($sql);
  1.1473 +		while ($row = $db->sql_fetchrow($result))
  1.1474 +		{
  1.1475 +			if (!sizeof($rows))
  1.1476 +			{
  1.1477 +				$sql_data .= "ALTER TABLE [$table_name] WITH NOCHECK ADD\n";
  1.1478 +				$sql_data .= "\tCONSTRAINT [{$row['CONSTRAINT_NAME']}] PRIMARY KEY  CLUSTERED \n\t(\n";
  1.1479 +			}
  1.1480 +			$rows[] = "\t\t[{$row['COLUMN_NAME']}]";
  1.1481 +		}
  1.1482 +		if (sizeof($rows))
  1.1483 +		{
  1.1484 +			$sql_data .= implode(",\n", $rows);
  1.1485 +			$sql_data .= "\n\t)  ON [PRIMARY] \nGO\n";
  1.1486 +		}
  1.1487 +		$db->sql_freeresult($result);
  1.1488 +
  1.1489 +		$index = array();
  1.1490 +		$sql = "EXEC sp_statistics '$table_name'";
  1.1491 +		$result = $db->sql_query($sql);
  1.1492 +		while ($row = $db->sql_fetchrow($result))
  1.1493 +		{
  1.1494 +			if ($row['TYPE'] == 3)
  1.1495 +			{
  1.1496 +				$index[$row['INDEX_NAME']][] = '[' . $row['COLUMN_NAME'] . ']';
  1.1497 +			}
  1.1498 +		}
  1.1499 +		$db->sql_freeresult($result);
  1.1500 +
  1.1501 +		foreach ($index as $index_name => $column_name)
  1.1502 +		{
  1.1503 +			$index[$index_name] = implode(', ', $column_name);
  1.1504 +		}
  1.1505 +
  1.1506 +		foreach ($index as $index_name => $columns)
  1.1507 +		{
  1.1508 +			$sql_data .= "\nCREATE  INDEX [$index_name] ON [$table_name]($columns) ON [PRIMARY]\nGO\n";
  1.1509 +		}
  1.1510 +		$this->flush($sql_data);
  1.1511 +	}
  1.1512 +
  1.1513 +	function write_data($table_name)
  1.1514 +	{
  1.1515 +		global $db;
  1.1516 +
  1.1517 +		if ($db->sql_layer === 'mssql')
  1.1518 +		{
  1.1519 +			$this->write_data_mssql($table_name);
  1.1520 +		}
  1.1521 +		else if($db->sql_layer === 'mssqlnative')
  1.1522 +		{
  1.1523 +			$this->write_data_mssqlnative($table_name);
  1.1524 +		}
  1.1525 +		else
  1.1526 +		{
  1.1527 +			$this->write_data_odbc($table_name);
  1.1528 +		}
  1.1529 +	}
  1.1530 +
  1.1531 +	function write_data_mssql($table_name)
  1.1532 +	{
  1.1533 +		global $db;
  1.1534 +		$ary_type = $ary_name = array();
  1.1535 +		$ident_set = false;
  1.1536 +		$sql_data = '';
  1.1537 +
  1.1538 +		// Grab all of the data from current table.
  1.1539 +		$sql = "SELECT *
  1.1540 +			FROM $table_name";
  1.1541 +		$result = $db->sql_query($sql);
  1.1542 +
  1.1543 +		$retrieved_data = mssql_num_rows($result);
  1.1544 +
  1.1545 +		$i_num_fields = mssql_num_fields($result);
  1.1546 +
  1.1547 +		for ($i = 0; $i < $i_num_fields; $i++)
  1.1548 +		{
  1.1549 +			$ary_type[$i] = mssql_field_type($result, $i);
  1.1550 +			$ary_name[$i] = mssql_field_name($result, $i);
  1.1551 +		}
  1.1552 +
  1.1553 +		if ($retrieved_data)
  1.1554 +		{
  1.1555 +			$sql = "SELECT 1 as has_identity
  1.1556 +				FROM INFORMATION_SCHEMA.COLUMNS
  1.1557 +				WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
  1.1558 +			$result2 = $db->sql_query($sql);
  1.1559 +			$row2 = $db->sql_fetchrow($result2);
  1.1560 +			if (!empty($row2['has_identity']))
  1.1561 +			{
  1.1562 +				$sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
  1.1563 +				$ident_set = true;
  1.1564 +			}
  1.1565 +			$db->sql_freeresult($result2);
  1.1566 +		}
  1.1567 +
  1.1568 +		while ($row = $db->sql_fetchrow($result))
  1.1569 +		{
  1.1570 +			$schema_vals = $schema_fields = array();
  1.1571 +
  1.1572 +			// Build the SQL statement to recreate the data.
  1.1573 +			for ($i = 0; $i < $i_num_fields; $i++)
  1.1574 +			{
  1.1575 +				$str_val = $row[$ary_name[$i]];
  1.1576 +
  1.1577 +				if (preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
  1.1578 +				{
  1.1579 +					$str_quote = '';
  1.1580 +					$str_empty = "''";
  1.1581 +					$str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
  1.1582 +				}
  1.1583 +				else if (preg_match('#date|timestamp#i', $ary_type[$i]))
  1.1584 +				{
  1.1585 +					if (empty($str_val))
  1.1586 +					{
  1.1587 +						$str_quote = '';
  1.1588 +					}
  1.1589 +					else
  1.1590 +					{
  1.1591 +						$str_quote = "'";
  1.1592 +					}
  1.1593 +				}
  1.1594 +				else
  1.1595 +				{
  1.1596 +					$str_quote = '';
  1.1597 +					$str_empty = 'NULL';
  1.1598 +				}
  1.1599 +
  1.1600 +				if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
  1.1601 +				{
  1.1602 +					$str_val = $str_empty;
  1.1603 +				}
  1.1604 +
  1.1605 +				$schema_vals[$i] = $str_quote . $str_val . $str_quote;
  1.1606 +				$schema_fields[$i] = $ary_name[$i];
  1.1607 +			}
  1.1608 +
  1.1609 +			// Take the ordered fields and their associated data and build it
  1.1610 +			// into a valid sql statement to recreate that field in the data.
  1.1611 +			$sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
  1.1612 +
  1.1613 +			$this->flush($sql_data);
  1.1614 +			$sql_data = '';
  1.1615 +		}
  1.1616 +		$db->sql_freeresult($result);
  1.1617 +
  1.1618 +		if ($retrieved_data && $ident_set)
  1.1619 +		{
  1.1620 +			$sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
  1.1621 +		}
  1.1622 +		$this->flush($sql_data);
  1.1623 +	}
  1.1624 +	
  1.1625 +	function write_data_mssqlnative($table_name)
  1.1626 +	{
  1.1627 +		global $db;
  1.1628 +		$ary_type = $ary_name = array();
  1.1629 +		$ident_set = false;
  1.1630 +		$sql_data = '';
  1.1631 +
  1.1632 +		// Grab all of the data from current table.
  1.1633 +		$sql = "SELECT * FROM $table_name";
  1.1634 +		$db->mssqlnative_set_query_options(array('Scrollable' => SQLSRV_CURSOR_STATIC));
  1.1635 +		$result = $db->sql_query($sql);
  1.1636 +
  1.1637 +		$retrieved_data = $db->mssqlnative_num_rows($result);
  1.1638 +
  1.1639 +		if (!$retrieved_data)
  1.1640 +		{
  1.1641 +			$db->sql_freeresult($result);
  1.1642 +			return;
  1.1643 +		}
  1.1644 +
  1.1645 +		$sql = "SELECT * FROM $table_name";
  1.1646 +		$result_fields = $db->sql_query_limit($sql, 1);
  1.1647 +
  1.1648 +		$row = new result_mssqlnative($result_fields);
  1.1649 +		$i_num_fields = $row->num_fields();
  1.1650 +		
  1.1651 +		for ($i = 0; $i < $i_num_fields; $i++)
  1.1652 +		{
  1.1653 +			$ary_type[$i] = $row->field_type($i);
  1.1654 +			$ary_name[$i] = $row->field_name($i);
  1.1655 +		}
  1.1656 +		$db->sql_freeresult($result_fields);
  1.1657 +
  1.1658 +		$sql = "SELECT 1 as has_identity
  1.1659 +			FROM INFORMATION_SCHEMA.COLUMNS
  1.1660 +			WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
  1.1661 +		$result2 = $db->sql_query($sql);
  1.1662 +		$row2 = $db->sql_fetchrow($result2);
  1.1663 +		
  1.1664 +		if (!empty($row2['has_identity']))
  1.1665 +		{
  1.1666 +			$sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
  1.1667 +			$ident_set = true;
  1.1668 +		}
  1.1669 +		$db->sql_freeresult($result2);
  1.1670 +
  1.1671 +		while ($row = $db->sql_fetchrow($result))
  1.1672 +		{
  1.1673 +			$schema_vals = $schema_fields = array();
  1.1674 +
  1.1675 +			// Build the SQL statement to recreate the data.
  1.1676 +			for ($i = 0; $i < $i_num_fields; $i++)
  1.1677 +			{
  1.1678 +				$str_val = $row[$ary_name[$i]];
  1.1679 +
  1.1680 +				// defaults to type number - better quote just to be safe, so check for is_int too
  1.1681 +				if (is_int($ary_type[$i]) || preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
  1.1682 +				{
  1.1683 +					$str_quote = '';
  1.1684 +					$str_empty = "''";
  1.1685 +					$str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
  1.1686 +				}
  1.1687 +				else if (preg_match('#date|timestamp#i', $ary_type[$i]))
  1.1688 +				{
  1.1689 +					if (empty($str_val))
  1.1690 +					{
  1.1691 +						$str_quote = '';
  1.1692 +					}
  1.1693 +					else
  1.1694 +					{
  1.1695 +						$str_quote = "'";
  1.1696 +					}
  1.1697 +				}
  1.1698 +				else
  1.1699 +				{
  1.1700 +					$str_quote = '';
  1.1701 +					$str_empty = 'NULL';
  1.1702 +				}
  1.1703 +
  1.1704 +				if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
  1.1705 +				{
  1.1706 +					$str_val = $str_empty;
  1.1707 +				}
  1.1708 +
  1.1709 +				$schema_vals[$i] = $str_quote . $str_val . $str_quote;
  1.1710 +				$schema_fields[$i] = $ary_name[$i];
  1.1711 +			}
  1.1712 +
  1.1713 +			// Take the ordered fields and their associated data and build it
  1.1714 +			// into a valid sql statement to recreate that field in the data.
  1.1715 +			$sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
  1.1716 +
  1.1717 +			$this->flush($sql_data);
  1.1718 +			$sql_data = '';
  1.1719 +		}
  1.1720 +		$db->sql_freeresult($result);
  1.1721 +
  1.1722 +		if ($ident_set)
  1.1723 +		{
  1.1724 +			$sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
  1.1725 +		}
  1.1726 +		$this->flush($sql_data);
  1.1727 +	}	
  1.1728 +	
  1.1729 +	function write_data_odbc($table_name)
  1.1730 +	{
  1.1731 +		global $db;
  1.1732 +		$ary_type = $ary_name = array();
  1.1733 +		$ident_set = false;
  1.1734 +		$sql_data = '';
  1.1735 +
  1.1736 +		// Grab all of the data from current table.
  1.1737 +		$sql = "SELECT *
  1.1738 +			FROM $table_name";
  1.1739 +		$result = $db->sql_query($sql);
  1.1740 +
  1.1741 +		$retrieved_data = odbc_num_rows($result);
  1.1742 +
  1.1743 +		if ($retrieved_data)
  1.1744 +		{
  1.1745 +			$sql = "SELECT 1 as has_identity
  1.1746 +				FROM INFORMATION_SCHEMA.COLUMNS
  1.1747 +				WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
  1.1748 +			$result2 = $db->sql_query($sql);
  1.1749 +			$row2 = $db->sql_fetchrow($result2);
  1.1750 +			if (!empty($row2['has_identity']))
  1.1751 +			{
  1.1752 +				$sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
  1.1753 +				$ident_set = true;
  1.1754 +			}
  1.1755 +			$db->sql_freeresult($result2);
  1.1756 +		}
  1.1757 +
  1.1758 +		$i_num_fields = odbc_num_fields($result);
  1.1759 +
  1.1760 +		for ($i = 0; $i < $i_num_fields; $i++)
  1.1761 +		{
  1.1762 +			$ary_type[$i] = odbc_field_type($result, $i + 1);
  1.1763 +			$ary_name[$i] = odbc_field_name($result, $i + 1);
  1.1764 +		}
  1.1765 +
  1.1766 +		while ($row = $db->sql_fetchrow($result))
  1.1767 +		{
  1.1768 +			$schema_vals = $schema_fields = array();
  1.1769 +
  1.1770 +			// Build the SQL statement to recreate the data.
  1.1771 +			for ($i = 0; $i < $i_num_fields; $i++)
  1.1772 +			{
  1.1773 +				$str_val = $row[$ary_name[$i]];
  1.1774 +
  1.1775 +				if (preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
  1.1776 +				{
  1.1777 +					$str_quote = '';
  1.1778 +					$str_empty = "''";
  1.1779 +					$str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
  1.1780 +				}
  1.1781 +				else if (preg_match('#date|timestamp#i', $ary_type[$i]))
  1.1782 +				{
  1.1783 +					if (empty($str_val))
  1.1784 +					{
  1.1785 +						$str_quote = '';
  1.1786 +					}
  1.1787 +					else
  1.1788 +					{
  1.1789 +						$str_quote = "'";
  1.1790 +					}
  1.1791 +				}
  1.1792 +				else
  1.1793 +				{
  1.1794 +					$str_quote = '';
  1.1795 +					$str_empty = 'NULL';
  1.1796 +				}
  1.1797 +
  1.1798 +				if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
  1.1799 +				{
  1.1800 +					$str_val = $str_empty;
  1.1801 +				}
  1.1802 +
  1.1803 +				$schema_vals[$i] = $str_quote . $str_val . $str_quote;
  1.1804 +				$schema_fields[$i] = $ary_name[$i];
  1.1805 +			}
  1.1806 +
  1.1807 +			// Take the ordered fields and their associated data and build it
  1.1808 +			// into a valid sql statement to recreate that field in the data.
  1.1809 +			$sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
  1.1810 +
  1.1811 +			$this->flush($sql_data);
  1.1812 +
  1.1813 +			$sql_data = '';
  1.1814 +
  1.1815 +		}
  1.1816 +		$db->sql_freeresult($result);
  1.1817 +
  1.1818 +		if ($retrieved_data && $ident_set)
  1.1819 +		{
  1.1820 +			$sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
  1.1821 +		}
  1.1822 +		$this->flush($sql_data);
  1.1823 +	}
  1.1824 +
  1.1825 +}
  1.1826 +
  1.1827 +/**
  1.1828 +* @package acp
  1.1829 +*/
  1.1830 +class oracle_extractor extends base_extractor
  1.1831 +{
  1.1832 +	function write_table($table_name)
  1.1833 +	{
  1.1834 +		global $db;
  1.1835 +		$sql_data = '-- Table: ' . $table_name . "\n";
  1.1836 +		$sql_data .= "DROP TABLE $table_name\n/\n";
  1.1837 +		$sql_data .= "\nCREATE TABLE $table_name (\n";
  1.1838 +
  1.1839 +		$sql = "SELECT COLUMN_NAME, DATA_TYPE, DATA_PRECISION, DATA_LENGTH, NULLABLE, DATA_DEFAULT
  1.1840 +			FROM ALL_TAB_COLS
  1.1841 +			WHERE table_name = '{$table_name}'";
  1.1842 +		$result = $db->sql_query($sql);
  1.1843 +
  1.1844 +		$rows = array();
  1.1845 +		while ($row = $db->sql_fetchrow($result))
  1.1846 +		{
  1.1847 +			$line = '  "' . $row['column_name'] . '" ' . $row['data_type'];
  1.1848 +
  1.1849 +			if ($row['data_type'] !== 'CLOB')
  1.1850 +			{
  1.1851 +				if ($row['data_type'] !== 'VARCHAR2' && $row['data_type'] !== 'CHAR')
  1.1852 +				{
  1.1853 +					$line .= '(' . $row['data_precision'] . ')';
  1.1854 +				}
  1.1855 +				else
  1.1856 +				{
  1.1857 +					$line .= '(' . $row['data_length'] . ')';
  1.1858 +				}
  1.1859 +			}
  1.1860 +
  1.1861 +			if (!empty($row['data_default']))
  1.1862 +			{
  1.1863 +				$line .= ' DEFAULT ' . $row['data_default'];
  1.1864 +			}
  1.1865 +
  1.1866 +			if ($row['nullable'] == 'N')
  1.1867 +			{
  1.1868 +				$line .= ' NOT NULL';
  1.1869 +			}
  1.1870 +			$rows[] = $line;
  1.1871 +		}
  1.1872 +		$db->sql_freeresult($result);
  1.1873 +
  1.1874 +		$sql = "SELECT A.CONSTRAINT_NAME, A.COLUMN_NAME
  1.1875 +			FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
  1.1876 +			WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
  1.1877 +				AND B.CONSTRAINT_TYPE = 'P'
  1.1878 +				AND A.TABLE_NAME = '{$table_name}'";
  1.1879 +		$result = $db->sql_query($sql);
  1.1880 +
  1.1881 +		$primary_key = array();
  1.1882 +		$contraint_name = '';
  1.1883 +		while ($row = $db->sql_fetchrow($result))
  1.1884 +		{
  1.1885 +			$constraint_name = '"' . $row['constraint_name'] . '"';
  1.1886 +			$primary_key[] = '"' . $row['column_name'] . '"';
  1.1887 +		}
  1.1888 +		$db->sql_freeresult($result);
  1.1889 +
  1.1890 +		if (sizeof($primary_key))
  1.1891 +		{
  1.1892 +			$rows[] = "  CONSTRAINT {$constraint_name} PRIMARY KEY (" . implode(', ', $primary_key) . ')';
  1.1893 +		}
  1.1894 +
  1.1895 +		$sql = "SELECT A.CONSTRAINT_NAME, A.COLUMN_NAME
  1.1896 +			FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
  1.1897 +			WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
  1.1898 +				AND B.CONSTRAINT_TYPE = 'U'
  1.1899 +				AND A.TABLE_NAME = '{$table_name}'";
  1.1900 +		$result = $db->sql_query($sql);
  1.1901 +
  1.1902 +		$unique = array();
  1.1903 +		$contraint_name = '';
  1.1904 +		while ($row = $db->sql_fetchrow($result))
  1.1905 +		{
  1.1906 +			$constraint_name = '"' . $row['constraint_name'] . '"';
  1.1907 +			$unique[] = '"' . $row['column_name'] . '"';
  1.1908 +		}
  1.1909 +		$db->sql_freeresult($result);
  1.1910 +
  1.1911 +		if (sizeof($unique))
  1.1912 +		{
  1.1913 +			$rows[] = "  CONSTRAINT {$constraint_name} UNIQUE (" . implode(', ', $unique) . ')';
  1.1914 +		}
  1.1915 +
  1.1916 +		$sql_data .= implode(",\n", $rows);
  1.1917 +		$sql_data .= "\n)\n/\n";
  1.1918 +
  1.1919 +		$sql = "SELECT A.REFERENCED_NAME, C.*
  1.1920 +			FROM USER_DEPENDENCIES A, USER_TRIGGERS B, USER_SEQUENCES C
  1.1921 +			WHERE A.REFERENCED_TYPE = 'SEQUENCE'
  1.1922 +				AND A.NAME = B.TRIGGER_NAME
  1.1923 +				AND B.TABLE_NAME = '{$table_name}'
  1.1924 +				AND C.SEQUENCE_NAME = A.REFERENCED_NAME";
  1.1925 +		$result = $db->sql_query($sql);
  1.1926 +
  1.1927 +		$type = request_var('type', '');
  1.1928 +
  1.1929 +		while ($row = $db->sql_fetchrow($result))
  1.1930 +		{
  1.1931 +			$sql_data .= "\nDROP SEQUENCE \"{$row['referenced_name']}\"\n/\n";
  1.1932 +			$sql_data .= "\nCREATE SEQUENCE \"{$row['referenced_name']}\"";
  1.1933 +
  1.1934 +			if ($type == 'full')
  1.1935 +			{
  1.1936 +				$sql_data .= ' START WITH ' . $row['last_number'];
  1.1937 +			}
  1.1938 +
  1.1939 +			$sql_data .= "\n/\n";
  1.1940 +		}
  1.1941 +		$db->sql_freeresult($result);
  1.1942 +
  1.1943 +		$sql = "SELECT DESCRIPTION, WHEN_CLAUSE, TRIGGER_BODY
  1.1944 +			FROM USER_TRIGGERS
  1.1945 +			WHERE TABLE_NAME = '{$table_name}'";
  1.1946 +		$result = $db->sql_query($sql);
  1.1947 +		while ($row = $db->sql_fetchrow($result))
  1.1948 +		{
  1.1949 +			$sql_data .= "\nCREATE OR REPLACE TRIGGER {$row['description']}WHEN ({$row['when_clause']})\n{$row['trigger_body']}\n/\n";
  1.1950 +		}
  1.1951 +		$db->sql_freeresult($result);
  1.1952 +
  1.1953 +		$sql = "SELECT A.INDEX_NAME, B.COLUMN_NAME
  1.1954 +			FROM USER_INDEXES A, USER_IND_COLUMNS B
  1.1955 +			WHERE A.UNIQUENESS = 'NONUNIQUE'
  1.1956 +				AND A.INDEX_NAME = B.INDEX_NAME
  1.1957 +				AND B.TABLE_NAME = '{$table_name}'";
  1.1958 +		$result = $db->sql_query($sql);
  1.1959 +
  1.1960 +		$index = array();
  1.1961 +
  1.1962 +		while ($row = $db->sql_fetchrow($result))
  1.1963 +		{
  1.1964 +			$index[$row['index_name']][] = $row['column_name'];
  1.1965 +		}
  1.1966 +
  1.1967 +		foreach ($index as $index_name => $column_names)
  1.1968 +		{
  1.1969 +			$sql_data .= "\nCREATE INDEX $index_name ON $table_name(" . implode(', ', $column_names) . ")\n/\n";
  1.1970 +		}
  1.1971 +		$db->sql_freeresult($result);
  1.1972 +		$this->flush($sql_data);
  1.1973 +	}
  1.1974 +
  1.1975 +	function write_data($table_name)
  1.1976 +	{
  1.1977 +		global $db;
  1.1978 +		$ary_type = $ary_name = array();
  1.1979 +
  1.1980 +		// Grab all of the data from current table.
  1.1981 +		$sql = "SELECT *
  1.1982 +			FROM $table_name";
  1.1983 +		$result = $db->sql_query($sql);
  1.1984 +
  1.1985 +		$i_num_fields = ocinumcols($result);
  1.1986 +
  1.1987 +		for ($i = 0; $i < $i_num_fields; $i++)
  1.1988 +		{
  1.1989 +			$ary_type[$i] = ocicolumntype($result, $i + 1);
  1.1990 +			$ary_name[$i] = ocicolumnname($result, $i + 1);
  1.1991 +		}
  1.1992 +
  1.1993 +		$sql_data = '';
  1.1994 +
  1.1995 +		while ($row = $db->sql_fetchrow($result))
  1.1996 +		{
  1.1997 +			$schema_vals = $schema_fields = array();
  1.1998 +
  1.1999 +			// Build the SQL statement to recreate the data.
  1.2000 +			for ($i = 0; $i < $i_num_fields; $i++)
  1.2001 +			{
  1.2002 +				// Oracle uses uppercase - we use lowercase
  1.2003 +				$str_val = $row[strtolower($ary_name[$i])];
  1.2004 +
  1.2005 +				if (preg_match('#char|text|bool|raw|clob#i', $ary_type[$i]))
  1.2006 +				{
  1.2007 +					$str_quote = '';
  1.2008 +					$str_empty = "''";
  1.2009 +					$str_val = sanitize_data_oracle($str_val);
  1.2010 +				}
  1.2011 +				else if (preg_match('#date|timestamp#i', $ary_type[$i]))
  1.2012 +				{
  1.2013 +					if (empty($str_val))
  1.2014 +					{
  1.2015 +						$str_quote = '';
  1.2016 +					}
  1.2017 +					else
  1.2018 +					{
  1.2019 +						$str_quote = "'";
  1.2020 +					}
  1.2021 +				}
  1.2022 +				else
  1.2023 +				{
  1.2024 +					$str_quote = '';
  1.2025 +					$str_empty = 'NULL';
  1.2026 +				}
  1.2027 +
  1.2028 +				if (empty($str_val) && $str_val !== '0')
  1.2029 +				{
  1.2030 +					$str_val = $str_empty;
  1.2031 +				}
  1.2032 +
  1.2033 +				$schema_vals[$i] = $str_quote . $str_val . $str_quote;
  1.2034 +				$schema_fields[$i] = '"' . $ary_name[$i] . '"';
  1.2035 +			}
  1.2036 +
  1.2037 +			// Take the ordered fields and their associated data and build it
  1.2038 +			// into a valid sql statement to recreate that field in the data.
  1.2039 +			$sql_data = "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ")\n/\n";
  1.2040 +
  1.2041 +			$this->flush($sql_data);
  1.2042 +		}
  1.2043 +		$db->sql_freeresult($result);
  1.2044 +	}
  1.2045 +
  1.2046 +	function write_start($prefix)
  1.2047 +	{
  1.2048 +		$sql_data = "--\n";
  1.2049 +		$sql_data .= "-- phpBB Backup Script\n";
  1.2050 +		$sql_data .= "-- Dump of tables for $prefix\n";
  1.2051 +		$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
  1.2052 +		$sql_data .= "--\n";
  1.2053 +		$this->flush($sql_data);
  1.2054 +	}
  1.2055 +}
  1.2056 +
  1.2057 +/**
  1.2058 +* @package acp
  1.2059 +*/
  1.2060 +class firebird_extractor extends base_extractor
  1.2061 +{
  1.2062 +	function write_start($prefix)
  1.2063 +	{
  1.2064 +		$sql_data = "--\n";
  1.2065 +		$sql_data .= "-- phpBB Backup Script\n";
  1.2066 +		$sql_data .= "-- Dump of tables for $prefix\n";
  1.2067 +		$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
  1.2068 +		$sql_data .= "--\n";
  1.2069 +		$this->flush($sql_data);
  1.2070 +	}
  1.2071 +
  1.2072 +	function write_data($table_name)
  1.2073 +	{
  1.2074 +		global $db;
  1.2075 +		$ary_type = $ary_name = array();
  1.2076 +
  1.2077 +		// Grab all of the data from current table.
  1.2078 +		$sql = "SELECT *
  1.2079 +			FROM $table_name";
  1.2080 +		$result = $db->sql_query($sql);
  1.2081 +
  1.2082 +		$i_num_fields = ibase_num_fields($result);
  1.2083 +
  1.2084 +		for ($i = 0; $i < $i_num_fields; $i++)
  1.2085 +		{
  1.2086 +			$info = ibase_field_info($result, $i);
  1.2087 +			$ary_type[$i] = $info['type'];
  1.2088 +			$ary_name[$i] = $info['name'];
  1.2089 +		}
  1.2090 +
  1.2091 +		while ($row = $db->sql_fetchrow($result))
  1.2092 +		{
  1.2093 +			$schema_vals = $schema_fields = array();
  1.2094 +
  1.2095 +			// Build the SQL statement to recreate the data.
  1.2096 +			for ($i = 0; $i < $i_num_fields; $i++)
  1.2097 +			{
  1.2098 +				$str_val = $row[strtolower($ary_name[$i])];
  1.2099 +
  1.2100 +				if (preg_match('#char|text|bool|varbinary|blob#i', $ary_type[$i]))
  1.2101 +				{
  1.2102 +					$str_quote = '';
  1.2103 +					$str_empty = "''";
  1.2104 +					$str_val = sanitize_data_generic(str_replace("'", "''", $str_val));
  1.2105 +				}
  1.2106 +				else if (preg_match('#date|timestamp#i', $ary_type[$i]))
  1.2107 +				{
  1.2108 +					if (empty($str_val))
  1.2109 +					{
  1.2110 +						$str_quote = '';
  1.2111 +					}
  1.2112 +					else
  1.2113 +					{
  1.2114 +						$str_quote = "'";
  1.2115 +					}
  1.2116 +				}
  1.2117 +				else
  1.2118 +				{
  1.2119 +					$str_quote = '';
  1.2120 +					$str_empty = 'NULL';
  1.2121 +				}
  1.2122 +
  1.2123 +				if (empty($str_val) && $str_val !== '0')
  1.2124 +				{
  1.2125 +					$str_val = $str_empty;
  1.2126 +				}
  1.2127 +
  1.2128 +				$schema_vals[$i] = $str_quote . $str_val . $str_quote;
  1.2129 +				$schema_fields[$i] = '"' . $ary_name[$i] . '"';
  1.2130 +			}
  1.2131 +
  1.2132 +			// Take the ordered fields and their associated data and build it
  1.2133 +			// into a valid sql statement to recreate that field in the data.
  1.2134 +			$sql_data = "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\n";
  1.2135 +
  1.2136 +			$this->flush($sql_data);
  1.2137 +		}
  1.2138 +		$db->sql_freeresult($result);
  1.2139 +	}
  1.2140 +
  1.2141 +	function write_table($table_name)
  1.2142 +	{
  1.2143 +		global $db;
  1.2144 +
  1.2145 +		$sql_data = '-- Table: ' . $table_name . "\n";
  1.2146 +		$sql_data .= "DROP TABLE $table_name;\n";
  1.2147 +
  1.2148 +		$data_types = array(7 => 'SMALLINT', 8 => 'INTEGER', 10 => 'FLOAT', 12 => 'DATE', 13 => 'TIME', 14 => 'CHARACTER', 27 => 'DOUBLE PRECISION', 35 => 'TIMESTAMP', 37 => 'VARCHAR', 40 => 'CSTRING', 261 => 'BLOB', 701 => 'DECIMAL', 702 => 'NUMERIC');
  1.2149 +
  1.2150 +		$sql_data .= "\nCREATE TABLE $table_name (\n";
  1.2151 +
  1.2152 +		$sql = 'SELECT DISTINCT R.RDB$FIELD_NAME as FNAME, R.RDB$NULL_FLAG as NFLAG, R.RDB$DEFAULT_SOURCE as DSOURCE, F.RDB$FIELD_TYPE as FTYPE, F.RDB$FIELD_SUB_TYPE as STYPE, F.RDB$FIELD_LENGTH as FLEN
  1.2153 +			FROM RDB$RELATION_FIELDS R
  1.2154 +			JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME
  1.2155 +			LEFT JOIN RDB$FIELD_DIMENSIONS D ON R.RDB$FIELD_SOURCE = D.RDB$FIELD_NAME
  1.2156 +			WHERE F.RDB$SYSTEM_FLAG = 0
  1.2157 +				AND R.RDB$RELATION_NAME = \''. $table_name . '\'
  1.2158 +			ORDER BY R.RDB$FIELD_POSITION';
  1.2159 +		$result = $db->sql_query($sql);
  1.2160 +
  1.2161 +		$rows = array();
  1.2162 +		while ($row = $db->sql_fetchrow($result))
  1.2163 +		{
  1.2164 +			$line = "\t" . '"' . $row['fname'] . '" ' . $data_types[$row['ftype']];
  1.2165 +
  1.2166 +			if ($row['ftype'] == 261 && $row['stype'] == 1)
  1.2167 +			{
  1.2168 +				$line .= ' SUB_TYPE TEXT';
  1.2169 +			}
  1.2170 +
  1.2171 +			if ($row['ftype'] == 37 || $row['ftype'] == 14)
  1.2172 +			{
  1.2173 +				$line .= ' (' . $row['flen'] . ')';
  1.2174 +			}
  1.2175 +
  1.2176 +			if (!empty($row['dsource']))
  1.2177 +			{
  1.2178 +				$line .= ' ' . $row['dsource'];
  1.2179 +			}
  1.2180 +
  1.2181 +			if (!empty($row['nflag']))
  1.2182 +			{
  1.2183 +				$line .= ' NOT NULL';
  1.2184 +			}
  1.2185 +			$rows[] = $line;
  1.2186 +		}
  1.2187 +		$db->sql_freeresult($result);
  1.2188 +
  1.2189 +		$sql_data .= implode(",\n", $rows);
  1.2190 +		$sql_data .= "\n);\n";
  1.2191 +		$keys = array();
  1.2192 +
  1.2193 +		$sql = 'SELECT I.RDB$FIELD_NAME as NAME
  1.2194 +			FROM RDB$RELATION_CONSTRAINTS RC, RDB$INDEX_SEGMENTS I, RDB$INDICES IDX
  1.2195 +			WHERE (I.RDB$INDEX_NAME = RC.RDB$INDEX_NAME)
  1.2196 +				AND (IDX.RDB$INDEX_NAME = RC.RDB$INDEX_NAME)
  1.2197 +				AND (RC.RDB$RELATION_NAME = \''. $table_name . '\')
  1.2198 +			ORDER BY I.RDB$FIELD_POSITION';
  1.2199 +		$result = $db->sql_query($sql);
  1.2200 +
  1.2201 +		while ($row = $db->sql_fetchrow($result))
  1.2202 +		{
  1.2203 +			$keys[] = $row['name'];
  1.2204 +		}
  1.2205 +
  1.2206 +		if (sizeof($keys))
  1.2207 +		{
  1.2208 +			$sql_data .= "\nALTER TABLE $table_name ADD PRIMARY KEY (" . implode(', ', $keys) . ');';
  1.2209 +		}
  1.2210 +
  1.2211 +		$db->sql_freeresult($result);
  1.2212 +
  1.2213 +		$sql = 'SELECT I.RDB$INDEX_NAME as INAME, I.RDB$UNIQUE_FLAG as UFLAG, S.RDB$FIELD_NAME as FNAME
  1.2214 +			FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON S.RDB$INDEX_NAME=I.RDB$INDEX_NAME
  1.2215 +			WHERE (I.RDB$SYSTEM_FLAG IS NULL  OR  I.RDB$SYSTEM_FLAG=0)
  1.2216 +				AND I.RDB$FOREIGN_KEY IS NULL
  1.2217 +				AND I.RDB$RELATION_NAME = \''. $table_name . '\'
  1.2218 +				AND I.RDB$INDEX_NAME NOT STARTING WITH \'RDB$\'
  1.2219 +			ORDER BY S.RDB$FIELD_POSITION';
  1.2220 +		$result = $db->sql_query($sql);
  1.2221 +
  1.2222 +		$index = array();
  1.2223 +		while ($row = $db->sql_fetchrow($result))
  1.2224 +		{
  1.2225 +			$index[$row['iname']]['unique'] = !empty($row['uflag']);
  1.2226 +			$index[$row['iname']]['values'][] = $row['fname'];
  1.2227 +		}
  1.2228 +
  1.2229 +		foreach ($index as $index_name => $data)
  1.2230 +		{
  1.2231 +			$sql_data .= "\nCREATE ";
  1.2232 +			if ($data['unique'])
  1.2233 +			{
  1.2234 +				$sql_data .= 'UNIQUE ';
  1.2235 +			}
  1.2236 +			$sql_data .= "INDEX $index_name ON $table_name(" . implode(', ', $data['values']) . ");";
  1.2237 +		}
  1.2238 +		$sql_data .= "\n";
  1.2239 +
  1.2240 +		$db->sql_freeresult($result);
  1.2241 +
  1.2242 +		$sql = 'SELECT D1.RDB$DEPENDENT_NAME as DNAME, D1.RDB$FIELD_NAME as FNAME, D1.RDB$DEPENDENT_TYPE, R1.RDB$RELATION_NAME
  1.2243 +			FROM RDB$DEPENDENCIES D1
  1.2244 +			LEFT JOIN RDB$RELATIONS R1 ON ((D1.RDB$DEPENDENT_NAME = R1.RDB$RELATION_NAME) AND (NOT (R1.RDB$VIEW_BLR IS NULL)))
  1.2245 +			WHERE (D1.RDB$DEPENDED_ON_TYPE = 0)
  1.2246 +				AND (D1.RDB$DEPENDENT_TYPE <> 3)
  1.2247 +				AND (D1.RDB$DEPENDED_ON_NAME = \'' . $table_name . '\')
  1.2248 +			UNION SELECT DISTINCT F2.RDB$RELATION_NAME, D2.RDB$FIELD_NAME, D2.RDB$DEPENDENT_TYPE, R2.RDB$RELATION_NAME FROM RDB$DEPENDENCIES D2, RDB$RELATION_FIELDS F2
  1.2249 +			LEFT JOIN RDB$RELATIONS R2 ON ((F2.RDB$RELATION_NAME = R2.RDB$RELATION_NAME) AND (NOT (R2.RDB$VIEW_BLR IS NULL)))
  1.2250 +			WHERE (D2.RDB$DEPENDENT_TYPE = 3)
  1.2251 +				AND (D2.RDB$DEPENDENT_NAME = F2.RDB$FIELD_SOURCE)
  1.2252 +				AND (D2.RDB$DEPENDED_ON_NAME = \'' . $table_name . '\')
  1.2253 +			ORDER BY 1, 2';
  1.2254 +		$result = $db->sql_query($sql);
  1.2255 +		while ($row = $db->sql_fetchrow($result))
  1.2256 +		{
  1.2257 +			$sql = 'SELECT T1.RDB$DEPENDED_ON_NAME as GEN, T1.RDB$FIELD_NAME, T1.RDB$DEPENDED_ON_TYPE
  1.2258 +				FROM RDB$DEPENDENCIES T1
  1.2259 +				WHERE (T1.RDB$DEPENDENT_NAME = \'' . $row['dname'] . '\')
  1.2260 +					AND (T1.RDB$DEPENDENT_TYPE = 2 AND T1.RDB$DEPENDED_ON_TYPE = 14)
  1.2261 +				UNION ALL SELECT DISTINCT D.RDB$DEPENDED_ON_NAME, D.RDB$FIELD_NAME, D.RDB$DEPENDED_ON_TYPE
  1.2262 +				FROM RDB$DEPENDENCIES D, RDB$RELATION_FIELDS F
  1.2263 +				WHERE (D.RDB$DEPENDENT_TYPE = 3)
  1.2264 +					AND (D.RDB$DEPENDENT_NAME = F.RDB$FIELD_SOURCE)
  1.2265 +					AND (F.RDB$RELATION_NAME = \'' . $row['dname'] . '\')
  1.2266 +				ORDER BY 1,2';
  1.2267 +			$result2 = $db->sql_query($sql);
  1.2268 +			$row2 = $db->sql_fetchrow($result2);
  1.2269 +			$db->sql_freeresult($result2);
  1.2270 +			$gen_name = $row2['gen'];
  1.2271 +
  1.2272 +			$sql_data .= "\nDROP GENERATOR " . $gen_name . ";";
  1.2273 +			$sql_data .= "\nSET TERM ^ ;";
  1.2274 +			$sql_data .= "\nCREATE GENERATOR " . $gen_name . "^";
  1.2275 +			$sql_data .= "\nSET GENERATOR  " . $gen_name . " TO 0^\n";
  1.2276 +			$sql_data .= "\nCREATE TRIGGER {$row['dname']} FOR $table_name";
  1.2277 +			$sql_data .= "\nBEFORE INSERT\nAS\nBEGIN";
  1.2278 +			$sql_data .= "\n  NEW.{$row['fname']} = GEN_ID(" . $gen_name . ", 1);";
  1.2279 +			$sql_data .= "\nEND^\n";
  1.2280 +			$sql_data .= "\nSET TERM ; ^\n";
  1.2281 +		}
  1.2282 +
  1.2283 +		$this->flush($sql_data);
  1.2284 +
  1.2285 +		$db->sql_freeresult($result);
  1.2286 +	}
  1.2287 +}
  1.2288 +
  1.2289 +// get how much space we allow for a chunk of data, very similar to phpMyAdmin's way of doing things ;-) (hey, we only do this for MySQL anyway :P)
  1.2290 +function get_usable_memory()
  1.2291 +{
  1.2292 +	$val = trim(@ini_get('memory_limit'));
  1.2293 +
  1.2294 +	if (preg_match('/(\\d+)([mkg]?)/i', $val, $regs))
  1.2295 +	{
  1.2296 +		$memory_limit = (int) $regs[1];
  1.2297 +		switch ($regs[2])
  1.2298 +		{
  1.2299 +
  1.2300 +			case 'k':
  1.2301 +			case 'K':
  1.2302 +				$memory_limit *= 1024;
  1.2303 +			break;
  1.2304 +
  1.2305 +			case 'm':
  1.2306 +			case 'M':
  1.2307 +				$memory_limit *= 1048576;
  1.2308 +			break;
  1.2309 +
  1.2310 +			case 'g':
  1.2311 +			case 'G':
  1.2312 +				$memory_limit *= 1073741824;
  1.2313 +			break;
  1.2314 +		}
  1.2315 +
  1.2316 +		// how much memory PHP requires at the start of export (it is really a little less)
  1.2317 +		if ($memory_limit > 6100000)
  1.2318 +		{
  1.2319 +			$memory_limit -= 6100000;
  1.2320 +		}
  1.2321 +
  1.2322 +		// allow us to consume half of the total memory available
  1.2323 +		$memory_limit /= 2;
  1.2324 +	}
  1.2325 +	else
  1.2326 +	{
  1.2327 +		// set the buffer to 1M if we have no clue how much memory PHP will give us :P
  1.2328 +		$memory_limit = 1048576;
  1.2329 +	}
  1.2330 +
  1.2331 +	return $memory_limit;
  1.2332 +}
  1.2333 +
  1.2334 +function sanitize_data_mssql($text)
  1.2335 +{
  1.2336 +	$data = preg_split('/[\n\t\r\b\f]/', $text);
  1.2337 +	preg_match_all('/[\n\t\r\b\f]/', $text, $matches);
  1.2338 +
  1.2339 +	$val = array();
  1.2340 +
  1.2341 +	foreach ($data as $value)
  1.2342 +	{
  1.2343 +		if (strlen($value))
  1.2344 +		{
  1.2345 +			$val[] = "'" . $value . "'";
  1.2346 +		}
  1.2347 +		if (sizeof($matches[0]))
  1.2348 +		{
  1.2349 +			$val[] = 'char(' . ord(array_shift($matches[0])) . ')';
  1.2350 +		}
  1.2351 +	}
  1.2352 +
  1.2353 +	return implode('+', $val);
  1.2354 +}
  1.2355 +
  1.2356 +function sanitize_data_oracle($text)
  1.2357 +{
  1.2358 +//	$data = preg_split('/[\0\n\t\r\b\f\'"\/\\\]/', $text);
  1.2359 +//	preg_match_all('/[\0\n\t\r\b\f\'"\/\\\]/', $text, $matches);
  1.2360 +	$data = preg_split('/[\0\b\f\'\/]/', $text);
  1.2361 +	preg_match_all('/[\0\r\b\f\'\/]/', $text, $matches);
  1.2362 +
  1.2363 +	$val = array();
  1.2364 +
  1.2365 +	foreach ($data as $value)
  1.2366 +	{
  1.2367 +		if (strlen($value))
  1.2368 +		{
  1.2369 +			$val[] = "'" . $value . "'";
  1.2370 +		}
  1.2371 +		if (sizeof($matches[0]))
  1.2372 +		{
  1.2373 +			$val[] = 'chr(' . ord(array_shift($matches[0])) . ')';
  1.2374 +		}
  1.2375 +	}
  1.2376 +
  1.2377 +	return implode('||', $val);
  1.2378 +}
  1.2379 +
  1.2380 +function sanitize_data_generic($text)
  1.2381 +{
  1.2382 +	$data = preg_split('/[\n\t\r\b\f]/', $text);
  1.2383 +	preg_match_all('/[\n\t\r\b\f]/', $text, $matches);
  1.2384 +
  1.2385 +	$val = array();
  1.2386 +
  1.2387 +	foreach ($data as $value)
  1.2388 +	{
  1.2389 +		if (strlen($value))
  1.2390 +		{
  1.2391 +			$val[] = "'" . $value . "'";
  1.2392 +		}
  1.2393 +		if (sizeof($matches[0]))
  1.2394 +		{
  1.2395 +			$val[] = "'" . array_shift($matches[0]) . "'";
  1.2396 +		}
  1.2397 +	}
  1.2398 +
  1.2399 +	return implode('||', $val);
  1.2400 +}
  1.2401 +
  1.2402 +// modified from PHP.net
  1.2403 +function fgetd(&$fp, $delim, $read, $seek, $eof, $buffer = 8192)
  1.2404 +{
  1.2405 +	$record = '';
  1.2406 +	$delim_len = strlen($delim);
  1.2407 +
  1.2408 +	while (!$eof($fp))
  1.2409 +	{
  1.2410 +		$pos = strpos($record, $delim);
  1.2411 +		if ($pos === false)
  1.2412 +		{
  1.2413 +			$record .= $read($fp, $buffer);
  1.2414 +			if ($eof($fp) && ($pos = strpos($record, $delim)) !== false)
  1.2415 +			{
  1.2416 +				$seek($fp, $pos + $delim_len - strlen($record), SEEK_CUR);
  1.2417 +				return substr($record, 0, $pos);
  1.2418 +			}
  1.2419 +		}
  1.2420 +		else
  1.2421 +		{
  1.2422 +			$seek($fp, $pos + $delim_len - strlen($record), SEEK_CUR);
  1.2423 +			return substr($record, 0, $pos);
  1.2424 +		}
  1.2425 +	}
  1.2426 +
  1.2427 +	return false;
  1.2428 +}
  1.2429 +
  1.2430 +function fgetd_seekless(&$fp, $delim, $read, $seek, $eof, $buffer = 8192)
  1.2431 +{
  1.2432 +	static $array = array();
  1.2433 +	static $record = '';
  1.2434 +
  1.2435 +	if (!sizeof($array))
  1.2436 +	{
  1.2437 +		while (!$eof($fp))
  1.2438 +		{
  1.2439 +			if (strpos($record, $delim) !== false)
  1.2440 +			{
  1.2441 +				$array = explode($delim, $record);
  1.2442 +				$record = array_pop($array);
  1.2443 +				break;
  1.2444 +			}
  1.2445 +			else
  1.2446 +			{
  1.2447 +				$record .= $read($fp, $buffer);
  1.2448 +			}
  1.2449 +		}
  1.2450 +		if ($eof($fp) && strpos($record, $delim) !== false)
  1.2451 +		{
  1.2452 +			$array = explode($delim, $record);
  1.2453 +			$record = array_pop($array);
  1.2454 +		}
  1.2455 +	}
  1.2456 +
  1.2457 +	if (sizeof($array))
  1.2458 +	{
  1.2459 +		return array_shift($array);
  1.2460 +	}
  1.2461 +
  1.2462 +	return false;
  1.2463 +}
  1.2464 +
  1.2465 +?>
  1.2466 \ No newline at end of file