plugin.php 5.8 KB

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