ruler1
MIS
- Feb 19, 2007
- 89
I am having a problem with removing a line from a text file. I will attempt to explain the problem the best i can. When i use the page i have to remove selected IPs from a list within a text file it deletes the line ok but it will add an odd character (which cant be copied) to the end of the line before the line that was deleted. below is a small example of what our lists will look like:
reason info:192.168.0.1-192.168.0.1
reason info:127.0.0.1-127.0.0.1
test reason:10.0.0.1-10.0.0.1
the text file always needs a blank line at the end of the file. if i wanted to remove the middle line it would end up something like below:
reason info:192.168.0.1-192.168.0.1o
test reason:10.0.0.1-10.0.0.1
where the little ' o ' is shown at the end of line 1 it would be where the odd character is placed but i cant copy it for some reason.
the code which is used to delete IPs from the file is below:
It is basically an IP blocklist that submits IPs to and from our database which can be used with PeerGuardian. i have been looking for ways to make this work before asking but i seem to have little success. thanks
reason info:192.168.0.1-192.168.0.1
reason info:127.0.0.1-127.0.0.1
test reason:10.0.0.1-10.0.0.1
the text file always needs a blank line at the end of the file. if i wanted to remove the middle line it would end up something like below:
reason info:192.168.0.1-192.168.0.1o
test reason:10.0.0.1-10.0.0.1
where the little ' o ' is shown at the end of line 1 it would be where the odd character is placed but i cant copy it for some reason.
the code which is used to delete IPs from the file is below:
Code:
// do submit changes (antiagent-list delete)
if(isset($_REQUEST["subm6"]))
{
if ( !$userdata['session_logged_in'] )
{
echo '<meta http-equiv="refresh" content="1;url=login.php">';
message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Session Expired, Please Login</span><br /><br /><img src=images/progbar.gif height=13 width=200 />");
}
$error_msg='';
if(!isset($_POST['ip45']) || empty($_POST['ip45']) && !isset($_POST['ip46']) || empty($_POST['ip46']) && !isset($_POST['ip47']) || empty($_POST['ip47']) && !isset($_POST['ip48']) || empty($_POST['ip48'])){$_POST['ip45']=$_POST['ip41'];$_POST['ip46']=$_POST['ip42'];$_POST['ip47']=$_POST['ip43'];$_POST['ip48']=$_POST['ip44'];}
if (!preg_match('/^[0-9]+$/' , trim($_POST['ip48'])) || trim($_POST['ip48']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '4'.$error_msg;} else { $ip48 = trim($_POST['ip48']); $ip48 = $ip48 + 0;}
if (!preg_match('/^[0-9]+$/' , trim($_POST['ip47'])) || trim($_POST['ip47']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '3'.$error_msg;} else { $ip47 = trim($_POST['ip47']); $ip47 = $ip47 + 0;}
if (!preg_match('/^[0-9]+$/' , trim($_POST['ip46'])) || trim($_POST['ip46']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '2'.$error_msg;} else { $ip46 = trim($_POST['ip46']); $ip46 = $ip46 + 0;}
if (!preg_match('/^[0-9]+$/' , trim($_POST['ip45'])) || trim($_POST['ip45']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '1'.$error_msg;} else { $ip45 = trim($_POST['ip45']); $ip45 = $ip45 + 0;}
if (!preg_match('/^[0-9]+$/' , trim($_POST['ip44'])) || trim($_POST['ip44']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '4'.$error_msg;} else { $ip44 = trim($_POST['ip44']); $ip44 = $ip44 + 0;}
if (!preg_match('/^[0-9]+$/' , trim($_POST['ip43'])) || trim($_POST['ip43']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '3'.$error_msg;} else { $ip43 = trim($_POST['ip43']); $ip43 = $ip43 + 0;}
if (!preg_match('/^[0-9]+$/' , trim($_POST['ip42'])) || trim($_POST['ip42']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '2'.$error_msg;} else { $ip42 = trim($_POST['ip42']); $ip42 = $ip42 + 0;}
if (!preg_match('/^[0-9]+$/' , trim($_POST['ip41'])) || trim($_POST['ip41']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '1'.$error_msg;} else { $ip41 = trim($_POST['ip41']); $ip41 = $ip41 + 0;}
if ($error_msg != '')
{
echo '<meta http-equiv="refresh" content="5;url=banlist_moderate.php">';
message_die(GENERAL_MESSAGE, "<span class='rpostbody'>" . $error_msg . "</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
}
else
{
$start = $ip41 . "." . $ip42 . "." . $ip43 . "." . $ip44;
$end = $ip45 . "." . $ip46 . "." . $ip47 . "." . $ip48;
$file = 'c:banlists/agencies-list.p2p';
$contents = file ($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$toKeep = array();
$fnd=0;
foreach ($contents as $line)
{
$pattern = '/(?<info>.*?):(?<startIP>\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})-(?<endIP>\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})/ims';
preg_match_all($pattern, $line, $matches);
if ($start == $matches['startIP'][0] && $end == $matches['endIP'][0])
{
$fnd=1;
}
else
{
$toKeep[] = $line;
}
}
writeToFile($toKeep);
if($fnd==1)
{
echo '<meta http-equiv="refresh" content="3;url=banlist_moderate.php">';
message_die(GENERAL_MESSAGE, "<span class='rpostbody'>IP Range Removed Successfully!</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
}
else
{
echo '<meta http-equiv="refresh" content="5;url=banlist_moderate.php">';
message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, IP Range Was Not Found!</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
}
}
}
It is basically an IP blocklist that submits IPs to and from our database which can be used with PeerGuardian. i have been looking for ways to make this work before asking but i seem to have little success. thanks