Where To Add Filter Code ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Where To Add Filter Code ?

Post by UniqueIdeaMan »

Hi,

I need to know on which line in the Mini Proxy I should add the "banned words" filter code so that when banned words are found on the proxied pages, then the banned words are substituted ?
https://github.com/joshdick/miniProxy/b ... iProxy.php

Here is the code that I need to add:

Filter Code:

Code: Select all

	<?php
	/*
ERROR HANDLING
*/
//declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
	// 1). Set banned words.
$banned_words = array("blow", "nut", "bull****");
// 2). $curl is going to be data type curl resource.
$curl = curl_init();
// 3). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'https://www.buzzfeed.com/mjs538/the-68-words-you-cant-say-on-tv?utm_term=.xlN0R1Go89#.pbdl8dYm3X');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
// 4). Run cURL (execute http request).
$result = curl_exec($curl);
if (curl_errno($curl)) {
    echo 'Error:' . curl_error($curl);
}
$response = curl_getinfo( $curl );
if($response['http_code'] == '200' )
{
    $regex = '/\b';     
    $regex .= implode('\b|\b', $banned_words);   
    $regex .= '\b/i'; 
    $substitute = '****';
    $cleanresult = preg_replace($regex, $substitute, $result);
    echo $cleanresult;
}
curl_close($curl);
?>



If you reckon the code is not sound then you are welcome to show a suitable example.
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: Where To Add Filter Code ?

Post by UniqueIdeaMan »

I added this filter code:
PHP Code:

Code: Select all

//SET THE BANNED WORDS. 
$banned_words = array("prick","dick","<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>"); 

//SUBSTITUTE THE BANNED WORDS ON PROXIED PAGE (CONTENT FILTERING). 
if($responseInfo['http_code'] == '200' ) 
    { 
      
        $regex = '/\b';      // The beginning of the regex string syntax 
        $regex .= implode('\b|\b', $banned_words);      // joins all the banned words to the string with correct regex syntax 
        $regex .= '\b/i';    // Adds ending to regex syntax. Final i makes it case insensitive 
        $substitute = '****'; 
        $cleanresponse = preg_replace($regex, $substitute, $response); 
        echo $cleanresponse; 
    }  
After this as you suggested:

$response = makeRequest($url);
$rawResponseHeaders = $response["headers"];
$responseBody = $response["body"];

But, I get error:

[text]Notice: Undefined variable: responseInfo in C:\xampp\htdocs\proxy\browser_experimenting.php on line 304[/text]

Never should have got that, as the variable is defined in line 169. (Maybe, it's within a condition. Hard to see as the original programmer made it messy).
And so, I lowered my filter code another line. Below these:
$response = makeRequest($url);
$rawResponseHeaders = $response["headers"];
$responseBody = $response["body"];
$responseInfo = $response["responseInfo"];

That way, my filter code is underneath the $responseInfo.
However, this time more errors:

[text]Warning: preg_replace(): Compilation failed: nothing to repeat at offset 21 in C:\xampp\htdocs\proxy\\browser_experimenting.php on line 311

Warning: preg_replace(): Compilation failed: nothing to repeat at offset 21 in C:\xampp\htdocs\proxy\\browser_experimenting.php on line 311

Notice: Array to string conversion in C:\xampp\htdocs\proxy\\browser_experimenting.php on line 311

Warning: preg_replace(): Compilation failed: nothing to repeat at offset 21 in C:\xampp\htdocs\proxy\\browser_experimenting.php on line 311

Notice: Array to string conversion in C:\xampp\htdocs\proxy\browser_experimenting.php on line 312
Array[/text]

I do not understand hy the preg_replace is failing this time when it did not before.

Anyway, earlier on, I placed my filter code on line 170 but no luck:
//Set the request URL.
curl_setopt($ch, CURLOPT_URL, $url);
//Make the request.
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);

On many of my 3hrs experiments, I have been shifting the filter code on many lines and even changing the variable name but no luck.
Changing this:

Code: Select all

//SET THE BANNED WORDS. 
$banned_words = array("prick","dick","<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>"); 

//SUBSTITUTE THE BANNED WORDS ON PROXIED PAGE (CONTENT FILTERING). 
if($responseInfo['http_code'] == '200' ) 
    { 
      
        $regex = '/\b';      // The beginning of the regex string syntax 
        $regex .= implode('\b|\b', $banned_words);      // joins all the banned words to the string with correct regex syntax 
        $regex .= '\b/i';    // Adds ending to regex syntax. Final i makes it case insensitive 
        $substitute = '****'; 
        $cleanresponse = preg_replace($regex, $substitute, $response); 
        echo $cleanresponse;
    }  
to this:

PHP Code:

Code: Select all

//SET THE BANNED WORDS. 
$banned_words = array("Prick","****","***"); 

//SUBSTITUTE THE BANNED WORDS ON PROXIED PAGE (CONTENT FILTERING). 
if($responseInfo['http_code'] == '200' ) 
    { 
      
        $regex = '/\b';      // The beginning of the regex string syntax 
        $regex .= implode('\b|\b', $banned_words);      // joins all the banned words to the string with correct regex syntax 
        $regex .= '\b/i';    // Adds ending to regex syntax. Final i makes it case insensitive 
        $substitute = '****'; 
        $url = preg_replace($regex, $substitute, $response); 
        echo $url;
    }  
