Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete line from txt issue

Status
Not open for further replies.

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:

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
 
you're probably picking up some stray carriage returns.

add the line in red as shown
Code:
 foreach ($contents as $line){
[red] trim($line);[/red]
 
ok the results are now different, if i had the example below:

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

and if i delete the middle line i get the results below:

reason info:192.168.0.1-192.168.0.1
test reason:10.0.0.1-10.0.0.1o

also it deletes the last empty line and places the odd character at the end of the last line.
 
we need to find out what the miscreant character is. can you upload the file to me here

thanks

 
thanks.

i can see nothing wrong in the file that you have uploaded.

can you post the writetofile code?
 
i have just noticed something, i should have added more lines before testing so i did just that, i cleared the text file and added the following:

TEST:0.0.0.0-0.0.0.0
TEST:0.0.0.1-0.0.0.1
TEST:0.0.0.2-0.0.0.2
TEST:0.0.0.3-0.0.0.3
TEST:0.0.0.4-0.0.0.4

i then proceeded to remove the line TEST:0.0.0.1-0.0.0.1 and i found that the problem is at the very end of the text file, it removes the empty line then places that odd character right after the TEST:0.0.0.4-0.0.0.4 on the same line.
in that text file i uploaded you will notice there is now no blank line at the end of the file and there is that odd character at the end of the last line.

i will remove the line TEST:0.0.0.2-0.0.0.2 using the code and upload the file right after.
 
here is the code for adding to the text file:

Code:
	// do submit changes (antiagent-list add)
	if(isset($_REQUEST["subm5"]))
	{
		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['ip37']) || empty($_POST['ip37']) && !isset($_POST['ip38']) || empty($_POST['ip38']) && !isset($_POST['ip39']) || empty($_POST['ip39']) && !isset($_POST['ip40']) || empty($_POST['ip40'])){$_POST['ip37']=$_POST['ip33'];$_POST['ip38']=$_POST['ip34'];$_POST['ip39']=$_POST['ip35'];$_POST['ip40']=$_POST['ip36'];}
		if (!preg_match('/^[0-9]+$/' , trim($_POST['ip40'])) || trim($_POST['ip40']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '4'.$error_msg;} else { $ip40 = trim($_POST['ip40']); $ip40 = $ip40 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($_POST['ip39'])) || trim($_POST['ip39']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '3'.$error_msg;} else { $ip39 = trim($_POST['ip39']); $ip39 = $ip39 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($_POST['ip38'])) || trim($_POST['ip38']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '2'.$error_msg;} else { $ip38 = trim($_POST['ip38']); $ip38 = $ip38 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($_POST['ip37'])) || trim($_POST['ip37']) > 255){$error_msg='<br />' . $lang['Err_End_Box'] . '1'.$error_msg;} else { $ip37 = trim($_POST['ip37']); $ip37 = $ip37 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($_POST['ip36'])) || trim($_POST['ip36']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '4'.$error_msg;} else { $ip36 = trim($_POST['ip36']); $ip36 = $ip36 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($_POST['ip35'])) || trim($_POST['ip35']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '3'.$error_msg;} else { $ip35 = trim($_POST['ip35']); $ip35 = $ip35 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($_POST['ip34'])) || trim($_POST['ip34']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '2'.$error_msg;} else { $ip34 = trim($_POST['ip34']); $ip34 = $ip34 + 0;}
		if (!preg_match('/^[0-9]+$/' , trim($_POST['ip33'])) || trim($_POST['ip33']) > 255){$error_msg='<br />' . $lang['Err_Start_Box'] . '1'.$error_msg;} else { $ip33 = trim($_POST['ip33']); $ip33 = $ip33 + 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
		{
			$reason = htmlentities($_REQUEST['reason3']);
		 	if($reason == 'Enter Reason Here' || $reason == '')
		 	{
				echo '<meta http-equiv="refresh" content="5;url=banlist_moderate.php">';
		 		message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, You Forgot To Select A Reason.</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
		 	}
			$file = 'c:banlists/agencies-list.p2p';
			$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
			$lines = implode ("\r\n", $lines); //turn in to string
			$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, $lines, $matches);
			array_walk($matches['info'], 'trim');

			$cc=0; 
			$enterIPstart = $ip33*256*256*256+$ip34*256*256+$ip35*256+$ip36+0;
			$enterIPend = $ip37*256*256*256+$ip38*256*256+$ip39*256+$ip40+0;

			while($matches['startIP'][$cc])
			{
				if(ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})", $matches['startIP'][$cc],$regs) && ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})", $matches['endIP'][$cc],$regs1))
				{
					$foundIPstart = $regs[1]*256*256*256+$regs[2]*256*256+$regs[3]*256+$regs[4]+0;
					$foundIPend = $regs1[1]*256*256*256+$regs1[2]*256*256+$regs1[3]*256+$regs1[4]+0;
				}
				// check to see if the end ip is lower than the start ip
				if ($enterIPstart > $enterIPend)
				{
					echo '<meta http-equiv="refresh" content="5;url=banlist_moderate.php">';
		 			message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, End IP cant be lower than the Start IP.</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
				}
				//check to see if the IP ot a range exists first within the entered range
				if ($enterIPstart >= $foundIPstart && $enterIPstart <= $foundIPend || $enterIPend <= $foundIPend && $enterIPend >= $foundIPstart)
				{
		 			message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, The IP you entered falls within this already banned range. Start IP: ". $matches['startIP'][$cc] ." End IP ". $matches['endIP'][$cc] ."</span><br /><br />Please Click Back!");
				}
				$count=0;
				while($ProtectIP[$count])
				{
					if(ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})-([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})", $ProtectIP[$count],$reg))
					{
						$ProtectIPstart = $reg[1]*256*256*256+$reg[2]*256*256+$reg[3]*256+$reg[4]+0;
						$ProtectIPend = $reg[5]*256*256*256+$reg[6]*256*256+$reg[7]*256+$reg[8]+0;
					}
					if ($ProtectIPstart >= $enterIPstart && $ProtectIPstart <= $enterIPend || $ProtectIPend >= $enterIPstart && $ProtectIPend <= $enterIPend)
					{
		 				message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, The IP you entered falls within Protected range. Start IP: ". $ProtectIP[$count] ."</span><br /><br />Please Click Back!");
					}
					$count=$count+1;
				}
				$cc++;
			}
			$fa = fopen("c:banlists/agencies-list.p2p", "a");
			if(! $fa)
			{
				echo '<meta http-equiv="refresh" content="5;url=banlist_moderate.php">';
				message_die(GENERAL_MESSAGE, "<span class='rpostbody'>Error, Failed to find file pedophiles-list.p2p</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
			}			
			fwrite($fa, $_REQUEST['reason3'] . ":" . $ip33 . "." . $ip34 . "." . $ip35 . "." . $ip36 . "-" . $ip37 . "." . $ip38 . "." . $ip39 . "." . $ip40 . "\r\n");
			fclose($fa);
			echo '<meta http-equiv="refresh" content="1;url=banlist_moderate.php">';
			message_die(GENERAL_MESSAGE, "<span class='rpostbody'>IP Range Added Successfully!</span><br /><br />You Are Being Redirected Back, Please wait<br /><br /><img src=images/progbar.gif height=13 width=200 />");
		}
	}

