ajax.dataloader.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // Copyright (C) 2010-2012 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. * Does load data from XML files (currently used in the setup only)
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * This page is called to perform "asynchronously" the setup actions
  26. * parameters
  27. * 'operation': one of 'compile_data_model', 'update_db_schema', 'after_db_creation', 'file'
  28. *
  29. * if 'operation' == 'update_db_schema':
  30. * 'mode': install | upgrade
  31. *
  32. * if 'operation' == 'after_db_creation':
  33. * 'mode': install | upgrade
  34. *
  35. * if 'operation' == 'file':
  36. * 'file': string Name of the file to load
  37. * 'session_status': string 'start', 'continue' or 'end'
  38. * 'percent': integer 0..100 the percentage of completion once the file has been loaded
  39. */
  40. define('SAFE_MINIMUM_MEMORY', 64*1024*1024);
  41. require_once('../approot.inc.php');
  42. require_once(APPROOT.'/application/utils.inc.php');
  43. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  44. require_once(APPROOT.'/setup/moduleinstaller.class.inc.php');
  45. $sMemoryLimit = trim(ini_get('memory_limit'));
  46. if (empty($sMemoryLimit))
  47. {
  48. // On some PHP installations, memory_limit does not exist as a PHP setting!
  49. // (encountered on a 5.2.0 under Windows)
  50. // In that case, ini_set will not work, let's keep track of this and proceed with the data load
  51. SetupPage::log_info("No memory limit has been defined in this instance of PHP");
  52. }
  53. else
  54. {
  55. // Check that the limit will allow us to load the data
  56. //
  57. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  58. if ($iMemoryLimit < SAFE_MINIMUM_MEMORY)
  59. {
  60. if (ini_set('memory_limit', SAFE_MINIMUM_MEMORY) === FALSE)
  61. {
  62. SetupPage::log_error("memory_limit is too small: $iMemoryLimit and can not be increased by the script itself.");
  63. }
  64. else
  65. {
  66. SetupPage::log_info("memory_limit increased from $iMemoryLimit to ".SAFE_MINIMUM_MEMORY.".");
  67. }
  68. }
  69. }
  70. function FatalErrorCatcher($sOutput)
  71. {
  72. if ( preg_match('|<phpfatalerror>.*</phpfatalerror>|s', $sOutput, $aMatches) )
  73. {
  74. header("HTTP/1.0 500 Internal server error.");
  75. foreach ($aMatches as $sMatch)
  76. {
  77. $errors .= strip_tags($sMatch)."\n";
  78. }
  79. $sOutput = "$errors\n";
  80. // Logging to a file does not work if the whole memory is exhausted...
  81. //SetupPage::log_error("Fatal error - in $__FILE__ , $errors");
  82. }
  83. return $sOutput;
  84. }
  85. //Define some bogus, invalid HTML tags that no sane
  86. //person would ever put in an actual document and tell
  87. //PHP to delimit fatal error warnings with them.
  88. ini_set('error_prepend_string', '<phpfatalerror>');
  89. ini_set('error_append_string', '</phpfatalerror>');
  90. // Starts the capture of the ouput, and sets a filter to capture the fatal errors.
  91. ob_start('FatalErrorCatcher'); // Start capturing the output, and pass it through the fatal error catcher
  92. require_once(APPROOT.'/core/config.class.inc.php');
  93. require_once(APPROOT.'/core/log.class.inc.php');
  94. require_once(APPROOT.'/core/kpi.class.inc.php');
  95. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  96. require_once('./xmldataloader.class.inc.php');
  97. require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
  98. // Never cache this page
  99. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  100. header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  101. /**
  102. * Main program
  103. */
  104. $sOperation = Utils::ReadParam('operation', '');
  105. try
  106. {
  107. switch($sOperation)
  108. {
  109. case 'async_action':
  110. ini_set('max_execution_time', max(240, ini_get('max_execution_time')));
  111. // While running the setup it is desirable to see any error that may happen
  112. ini_set('display_errors', true);
  113. ini_set('display_startup_errors', true);
  114. require_once(APPROOT.'/setup/wizardcontroller.class.inc.php');
  115. require_once(APPROOT.'/setup/wizardsteps.class.inc.php');
  116. $sClass = utils::ReadParam('step_class', '');
  117. $sState = utils::ReadParam('step_state', '');
  118. $sActionCode = utils::ReadParam('code', '');
  119. $aParams = utils::ReadParam('params', array(), false, 'raw_data');
  120. $oPage = new ajax_page('');
  121. $oDummyController = new WizardController('');
  122. if (is_subclass_of($sClass, 'WizardStep'))
  123. {
  124. $oStep = new $sClass($oDummyController, $sState);
  125. $sConfigFile = utils::GetConfigFilePath();
  126. if (file_exists($sConfigFile) && !is_writable($sConfigFile) && $oStep->RequiresWritableConfig())
  127. {
  128. $oPage->error("<b>Error:</b> the configuration file '".$sConfigFile."' already exists and cannot be overwritten.");
  129. $oPage->p("The wizard cannot modify the configuration file for you. If you want to upgrade ".ITOP_APPLICATION.", make sure that the file '<b>".realpath($sConfigFile)."</b>' can be modified by the web server.");
  130. $oPage->output();
  131. }
  132. else
  133. {
  134. $oStep->AsyncAction($oPage, $sActionCode, $aParams);
  135. }
  136. }
  137. $oPage->output();
  138. break;
  139. default:
  140. throw(new Exception("Error unsupported operation '$sOperation'"));
  141. }
  142. }
  143. catch(Exception $e)
  144. {
  145. header("HTTP/1.0 500 Internal server error.");
  146. echo "<p>An error happened while processing the installation:</p>\n";
  147. echo '<p>'.$e."</p>\n";
  148. SetupPage::log_error("An error happened while processing the installation: ".$e);
  149. }
  150. if (function_exists('memory_get_peak_usage'))
  151. {
  152. if ($sOperation == 'file')
  153. {
  154. SetupPage::log_info("loading file '$sFileName', peak memory usage. ".memory_get_peak_usage());
  155. }
  156. else
  157. {
  158. SetupPage::log_info("operation '$sOperation', peak memory usage. ".memory_get_peak_usage());
  159. }
  160. }
  161. ?>