Sometimes, even removed the echoes when I saw the proxy showing duplicate of the page where when the top version was proxied with no content filtering and the bottom version unproxied with content filtering. And vice versa.

[text]echo $cleanresponse;[/text]

[text]echo $url;[/text]

I reckon the answer lies in the filter code. I'm not doing it right. Any example I can see from you on how the filter should be coded and put under which particular line ?

The original Mini Proxy code is here:

https://github.com/joshdick/miniProxy/b ... iProxy.php
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: Where To Add Filter Code ?

Post by UniqueIdeaMan »

UPDATE:
Now. I get this error only. The rest mentioned earlier are gone:

Notice: Array to string conversion in C:\xampp\htdocs\e_id\browser_experimenting.php on line 311
Notice: Array to string conversion in C:\xampp\htdocs\e_id\browser_experimenting.php on line 312

Line 311 & 312 looks like this:

Code: Select all

//SET THE BANNED WORDS.
$banned_words = array("Prick","Dick","<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>");

//SUBSTITUTE THE BANNED WORDS ON PROXIED PAGE (CONTENT FILTERING).
if($responseInfo['http_code'] == '200' )
    {
	 
        $regex = '/\b';      // The beginning of the regex string syntax
        $regex .= implode('\b|\b', $banned_words);      // joins all the banned words to the string with correct regex syntax
        $regex .= '\b/i';    // Adds ending to regex syntax. Final i makes it case insensitive
        $substitute = '****';
        $url = preg_replace($regex, $substitute, $response);
        echo $url;
	}
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: Where To Add Filter Code ?

Post by UniqueIdeaMan »

I have resolved it.

Code: Select all

if (!$urlIsValid) {
  die("Error: The requested URL was disallowed by the server administrator.");
}
$response = makeRequest($url);
$rawResponseHeaders = $response["headers"];
$responseBody = $response["body"];
$responseInfo = $response["responseInfo"];

//SET THE BANNED WORDS.
$banned_words = array("Prick","Dick","<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>","cock","<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>");

//SUBSTITUTE THE BANNED WORDS ON PROXIED PAGE (CONTENT FILTERING).
if($responseInfo['http_code'] == '200' )
    {
	 
        $regex = '/\b';      // The beginning of the regex string syntax
        $regex .= implode('\b|\b', $banned_words);      // joins all the banned words to the string with correct regex syntax
        $regex .= '\b/i';    // Adds ending to regex syntax. Final i makes it case insensitive
        $substitute = '****';
        $responseBody = preg_replace($regex, $substitute, $responseBody);        
	}
Changed:

Code: Select all

$url = preg_replace($regex, $substitute, $response);
to this:

Code: Select all

$responseBody = preg_replace($regex, $substitute, $responseBody);
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: Where To Add Filter Code ?

Post by UniqueIdeaMan »

Just before I packup for the night, I want to ask a question.
You know the $responseBody was carrying the html of the page. And, I was checking for banned words on the page and replacing the banned words with ****. Was doing all that, with this code:

Code: Select all

if($responseBody == 
if($responseInfo['http_code'] == '200' )
    {
	 
        $regex = '/\b';      // The beginning of the regex string syntax
        $regex .= implode('\b|\b', $banned_words);      // joins all the banned words to the string with correct regex syntax
        $regex .= '\b/i';    // Adds ending to regex syntax. Final i makes it case insensitive
        $substitute = '****';
        $responseBody = preg_replace($regex, $substitute, $responseBody);        
	}
Now, imagine that, I no longer want the filter to replace banned words but give alert to the user insttead that the banned words exist on the page (the page the user is currently viewing).
How would I code it ?
The Pseudo Code is this, which I need to convert to php:

IF->$response->contains any of the array values;
Then->Echo: Banned word "blah blah" is found!"".

Here is my attempt:

Code: Select all

Run cURL (execute http request).
$result = curl_exec($curl);
$response = curl_getinfo( $curl );

if( $response['http_code'] == '200' )
{
    //Set banned words.
    $banned_words = array("blow", "nut", "bull****");

    //Separate each words found on the cURL fetched page.
    $word = explode(" ", $result);
    
   //var_dump($word);

    for($i = 0; $i <= count($word); $i++){
        foreach ($banned_words as $ban) {
            if (stripos($word[$i],$ban) !== FALSE){
                echo "word: $word[$i]<br />";
                echo "Match: $ban<br>";
            }else{
                echo "word: $word[$i]<br />";
                echo "No Match: $ban<br>";  
            }
        }
    }
}
Now, it is your turn to find flaw in it and show me a correction.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Where To Add Filter Code ?

Post by thinsoldier »

To alert the user of bad words as they type would require the use of javascript.
Warning: I have no idea what I'm talking about.
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Re: Where To Add Filter Code ?

Post by UniqueIdeaMan »

thinsoldier wrote:To alert the user of bad words as they type would require the use of javascript.
No. Not as they type but when one is spotted when the proxy or curl fetches the page.
Post Reply