PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Dec 15, 2017 4:49 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Oct 17, 2017 7:17 pm 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 179
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:

Syntax: [ Download ] [ Hide ]
        <?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.


Top
 Profile  
 
PostPosted: Fri Oct 20, 2017 8:56 am 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 179
I added this filter code:
PHP Code:
Syntax: [ Download ] [ Hide ]
//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:

Syntax: [ Download ] [ Hide ]
Notice: Undefined variable: responseInfo in C:\xampp\htdocs\proxy\browser_experimenting.php on line 304


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:

Syntax: [ Download ] [ Hide ]
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


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:

Syntax: [ Download ] [ Hide ]
//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:
Syntax: [ Download ] [ Hide ]
//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.

Syntax: [ Download ] [ Hide ]
echo $cleanresponse;


Syntax: [ Download ] [ Hide ]
echo $url;


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


Top
 Profile  
 
PostPosted: Fri Oct 20, 2017 3:03 pm 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 179
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:

Syntax: [ Download ] [ Hide ]
//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;
        }
 


Top
 Profile  
 
PostPosted: Fri Oct 20, 2017 6:56 pm 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 179
I have resolved it.
Syntax: [ Download ] [ Hide ]
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:
Syntax: [ Download ] [ Hide ]
$url = preg_replace($regex, $substitute, $response);


to this:

Syntax: [ Download ] [ Hide ]
$responseBody = preg_replace($regex, $substitute, $responseBody);


Top
 Profile  
 
PostPosted: Fri Oct 20, 2017 7:49 pm 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 179
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:
Syntax: [ Download ] [ Hide ]
if&#40;$responseBody ==
if&#40;$responseInfo[&#39;http_code&#39;] == &#39;200&#39; &#41;
   {
         
        $regex = &#39;/\b&#39;;      // The beginning of the regex string syntax
       $regex .= implode&#40;&#39;\b|\b&#39;, $banned_words&#41;;      // joins all the banned words to the string with correct regex syntax
       $regex .= &#39;\b/i&#39;;    // Adds ending to regex syntax. Final i makes it case insensitive
       $substitute = &#39;****&#39;;
       $responseBody = preg_replace&#40;$regex, $substitute, $responseBody&#41;;        
        }


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:
Syntax: [ Download ] [ Hide ]
Run cURL &#40;execute http request&#41;.
$result = curl_exec&#40;$curl&#41;;
$response = curl_getinfo&#40; $curl &#41;;

if&#40; $response[&#39;http_code&#39;] == &#39;200&#39; &#41;
{
    //Set banned words.
    $banned_words = array&#40;"blow", "nut", "bull****"&#41;;

    //Separate each words found on the cURL fetched page.
    $word = explode&#40;" ", $result&#41;;
   
   //var_dump&#40;$word&#41;;

    for&#40;$i = 0; $i <= count&#40;$word&#41;; $i++&#41;{
       foreach &#40;$banned_words as $ban&#41; {
           if &#40;stripos&#40;$word[$i],$ban&#41; !== FALSE&#41;{
               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.


Top
 Profile  
 
PostPosted: Sat Oct 21, 2017 8:16 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 341
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group