saversites Posted October 19, 2017 Report Share Posted October 19, 2017 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/blob/master/miniProxy.php Here is the code that I need to add: Filter Code: <?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. Quote Link to comment Share on other sites More sharing options...
administrator Posted October 19, 2017 Report Share Posted October 19, 2017 Just create a function and call it on the return to apply the filter. Quote Link to comment Share on other sites More sharing options...
saversites Posted October 19, 2017 Author Report Share Posted October 19, 2017 Thanks. But on which line should I call the function ? That's the answer I've been experimenting and seeking for the past 2.5hrs fruitlessly. On which line here ? https://github.com/joshdick/miniProxy/blob/master/miniProxy.php Quote Link to comment Share on other sites More sharing options...
administrator Posted October 20, 2017 Report Share Posted October 20, 2017 Call the function just before you print the strings to page. Stef Quote Link to comment Share on other sites More sharing options...
saversites Posted October 20, 2017 Author Report Share Posted October 20, 2017 Thanks. But, at a loss here. I added this filter code: //SET THE BANNED WORDS. $banned_words = array("Prick","dick","bullshit"); //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 someone suggested: $response = makeRequest($url); $rawResponseHeaders = $response["headers"]; $responseBody = $response["body"]; But, I get error: Notice: Undefined variable: responseInfo in C:\xampp\htdocs\proxy\browser_experimenting.php on line 304 I never should have got that error, 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: 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 why 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: //SET THE BANNED WORDS. $banned_words = array("prick","dick","bullshit"); //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 = '****'; [B]$cleanresponse [/B]= preg_replace($regex, $substitute, $response); [B]echo $cleanresponse;[/B] } to this: //SET THE BANNED WORDS. $banned_words = array("prick","dick","bullshit"); //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 = '****'; [B]$url[/B] = preg_replace($regex, $substitute, $response); [B]echo $url;[/B] } Sometimes, I even removed the echoes when I saw the proxy showing a 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. [B]echo $cleanresponse;[/B] [B]echo $url;[/B] 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/blob/master/miniProxy.php Quote Link to comment Share on other sites More sharing options...
saversites Posted October 20, 2017 Author Report Share Posted October 20, 2017 Attached is my full modified code of the original Mini Proxy script. My own comments are in CAPITALS. The codes immediately following the UPPER CASED comments are mine. Added a $url logger so the proxied page's url gets logged onto my db. Now need to add the content filter. browser_experimenting.php Quote Link to comment Share on other sites More sharing options...
saversites Posted October 20, 2017 Author Report Share Posted October 20, 2017 (edited) UPDATE: Now. I get this error only. The rest mentioned earlier are gone: Notice: Array to string conversion in C:\xampp\htdocs\proxy\browser_experimenting.php on line 311Notice: Array to string conversion in C:\xampp\htdocs\proxy\browser_experimenting.php on line 312 Line 311 & 312 looks like this: //SET THE BANNED WORDS. $banned_words = array("Prick","Dick","bullshit"); //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; } Edited October 20, 2017 by saversites Quote Link to comment Share on other sites More sharing options...
administrator Posted October 20, 2017 Report Share Posted October 20, 2017 Hi, The message 'Array to string conversion ' suggest that PHP is having trouble converting an array to a string. I don't see it in the code you pasted, so trying looking before lines 311 ... often times the error will actually appear beforehand. Or, if the variable $banned_words, is an array ... perhaps you have to convert it to a string. Stef Quote Link to comment Share on other sites More sharing options...
saversites Posted October 20, 2017 Author Report Share Posted October 20, 2017 (edited) 6 hours ago, administrator said: Hi, The message 'Array to string conversion ' suggest that PHP is having trouble converting an array to a string. I don't see it in the code you pasted, so trying looking before lines 311 ... often times the error will actually appear beforehand. Or, if the variable $banned_words, is an array ... perhaps you have to convert it to a string. Stef 300 - //SET THE BANNED WORDS. 301 - $banned_words = array("Prick","Dick","bullshit"); 303 - //SUBSTITUTE THE BANNED WORDS ON PROXIED PAGE (CONTENT FILTERING). 304 - if($responseInfo['http_code'] == '200' ) 305 - { 307 - $regex = '/\b'; // The beginning of the regex string syntax 308 - $regex .= implode('\b|\b', $banned_words); // joins all the banned words to the string with correct regex syntax 309 - $regex .= '\b/i'; // Adds ending to regex syntax. Final i makes it case insensitive 310 - $substitute = '****'; 311 - $url = preg_replace($regex, $substitute, $response); 312 - echo $url; 313 - } Edited October 20, 2017 by saversites Quote Link to comment Share on other sites More sharing options...
administrator Posted October 20, 2017 Report Share Posted October 20, 2017 Try converting the array: 1 hour ago, saversites said: $banned_words ... to a string and then continue from there. Stef Quote Link to comment Share on other sites More sharing options...
saversites Posted October 21, 2017 Author Report Share Posted October 21, 2017 (edited) I manage to fix my on code: 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","****","bull****","****","asshole"); //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); } I changed this: $url = preg_replace($regex, $substitute, $response); to this: $responseBody = preg_replace($regex, $substitute, $responseBody); Edited October 21, 2017 by saversites 1 Quote Link to comment Share on other sites More sharing options...
administrator Posted October 22, 2017 Report Share Posted October 22, 2017 That makes sense, you are populating: $responseBody ... with the filter string data. Stef 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.