plugin.php 5.8 KB

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