Condition excluding XML output from server export was broken
[phpmyadmin/crack.git] / libraries / export / xml.php
blob87f6fe018098aa4bb55f44778047b8080a088cd3
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build XML dumps of tables
6 * @package phpMyAdmin-Export
7 * @subpackage XML
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 if (!strlen($GLOBALS['db'])) { /* Can't do server export */
14 return;
17 if (isset($plugin_list)) {
18 $plugin_list['xml'] = array(
19 'text' => __('XML'),
20 'extension' => 'xml',
21 'mime_type' => 'text/xml',
22 'options' => array(
23 array('type' => 'begin_group', 'name' => 'general_opts'),
24 array('type' => 'hidden', 'name' => 'structure_or_data'),
25 array('type' => 'end_group')
27 'options_text' => __('Options')
30 /* Export structure */
31 $plugin_list['xml']['options'][] = array(
32 'type' => 'begin_group',
33 'name' => 'structure',
34 'text' => __('Object creation options (all are recommended)')
36 $plugin_list['xml']['options'][] = array(
37 'type' => 'bool',
38 'name' => 'export_functions',
39 'text' => __('Functions')
41 $plugin_list['xml']['options'][] = array(
42 'type' => 'bool',
43 'name' => 'export_procedures',
44 'text' => __('Procedures')
46 $plugin_list['xml']['options'][] = array(
47 'type' => 'bool',
48 'name' => 'export_tables',
49 'text' => __('Tables')
51 $plugin_list['xml']['options'][] = array(
52 'type' => 'bool',
53 'name' => 'export_triggers',
54 'text' => __('Triggers')
56 $plugin_list['xml']['options'][] = array(
57 'type' => 'bool',
58 'name' => 'export_views',
59 'text' => __('Views')
61 $plugin_list['xml']['options'][] = array(
62 'type' => 'end_group'
65 /* Data */
66 $plugin_list['xml']['options'][] = array(
67 'type' => 'begin_group',
68 'name' => 'data',
69 'text' => __('Data dump options')
71 $plugin_list['xml']['options'][] = array(
72 'type' => 'bool',
73 'name' => 'export_contents',
74 'text' => __('Export contents')
76 $plugin_list['xml']['options'][] = array(
77 'type' => 'end_group'
79 } else {
81 /**
82 * Outputs export footer
84 * @return bool Whether it suceeded
86 * @access public
88 function PMA_exportFooter()
90 $foot = '</pma_xml_export>';
92 return PMA_exportOutputHandler($foot);
95 /**
96 * Outputs export header
98 * @return bool Whether it suceeded
100 * @access public
102 function PMA_exportHeader()
104 global $crlf;
105 global $cfg;
106 global $db;
107 global $table;
108 global $tables;
110 $export_struct = isset($GLOBALS['xml_export_functions']) || isset($GLOBALS['xml_export_procedures'])
111 || isset($GLOBALS['xml_export_tables']) || isset($GLOBALS['xml_export_triggers'])
112 || isset($GLOBALS['xml_export_views']);
113 $export_data = isset($GLOBALS['xml_export_contents']) ? true : false;
115 if ($GLOBALS['output_charset_conversion']) {
116 $charset = $GLOBALS['charset_of_file'];
117 } else {
118 $charset = 'utf-8';
121 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
122 . '<!--' . $crlf
123 . '- phpMyAdmin XML Dump' . $crlf
124 . '- version ' . PMA_VERSION . $crlf
125 . '- http://www.phpmyadmin.net' . $crlf
126 . '-' . $crlf
127 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
128 if (!empty($cfg['Server']['port'])) {
129 $head .= ':' . $cfg['Server']['port'];
131 $head .= $crlf
132 . '- ' . __('Generation Time') . ': ' . PMA_localisedDate() . $crlf
133 . '- ' . __('Server version') . ': ' . PMA_MYSQL_STR_VERSION . $crlf
134 . '- ' . __('PHP Version') . ': ' . phpversion() . $crlf
135 . '-->' . $crlf . $crlf;
137 $head .= '<pma_xml_export version="1.0"' . (($export_struct) ? ' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf;
139 if ($export_struct) {
140 $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = \''.PMA_sqlAddSlashes($db).'\' LIMIT 1');
141 $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
142 $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
144 $head .= ' <!--' . $crlf;
145 $head .= ' - Structure schemas' . $crlf;
146 $head .= ' -->' . $crlf;
147 $head .= ' <pma:structure_schemas>' . $crlf;
148 $head .= ' <pma:database name="' . htmlspecialchars($db) . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf;
150 if (count($tables) == 0) {
151 $tables[] = $table;
154 foreach ($tables as $table) {
155 // Export tables and views
156 $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0);
157 $tbl = $result[$table][1];
159 $is_view = PMA_Table::isView($db, $table);
161 if ($is_view) {
162 $type = 'view';
163 } else {
164 $type = 'table';
167 if ($is_view && ! isset($GLOBALS['xml_export_views'])) {
168 continue;
171 if (! $is_view && ! isset($GLOBALS['xml_export_tables'])) {
172 continue;
175 $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf;
177 $tbl = " " . htmlspecialchars($tbl);
178 $tbl = str_replace("\n", "\n ", $tbl);
180 $head .= $tbl . ';' . $crlf;
181 $head .= ' </pma:' . $type . '>' . $crlf;
183 if (isset($GLOBALS['xml_export_triggers']) && $GLOBALS['xml_export_triggers']) {
184 // Export triggers
185 $triggers = PMA_DBI_get_triggers($db, $table);
186 if ($triggers) {
187 foreach ($triggers as $trigger) {
188 $code = $trigger['create'];
189 $head .= ' <pma:trigger name="' . $trigger['name'] . '">' . $crlf;
191 // Do some formatting
192 $code = substr(rtrim($code), 0, -3);
193 $code = " " . htmlspecialchars($code);
194 $code = str_replace("\n", "\n ", $code);
196 $head .= $code . $crlf;
197 $head .= ' </pma:trigger>' . $crlf;
200 unset($trigger);
201 unset($triggers);
206 if (isset($GLOBALS['xml_export_functions']) && $GLOBALS['xml_export_functions']) {
207 // Export functions
208 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
209 if ($functions) {
210 foreach ($functions as $function) {
211 $head .= ' <pma:function name="' . $function . '">' . $crlf;
213 // Do some formatting
214 $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
215 $sql = rtrim($sql);
216 $sql = " " . htmlspecialchars($sql);
217 $sql = str_replace("\n", "\n ", $sql);
219 $head .= $sql . $crlf;
220 $head .= ' </pma:function>' . $crlf;
223 unset($create_func);
224 unset($function);
225 unset($functions);
229 if (isset($GLOBALS['xml_export_procedures']) && $GLOBALS['xml_export_procedures']) {
230 // Export procedures
231 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
232 if ($procedures) {
233 foreach ($procedures as $procedure) {
234 $head .= ' <pma:procedure name="' . $procedure . '">' . $crlf;
236 // Do some formatting
237 $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
238 $sql = rtrim($sql);
239 $sql = " " . htmlspecialchars($sql);
240 $sql = str_replace("\n", "\n ", $sql);
242 $head .= $sql . $crlf;
243 $head .= ' </pma:procedure>' . $crlf;
246 unset($create_proc);
247 unset($procedure);
248 unset($procedures);
252 unset($result);
254 $head .= ' </pma:database>' . $crlf;
255 $head .= ' </pma:structure_schemas>' . $crlf;
257 if ($export_data) {
258 $head .= $crlf;
262 return PMA_exportOutputHandler($head);
266 * Outputs database header
268 * @param string $db Database name
269 * @return bool Whether it suceeded
271 * @access public
273 function PMA_exportDBHeader($db)
275 global $crlf;
277 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
278 $head = ' <!--' . $crlf
279 . ' - ' . __('Database') . ': ' . '\'' . $db . '\'' . $crlf
280 . ' -->' . $crlf
281 . ' <database name="' . htmlspecialchars($db) . '">' . $crlf;
283 return PMA_exportOutputHandler($head);
284 } else {
285 return true;
290 * Outputs database footer
292 * @param string $db Database name
293 * @return bool Whether it suceeded
295 * @access public
297 function PMA_exportDBFooter($db)
299 global $crlf;
301 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
302 return PMA_exportOutputHandler(' </database>' . $crlf);
303 } else {
304 return true;
309 * Outputs CREATE DATABASE statement
311 * @param string $db Database name
312 * @return bool Whether it suceeded
314 * @access public
316 function PMA_exportDBCreate($db)
318 return true;
322 * Outputs the content of a table in XML format
324 * @param string $db database name
325 * @param string $table table name
326 * @param string $crlf the end of line sequence
327 * @param string $error_url the url to go back in case of error
328 * @param string $sql_query SQL query for obtaining data
329 * @return bool Whether it suceeded
331 * @access public
333 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
336 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
337 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
339 $columns_cnt = PMA_DBI_num_fields($result);
340 $columns = array();
341 for ($i = 0; $i < $columns_cnt; $i++) {
342 $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
344 unset($i);
346 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
347 if (!PMA_exportOutputHandler($buffer)) {
348 return false;
351 while ($record = PMA_DBI_fetch_row($result)) {
352 $buffer = ' <table name="' . htmlspecialchars($table) . '">' . $crlf;
353 for ($i = 0; $i < $columns_cnt; $i++) {
354 // If a cell is NULL, still export it to preserve the XML structure
355 if (!isset($record[$i]) || is_null($record[$i])) {
356 $record[$i] = 'NULL';
358 $buffer .= ' <column name="' . htmlspecialchars($columns[$i]) . '">' . htmlspecialchars((string)$record[$i])
359 . '</column>' . $crlf;
361 $buffer .= ' </table>' . $crlf;
363 if (!PMA_exportOutputHandler($buffer)) {
364 return false;
367 PMA_DBI_free_result($result);
370 return true;
371 } // end of the 'PMA_getTableXML()' function