File: /srv/admin/htdocs/anacrontab.php
<?php
require_once('_util.php');
?>
<!doctype html>
<html lang="fr">
<meta charset="utf-8">
<title>Edit anacrontab</title>
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="<?= css_path(); ?>">
<link rel="shortcut icon" href="<?= favicon_path(); ?>">
</head>
<body>
<?php require(header_file()); ?>
<section id="main">
<h1>Edit anacrontab</h1>
<?php
$path_anacrontab = '/srv/data/etc/cron/';
$files = 'anacrontab';
$custom_save = '/srv/data/home/.anacrontab.save';
// Anacrontab Restoration
if (!empty($_GET['restore']) AND $_GET['restore'] == True) {
if (file_exists($custom_save)) {
$save_content = file_get_contents($custom_save);
file_put_contents($path_anacrontab . $files, str_replace("\r", "", $save_content));
echo '<p class="done">The restore have been perform</p>';
} else {
echo '<div class="error"><p"><strong>Error :</strong><br/>No anacrontab file to restore</p></div>';
}
}
// Anacrontab Edition
if (!empty($_POST['content'])) {
$line_content = explode("\n",$_POST['content']);
$not_valid_line = [];
$regexp_list = [
"/^[0-9]*@/", # Syntax @period num@period
"/^\\s*#/", # Comment line
"/^\\s*$/" # Empty line
];
foreach ($line_content as $line) {
foreach($regexp_list as $regexp) {
if (preg_match($regexp, $line))
continue 2;
}
$not_valid_line[] = $line;
}
if (empty($not_valid_line)) {
// copy of original file to put_content
copy($path_anacrontab . $files, $custom_save);
file_put_contents($path_anacrontab . $files, str_replace("\r", "", $_POST['content']));
echo '<p class="done">The modification has been performed</p>';
}
else {
echo '<div class="error"><p><strong>Error :</strong><br/> The following lines have invalid format not matching @period or num@period : <ul>';
foreach ($not_valid_line as $line) {
echo '<li>' . $line .'</li>';
}
echo '</ul></p></div>';
}
}
?>
<div class="columns">
<form method="post" class="column-50 left">
<fieldset>
<textarea id="content" name="content" readonly rows="30" cols="150" autofocus>
<?php
if (!empty($not_valid_line)) {
$content = explode("\n", $_POST['content']);
}
else {
$content = file($path_anacrontab . $files);
}
foreach ($content as $line) {
echo $line;
}
?>
</textarea>
<p class="clear"><input class="button" type="button" onClick="return AreaEdit()" value="Edit file"/> <input id="submit" class="button" type="submit" value="Save" disabled/> <a href="?restore=1">Restore the last version of anacrontab</a></p>
</fieldset>
</form>
</div>
</section>
<script type="text/javascript">
function AreaEdit(){
if (confirm('Change in this file content will overwrite the anacrontab of your instance. Be carefull ! Do you really want to edit this file?')){
document.getElementById("content").readOnly=false;
document.getElementById("submit").disabled=false;
}
}
</script>
</body>
</html>