If userX try to add an address that is already in the file, it while generate an errormsg. And if userX try to remove an address that is not in the file, it while result in an errormsg.
Well, I thought that I had solved it... but it seems to me that the driectory that I have my file in is accessed (at least the directory seems to been "touched").
Wanna see the code? (some of the comments are in swedish)
Code:
if ($alltOK) {
$file = "./temp/test"; // Filen som innehåller adresser.
$lock = $file . ".lock"; // Låsfilen som ev. låser upp användadet av "Filen".
$temp = $file . ".temp"; // Tempfilen som använd vid
$mailAdmin = "list-admin@skaraborg.nsf.scout.se";
$mailExtra = "From: list-admin@skaraborg.nsf.scout.se";
if (!file_exists($lock)) {
// SKAPA LÅSFILEN
if (!copy($file,$lock)) die ("Ett internt fel har uppstått! Försök igen senare."); // Skapa låsfilen.
$notInFile = true;
// KONTROLLERA FILINNEHÅLL
$content = @file($file) or die ("Kunde inte öppna en för detta nödvändig fil...");
foreach ($content as $line_num => $line) {
if (preg_match("/\^$email$/i", $line)) {
$notInFile = false;
}
}
// LÄGG TILL I FILEN!
if ($action=="subcribe") {
if ($notInFile) {
$fh = fopen($file, "a");
flock($fh, 2);
fwrite($fh, "$email\n");
flock($fh, 3);
fclose($fh);
// SKAPA MAIL TILL ADMIN
$mailSubject = "$name har lagts till i e-postlistan";
$mailMsg = "$name från $group har med e-postadressen \"$email\" lagts till i e-postlistan.";
// SKICKA MAIL TILL ADMIN
mail($mailAdmin, $mailSubject, $mailMsg, $mailExtra);
// SKAPA MAIL TILL USER
$mailSubject = "Bekräftelse: Du har lagts till i vår e-postlista";
$mailMsg = "Detta är en bekräftelse på att du fortsättningsvis kommer att erhålla de ";
$mailMsg .= "meddelande som skickas till vår e-postlista.\n\nI din anmälan har följande";
$mailMsg .= " uppgifter skickats till vår listadministratör.\nDitt namn: $name\nDin kår: ";
$mailMsg .= "$group\nE-postadress: $_POST['epost']\n\nSkulle något vara felaktigt, kontakta ";
$mailMsg .= "oss.\n\nFör avanmälan, besök \"www.skaraborg.nsf.scout.se/epostlista.php\"\n\n";
$mailMsg .= "Med vänliga hälsningar\nlist-admin@skaraborg.nsf.scout.se";
// SKICKA MAIL TILL USER
mail($email, $mailSubject, $mailMsg, $mailExtra);
} else {
mail($mailAdmin, "Tillägg", "Misslyckat", $mailExtra);
}
}
// TA BORT UR FILEN!
else if ($action=="unsubcribe") {
if (!($notInFile)) {
// Skapa kod för att ta bort rader ut filen...
$fh = fopen($temp, "w");
$content = @file($file) or die ("Kunde inte öppna en för detta nödvändig fil...");
foreach ($content as $line_num => $line) {
if (!(preg_match("/\^$mailTo_User$/i", $line))) {
fwrite($fh, "$line\n");
}
}
fclose($fh);
copy($temp, $file);
unlink($temp);
// SKAPA MAIL TILL ADMIN
$mailSubject_Admin = "$name har tagits bort i e-postlistan";
$mailMsg_Admin = "$name från $group har med e-postadressen \"$email\" tagits bort i e-postlistan.";
mail($mailAdmin, $mailSubject, $mailMsg, $mailExtra);
// SKAPA MAIL TILL USER
$mailSubject = "Bekräftelse: Du har tagits bort i vår e-postlista";
$mailMsg = "Detta är en bekräftelse på att du fortsättningsvis inte kommer att erhålla de ";
$mailMsg .= "meddelande som skickas till vår e-postlista.\n\nI din avanmälan har följande";
$mailMsg .= " uppgifter skickats till vår listadministratör.\nDitt namn: $name\nDin kår: ";
$mailMsg .= "$group\nE-postadress: $_POST['epost']\n\nSkulle något vara felaktigt, kontakta ";
$mailMsg .= "oss.\n\nMed vänliga hälsningar\nlist-admin@skaraborg.nsf.scout.se";
mail($email, $mailSubject, $mailMsg, $mailExtra);
} else {
mail($mailAdmin, "Borttagning", "Misslyckat", $mailExtra);
}
}
// TA BORT LÅSFILEN
unlink($lock);
}
}
$contents = @file($file) or die ("Failed to open <a href=\"$file\">$file</a>. Please be sure it is an absolute URL.");
foreach ($contents as $line_num => $line) {
if (preg_match("/^$search$/", $line)) {
if ($_POST['addremove'] == "add") {
#email was alread in file -- can't add
echo "$search is already in the file";
}
else { #remove was checked
$addresstoremove = $line;
$y = 1;
}
$x = 1; #a match was found
}
$new_file .= $line;
}
if ($y == 1) { #remove email from file
$new_file = preg_replace("/$addresstoremove/","",$new_file);
$fp = fopen ($file,"w");
fwrite ($fp, "$new_file");
fclose ($fp);
echo "$addresstoremove was removed from the file.";
}
if ($x != 1) { #if a match was not found above
if ($_POST['addremove'] == "add") {
#add email to file
$fp = fopen ($file,"a");
fwrite ($fp, "\n$search");
fclose ($fp);
echo "$search was added to the file.";
}
else { #email was not in file to remove
echo "$search was not found in the file.";
}
}
}
lol... Glad to hear you kept working on it and didn't just sit around waiting for someone to give you an answer. But, as you see, there is no need to use a temp file... What you do is up to you...
Bookmarks