sorry if its a bit long
 
in the file that you uploaded there IS a line feed at the end of the file.
i suspect that this is a platform compatibility issue. if you post the writetofile code snip in this forum then we may be able to advise further.
 
the writetofile code is posted just before your last post. i think to see the odd char you have to open the text file in notepad.
 
hi, did you have any success with the code? i have been trying everything i know but cant seem to fathom it out.
From what i know there are several people we have who has been submitting IPs to our lists and they have told me some use Windows and some use Linux and i know some use Firefox and others use IE but i hav'nt got reply's from all of the people yet. When they all submit IPs there seems to be no problems at all infact it appears to save perfectly.
From what i have seen it only seems to be when removing an IP from the list.

thanks for your time

 
i still can't see a function called writetofile().


 
i believe the line below is what writes to the file:

Code:
fwrite($fa, $_REQUEST['reason3'] . ":" . $ip33 . "." . $ip34 . "." . $ip35 . "." . $ip36 . "-" . $ip37 . "." . $ip38 . "." . $ip39 . "." . $ip40 . "\r\n");

maybe that is whats wrong? i've been looking at this code for so long now and cant seem to progress anyhere.

thanks
 
sorry but i am looking for the function called writetofile. it should start like this

Code:
function writetofile($value){


//various guff goes here

}
 
sorry my mistake, i guess im more tired than i thought. here is the code below:

Code:
function writeToFile($toKeep)
{
	global $file;
	$w = file_put_contents($file, implode("\n", $toKeep));
	if ($w === false) { return false; } else {return true;}
}

 
ok

you don't need this line
Code:
       if(ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})", $matches['startIP'][$cc],$regs) && ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})", $matches['endIP'][$cc],$regs1))

as we already know that the values are proper ip addresses. if you're going to do IP address segmentation comparisons, i would use the ip to decimal functions that i posted in your other thread.

and then you are doing an fwrite in the main body of the code AND a call to writetofile. the latter is overwriting the former. it's all got a bit messy: i'd guess because you're not clear over what each bit of the code is doing.

matching ip ranges is not a clever way of managing authorisation unless you are within a controlled subnet/LAN. and if this is the case you would be better off doing so within apache. if you need to control authorisation then do so through an authentication mechanism such as challenge-response or similar.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top