addHtml('The following text will be added in the /.htaccess');
$form->addText('ip', 'IP');
$form->addTextarea('text', 'htaccess', ['rows' => '15']);
$config = [
'ToolbarSet' => 'Documents',
'Width' => '100%',
'Height' => '400',
//'fullPage' => true,
'allowedContent' => true
];
$form->addHtmlEditor(
'maintenance',
'Maintenance',
true,
true,
$config
);
$form->addButtonSave(get_lang('Save'));
$content = '';
if (is_file($maintenanceHtml)) {
$content = file_get_contents($maintenanceHtml);
}
if (empty($content)) {
$content = '
';
}
$ip = api_get_plugin_setting('edit_htaccess', 'ip');
$ip = api_get_real_ip();
$ipSubList = explode('.', $ip);
$implode = implode('\.', $ipSubList);
$append = api_get_configuration_value('url_append');
$default = '
RewriteCond %{REQUEST_URI} !'.$append.'/maintenance.html$
RewriteCond %{REMOTE_HOST} !^'.$implode.'
RewriteRule \.*$ '.$append.'/maintenance.html [R=302,L]
';
if (empty($block)) {
$block = $default;
}
$form->setDefaults(['text' => $block, 'maintenance' => $content, 'ip' => $ip]);
if ($form->validate()) {
$values = $form->getSubmitValues();
$text = $values['text'];
$content = $values['maintenance'];
// Restore htaccess with out the block
$newFileContent = $contentNoBlock;
$newFileContent .= $beginLine.PHP_EOL;
$newFileContent .= $text.PHP_EOL;
$newFileContent .= $endLine;
file_put_contents($file, $newFileContent);
$handle = curl_init(api_get_path(WEB_PATH));
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
// Looks htaccess contains errors. Restore as it was.
if ($httpCode != 200) {
Display::addFlash(
Display::return_message(
'Check your htaccess instructions. The original file was restored.',
'warning'
)
);
file_put_contents($file, $originalContent);
} else {
file_put_contents($maintenanceHtml, $content);
Display::addFlash(Display::return_message('Saved'));
}
}
$plugin_info['settings_form'] = $form;
}