ajax.dataloader.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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('../approot.inc.php');
  33. require_once(APPROOT.'/application/utils.inc.php');
  34. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  35. $sMemoryLimit = trim(ini_get('memory_limit'));
  36. if (empty($sMemoryLimit))
  37. {
  38. // On some PHP installations, memory_limit does not exist as a PHP setting!
  39. // (encountered on a 5.2.0 under Windows)
  40. // In that case, ini_set will not work, let's keep track of this and proceed with the data load
  41. SetupWebPage::log_info("No memory limit has been defined in this instance of PHP");
  42. }
  43. else
  44. {
  45. // Check that the limit will allow us to load the data
  46. //
  47. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  48. if ($iMemoryLimit < SAFE_MINIMUM_MEMORY)
  49. {
  50. if (ini_set('memory_limit', SAFE_MINIMUM_MEMORY) === FALSE)
  51. {
  52. SetupWebPage::log_error("memory_limit is too small: $iMemoryLimit and can not be increased by the script itself.");
  53. }
  54. else
  55. {
  56. SetupWebPage::log_info("memory_limit increased from $iMemoryLimit to ".SAFE_MINIMUM_MEMORY.".");
  57. }
  58. }
  59. }
  60. function FatalErrorCatcher($sOutput)
  61. {
  62. if ( preg_match('|<phpfatalerror>.*</phpfatalerror>|s', $sOutput, $aMatches) )
  63. {
  64. header("HTTP/1.0 500 Internal server error.");
  65. foreach ($aMatches as $sMatch)
  66. {
  67. $errors .= strip_tags($sMatch)."\n";
  68. }
  69. $sOutput = "$errors\n";
  70. // Logging to a file does not work if the whole memory is exhausted...
  71. //SetupWebPage::log_error("Fatal error - in $__FILE__ , $errors");
  72. }
  73. return $sOutput;
  74. }
  75. //Define some bogus, invalid HTML tags that no sane
  76. //person would ever put in an actual document and tell
  77. //PHP to delimit fatal error warnings with them.
  78. ini_set('error_prepend_string', '<phpfatalerror>');
  79. ini_set('error_append_string', '</phpfatalerror>');
  80. // Starts the capture of the ouput, and sets a filter to capture the fatal errors.
  81. ob_start('FatalErrorCatcher'); // Start capturing the output, and pass it through the fatal error catcher
  82. require_once(APPROOT.'/core/config.class.inc.php');
  83. require_once(APPROOT.'/core/log.class.inc.php');
  84. require_once(APPROOT.'/core/kpi.class.inc.php');
  85. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  86. require_once('./xmldataloader.class.inc.php');
  87. define('TMP_CONFIG_FILE', APPROOT.'/tmp-config-itop.php');
  88. //define('FINAL_CONFIG_FILE', APPROOT.'/config-itop.php');
  89. // Never cache this page
  90. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  91. header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  92. /**
  93. * Main program
  94. */
  95. $sFileName = Utils::ReadParam('file', '');
  96. $sSessionStatus = Utils::ReadParam('session_status', '');
  97. $iPercent = (integer)Utils::ReadParam('percent', 0);
  98. SetupWebPage::log_info("Loading file: $sFileName");
  99. try
  100. {
  101. if (empty($sFileName) || !file_exists($sFileName))
  102. {
  103. throw(new Exception("File $sFileName does not exist"));
  104. }
  105. $oDataLoader = new XMLDataLoader(TMP_CONFIG_FILE); // When called by the wizard, the final config is not yet there
  106. if ($sSessionStatus == 'start')
  107. {
  108. $oChange = MetaModel::NewObject("CMDBChange");
  109. $oChange->Set("date", time());
  110. $oChange->Set("userinfo", "Initialization");
  111. $iChangeId = $oChange->DBInsert();
  112. SetupWebPage::log_info("starting data load session");
  113. $oDataLoader->StartSession($oChange);
  114. }
  115. $oDataLoader->LoadFile($sFileName);
  116. $sResult = sprintf("loading of %s done. (Overall %d %% completed).", basename($sFileName), $iPercent);
  117. //echo $sResult;
  118. SetupWebPage::log_info($sResult);
  119. if ($sSessionStatus == 'end')
  120. {
  121. $oDataLoader->EndSession();
  122. SetupWebPage::log_info("ending data load session");
  123. }
  124. }
  125. catch(Exception $e)
  126. {
  127. header("HTTP/1.0 500 Internal server error.");
  128. echo "<p>An error happened while loading the data</p>\n";
  129. echo '<p>'.$e."</p>\n";
  130. SetupWebPage::log_error("An error happened while loading the data. ".$e);
  131. }
  132. if (function_exists('memory_get_peak_usage'))
  133. {
  134. SetupWebPage::log_info("loading file '$sFileName', peak memory usage. ".memory_get_peak_usage());
  135. }
  136. ?>