Fix for issue #291
[openemr.git] / phpmyadmin / tbl_export.php
bloba168c0a3c0c9a127dbbf87fcb9b29abdd192a551
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Table export
6 * @package PhpMyAdmin
7 */
9 /**
12 require_once 'libraries/common.inc.php';
13 require_once 'libraries/config/page_settings.class.php';
14 require_once 'libraries/display_export.lib.php';
16 PMA_PageSettings::showGroup('Export');
18 $response = PMA_Response::getInstance();
19 $header = $response->getHeader();
20 $scripts = $header->getScripts();
21 $scripts->addFile('export.js');
23 // Get the relation settings
24 $cfgRelation = PMA_getRelationsParam();
26 // handling export template actions
27 if (isset($_REQUEST['templateAction']) && $cfgRelation['exporttemplateswork']) {
29 if (isset($_REQUEST['templateId'])) {
30 $templateId = $_REQUEST['templateId'];
31 $id = PMA_Util::sqlAddSlashes($templateId);
34 $templateTable = PMA_Util::backquote($cfgRelation['db']) . '.'
35 . PMA_Util::backquote($cfgRelation['export_templates']);
36 $user = PMA_Util::sqlAddSlashes($GLOBALS['cfg']['Server']['user']);
38 switch ($_REQUEST['templateAction']) {
39 case 'create':
40 $query = "INSERT INTO " . $templateTable . "("
41 . " `username`, `export_type`,"
42 . " `template_name`, `template_data`"
43 . ") VALUES ("
44 . "'" . $user . "', "
45 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['exportType']) . "', "
46 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['templateName']) . "', "
47 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['templateData']) . "');";
48 break;
49 case 'load':
50 $query = "SELECT `template_data` FROM " . $templateTable
51 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
52 break;
53 case 'update':
54 $query = "UPDATE " . $templateTable . " SET `template_data` = "
55 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['templateData']) . "'"
56 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
57 break;
58 case 'delete':
59 $query = "DELETE FROM " . $templateTable
60 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
61 break;
62 default:
63 break;
66 $result = PMA_queryAsControlUser($query, false);
68 $response = PMA_Response::getInstance();
69 if (! $result) {
70 $error = $GLOBALS['dbi']->getError($GLOBALS['controllink']);
71 $response->isSuccess(false);
72 $response->addJSON('message', $error);
73 exit;
76 $response->isSuccess(true);
77 if ('create' == $_REQUEST['templateAction']) {
78 $response->addJSON(
79 'data',
80 PMA_getOptionsForExportTemplates($_REQUEST['exportType'])
82 } elseif ('load' == $_REQUEST['templateAction']) {
83 $data = null;
84 while ($row = $GLOBALS['dbi']->fetchAssoc(
85 $result, $GLOBALS['controllink']
86 )) {
87 $data = $row['template_data'];
89 $response->addJSON('data', $data);
91 $GLOBALS['dbi']->freeResult($result);
92 exit;
95 /**
96 * Gets tables information and displays top links
98 require_once 'libraries/tbl_common.inc.php';
99 $url_query .= '&amp;goto=tbl_export.php&amp;back=tbl_export.php';
100 require_once 'libraries/tbl_info.inc.php';
102 // Dump of a table
104 $export_page_title = __('View dump (schema) of table');
106 // When we have some query, we need to remove LIMIT from that and possibly
107 // generate WHERE clause (if we are asked to export specific rows)
109 if (! empty($sql_query)) {
110 $parser = new SqlParser\Parser($sql_query);
112 if ((!empty($parser->statements[0]))
113 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
116 // Finding aliases and removing them, but we keep track of them to be
117 // able to replace them in select expression too.
118 $aliases = array();
119 foreach ($parser->statements[0]->from as $from) {
120 if ((!empty($from->table)) && (!empty($from->alias))) {
121 $aliases[$from->alias] = $from->table;
122 // We remove the alias of the table because they are going to
123 // be replaced anyway.
124 $from->alias = null;
125 $from->expr = null; // Force rebuild.
129 // Rebuilding the SELECT and FROM clauses.
130 $replaces = array(
131 array(
132 'FROM', 'FROM ' . SqlParser\Components\ExpressionArray::build(
133 $parser->statements[0]->from
138 // Checking if the WHERE clause has to be replaced.
139 if ((!empty($where_clause)) && (is_array($where_clause))) {
140 $replaces[] = array(
141 'WHERE', 'WHERE (' . implode(') OR (', $where_clause) . ')'
145 // Preparing to remove the LIMIT clause.
146 $replaces[] = array('LIMIT', '');
148 // Replacing the clauses.
149 $sql_query = SqlParser\Utils\Query::replaceClauses(
150 $parser->statements[0],
151 $parser->list,
152 $replaces
155 // Removing the aliases by finding the alias followed by a dot.
156 $tokens = SqlParser\Lexer::getTokens($sql_query);
157 foreach ($aliases as $alias => $table) {
158 $tokens = SqlParser\Utils\Tokens::replaceTokens(
159 $tokens,
160 array(
161 array(
162 'value_str' => $alias,
164 array(
165 'type' => SqlParser\Token::TYPE_OPERATOR,
166 'value_str' => '.',
169 array(
170 new SqlParser\Token($table),
171 new SqlParser\Token('.',SqlParser\Token::TYPE_OPERATOR)
175 $sql_query = SqlParser\TokensList::build($tokens);
178 echo PMA_Util::getMessage(PMA_Message::success());
181 $export_type = 'table';
182 require_once 'libraries/display_export.inc.php';