PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/phpPgAdmin-5.0.4/dataexport.php

#
PHP | 345 lines | 286 code | 20 blank | 39 comment | 52 complexity | 98880d35a046c05ddbf93624fc9db9ef MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Does an export to the screen or as a download. This checks to
  4. * see if they have pg_dump set up, and will use it if possible.
  5. *
  6. * $Id: dataexport.php,v 1.26 2007/07/12 19:26:22 xzilla Exp $
  7. */
  8. $extensions = array(
  9. 'sql' => 'sql',
  10. 'copy' => 'sql',
  11. 'csv' => 'csv',
  12. 'tab' => 'txt',
  13. 'html' => 'html',
  14. 'xml' => 'xml'
  15. );
  16. // Prevent timeouts on large exports (non-safe mode only)
  17. if (!ini_get('safe_mode')) set_time_limit(0);
  18. // if (!isset($_REQUEST['table']) && !isset($_REQUEST['query']))
  19. // What must we do in this case? Maybe redirect to the homepage?
  20. // If format is set, then perform the export
  21. if (isset($_REQUEST['what'])) {
  22. // Include application functions
  23. $_no_output = true;
  24. include_once('./libraries/lib.inc.php');
  25. switch ($_REQUEST['what']) {
  26. case 'dataonly':
  27. // Check to see if they have pg_dump set up and if they do, use that
  28. // instead of custom dump code
  29. if ($misc->isDumpEnabled()
  30. && ($_REQUEST['d_format'] == 'copy' || $_REQUEST['d_format'] == 'sql')) {
  31. include('./dbexport.php');
  32. exit;
  33. }
  34. else {
  35. $format = $_REQUEST['d_format'];
  36. $oids = isset($_REQUEST['d_oids']);
  37. }
  38. break;
  39. case 'structureonly':
  40. // Check to see if they have pg_dump set up and if they do, use that
  41. // instead of custom dump code
  42. if ($misc->isDumpEnabled()) {
  43. include('./dbexport.php');
  44. exit;
  45. }
  46. else $clean = isset($_REQUEST['s_clean']);
  47. break;
  48. case 'structureanddata':
  49. // Check to see if they have pg_dump set up and if they do, use that
  50. // instead of custom dump code
  51. if ($misc->isDumpEnabled()) {
  52. include('./dbexport.php');
  53. exit;
  54. }
  55. else {
  56. $format = $_REQUEST['sd_format'];
  57. $clean = isset($_REQUEST['sd_clean']);
  58. $oids = isset($_REQUEST['sd_oids']);
  59. }
  60. break;
  61. }
  62. // Make it do a download, if necessary
  63. if ($_REQUEST['output'] == 'download') {
  64. // Set headers. MSIE is totally broken for SSL downloading, so
  65. // we need to have it download in-place as plain text
  66. if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && isset($_SERVER['HTTPS'])) {
  67. header('Content-Type: text/plain');
  68. }
  69. else {
  70. header('Content-Type: application/download');
  71. if (isset($extensions[$format]))
  72. $ext = $extensions[$format];
  73. else
  74. $ext = 'txt';
  75. header('Content-Disposition: attachment; filename=dump.' . $ext);
  76. }
  77. }
  78. else {
  79. header('Content-Type: text/plain');
  80. }
  81. if (isset($_REQUEST['query'])) $_REQUEST['query'] = trim(urldecode($_REQUEST['query']));
  82. // Set the schema search path
  83. if (isset($_REQUEST['search_path'])) {
  84. $data->setSearchPath(array_map('trim',explode(',',$_REQUEST['search_path'])));
  85. }
  86. // Set up the dump transaction
  87. $status = $data->beginDump();
  88. // If the dump is not dataonly then dump the structure prefix
  89. if ($_REQUEST['what'] != 'dataonly')
  90. echo $data->getTableDefPrefix($_REQUEST['table'], $clean);
  91. // If the dump is not structureonly then dump the actual data
  92. if ($_REQUEST['what'] != 'structureonly') {
  93. // Get database encoding
  94. $dbEncoding = $data->getDatabaseEncoding();
  95. // Set fetch mode to NUM so that duplicate field names are properly returned
  96. $data->conn->setFetchMode(ADODB_FETCH_NUM);
  97. // Execute the query, if set, otherwise grab all rows from the table
  98. if (isset($_REQUEST['table']))
  99. $rs = $data->dumpRelation($_REQUEST['table'], $oids);
  100. else
  101. $rs = $data->conn->Execute($_REQUEST['query']);
  102. if ($format == 'copy') {
  103. $data->fieldClean($_REQUEST['table']);
  104. echo "COPY \"{$_REQUEST['table']}\"";
  105. if ($oids) echo " WITH OIDS";
  106. echo " FROM stdin;\n";
  107. while (!$rs->EOF) {
  108. $first = true;
  109. while(list($k, $v) = each($rs->fields)) {
  110. // Escape value
  111. $v = $data->escapeBytea($v);
  112. // We add an extra escaping slash onto octal encoded characters
  113. $v = preg_replace('/\\\\([0-7]{3})/', '\\\\\1', $v);
  114. if ($first) {
  115. echo (is_null($v)) ? '\\N' : $v;
  116. $first = false;
  117. }
  118. else echo "\t", (is_null($v)) ? '\\N' : $v;
  119. }
  120. echo "\n";
  121. $rs->moveNext();
  122. }
  123. echo "\\.\n";
  124. }
  125. elseif ($format == 'html') {
  126. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n";
  127. echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n";
  128. echo "<head>\r\n";
  129. echo "\t<title></title>\r\n";
  130. echo "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset={$data->codemap[$dbEncoding]}\" />\r\n";
  131. echo "</head>\r\n";
  132. echo "<body>\r\n";
  133. echo "<table class=\"phppgadmin\">\r\n";
  134. echo "\t<tr>\r\n";
  135. if (!$rs->EOF) {
  136. // Output header row
  137. $j = 0;
  138. foreach ($rs->fields as $k => $v) {
  139. $finfo = $rs->fetchField($j++);
  140. if ($finfo->name == $data->id && !$oids) continue;
  141. echo "\t\t<th>", $misc->printVal($finfo->name, true), "</th>\r\n";
  142. }
  143. }
  144. echo "\t</tr>\r\n";
  145. while (!$rs->EOF) {
  146. echo "\t<tr>\r\n";
  147. $j = 0;
  148. foreach ($rs->fields as $k => $v) {
  149. $finfo = $rs->fetchField($j++);
  150. if ($finfo->name == $data->id && !$oids) continue;
  151. echo "\t\t<td>", $misc->printVal($v, true, $finfo->type), "</td>\r\n";
  152. }
  153. echo "\t</tr>\r\n";
  154. $rs->moveNext();
  155. }
  156. echo "</table>\r\n";
  157. echo "</body>\r\n";
  158. echo "</html>\r\n";
  159. }
  160. elseif ($format == 'xml') {
  161. echo "<?xml version=\"1.0\"";
  162. if (isset($data->codemap[$dbEncoding]))
  163. echo " encoding=\"{$data->codemap[$dbEncoding]}\"";
  164. echo " ?>\n";
  165. echo "<data>\n";
  166. if (!$rs->EOF) {
  167. // Output header row
  168. $j = 0;
  169. echo "\t<header>\n";
  170. foreach ($rs->fields as $k => $v) {
  171. $finfo = $rs->fetchField($j++);
  172. $name = htmlspecialchars($finfo->name);
  173. $type = htmlspecialchars($finfo->type);
  174. echo "\t\t<column name=\"{$name}\" type=\"{$type}\" />\n";
  175. }
  176. echo "\t</header>\n";
  177. }
  178. echo "\t<records>\n";
  179. while (!$rs->EOF) {
  180. $j = 0;
  181. echo "\t\t<row>\n";
  182. foreach ($rs->fields as $k => $v) {
  183. $finfo = $rs->fetchField($j++);
  184. $name = htmlspecialchars($finfo->name);
  185. if (!is_null($v)) $v = htmlspecialchars($v);
  186. echo "\t\t\t<column name=\"{$name}\"", (is_null($v) ? ' null="null"' : ''), ">{$v}</column>\n";
  187. }
  188. echo "\t\t</row>\n";
  189. $rs->moveNext();
  190. }
  191. echo "\t</records>\n";
  192. echo "</data>\n";
  193. }
  194. elseif ($format == 'sql') {
  195. $data->fieldClean($_REQUEST['table']);
  196. while (!$rs->EOF) {
  197. echo "INSERT INTO \"{$_REQUEST['table']}\" (";
  198. $first = true;
  199. $j = 0;
  200. foreach ($rs->fields as $k => $v) {
  201. $finfo = $rs->fetchField($j++);
  202. $k = $finfo->name;
  203. // SQL (INSERT) format cannot handle oids
  204. // if ($k == $data->id) continue;
  205. // Output field
  206. $data->fieldClean($k);
  207. if ($first) echo "\"{$k}\"";
  208. else echo ", \"{$k}\"";
  209. if (!is_null($v)) {
  210. // Output value
  211. // addCSlashes converts all weird ASCII characters to octal representation,
  212. // EXCEPT the 'special' ones like \r \n \t, etc.
  213. $v = addCSlashes($v, "\0..\37\177..\377");
  214. // We add an extra escaping slash onto octal encoded characters
  215. $v = preg_replace('/\\\\([0-7]{3})/', '\\\1', $v);
  216. // Finally, escape all apostrophes
  217. $v = str_replace("'", "''", $v);
  218. }
  219. if ($first) {
  220. $values = (is_null($v) ? 'NULL' : "'{$v}'");
  221. $first = false;
  222. }
  223. else $values .= ', ' . ((is_null($v) ? 'NULL' : "'{$v}'"));
  224. }
  225. echo ") VALUES ({$values});\n";
  226. $rs->moveNext();
  227. }
  228. }
  229. else {
  230. switch ($format) {
  231. case 'tab':
  232. $sep = "\t";
  233. break;
  234. case 'csv':
  235. default:
  236. $sep = ',';
  237. break;
  238. }
  239. if (!$rs->EOF) {
  240. // Output header row
  241. $first = true;
  242. foreach ($rs->fields as $k => $v) {
  243. $finfo = $rs->fetchField($k);
  244. $v = $finfo->name;
  245. if (!is_null($v)) $v = str_replace('"', '""', $v);
  246. if ($first) {
  247. echo "\"{$v}\"";
  248. $first = false;
  249. }
  250. else echo "{$sep}\"{$v}\"";
  251. }
  252. echo "\r\n";
  253. }
  254. while (!$rs->EOF) {
  255. $first = true;
  256. foreach ($rs->fields as $k => $v) {
  257. if (!is_null($v)) $v = str_replace('"', '""', $v);
  258. if ($first) {
  259. echo (is_null($v)) ? "\"\\N\"" : "\"{$v}\"";
  260. $first = false;
  261. }
  262. else echo is_null($v) ? "{$sep}\"\\N\"" : "{$sep}\"{$v}\"";
  263. }
  264. echo "\r\n";
  265. $rs->moveNext();
  266. }
  267. }
  268. }
  269. // If the dump is not dataonly then dump the structure suffix
  270. if ($_REQUEST['what'] != 'dataonly') {
  271. // Set fetch mode back to ASSOC for the table suffix to work
  272. $data->conn->setFetchMode(ADODB_FETCH_ASSOC);
  273. echo $data->getTableDefSuffix($_REQUEST['table']);
  274. }
  275. // Finish the dump transaction
  276. $status = $data->endDump();
  277. }
  278. else {
  279. // Include application functions
  280. include_once('./libraries/lib.inc.php');
  281. $misc->printHeader($lang['strexport']);
  282. $misc->printBody();
  283. $misc->printTrail(isset($_REQUEST['subject']) ? $_REQUEST['subject'] : 'database');
  284. $misc->printTitle($lang['strexport']);
  285. if (isset($msg)) $misc->printMsg($msg);
  286. echo "<form action=\"dataexport.php\" method=\"post\">\n";
  287. echo "<table>\n";
  288. echo "<tr><th class=\"data\">{$lang['strformat']}:</th><td><select name=\"d_format\">\n";
  289. // COPY and SQL require a table
  290. if (isset($_REQUEST['table'])) {
  291. echo "<option value=\"copy\">COPY</option>\n";
  292. echo "<option value=\"sql\">SQL</option>\n";
  293. }
  294. echo "<option value=\"csv\">CSV</option>\n";
  295. echo "<option value=\"tab\">{$lang['strtabbed']}</option>\n";
  296. echo "<option value=\"html\">XHTML</option>\n";
  297. echo "<option value=\"xml\">XML</option>\n";
  298. echo "</select></td></tr>";
  299. echo "</table>\n";
  300. echo "<h3>{$lang['stroptions']}</h3>\n";
  301. echo "<p><input type=\"radio\" id=\"output1\" name=\"output\" value=\"show\" checked=\"checked\" /><label for=\"output1\">{$lang['strshow']}</label>\n";
  302. echo "<br/><input type=\"radio\" id=\"output2\" name=\"output\" value=\"download\" /><label for=\"output2\">{$lang['strdownload']}</label></p>\n";
  303. echo "<p><input type=\"hidden\" name=\"action\" value=\"export\" />\n";
  304. echo "<input type=\"hidden\" name=\"what\" value=\"dataonly\" />\n";
  305. if (isset($_REQUEST['table'])) {
  306. echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
  307. }
  308. echo "<input type=\"hidden\" name=\"query\" value=\"", htmlspecialchars(urlencode($_REQUEST['query'])), "\" />\n";
  309. if (isset($_REQUEST['search_path'])) {
  310. echo "<input type=\"hidden\" name=\"search_path\" value=\"", htmlspecialchars($_REQUEST['search_path']), "\" />\n";
  311. }
  312. echo $misc->form;
  313. echo "<input type=\"submit\" value=\"{$lang['strexport']}\" /></p>\n";
  314. echo "</form>\n";
  315. $misc->printFooter();
  316. }
  317. ?>