config.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. // Copyright (C) 2014 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. else
  92. {
  93. $oP->add_style(
  94. <<<EOF
  95. textarea {
  96. -webkit-box-sizing: border-box;
  97. -moz-box-sizing: border-box;
  98. box-sizing: border-box;
  99. width: 100%;
  100. height: 550px;
  101. }
  102. .current_line {
  103. display: none;
  104. margin-left: 20px;
  105. }
  106. EOF
  107. );
  108. $sConfigFile = APPROOT.'conf/'.utils::GetCurrentEnvironment().'/config-itop.php';
  109. if ($sOperation == 'save')
  110. {
  111. $sConfig = utils::ReadParam('new_config', '', false, 'raw_data');
  112. $sOrginalConfig = utils::ReadParam('prev_config', '', false, 'raw_data');
  113. if ($sConfig == $sOrginalConfig)
  114. {
  115. $oP->add('<div id="save_result" class="header_message">'.Dict::S('config-no-change').'</div>');
  116. }
  117. else
  118. {
  119. try
  120. {
  121. TestConfig($sConfig, $oP); // throws exceptions
  122. @chmod($sConfigFile, 0770); // Allow overwriting the file
  123. file_put_contents($sConfigFile, $sConfig);
  124. @chmod($sConfigFile, 0444); // Read-only
  125. $oP->p('<div id="save_result" class="header_message message_ok">'.Dict::S('Successfully recorded.').'</div>');
  126. $sOrginalConfig = str_replace("\r\n", "\n", file_get_contents($sConfigFile));
  127. }
  128. catch (Exception $e)
  129. {
  130. $oP->p('<div id="save_result" class="header_message message_error">'.$e->getMessage().'</div>');
  131. }
  132. }
  133. }
  134. else
  135. {
  136. $sConfig = str_replace("\r\n", "\n", file_get_contents($sConfigFile));
  137. $sOrginalConfig = $sConfig;
  138. }
  139. $sConfigEscaped = htmlentities($sConfig, ENT_QUOTES, 'UTF-8');
  140. $sOriginalConfigEscaped = htmlentities($sOrginalConfig, ENT_QUOTES, 'UTF-8');
  141. $oP->p(Dict::S('config-edit-intro'));
  142. $oP->add("<form method=\"POST\">");
  143. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"save\">");
  144. $oP->add("<input type=\"submit\" value=\"".Dict::S('config-apply')."\"><button onclick=\"ResetConfig(); return false;\">".Dict::S('config-cancel')."</button>");
  145. $oP->add("<span class=\"current_line\">".Dict::Format('config-current-line', "<span class=\"line_number\"></span>")."</span>");
  146. $oP->add("<input type=\"hidden\" id=\"prev_config\" name=\"prev_config\" value=\"$sOriginalConfigEscaped\">");
  147. $oP->add("<textarea id =\"new_config\" name=\"new_config\" onkeyup=\"UpdateLineNumber();\" onmouseup=\"UpdateLineNumber();\">$sConfigEscaped</textarea>");
  148. $oP->add("<input type=\"submit\" value=\"".Dict::S('config-apply')."\"><button onclick=\"ResetConfig(); return false;\">".Dict::S('config-cancel')."</button>");
  149. $oP->add("<span class=\"current_line\">".Dict::Format('config-current-line', "<span class=\"line_number\"></span>")."</span>");
  150. $oP->add("</form>");
  151. $sConfirmCancel = addslashes(Dict::S('config-confirm-cancel'));
  152. $oP->add_script(
  153. <<<EOF
  154. function UpdateLineNumber()
  155. {
  156. var oTextArea = $('#new_config')[0];
  157. $('.line_number').html(oTextArea.value.substr(0, oTextArea.selectionStart).split("\\n").length);
  158. $('.current_line').show();
  159. }
  160. function ResetConfig()
  161. {
  162. if ($('#new_config').val() != $('#prev_config').val())
  163. {
  164. if (confirm('$sConfirmCancel'))
  165. {
  166. $('#new_config').val($('#prev_config').val());
  167. }
  168. }
  169. $('.current_line').hide();
  170. $('#save_result').hide();
  171. return false;
  172. }
  173. function setCursorPos(input, start, end) {
  174. if (arguments.length < 3) end = start;
  175. if ("selectionStart" in input) {
  176. setTimeout(function() {
  177. input.selectionStart = start;
  178. input.selectionEnd = end;
  179. }, 1);
  180. }
  181. else if (input.createTextRange) {
  182. var rng = input.createTextRange();
  183. rng.moveStart("character", start);
  184. rng.collapse();
  185. rng.moveEnd("character", end - start);
  186. rng.select();
  187. }
  188. }
  189. EOF
  190. );
  191. }
  192. }
  193. catch(Exception $e)
  194. {
  195. $oP->p('<b>'.$e->getMessage().'</b>');
  196. }
  197. $oP->output();