Why str_replace Failing On "action =" ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bits2017
    New Member
    • Jun 2017
    • 39

    Why str_replace Failing On "action =" ?

    Php Experts,

    Look at this cURL script and let me know what you think.

    The cURL fetches pages just like web proxies do.
    The script shows you an html form. It has a field: Url.
    You type a url and cURL fecthes that page and proxies all links by preceding:
    proxified_page_ test.php?url_to _proxify=

    So, if the fetched page contains a link on it's page like so:

    http://google.com/contactus.html

    The script would then string_replace that link with the following before displaying it on you screen:

    proxified_page_ test.php?url_to _proxify=http://google.com/contactus.html

    That way, when you click the link, you don't go direct to the page but to a proxied version of it.

    Now, here is the problem ...
    When you fetch google homepage then it gets fetched alright. But when you do a keyword search then you should see a proxied page of the google serp (searchengine result page) but you see an invalid or dead page instead.
    Keyword search features or forms usually have the "action = url".
    The script is supposed to str_replace the "action =" to:

    action= "proxified_page _test.php?url_t o_proxify=\".$d omain\""'

    But, for some reason, this replacement is not taking place. That is the problem.
    This not doing it's str_replace job:

    Code:
    $pattern = array('action="', 'action = "', 'action= "', 'action ="', "action='", "action = '", "action= '", "action='");
            $replace = array('action="proxified_page_test.php?url_to_proxify=\".$domain\""', 'action = "proxified_page_test.php?url_to_proxify=\".$domain\""', 'action= "proxified_page_test.php?url_to_proxify=\".$domain\""', 'action ="proxified_page_test.php?url_to_proxify=\".$domain\""', "action='proxified_page_test.php?url_to_proxify=\".$domain\"'", "action = 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "action= 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "action ='proxified_page_test.php?url_to_proxify=\".$domain\"'");
            $string_replaced_data = str_replace($pattern, $replace, $curl_result);
            echo var_dump($string_replaced_data);
            print_r($curl_result);

    cURL full Script:

    Code:
    <?php
    //STEP 1: ERROR HANDLING
    declare(strict_types=1);
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    //For All Error, Warning and Notice
    error_reporting(E_ALL);
    E_DEPRECATED;
    /* STEP 2:
    The IF gets triggered as soon as the "submit" button is clicked in the ui text box labeled: Url
    Following IF code deals with GET method.
    */
    if(isset($_GET["url_to_proxify"]) === TRUE)
       {
            echo "IF got triggered!";
            $url_to_proxify = filter_input(INPUT_GET, 'url_to_proxify', FILTER_VALIDATE_URL);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "$url_to_proxify");
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($ch, CURLOPT_HEADER, 5);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            $curl_result = curl_exec($ch);        
            $domain = parse_url($url_to_proxify, PHP_URL_HOST);
            echo var_dump($domain);
            //Add proxy link on all links present on proxified page
            $pattern = array("http://", "https://", "http://www.", "https://www.", "localhost");
            $replace = array("proxified_page_test.php?url_to_proxify=http://\".$domain\"", "proxified_page_test.php?url_to_proxify=https://\".$domain\"", "proxified_page_test.php?url_to_proxify=http://www.\".$domain\"", "proxified_page_test.php?url_to_proxify=https://www.\".$domain\"", "proxified_page_test.php?url_to_proxify=http://www.\".$domain\"");
            $string_replaced_data = str_replace($pattern, $replace, $curl_result);
            echo var_dump($string_replaced_data);
            //Add proxy link on all Image Links (Eg. Google Img File)        
            $pattern = array('src="', 'src = "', 'src= "', 'src ="', "src='", "src = '", "src= '", "src='");
            $replace = array('src="proxified_page_test.php?url_to_proxify=\".$domain\""', 'src = "proxified_page_test.php?url_to_proxify=\".$domain\""', 'src= "proxified_page_test.php?url_to_proxify=\".$domain\""', 'src ="proxified_page_test.php?url_to_proxify=\".$domain\""', "src='proxified_page_test.php?url_to_proxify=\".$domain\"'", "src = 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "src= 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "src ='proxified_page_test.php?url_to_proxify=\".$domain\"'");
            $string_replaced_data = str_replace($pattern, $replace, $curl_result);
            echo var_dump($string_replaced_data);
            //Add proxy link on all links presented by the searchengine result pages (SERPS). Eg. Google Search Pages (SERPs)
            $pattern = array('action="', 'action = "', 'action= "', 'action ="', "action='", "action = '", "action= '", "action='");
            $replace = array('action="proxified_page_test.php?url_to_proxify=\".$domain\""', 'action = "proxified_page_test.php?url_to_proxify=\".$domain\""', 'action= "proxified_page_test.php?url_to_proxify=\".$domain\""', 'action ="proxified_page_test.php?url_to_proxify=\".$domain\""', "action='proxified_page_test.php?url_to_proxify=\".$domain\"'", "action = 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "action= 'proxified_page_test.php?url_to_proxify=\".$domain\"'", "action ='proxified_page_test.php?url_to_proxify=\".$domain\"'");
            $string_replaced_data = str_replace($pattern, $replace, $curl_result);
            echo var_dump($string_replaced_data);
            print_r($curl_result);
        curl_close($ch);
        
                
    }
    else
        {
            echo "ELSE got triggered!";
            //Html Form
            ?>
            <html>
                <body>   
                    <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "GET">
                    Url: <input type = "text" name = "url_to_proxify" />
                    <input type = "submit" />
          </form>      
       </body>
    </html>
    <?php
        }
    ?>
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Why are you using str_replace instead of a regex?

    You should clean up the code formatting to make it more readable and maintainable so that it will be easier to troubleshoot. Line lengths of 500+ characters is a very poor coding practice.

    For example, your $pattern and $replace var assignments would be much more readable and maintainable if you put each array element in its own line, like this:
    Code:
    $pattern = array('action="',
                     'action = "',
    				 'action= "',
    				 'action ="',
    				 "action='",
    				 "action = '",
    				 "action= '",
    				 "action='");
    
    $replace = array('action="proxified_page_test.php?url_to_proxify=\".$domain\""',
    				 'action = "proxified_page_test.php?url_to_proxify=\".$domain\""',
    				 'action= "proxified_page_test.php?url_to_proxify=\".$domain\""',
    				 'action ="proxified_page_test.php?url_to_proxify=\".$domain\""',
    				 "action='proxified_page_test.php?url_to_proxify=\".$domain\"'",
    				 "action = 'proxified_page_test.php?url_to_proxify=\".$domain\"'",
    				 "action= 'proxified_page_test.php?url_to_proxify=\".$domain\"'",
    				 "action ='proxified_page_test.php?url_to_proxify=\".$domain\"'");
    When you format your code in this way, it makes it easier to see that you have a quoting problem, which may be part of the reason why your string replacement is failing.
    Last edited by RonB; Sep 19 '17, 02:55 PM.

    Comment

    • bits2017
      New Member
      • Jun 2017
      • 39

      #3
      RonB,

      I tried your suggestion. But still not working.
      Thanks for the attempt, though!
      Try again to solve the issue. Other forums have failed. Even a youtube tutorial has failed! It seems everyone fails! Even me!

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        What part is failing? Is the string replacement failing or is that part ok but fails later when you try to use that modified string?

        Can you post the content of $curl_result so I can run some tests to get the replacement portion working?

        I should point out that manually parsing html is a bad idea. You should be using an html parser designed for this purpose. Manual parsing is very fragile and is only semi ok when the html is very basic/simple and doesn't change in any way.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          I'm not sure I understand. Why are you searching and replacing the phrase action=?

          When you do a search on google, the url that comes out is https://www.google.com/search?q=test, there is no action= to replace.

          More than likely, you need to urlencode the target so that it knows the whole string is part of the same argument.

          Comment

          Working...