config.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. // Copyright (C) 2014-2016 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. * Monitor the backup
  20. *
  21. * @copyright Copyright (C) 2013 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once('../../approot.inc.php');
  25. require_once(APPROOT.'application/application.inc.php');
  26. require_once(APPROOT.'application/itopwebpage.class.inc.php');
  27. require_once(APPROOT.'application/startup.inc.php');
  28. require_once(APPROOT.'application/loginwebpage.class.inc.php');
  29. function TestConfig($sContents, $oP)
  30. {
  31. try
  32. {
  33. ini_set('display_errors', 1);
  34. ob_start();
  35. eval('?'.'>'.trim($sContents));
  36. $sNoise = trim(ob_get_contents());
  37. ob_end_clean();
  38. }
  39. catch (Exception $e)
  40. {
  41. // well, never reach in case of parsing error :-(
  42. throw new Exception('Error in configuration: '.$e->getMessage());
  43. }
  44. if (strlen($sNoise) > 0)
  45. {
  46. if (preg_match("/(Error|Parse error|Notice|Warning): (.+) in \S+ : eval\(\)'d code on line (\d+)/i", strip_tags($sNoise), $aMatches))
  47. {
  48. $sMessage = $aMatches[2];
  49. $sLine = $aMatches[3];
  50. $iLine = (int) $sLine;
  51. // Highlight the line
  52. $aLines = explode("\n", $sContents);
  53. $iStart = 0;
  54. for ($i = 0 ; $i < $iLine - 1; $i++) $iStart += strlen($aLines[$i]);
  55. $iEnd = $iStart + strlen($aLines[$iLine - 1]);
  56. $iTotalLines = count($aLines);
  57. $oP->add_ready_script(
  58. <<<EOF
  59. setCursorPos($('#new_config')[0], $iStart, $iEnd);
  60. $('#new_config')[0].focus();
  61. var iScroll = Math.floor($('#new_config')[0].scrollHeight * ($iLine - 20) / $iTotalLines);
  62. $('#new_config').scrollTop(iScroll);
  63. EOF
  64. );
  65. $sMessage = Dict::Format('config-parse-error', $sMessage, $sLine);
  66. throw new Exception($sMessage);
  67. }
  68. else
  69. {
  70. // Note: sNoise is an html output, but so far it was ok for me (e.g. showing the entire call stack)
  71. throw new Exception('Syntax error in configuration file: <tt>'.$sNoise.'</tt>');
  72. }
  73. }
  74. }
  75. /////////////////////////////////////////////////////////////////////
  76. // Main program
  77. //
  78. LoginWebPage::DoLogin(true); // Check user rights and prompt if needed (must be admin)
  79. //$sOperation = utils::ReadParam('operation', 'menu');
  80. //$oAppContext = new ApplicationContext();
  81. $oP = new iTopWebPage(Dict::S('config-edit-title'));
  82. $oP->set_base(utils::GetAbsoluteUrlAppRoot().'pages/');
  83. try
  84. {
  85. $sOperation = utils::ReadParam('operation', '');
  86. $oP->add("<h1>".Dict::S('config-edit-title')."</h1>");
  87. if (MetaModel::GetConfig()->Get('demo_mode'))
  88. {
  89. $oP->add("<div class=\"header_message message_info\">Sorry, iTop is in <b>demonstration mode</b>: the configuration file cannot be edited.</div>");
  90. }
  91. if (MetaModel::GetModuleSetting('itop-config', 'config_editor', '') == 'disabled')
  92. {
  93. $oP->add("<div class=\"header_message message_info\">iTop interactive edition of the configuration as been disabled. See <tt>'config_editor' => 'disabled'</tt> in the configuration file.</div>");
  94. }
  95. else
  96. {
  97. $oP->add_style(
  98. <<<EOF
  99. textarea {
  100. -webkit-box-sizing: border-box;
  101. -moz-box-sizing: border-box;
  102. box-sizing: border-box;
  103. width: 100%;
  104. height: 550px;
  105. }
  106. .current_line {
  107. display: none;
  108. margin-left: 20px;
  109. }
  110. EOF
  111. );
  112. $sConfigFile = APPROOT.'conf/'.utils::GetCurrentEnvironment().'/config-itop.php';
  113. if ($sOperation == 'save')
  114. {
  115. $sConfig = utils::ReadParam('new_config', '', false, 'raw_data');
  116. $sTransactionId = utils::ReadParam('transaction_id', '');
  117. $sOrginalConfig = utils::ReadParam('prev_config', '', false, 'raw_data');
  118. if (!utils::IsTransactionValid($sTransactionId, true))
  119. {
  120. $oP->add("<div class=\"header_message message_info\">Error: invalid Transaction ID. The configuration was <b>NOT</b> modified.</div>");
  121. }
  122. else
  123. {
  124. if ($sConfig == $sOrginalConfig)
  125. {
  126. $oP->add('<div id="save_result" class="header_message">'.Dict::S('config-no-change').'</div>');
  127. }
  128. else
  129. {
  130. try
  131. {
  132. TestConfig($sConfig, $oP); // throws exceptions
  133. @chmod($sConfigFile, 0770); // Allow overwriting the file
  134. $sTmpFile = tempnam(SetupUtils::GetTmpDir(), 'itop-cfg-');
  135. // Don't write the file as-is since it would allow to inject any kind of PHP code.
  136. // Instead write the interpreted version of the file
  137. // Note:
  138. // The actual raw PHP code will anyhow be interpreted exactly twice: once in TestConfig() above
  139. // and a second time during the load of the Config object below.
  140. // If you are really concerned about an iTop administrator crafting some malicious
  141. // PHP code inside the config file, then turn off the interactive configuration
  142. // editor by adding the configuration parameter:
  143. // 'itop-config' => array(
  144. // 'config_editor' => 'disabled',
  145. // )
  146. file_put_contents($sTmpFile, $sConfig);
  147. $oTempConfig = new Config($sTmpFile, true);
  148. $oTempConfig->WriteToFile($sConfigFile);
  149. @unlink($sTmpFile);
  150. @chmod($sConfigFile, 0444); // Read-only
  151. $oP->p('<div id="save_result" class="header_message message_ok">'.Dict::S('Successfully recorded.').'</div>');
  152. $sOrginalConfig = str_replace("\r\n", "\n", file_get_contents($sConfigFile));
  153. }
  154. catch (Exception $e)
  155. {
  156. $oP->p('<div id="save_result" class="header_message message_error">'.$e->getMessage().'</div>');
  157. }
  158. }
  159. }
  160. }
  161. else
  162. {
  163. $sConfig = str_replace("\r\n", "\n", file_get_contents($sConfigFile));
  164. $sOrginalConfig = $sConfig;
  165. }
  166. $sConfigEscaped = htmlentities($sConfig, ENT_QUOTES, 'UTF-8');
  167. $sOriginalConfigEscaped = htmlentities($sOrginalConfig, ENT_QUOTES, 'UTF-8');
  168. $oP->p(Dict::S('config-edit-intro'));
  169. $oP->add("<form method=\"POST\">");
  170. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"save\">");
  171. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">");
  172. $oP->add("<input type=\"submit\" value=\"".Dict::S('config-apply')."\"><button onclick=\"ResetConfig(); return false;\">".Dict::S('config-cancel')."</button>");
  173. $oP->add("<span class=\"current_line\">".Dict::Format('config-current-line', "<span class=\"line_number\"></span>")."</span>");
  174. $oP->add("<input type=\"hidden\" id=\"prev_config\" name=\"prev_config\" value=\"$sOriginalConfigEscaped\">");
  175. $oP->add("<textarea id =\"new_config\" name=\"new_config\" onkeyup=\"UpdateLineNumber();\" onmouseup=\"UpdateLineNumber();\">$sConfigEscaped</textarea>");
  176. $oP->add("<input type=\"submit\" value=\"".Dict::S('config-apply')."\"><button onclick=\"ResetConfig(); return false;\">".Dict::S('config-cancel')."</button>");
  177. $oP->add("<span class=\"current_line\">".Dict::Format('config-current-line', "<span class=\"line_number\"></span>")."</span>");
  178. $oP->add("</form>");
  179. $sConfirmCancel = addslashes(Dict::S('config-confirm-cancel'));
  180. $oP->add_script(
  181. <<<EOF
  182. function UpdateLineNumber()
  183. {
  184. var oTextArea = $('#new_config')[0];
  185. $('.line_number').html(oTextArea.value.substr(0, oTextArea.selectionStart).split("\\n").length);
  186. $('.current_line').show();
  187. }
  188. function ResetConfig()
  189. {
  190. if ($('#new_config').val() != $('#prev_config').val())
  191. {
  192. if (confirm('$sConfirmCancel'))
  193. {
  194. $('#new_config').val($('#prev_config').val());
  195. }
  196. }
  197. $('.current_line').hide();
  198. $('#save_result').hide();
  199. return false;
  200. }
  201. function setCursorPos(input, start, end) {
  202. if (arguments.length < 3) end = start;
  203. if ("selectionStart" in input) {
  204. setTimeout(function() {
  205. input.selectionStart = start;
  206. input.selectionEnd = end;
  207. }, 1);
  208. }
  209. else if (input.createTextRange) {
  210. var rng = input.createTextRange();
  211. rng.moveStart("character", start);
  212. rng.collapse();
  213. rng.moveEnd("character", end - start);
  214. rng.select();
  215. }
  216. }
  217. EOF
  218. );
  219. }
  220. }
  221. catch(Exception $e)
  222. {
  223. $oP->p('<b>'.$e->getMessage().'</b>');
  224. }
  225. $oP->output();