<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- These files are licenced under the GNU GPL, which you can find here:
http://www.gnu.org/copyleft/gpl.html
-->
<!-- These files were written by Liam Delahunty, he has agreed that his work be released under the GPL -->
<!-- This file is designed to write and make changes to /etc/aliases -->
<html>
<head>
<title>Editing the mail aliases file</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

// This is the stylesheet and an example may be found here
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<?php

// this bit is for the uneditable (by the users) section, so you can have one file do it all.
// This can be a relative path as we see below or a fixed path as you see in the Windows example.
// $default_path = "C:\\Program Files\\Apache Group\\Apache\\htdocs\\zzzz\\x_default.txt";
// The default file can be found as an example here
$default_path = "default_file";

// this is their editable stuff and an example can be found here
// $my_path = "C:\\Program Files\\Apache Group\\Apache\\htdocs\\zzzz\\x_working.txt";
$my_path = "edit_file";

// this is the final real document that combines the two...
// $final_path = "C:\\Program Files\\Apache Group\\Apache\\htdocs\\zzzz\\x_final.txt";
$final_path = "/etc/aliases";

function err_display($err_array,$err){
if ($err_array["$err"]){
print ("<tr>");
}else{
print ("<tr>");
}
}

// if you want to print any thing on the web page (but not in the file) just write print statements...
print ("<h1>Aliases config file</h1>");
print ("<p><a href=\"http://www.harkness.co.uk/\">Go somewhere else</a></p>");

if ($submit){
// what kind of checking can we do?

// let's make sure it's not empty at least.
if ($my_field == ""){
$err_array[my_field] = "<b>Error</b> New entry text must not be blank.";
}else{
// read the contents, if it's already there don't bother adding it again. Tell 'em so.
$fp = fopen("$final_path", "r");
$final_data = fread($fp,filesize($final_path));

fclose($fp);
if (eregi($my_field, $final_data)){
$err_array[my_field] = "<b>Error</b> Entry already exists.";
}

// simple check if it's an IP address...
//if (!preg_match("/^\d+\.\d+\.\d+\.\d+/",$my_field)){
//      $err_array[my_field] = "<b>Error</b> Not an IP address.";
//}
}

if (count($err_array)!=0){
// display errors
print ("<p>Please correct the $count_err error(s):<br>");
while (list($index,$value) = each($err_array)){
print ("$value<br>");
}
print ("</p>\n");
}else{
// add it damn you!

// add line break too.
$my_field .= "\n";

$mp = fopen("$my_path", "a+");
fwrite($mp, $my_field);
fclose($mp);

$mp = fopen("$my_path", "r");
$my_data = fread($mp,filesize($my_path));
fclose($mp);

$dp = fopen("$default_path", "r");
$default_data = fread($dp,filesize($default_path));
fclose($dp);

$fp = fopen("$final_path", "w+");
$final_data = $default_data ."\n" . $my_data;
$final_work = (fwrite($fp, $final_data));
if ($final_work) {
print ("<p>The working files have just been updated.</p>");
}else{
print ("<p><b>Error.</b> ".__LINE__."</p>");
}
fclose($fp);

}
}

if ($delete){
// read in working file,
$working = file("$my_path");
$working_lines = count($working);
sort($working);
print ("<p>Delete $working[$key]</p>");
for ($i=0;$i<$working_lines;$i++){
if ($i != "$key"){
$new_working .= "$working[$i]";
}else{
// nu'ting
}
}
// probably a windows thing with an extra line breaks on a delete...//
$new_working = eregi_replace("\n\n","\n",$new_working);
$new_working = eregi_replace("\r\n","\n",$new_working);
// now write the new file
$mp = fopen("$my_path", "w+");
fwrite($mp, $new_working);
fclose($mp);
// and update the final
$dp = fopen("$default_path", "r");
$default_data = fread($dp,filesize($default_path));
fclose($dp);

$fp = fopen("$final_path", "w+");
$final_data = $default_data ."\n" . $new_working;
$final_work = (fwrite($fp, $final_data));
if ($final_work) {
print ("<p>The working files have just been updated.</p>");
}else{
print ("<p><b>Error.</b> ".__LINE__."</p>");
}
fclose($fp);
}

// display the default stuff please
if ($dp = fopen("$default_path", "r")){
$default_data = fread($dp,filesize($default_path));
fclose($dp);
print ("<h2>Default Set-up.</h2>");
$format_default_data = nl2br($default_data);
print ("<p>$format_default_data</p>");

}else{
print ("<p><b>Error.</b> ".__LINE__."</p>");
}


// read in my edits, if not just create the empty file
if (file_exists($my_path)){
// we read the working data as an array
$working = file("$my_path");
$working_lines = count($working);

// Sort it?
sort($working);

print ("<table>");
for ($i=0;$i<$working_lines;$i++){
print ("<tr><td>$working[$i]</td><td><a href=\"$PHP_SELF?delete=1&key=$i\">delete</a></td><td><a href=\"$PHP_SELF?edit=1&my_field=$working[$i]\">copy</a></td></tr>\n");
}
print ("</table>");
}else{
// create blank file.
$mp = fopen("$my_path", "w+");
fclose($mp);
}


// this is the real file for Xander to use
if (!file_exists($final_path)){
$fp = fopen("$final_path", "w+");
$final_data = $default_data ."\n" . $my_data;
$final_work = (fwrite($fp, $final_data));
if ($final_work) {
print ("<p>The working files have just been created.</p>");
}else{
print ("<p><b>Error.</b> ".__LINE__."</p>");
}
fclose($fp);
}

print ("<form action=\"$PHP_SELF\" method=\"post\">\n");
print ("<table>");

err_display($err_array,my_field);
print ("<td>Enter text for new entry</td><td><input type=\"Text\" name=\"my_field\" value=\"$my_field\"></td></tr>");
print ("<tr><td>&nbsp;</td><td><input type=\"Submit\" name=\"submit\" value=\"Click here to add\"></td></tr>");

print ("</table>");
print ("</form>\n");

?>
</body>
</html>