<?php
$dir = 'basic';
$doc = 'email.txt';
$file = "$dir/$doc";
/**********************************
Delete Found Entries
**********************************/
if(isset($_POST['delButton'])){
// Open file
$fh = fopen($file, 'r');
while(!feof($fh)){
$f_contents[] = fgets($fh);
}
fclose($fh);
// Find a match
// Purge out the line to be deleted
// And reset the array to be rewritten
foreach($f_contents as $key => $line){
$line = trim($line);
if($line == $_POST['toChange']){
$f_contents[$key] = '';
$message = "Deleted <b><i>$_POST[toChange]</i></b>";
}
}
//Write file contents back into the text file
$fh = fopen($file, 'w');
foreach($f_contents as $line){
fwrite($fh, $line);
}
fclose($fh);
$f_contents = array();
header("Location: $PHP_SELF");
}
/**********************************
Find Entry
**********************************/
if(isset($_POST['findButton'])){
if($_POST['findEntry'] != ''){
// Open file
$fh = fopen($file, 'r');
while(!feof($fh)){
$f_contents[] = fgets($fh);
}
fclose($fh);
// search for entries
$findEntry = trim($_POST['findEntry']);
foreach($f_contents as $value){
$value = trim($value);
if(eregi($findEntry, $value)){
$matches[] = $value;
}
}
/**********************************
Show Found Entries
**********************************/
$countMatches = count($matches);
if($countMatches > 1){
$word = 'matches';
} else {
$word = 'match';
}
?>
<html>
<head>
<title>Week #11 BASIC ~ Lestat</title>
<link rel="stylesheet" type="text/css" href="/css/scriptskewl.css">
</head>
<body>
<?
if($countMatches > 0){
print("Found $countMatches $word for <b><i>"$findEntry"</i></b><br /><br />");
print("<table style=\"border-collapse:collapse;border:0px solid black;\">");
foreach($matches as $value){
if($value != ''){
print("<form method=\"POST\" action=\"$PHP_SELF\">
<input type=\"hidden\" name=\"toChange\" value=\"$value\" />
<tr style=\"border:1px solid black;\">
<td style=\"padding:0;\">$value</td>
<td style=\"padding:0;padding-left:20px;\"><input type=\"submit\" name=\"delButton\" value=\"Delete\" /></td>
<td style=\"padding:0;padding-left:5px;\"><input type=\"submit\" name=\"modButton\" value=\"Modify\" /></td>
</tr>
</form>
");
}
}
print("</table></body></html>");
exit();
} else {
print("No Matches Found");
exit();
}
} else {
print("Nothing To Search For");
}
}
/**********************************
Add Entry
**********************************/
if(isset($_POST['addButton'])){
$newEntry = $_POST['newEntry'];
// Get File Contents
$fh = fopen($file, 'r');
while(!feof($fh)){
$f_contents[] = fgets($fh);
}
fclose($fh);
// Clean spaces from values
foreach($f_contents as $key =>$value){
$value = trim($value);
$f_contents[$key] = $value;
}
// If no Entry
if($newEntry == ''){
$message = "No Entry to Add<br />";
} else {
// see if entry already exists
if(in_array($newEntry , $f_contents)){ // if it does...
$message = "<b><i>$newEntry</i></b> Already Exists<br />";
} else {
$message = "Added <b><i>$newEntry</i></b><br />";
$fh = fopen($file , 'a'); // Otherwise make the add
fputs($fh, $newEntry . "\n");
fclose($fh);
}
}
$f_contents = array();
}
/**********************************
Modify Found Entries Form
**********************************/
// Show Mod Form
if(isset($_POST['modButton'])){
?>
<html>
<head>
<title>Week #11 BASIC ~ Lestat</title>
<link rel="stylesheet" type="text/css" href="/css/scriptskewl.css">
</head>
<body>
<form method="post" action="<?php $PHP_SELF; ?>">
<input type="hidden" name="oldValue" value="<?php print("$_POST[toChange]"); ?>" />
Change:<input type="text" name="newValue" value="<?php print("$_POST[toChange]");?>" /><input type="submit" name="changeButton" value="Submit Change" />
</form>
</body></html>
<?php
exit();
}
// Do the Change
if(isset($_POST['changeButton'])){
// Open file
$fh = fopen($file, 'r');
while(!feof($fh)){
$f_contents[] = fgets($fh);
}
fclose($fh);
$oldValue = trim($_POST['oldValue']);
$newValue = trim($_POST['newValue']);
if($f_contents = preg_replace("/$oldValue/", $newValue, $f_contents)){
$message = "Changed <b><i>$oldValue</i></b> to <b><i>$newValue</i></b>";
}
//Write file contents back into the text file
$fh = fopen($file, 'w');
foreach($f_contents as $line){
fwrite($fh, $line);
}
fclose($fh);
$f_contents = array();
}
?>
<html>
<head>
<title>Week #11 BASIC ~ Lestat</title>
<link rel="stylesheet" type="text/css" href="/css/scriptskewl.css">
</head>
<body>
<?php
if($message){
print("$message");
}
?>
<form method="POST" action="<?php $PHP_SELF; ?>" style="float:left;">
Add An Entry:<input type="text" name="newEntry" /> <input type="submit" name="addButton" value="Add" /><br />
Find An Entry:<input type="text" name="findEntry" /> <input type="submit" name="findButton" value="Search" />
</form>
<?php
/**********************************
Show Entries
**********************************/
// Get contents
$fh = fopen($file, 'r');
while(!feof($fh)){
$f_contents[] = fgets($fh);
}
fclose($fh);
// Show Entries
if($f_contents[0] != ''){
?>
<table border="1" style="margin-left:10px;border-collapse:collapse;border:1px solid black;">
<tr>
<td><b>Entries:</b></td>
</tr>
<tr>
<td style="padding:0;">
<?php
foreach($f_contents as $value){
$value = trim($value);
if($value != ''){
print("$value<br />");
}
}
?>
</td>
</tr>
</table>
<?php
} else {
print("No entries exist<br />");
}
?>
<!------------- END ------------->
<br />
<a href='../index.html'>Index</a><br>
<iframe src='wk11basicsrc.php' width='100%' height='300'></iframe>
<br>
<a href='../index.html'>Index</a>
</body>
</html>