plugin.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Maintenance mode facilitator plugin
  5. * @package chamilo.plugin
  6. */
  7. $plugin = MaintenanceModePlugin::create();
  8. $plugin_info = $plugin->get_info();
  9. $editFile = false;
  10. $file = api_get_path(SYS_PATH).'.htaccess';
  11. $maintenanceHtml = api_get_path(SYS_PATH).'maintenance.html';
  12. if ($plugin->isEnabled()) {
  13. if (!file_exists($file)) {
  14. Display::addFlash(
  15. Display::return_message(
  16. "$file does not exists. ",
  17. 'warning'
  18. )
  19. );
  20. } else {
  21. if (is_readable($file) && is_writable($file)) {
  22. $editFile = true;
  23. } else {
  24. if (!is_readable($file)) {
  25. Display::addFlash(
  26. Display::return_message("$file is not readable", 'warning')
  27. );
  28. }
  29. if (!is_writable($file)) {
  30. Display::addFlash(
  31. Display::return_message("$file is not writable", 'warning')
  32. );
  33. }
  34. }
  35. }
  36. }
  37. if ($editFile && api_is_platform_admin()) {
  38. $originalContent = file_get_contents($file);
  39. $beginLine = '###@@ This part was generated by the edit_htaccess plugin @@##';
  40. $endLine = '###@@ End @@##';
  41. $handler = fopen($file, 'r');
  42. $deleteLinesList = [];
  43. $deleteLine = false;
  44. $contentNoBlock = '';
  45. $block = '';
  46. while (!feof($handler)) {
  47. $line = fgets($handler);
  48. $lineTrimmed = trim($line);
  49. if ($lineTrimmed == $beginLine) {
  50. $deleteLine = true;
  51. }
  52. if ($deleteLine) {
  53. $block .= $line;
  54. } else {
  55. $contentNoBlock .= $line;
  56. }
  57. if ($lineTrimmed == $endLine) {
  58. $deleteLine = false;
  59. }
  60. }
  61. fclose($handler);
  62. $block = str_replace($beginLine, '', $block);
  63. $block = str_replace($endLine, '', $block);
  64. $form = new FormValidator('htaccess');
  65. $form->addHtml('The following text will be added in the /.htaccess');
  66. $element = $form->addText(
  67. 'ip',
  68. [$plugin->get_lang('IPAdmin'), $plugin->get_lang('IPAdminDescription')]
  69. );
  70. $element->freeze();
  71. $form->addTextarea('text', 'htaccess', ['rows' => '15']);
  72. $config = [
  73. 'ToolbarSet' => 'Documents',
  74. 'Width' => '100%',
  75. 'Height' => '400',
  76. 'allowedContent' => true
  77. ];
  78. $form->addHtmlEditor(
  79. 'maintenance',
  80. 'Maintenance',
  81. true,
  82. true,
  83. $config
  84. );
  85. $form->addCheckBox('active', null, get_lang('Active'));
  86. $form->addButtonSave(get_lang('Save'));
  87. $content = '';
  88. if (file_exists($maintenanceHtml)) {
  89. $content = file_get_contents($maintenanceHtml);
  90. }
  91. if (empty($content)) {
  92. $content = '<html><head><title></title></head><body></body></html>';
  93. }
  94. $isActive = api_get_plugin_setting('maintenancemode', 'active');
  95. $ip = api_get_real_ip();
  96. if ($ip == '::1') {
  97. $ip = '127.0.0.1';
  98. }
  99. $ipSubList = explode('.', $ip);
  100. $implode = implode('\.', $ipSubList);
  101. $append = api_get_configuration_value('url_append');
  102. $default = '
  103. RewriteCond %{REQUEST_URI} !'.$append.'/maintenance.html$
  104. RewriteCond %{REMOTE_HOST} !^'.$implode.'
  105. RewriteRule \.*$ '.$append.'/maintenance.html [R=302,L]
  106. ';
  107. if (empty($block)) {
  108. $block = $default;
  109. }
  110. $form->setDefaults([
  111. 'text' => $block,
  112. 'maintenance' => $content,
  113. 'ip' => $ip,
  114. 'active' => $isActive,
  115. ]);
  116. if ($form->validate()) {
  117. $values = $form->getSubmitValues();
  118. $text = $values['text'];
  119. $active = isset($values['active']) ? true : false;
  120. $content = $values['maintenance'];
  121. // Restore htaccess with out the block
  122. $newFileContent = $beginLine.PHP_EOL;
  123. $newFileContent .= trim($text).PHP_EOL;
  124. $newFileContent .= $endLine;
  125. $newFileContent .= PHP_EOL;
  126. $newFileContent .= $contentNoBlock;
  127. // Remove ^m chars
  128. $newFileContent = str_ireplace("\x0D", '', $newFileContent);
  129. file_put_contents($file, $newFileContent);
  130. $handle = curl_init(api_get_path(WEB_PATH));
  131. curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  132. $response = curl_exec($handle);
  133. $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  134. curl_close($handle);
  135. // Looks htaccess contains errors. Restore as it was.
  136. if ($httpCode != 200) {
  137. Display::addFlash(
  138. Display::return_message(
  139. 'Check your htaccess instructions. The original file was restored.',
  140. 'warning'
  141. )
  142. );
  143. $originalContent = str_replace("\x0D", '', $originalContent);
  144. file_put_contents($file, $originalContent);
  145. } else {
  146. $result = file_put_contents($maintenanceHtml, $content);
  147. if ($result === false) {
  148. Display::addFlash(
  149. Display::return_message(
  150. sprintf($plugin->get_lang('MaintenanceFileNotPresent'), $maintenanceHtml),
  151. 'warning'
  152. )
  153. );
  154. }
  155. }
  156. if ($active == false) {
  157. $message = $plugin->get_lang('MaintenanceModeIsOff');
  158. $contentNoBlock = str_replace("\x0D", '', $contentNoBlock);
  159. file_put_contents($file, $contentNoBlock);
  160. } else {
  161. $message = $plugin->get_lang('MaintenanceModeIsOn');
  162. }
  163. Display::addFlash(Display::return_message($message));
  164. }
  165. $plugin_info['settings_form'] = $form;
  166. }