ajax.dataloader.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. ini_set('max_execution_time', max(3600, ini_get('max_execution_time'))); // Under Windows SQL/backup operations are part of the timeout and require extra time
  46. $sMemoryLimit = trim(ini_get('memory_limit'));
  47. if (empty($sMemoryLimit))
  48. {
  49. // On some PHP installations, memory_limit does not exist as a PHP setting!
  50. // (encountered on a 5.2.0 under Windows)
  51. // In that case, ini_set will not work, let's keep track of this and proceed with the data load
  52. SetupPage::log_info("No memory limit has been defined in this instance of PHP");
  53. }
  54. else
  55. {
  56. // Check that the limit will allow us to load the data
  57. //
  58. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  59. if ($iMemoryLimit < SAFE_MINIMUM_MEMORY)
  60. {
  61. if (ini_set('memory_limit', SAFE_MINIMUM_MEMORY) === FALSE)
  62. {
  63. SetupPage::log_error("memory_limit is too small: $iMemoryLimit and can not be increased by the script itself.");
  64. }
  65. else
  66. {
  67. SetupPage::log_info("memory_limit increased from $iMemoryLimit to ".SAFE_MINIMUM_MEMORY.".");
  68. }
  69. }
  70. }
  71. function FatalErrorCatcher($sOutput)
  72. {
  73. if ( preg_match('|<phpfatalerror>.*</phpfatalerror>|s', $sOutput, $aMatches) )
  74. {
  75. header("HTTP/1.0 500 Internal server error.");
  76. $errors = '';
  77. foreach ($aMatches as $sMatch)
  78. {
  79. $errors .= strip_tags($sMatch)."\n";
  80. }
  81. $sOutput = "$errors\n";
  82. // Logging to a file does not work if the whole memory is exhausted...
  83. //SetupPage::log_error("Fatal error - in $__FILE__ , $errors");
  84. }
  85. return $sOutput;
  86. }
  87. //Define some bogus, invalid HTML tags that no sane
  88. //person would ever put in an actual document and tell
  89. //PHP to delimit fatal error warnings with them.
  90. ini_set('error_prepend_string', '<phpfatalerror>');
  91. ini_set('error_append_string', '</phpfatalerror>');
  92. // Starts the capture of the ouput, and sets a filter to capture the fatal errors.
  93. ob_start('FatalErrorCatcher'); // Start capturing the output, and pass it through the fatal error catcher
  94. require_once(APPROOT.'/core/config.class.inc.php');
  95. require_once(APPROOT.'/core/log.class.inc.php');
  96. require_once(APPROOT.'/core/kpi.class.inc.php');
  97. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  98. require_once('./xmldataloader.class.inc.php');
  99. require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
  100. // Never cache this page
  101. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  102. header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  103. /**
  104. * Main program
  105. */
  106. $sOperation = Utils::ReadParam('operation', '');
  107. try
  108. {
  109. switch($sOperation)
  110. {
  111. case 'async_action':
  112. ini_set('max_execution_time', max(240, ini_get('max_execution_time')));
  113. // While running the setup it is desirable to see any error that may happen
  114. ini_set('display_errors', true);
  115. ini_set('display_startup_errors', true);
  116. require_once(APPROOT.'/setup/wizardcontroller.class.inc.php');
  117. require_once(APPROOT.'/setup/wizardsteps.class.inc.php');
  118. $sClass = utils::ReadParam('step_class', '');
  119. $sState = utils::ReadParam('step_state', '');
  120. $sActionCode = utils::ReadParam('code', '');
  121. $aParams = utils::ReadParam('params', array(), false, 'raw_data');
  122. $oPage = new ajax_page('');
  123. $oDummyController = new WizardController('');
  124. if (is_subclass_of($sClass, 'WizardStep'))
  125. {
  126. $oStep = new $sClass($oDummyController, $sState);
  127. $sConfigFile = utils::GetConfigFilePath();
  128. if (file_exists($sConfigFile) && !is_writable($sConfigFile) && $oStep->RequiresWritableConfig())
  129. {
  130. $oPage->error("<b>Error:</b> the configuration file '".$sConfigFile."' already exists and cannot be overwritten.");
  131. $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.");
  132. $oPage->output();
  133. }
  134. else
  135. {
  136. $oStep->AsyncAction($oPage, $sActionCode, $aParams);
  137. }
  138. }
  139. $oPage->output();
  140. break;
  141. default:
  142. throw(new Exception("Error unsupported operation '$sOperation'"));
  143. }
  144. }
  145. catch(Exception $e)
  146. {
  147. header("HTTP/1.0 500 Internal server error.");
  148. echo "<p>An error happened while processing the installation:</p>\n";
  149. echo '<p>'.$e."</p>\n";
  150. SetupPage::log_error("An error happened while processing the installation: ".$e);
  151. }
  152. if (function_exists('memory_get_peak_usage'))
  153. {
  154. if ($sOperation == 'file')
  155. {
  156. SetupPage::log_info("loading file '$sFileName', peak memory usage. ".memory_get_peak_usage());
  157. }
  158. else
  159. {
  160. SetupPage::log_info("operation '$sOperation', peak memory usage. ".memory_get_peak_usage());
  161. }
  162. }
  163. ?>