Issues (1798)

public/plugin/MaintenanceMode/plugin.php (1 issue)

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