ajax.dataloader.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. $errors = '';
  76. foreach ($aMatches as $sMatch)
  77. {
  78. $errors .= strip_tags($sMatch)."\n";
  79. }
  80. $sOutput = "$errors\n";
  81. // Logging to a file does not work if the whole memory is exhausted...
  82. //SetupPage::log_error("Fatal error - in $__FILE__ , $errors");
  83. }
  84. return $sOutput;
  85. }
  86. //Define some bogus, invalid HTML tags that no sane
  87. //person would ever put in an actual document and tell
  88. //PHP to delimit fatal error warnings with them.
  89. ini_set('error_prepend_string', '<phpfatalerror>');
  90. ini_set('error_append_string', '</phpfatalerror>');
  91. // Starts the capture of the ouput, and sets a filter to capture the fatal errors.
  92. ob_start('FatalErrorCatcher'); // Start capturing the output, and pass it through the fatal error catcher
  93. require_once(APPROOT.'/core/config.class.inc.php');
  94. require_once(APPROOT.'/core/log.class.inc.php');
  95. require_once(APPROOT.'/core/kpi.class.inc.php');
  96. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  97. require_once('./xmldataloader.class.inc.php');
  98. require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
  99. // Never cache this page
  100. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  101. header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  102. /**
  103. * Main program
  104. */
  105. $sOperation = Utils::ReadParam('operation', '');
  106. try
  107. {
  108. switch($sOperation)
  109. {
  110. case 'async_action':
  111. ini_set('max_execution_time', max(240, ini_get('max_execution_time')));
  112. // While running the setup it is desirable to see any error that may happen
  113. ini_set('display_errors', true);
  114. ini_set('display_startup_errors', true);
  115. require_once(APPROOT.'/setup/wizardcontroller.class.inc.php');
  116. require_once(APPROOT.'/setup/wizardsteps.class.inc.php');
  117. $sClass = utils::ReadParam('step_class', '');
  118. $sState = utils::ReadParam('step_state', '');
  119. $sActionCode = utils::ReadParam('code', '');
  120. $aParams = utils::ReadParam('params', array(), false, 'raw_data');
  121. $oPage = new ajax_page('');
  122. $oDummyController = new WizardController('');
  123. if (is_subclass_of($sClass, 'WizardStep'))
  124. {
  125. $oStep = new $sClass($oDummyController, $sState);
  126. $sConfigFile = utils::GetConfigFilePath();
  127. if (file_exists($sConfigFile) && !is_writable($sConfigFile) && $oStep->RequiresWritableConfig())
  128. {
  129. $oPage->error("<b>Error:</b> the configuration file '".$sConfigFile."' already exists and cannot be overwritten.");
  130. $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.");
  131. $oPage->output();
  132. }
  133. else
  134. {
  135. $oStep->AsyncAction($oPage, $sActionCode, $aParams);
  136. }
  137. }
  138. $oPage->output();
  139. break;
  140. default:
  141. throw(new Exception("Error unsupported operation '$sOperation'"));
  142. }
  143. }
  144. catch(Exception $e)
  145. {
  146. header("HTTP/1.0 500 Internal server error.");
  147. echo "<p>An error happened while processing the installation:</p>\n";
  148. echo '<p>'.$e."</p>\n";
  149. SetupPage::log_error("An error happened while processing the installation: ".$e);
  150. }
  151. if (function_exists('memory_get_peak_usage'))
  152. {
  153. if ($sOperation == 'file')
  154. {
  155. SetupPage::log_info("loading file '$sFileName', peak memory usage. ".memory_get_peak_usage());
  156. }
  157. else
  158. {
  159. SetupPage::log_info("operation '$sOperation', peak memory usage. ".memory_get_peak_usage());
  160. }
  161. }
  162. ?>