xmlpage.class.inc.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // Copyright (C) 2010-2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Class XMLPage
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT."/application/webpage.class.inc.php");
  25. /**
  26. * Simple web page with no includes or fancy formatting, useful to generateXML documents
  27. * The page adds the content-type text/XML and the encoding into the headers
  28. */
  29. class XMLPage extends WebPage
  30. {
  31. /**
  32. * For big XML files, it's better NOT to store everything in memory and output the XML piece by piece
  33. */
  34. var $m_bPassThrough;
  35. var $m_bHeaderSent;
  36. function __construct($s_title, $bPassThrough = false)
  37. {
  38. parent::__construct($s_title);
  39. $this->m_bPassThrough = $bPassThrough;
  40. $this->m_bHeaderSent = false;
  41. $this->add_header("Content-type: text/xml; charset=utf-8");
  42. $this->add_header("Cache-control: no-cache");
  43. $this->add_header("Content-location: export.xml");
  44. }
  45. public function output()
  46. {
  47. if (!$this->m_bPassThrough)
  48. {
  49. $this->s_content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n".trim($this->s_content);
  50. $this->add_header("Content-Length: ".strlen($this->s_content));
  51. foreach($this->a_headers as $s_header)
  52. {
  53. header($s_header);
  54. }
  55. echo $this->s_content;
  56. }
  57. if (class_exists('MetaModel'))
  58. {
  59. MetaModel::RecordQueryTrace();
  60. }
  61. }
  62. public function add($sText)
  63. {
  64. if (!$this->m_bPassThrough)
  65. {
  66. parent::add($sText);
  67. }
  68. else
  69. {
  70. if ($this->m_bHeaderSent)
  71. {
  72. echo $sText;
  73. }
  74. else
  75. {
  76. $s_captured_output = ob_get_contents();
  77. ob_end_clean();
  78. foreach($this->a_headers as $s_header)
  79. {
  80. header($s_header);
  81. }
  82. echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n";
  83. echo trim($s_captured_output);
  84. echo trim($this->s_content);
  85. echo $sText;
  86. $this->m_bHeaderSent = true;
  87. }
  88. }
  89. }
  90. public function small_p($sText)
  91. {
  92. }
  93. public function table($aConfig, $aData, $aParams = array())
  94. {
  95. }
  96. public function TrashUnexpectedOutput()
  97. {
  98. if (!$this->m_bPassThrough)
  99. {
  100. parent::TrashUnexpectedOutput();
  101. }
  102. }
  103. }
  104. ?>