diff options
author | Felipe Figueroa <amenadiel@gmail.com> | 2018-06-05 18:32:43 +0300 |
---|---|---|
committer | Felipe Figueroa <amenadiel@gmail.com> | 2018-06-05 18:32:43 +0300 |
commit | c213cdbb9b04386beb8a60e8f8e69454b4002a0c (patch) | |
tree | 6ad64aea338c836c0dbb1d96453cabe03d0817cc /src/controllers/DataexportController.php | |
parent | 9265414ac513c8ec572883983a58c82d3bda85a5 (diff) |
repairs export to html
Diffstat (limited to 'src/controllers/DataexportController.php')
-rw-r--r-- | src/controllers/DataexportController.php | 415 |
1 files changed, 224 insertions, 191 deletions
diff --git a/src/controllers/DataexportController.php b/src/controllers/DataexportController.php index 486da083..726babda 100644 --- a/src/controllers/DataexportController.php +++ b/src/controllers/DataexportController.php @@ -34,6 +34,10 @@ class DataexportController extends BaseController // What must we do in this case? Maybe redirect to the homepage? $format = 'N/A'; + + // force behavior to assume there is no pg_dump in the system + $forcemimic = false; + // If format is set, then perform the export if (!isset($_REQUEST['what'])) { return $this->doDefault(); @@ -49,7 +53,7 @@ class DataexportController extends BaseController case 'dataonly': // Check to see if they have pg_dump set up and if they do, use that // instead of custom dump code - if ($this->misc->isDumpEnabled() && ('copy' == $_REQUEST['d_format'] || 'sql' == $_REQUEST['d_format'])) { + if (!$forcemimic && $this->misc->isDumpEnabled() && ('copy' == $_REQUEST['d_format'] || 'sql' == $_REQUEST['d_format'])) { $this->prtrace('DUMP ENABLED, d_format is', $_REQUEST['d_format']); $dbexport_controller = new \PHPPgAdmin\Controller\DbexportController($this->getContainer()); @@ -63,7 +67,7 @@ class DataexportController extends BaseController case 'structureonly': // Check to see if they have pg_dump set up and if they do, use that // instead of custom dump code - if ($this->misc->isDumpEnabled()) { + if (!$forcemimic && $this->misc->isDumpEnabled()) { $dbexport_controller = new \PHPPgAdmin\Controller\DbexportController($this->getContainer()); return $dbexport_controller->render(); @@ -74,7 +78,7 @@ class DataexportController extends BaseController case 'structureanddata': // Check to see if they have pg_dump set up and if they do, use that // instead of custom dump code - if ($this->misc->isDumpEnabled()) { + if (!$forcemimic && $this->misc->isDumpEnabled()) { $dbexport_controller = new \PHPPgAdmin\Controller\DbexportController($this->getContainer()); return $dbexport_controller->render(); @@ -98,7 +102,6 @@ class DataexportController extends BaseController // if (!isset($_REQUEST['table']) && !isset($_REQUEST['query'])) // What must we do in this case? Maybe redirect to the homepage? - $format = 'N/A'; // If format is set, then perform the export if (!isset($_REQUEST['what'])) { return $this->doDefault(); @@ -144,6 +147,8 @@ class DataexportController extends BaseController // Set the schema search path if (isset($_REQUEST['search_path'])) { $data->setSearchPath(array_map('trim', explode(',', $_REQUEST['search_path']))); + } elseif (isset($_REQUEST['schema'])) { + $data->setSearchPath($_REQUEST['schema']); } $subject = $this->coalesceArr($_REQUEST, 'subject', 'table')['subject']; @@ -154,6 +159,8 @@ class DataexportController extends BaseController $status = $data->beginDump(); $this->prtrace('subject', $subject); $this->prtrace('object', $object); + $tabledefprefix = ''; + $tabledefsuffix = ''; // If the dump is not dataonly then dump the structure prefix if ('dataonly' != $_REQUEST['what']) { @@ -175,203 +182,26 @@ class DataexportController extends BaseController $rs = $data->dumpRelation($object, $oids); } else { $rs = $data->conn->Execute($_REQUEST['query']); + $this->prtrace('$_REQUEST[query]', $_REQUEST['query']); } - $this->prtrace('$_REQUEST[query]', $_REQUEST['query']); if ('copy' == $format) { - $data->fieldClean($object); - echo "COPY \"{$_REQUEST['table']}\""; - if ($oids) { - echo ' WITH OIDS'; - } - - echo " FROM stdin;\n"; - while (!$rs->EOF) { - $first = true; - //while (list($k, $v) = each($rs->fields)) { - foreach ($rs->fields as $k => $v) { - // Escape value - $v = $data->escapeBytea($v); - - // We add an extra escaping slash onto octal encoded characters - $v = preg_replace('/\\\\([0-7]{3})/', '\\\\\1', $v); - if ($first) { - echo (is_null($v)) ? '\\N' : $v; - $first = false; - } else { - echo "\t", (is_null($v)) ? '\\N' : $v; - } - } - echo "\n"; - $rs->moveNext(); - } - echo "\\.\n"; + $this->_mimicCopy($data, $object, $oids, $rs); } elseif ('html' == $format) { - echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n"; - echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"; - echo "<head>\r\n"; - echo "\t<title></title>\r\n"; - echo "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n"; - echo "</head>\r\n"; - echo "<body>\r\n"; - echo "<table class=\"phppgadmin\">\r\n"; - echo "\t<tr>\r\n"; - if (!$rs->EOF) { - // Output header row - $j = 0; - foreach ($rs->fields as $k => $v) { - $finfo = $rs->fetchField($j++); - if ($finfo->name == $data->id && !$oids) { - continue; - } - - echo "\t\t<th>", $this->misc->printVal($finfo->name, true), "</th>\r\n"; - } - } - echo "\t</tr>\r\n"; - while (!$rs->EOF) { - echo "\t<tr>\r\n"; - $j = 0; - foreach ($rs->fields as $k => $v) { - $finfo = $rs->fetchField($j++); - if ($finfo->name == $data->id && !$oids) { - continue; - } - - echo "\t\t<td>", $this->misc->printVal($v, true, $finfo->type), "</td>\r\n"; - } - echo "\t</tr>\r\n"; - $rs->moveNext(); - } - echo "</table>\r\n"; - echo "</body>\r\n"; - echo "</html>\r\n"; + $response = $response + ->withHeader('Content-type', 'text/html'); + $this->_mimicHtml($data, $object, $oids, $rs); } elseif ('xml' == $format) { - echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"; - echo "<data>\n"; - if (!$rs->EOF) { - // Output header row - $j = 0; - echo "\t<header>\n"; - foreach ($rs->fields as $k => $v) { - $finfo = $rs->fetchField($j++); - $name = htmlspecialchars($finfo->name); - $type = htmlspecialchars($finfo->type); - echo "\t\t<column name=\"{$name}\" type=\"{$type}\" />\n"; - } - echo "\t</header>\n"; - } - echo "\t<records>\n"; - while (!$rs->EOF) { - $j = 0; - echo "\t\t<row>\n"; - foreach ($rs->fields as $k => $v) { - $finfo = $rs->fetchField($j++); - $name = htmlspecialchars($finfo->name); - if (!is_null($v)) { - $v = htmlspecialchars($v); - } - - echo "\t\t\t<column name=\"{$name}\"", (is_null($v) ? ' null="null"' : ''), ">{$v}</column>\n"; - } - echo "\t\t</row>\n"; - $rs->moveNext(); - } - echo "\t</records>\n"; - echo "</data>\n"; + $response = $response + ->withHeader('Content-type', 'application/xml'); + $this->_mimicXml($data, $object, $oids, $rs); } elseif ('sql' == $format) { - $data->fieldClean($object); - while (!$rs->EOF) { - echo "INSERT INTO \"{$object}\" ("; - $first = true; - $j = 0; - foreach ($rs->fields as $k => $v) { - $finfo = $rs->fetchField($j++); - $k = $finfo->name; - // SQL (INSERT) format cannot handle oids - // if ($k == $data->id) continue; - // Output field - $data->fieldClean($k); - if ($first) { - echo "\"{$k}\""; - } else { - echo ", \"{$k}\""; - } - - if (!is_null($v)) { - // Output value - // addCSlashes converts all weird ASCII characters to octal representation, - // EXCEPT the 'special' ones like \r \n \t, etc. - $v = addcslashes($v, "\0..\37\177..\377"); - // We add an extra escaping slash onto octal encoded characters - $v = preg_replace('/\\\\([0-7]{3})/', '\\\1', $v); - // Finally, escape all apostrophes - $v = str_replace("'", "''", $v); - } - if ($first) { - $values = (is_null($v) ? 'NULL' : "'{$v}'"); - $first = false; - } else { - $values .= ', '.((is_null($v) ? 'NULL' : "'{$v}'")); - } - } - echo ") VALUES ({$values});\n"; - $rs->moveNext(); - } + $this->_mimicSQL($data, $object, $oids, $rs); } else { - switch ($format) { - case 'tab': - $sep = "\t"; - - break; - case 'csv': - default: - $sep = ','; - - break; - } - if (!$rs->EOF) { - // Output header row - $first = true; - foreach ($rs->fields as $k => $v) { - $finfo = $rs->fetchField($k); - $v = $finfo->name; - if (!is_null($v)) { - $v = str_replace('"', '""', $v); - } - - if ($first) { - echo "\"{$v}\""; - $first = false; - } else { - echo "{$sep}\"{$v}\""; - } - } - echo "\r\n"; - } - while (!$rs->EOF) { - $first = true; - foreach ($rs->fields as $k => $v) { - if (!is_null($v)) { - $v = str_replace('"', '""', $v); - } - - if ($first) { - echo (is_null($v)) ? '"\\N"' : "\"{$v}\""; - $first = false; - } else { - echo is_null($v) ? "{$sep}\"\\N\"" : "{$sep}\"{$v}\""; - } - } - echo "\r\n"; - $rs->moveNext(); - } + $this->_csvOrTab($data, $object, $oids, $rs, $format); } } - - // If the dump is not dataonly then dump the structure suffix if ('dataonly' != $_REQUEST['what']) { - // Set fetch mode back to ASSOC for the table suffix to work $data->conn->setFetchMode(\ADODB_FETCH_ASSOC); $tabledefsuffix = $data->getTableDefSuffix($object); $this->prtrace('tabledefsuffix', $tabledefsuffix); @@ -437,4 +267,207 @@ class DataexportController extends BaseController $this->printFooter(); } + + private function _mimicCopy($data, $object, $oids, $rs) + { + $data->fieldClean($object); + echo "COPY \"{$_REQUEST['table']}\""; + if ($oids) { + echo ' WITH OIDS'; + } + + echo " FROM stdin;\n"; + while (!$rs->EOF) { + $first = true; + //while (list($k, $v) = each($rs->fields)) { + foreach ($rs->fields as $k => $v) { + // Escape value + $v = $data->escapeBytea($v); + + // We add an extra escaping slash onto octal encoded characters + $v = preg_replace('/\\\\([0-7]{3})/', '\\\\\1', $v); + if ($first) { + echo (is_null($v)) ? '\\N' : $v; + $first = false; + } else { + echo "\t", (is_null($v)) ? '\\N' : $v; + } + } + echo "\n"; + $rs->moveNext(); + } + echo "\\.\n"; + } + + private function _mimicHtml($data, $object, $oids, $rs) + { + echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n"; + echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"; + echo "<head>\r\n"; + echo "\t<title></title>\r\n"; + echo "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n"; + echo "</head>\r\n"; + echo "<body>\r\n"; + echo "<table class=\"phppgadmin\">\r\n"; + echo "\t<tr>\r\n"; + if (!$rs->EOF) { + // Output header row + $j = 0; + foreach ($rs->fields as $k => $v) { + $finfo = $rs->fetchField($j++); + if ($finfo->name == $data->id && !$oids) { + continue; + } + + echo "\t\t<th>", $this->misc->printVal($finfo->name, 'verbatim'), "</th>\r\n"; + } + } + echo "\t</tr>\r\n"; + while (!$rs->EOF) { + echo "\t<tr>\r\n"; + $j = 0; + foreach ($rs->fields as $k => $v) { + $finfo = $rs->fetchField($j++); + if ($finfo->name == $data->id && !$oids) { + continue; + } + + echo "\t\t<td>", $this->misc->printVal($v, 'verbatim', $finfo->type), "</td>\r\n"; + } + echo "\t</tr>\r\n"; + $rs->moveNext(); + } + echo "</table>\r\n"; + echo "</body>\r\n"; + echo "</html>\r\n"; + } + + private function _mimicXml($data, $object, $oids, $rs) + { + echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"; + echo "<data>\n"; + if (!$rs->EOF) { + // Output header row + $j = 0; + echo "\t<header>\n"; + foreach ($rs->fields as $k => $v) { + $finfo = $rs->fetchField($j++); + $name = htmlspecialchars($finfo->name); + $type = htmlspecialchars($finfo->type); + echo "\t\t<column name=\"{$name}\" type=\"{$type}\" />\n"; + } + echo "\t</header>\n"; + } + echo "\t<records>\n"; + while (!$rs->EOF) { + $j = 0; + echo "\t\t<row>\n"; + foreach ($rs->fields as $k => $v) { + $finfo = $rs->fetchField($j++); + $name = htmlspecialchars($finfo->name); + if (!is_null($v)) { + $v = htmlspecialchars($v); + } + + echo "\t\t\t<column name=\"{$name}\"", (is_null($v) ? ' null="null"' : ''), ">{$v}</column>\n"; + } + echo "\t\t</row>\n"; + $rs->moveNext(); + } + echo "\t</records>\n"; + echo "</data>\n"; + } + + private function _mimicSQL($data, $object, $oids, $rs) + { + $data->fieldClean($object); + while (!$rs->EOF) { + echo "INSERT INTO \"{$object}\" ("; + $first = true; + $j = 0; + foreach ($rs->fields as $k => $v) { + $finfo = $rs->fetchField($j++); + $k = $finfo->name; + // SQL (INSERT) format cannot handle oids + // if ($k == $data->id) continue; + // Output field + $data->fieldClean($k); + if ($first) { + echo "\"{$k}\""; + } else { + echo ", \"{$k}\""; + } + + if (!is_null($v)) { + // Output value + // addCSlashes converts all weird ASCII characters to octal representation, + // EXCEPT the 'special' ones like \r \n \t, etc. + $v = addcslashes($v, "\0..\37\177..\377"); + // We add an extra escaping slash onto octal encoded characters + $v = preg_replace('/\\\\([0-7]{3})/', '\\\1', $v); + // Finally, escape all apostrophes + $v = str_replace("'", "''", $v); + } + if ($first) { + $values = (is_null($v) ? 'NULL' : "'{$v}'"); + $first = false; + } else { + $values .= ', '.((is_null($v) ? 'NULL' : "'{$v}'")); + } + } + echo ") VALUES ({$values});\n"; + $rs->moveNext(); + } + } + + private function _csvOrTab($data, $object, $oids, $rs, $format) + { + switch ($format) { + case 'tab': + $sep = "\t"; + + break; + case 'csv': + default: + $sep = ','; + + break; + } + if (!$rs->EOF) { + // Output header row + $first = true; + foreach ($rs->fields as $k => $v) { + $finfo = $rs->fetchField($k); + $v = $finfo->name; + if (!is_null($v)) { + $v = str_replace('"', '""', $v); + } + + if ($first) { + echo "\"{$v}\""; + $first = false; + } else { + echo "{$sep}\"{$v}\""; + } + } + echo "\r\n"; + } + while (!$rs->EOF) { + $first = true; + foreach ($rs->fields as $k => $v) { + if (!is_null($v)) { + $v = str_replace('"', '""', $v); + } + + if ($first) { + echo (is_null($v)) ? '"\\N"' : "\"{$v}\""; + $first = false; + } else { + echo is_null($v) ? "{$sep}\"\\N\"" : "{$sep}\"{$v}\""; + } + } + echo "\r\n"; + $rs->moveNext(); + } + } } |