Issues (2128)

plugin/maintenancemode/plugin.php (1 issue)

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

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
192
            $message = $plugin->get_lang('MaintenanceModeIsOff');
193
            $contentNoBlock = str_replace("\x0D", '', $contentNoBlock);
194
            file_put_contents($file, $contentNoBlock);
195
        } else {
196
            $message = $plugin->get_lang('MaintenanceModeIsOn');
197
        }
198
        Display::addFlash(Display::return_message($message));
199
    }
200
    $plugin_info['settings_form'] = $form;
201
}
202