ajax.dataloader.php 6.4 KB

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