/download.php

https://github.com/fevrier/glossaire · PHP · 114 lines · 72 code · 20 blank · 22 comment · 7 complexity · a3a6b4c0b9d3927d70645f02ef68bbe7 MD5 · raw file

  1. <?php
  2. /*
  3. * Glossary's search engine.
  4. *
  5. * Copyright (C) 2006 Jonathan Ernst
  6. * Copyright (C) 2006-2019 Jean-Philippe Guérard
  7. * Copyright (C) 2019 Stéphane Aulery
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. * You should have received a copy of the GNU General Public License
  18. * along with the Glossary. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. error_reporting(E_ALL);
  21. ini_set("display_errors", 1);
  22. require("./includes/constants.inc.php");
  23. require("./includes/config.inc.php");
  24. $format = array_key_exists("f", $_GET) ? $_GET["f"] : "";
  25. $format = strtolower($format);
  26. if (!in_array($format, explode(",", GLO_EXPORT_FORMATS))) {
  27. echo _('Error: a GET parameter of name "f" must be defined to specifiy the output format. The available formats are:');
  28. echo " " . GLO_EXPORT_FORMATS . "\n";
  29. exit(1);
  30. }
  31. require("./includes/mysql.inc.php");
  32. $sQuery = "
  33. SELECT
  34. lng_source,
  35. lng_target,
  36. source,
  37. comment
  38. FROM glossary
  39. WHERE
  40. state != 'deleted'
  41. AND source NOT LIKE '*%*'
  42. ORDER BY lng_source ASC
  43. ";
  44. $hResult = mysqli_query($mysqllink, $sQuery);
  45. $colNames = array(
  46. $config['lng_source'],
  47. $config['lng_target'],
  48. _("comment"),
  49. _("source"),
  50. );
  51. header('Content-Disposition: attachment; filename="export.'.$format.'"');
  52. // We asked a file
  53. switch($format) {
  54. case "csv":
  55. // cf. https://tools.ietf.org/html/rfc4180
  56. header('Content-type: text/csv');
  57. $qt = '"';
  58. $sep = ",";
  59. $eol = "\r\n";
  60. for ($i = 0; $i < count($colNames); $i++) {
  61. echo $qt.$colNames[$i].$qt;
  62. if ($i + 1 < count($colNames)) {
  63. echo $sep;
  64. }
  65. }
  66. echo $eol;
  67. while($aRow = mysqli_fetch_row($hResult)) {
  68. for($i = 0; $i < count($aRow); $i++) {
  69. echo $qt . str_replace($qt, $qt.$qt, $aRow[$i]) . $qt;
  70. if($i + 1 < count($aRow)) echo $sep;
  71. }
  72. echo $eol;
  73. }
  74. break;
  75. case "xml":
  76. header('Content-type: text/xml');
  77. $gloXML = new SimpleXMLElement("<glossary></glossary>");
  78. while($aRow = mysqli_fetch_row($hResult)) {
  79. $entryXML = $gloXML->addChild('entry');
  80. for($i = 0; $i < count($colNames); $i++) {
  81. $entryXML->addChild($colNames[$i], $aRow[$i]);
  82. }
  83. }
  84. echo $gloXML->asXML();
  85. $gloXML = NULL;
  86. break;
  87. default:
  88. header('Content-type: text/plain');
  89. echo _("Error: unknown format: " . $format . ". This point is not reachable in normal case...");
  90. exit(1);
  91. break;
  92. }