ajax.dataloader.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Does load data from XML files (currently used in the setup only)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * This page is called to load "asynchronously" some xml file into the database
  26. * parameters
  27. * 'file' string Name of the file to load
  28. * 'session_status' string 'start', 'continue' or 'end'
  29. * 'percent' integer 0..100 the percentage of completion once the file has been loaded
  30. */
  31. define('SAFE_MINIMUM_MEMORY', 32*1024*1024);
  32. require_once('../application/utils.inc.php');
  33. require_once('./setuppage.class.inc.php');
  34. $sMemoryLimit = trim(ini_get('memory_limit'));
  35. if (empty($sMemoryLimit))
  36. {
  37. // On some PHP installations, memory_limit does not exist as a PHP setting!
  38. // (encountered on a 5.2.0 under Windows)
  39. // In that case, ini_set will not work, let's keep track of this and proceed with the data load
  40. SetupWebPage::log_info("No memory limit has been defined in this instance of PHP");
  41. }
  42. else
  43. {
  44. // Check that the limit will allow us to load the data
  45. //
  46. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  47. if ($iMemoryLimit < SAFE_MINIMUM_MEMORY)
  48. {
  49. if (ini_set('memory_limit', SAFE_MINIMUM_MEMORY) === FALSE)
  50. {
  51. SetupWebPage::log_error("memory_limit is too small: $iMemoryLimit and can not be increased by the script itself.");
  52. }
  53. else
  54. {
  55. SetupWebPage::log_info("memory_limit increased from $iMemoryLimit to ".SAFE_MINIMUM_MEMORY.".");
  56. }
  57. }
  58. }
  59. function FatalErrorCatcher($sOutput)
  60. {
  61. if ( preg_match('|<phpfatalerror>.*</phpfatalerror>|s', $sOutput, $aMatches) )
  62. {
  63. header("HTTP/1.0 500 Internal server error.");
  64. foreach ($aMatches as $sMatch)
  65. {
  66. $errors .= strip_tags($sMatch)."\n";
  67. }
  68. $sOutput = "$errors\n";
  69. // Logging to a file does not work if the whole memory is exhausted...
  70. //SetupWebPage::log_error("Fatal error - in $__FILE__ , $errors");
  71. }
  72. return $sOutput;
  73. }
  74. //Define some bogus, invalid HTML tags that no sane
  75. //person would ever put in an actual document and tell
  76. //PHP to delimit fatal error warnings with them.
  77. ini_set('error_prepend_string', '<phpfatalerror>');
  78. ini_set('error_append_string', '</phpfatalerror>');
  79. // Starts the capture of the ouput, and sets a filter to capture the fatal errors.
  80. ob_start('FatalErrorCatcher'); // Start capturing the output, and pass it through the fatal error catcher
  81. require_once('../core/config.class.inc.php');
  82. require_once('../core/log.class.inc.php');
  83. require_once('../core/cmdbsource.class.inc.php');
  84. require_once('./xmldataloader.class.inc.php');
  85. define('TMP_CONFIG_FILE', '../tmp-config-itop.php');
  86. //define('FINAL_CONFIG_FILE', '../config-itop.php');
  87. // Never cache this page
  88. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  89. header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  90. /**
  91. * Main program
  92. */
  93. $sFileName = Utils::ReadParam('file', '');
  94. $sSessionStatus = Utils::ReadParam('session_status', '');
  95. $iPercent = (integer)Utils::ReadParam('percent', 0);
  96. SetupWebPage::log_info("Loading file: $sFileName");
  97. try
  98. {
  99. if (empty($sFileName) || !file_exists($sFileName))
  100. {
  101. throw(new Exception("File $sFileName does not exist"));
  102. }
  103. $oDataLoader = new XMLDataLoader(TMP_CONFIG_FILE); // When called by the wizard, the final config is not yet there
  104. if ($sSessionStatus == 'start')
  105. {
  106. $oChange = MetaModel::NewObject("CMDBChange");
  107. $oChange->Set("date", time());
  108. $oChange->Set("userinfo", "Initialization");
  109. $iChangeId = $oChange->DBInsert();
  110. SetupWebPage::log_info("starting data load session");
  111. $oDataLoader->StartSession($oChange);
  112. }
  113. $oDataLoader->LoadFile($sFileName);
  114. $sResult = sprintf("loading of %s done. (Overall %d %% completed).", basename($sFileName), $iPercent);
  115. echo $sResult;
  116. SetupWebPage::log_info($sResult);
  117. if ($sSessionStatus == 'end')
  118. {
  119. $oDataLoader->EndSession();
  120. SetupWebPage::log_info("ending data load session");
  121. }
  122. }
  123. catch(Exception $e)
  124. {
  125. echo "<p>An error happened while loading the data</p>\n";
  126. echo '<p>'.$e."</p>\n";
  127. SetupWebPage::log_error("An error happened while loading the data. ".$e);
  128. }
  129. if (function_exists('memory_get_peak_usage'))
  130. {
  131. SetupWebPage::log_info("loading file '$sFileName', peak memory usage. ".memory_get_peak_usage());
  132. }
  133. ?>