feulf/raintpl

Issue with img near inputs

Closed this issue · 5 comments

Hi,

I'm using RainTPL 2.7.2 (last version available) and I have some problems with img src. I found out the problem is because my img is near an input field.

Here's my template code (only the relevant part) :

<p><label for="password" class="label-block">Password : </label><input type="password" name="password" id="password"/> <a href="" onclick="toggle_password('password'); return false;"><img src="img/toggleVisible.png" alt="Toggle visible"/></a></p>

If I set rainTPL to replace paths in both img, input and a, I get a src like tpl/tpl/img/toggleVisible.png which doesn't work.

If, on the contrary, I set it to not replace paths in input, I get the correct src replaced (tpl/img/toggleVisible.png).

I'll post again if I can find exactly where does this problem comes from.

After some tests, it's the third expressions in path replace that is a problem :

    $exp = array_merge( $exp , array( '/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) );

(line 670).

I think it's a greedy problem. If I add a flag U, for ungreedy, and relace everything (including input), it works. I didn't test it much more, so I don't know if it can add some other problems.

Hi Lucas, if you're able to solve this issue send a pull req. and I'll
merge it.

Thanks

On Wed, Sep 4, 2013 at 5:16 PM, Lucas Verney notifications@github.comwrote:

After some tests, it's the third expressions in path replace that is a
problem :

$exp = array_merge( $exp , array( '/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) );

(line 670).

I think it's a greedy problem. If I add a flag U, for ungreedy, and relace
everything (including input), it works. I didn't test it much more, so I
don't know if it can add some other problems.


Reply to this email directly or view it on GitHubhttps://github.com//issues/33#issuecomment-23824633
.

The following code works for me. I hope it won't add any side effects. I'll post a pull request as soon as I could make some advanced tests.

if( in_array( "img", self::$path_replace_list ) ){
                $exp = array( '/<img(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/iU', '/<img(.*?)src=(?:")([^"]+?)#(?:")/iU', '/<img(.*?)src="(.*?)"/iU', '/<img(.*?)src=(?:\@)([^"]+?)(?:\@)/iU' );
                $sub = array( '<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"' );
            }

            if( in_array( "script", self::$path_replace_list ) ){
                $exp = array_merge( $exp , array( '/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/iU', '/<script(.*?)src=(?:")([^"]+?)#(?:")/iU', '/<script(.*?)src="(.*?)"/iU', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/iU' ) );
                $sub = array_merge( $sub , array( '<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"' ) );
            }

            if( in_array( "link", self::$path_replace_list ) ){
                $exp = array_merge( $exp , array( '/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/iU', '/<link(.*?)href=(?:")([^"]+?)#(?:")/iU', '/<link(.*?)href="(.*?)"/iU', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/iU' ) );
                $sub = array_merge( $sub , array( '<link$1href=@$2://$3@', '<link$1href=@$2@' , '<link$1href="' . $path . '$2"', '<link$1href="$2"' ) );
            }

            if( in_array( "a", self::$path_replace_list ) ){
                $exp = array_merge( $exp , array( '/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/iU', '/<a(.*?)href="(.*?)"/iU', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/iU'  ) );
                $sub = array_merge( $sub , array( '<a$1href=@$2$3@', '<a$1href="' . self::$base_url . '$2"', '<a$1href="$2"' ) );
            }

            if( in_array( "input", self::$path_replace_list ) ){
                $exp = array_merge( $exp , array( '/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/iU', '/<input(.*?)src=(?:")([^"]+?)#(?:")/iU', '/<input(.*?)src="(.*?)"/iU', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/iU' ) );
                $sub = array_merge( $sub , array( '<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"' ) );
            }

@RainPHP : Do you know if there's any special reason not to set the "i" flag on the third test (src="(.*?)" for each $exp array ? I added it in the previous code.

cool thanks

On Thu, Sep 5, 2013 at 8:41 AM, Lucas Verney notifications@github.comwrote:

The following code works for me. I hope it won't add any side effects.
I'll post a pull request as soon as I could make some advanced tests.

if( in_array( "img", self::$path_replace_list ) ){
$exp = array( '/<img(.?)src=(?:")(http|https)://([^"]+?)(?:")/iU', '/<img(.?)src=(?:")([^"]+?)#(?:")/iU', '/<img(.?)src="(.?)"/iU', '/<img(.*?)src=(?:@)([^"]+?)(?:@)/iU' );
$sub = array( '<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"' );
}

        if( in_array( "script", self::$path_replace_list ) ){
            $exp = array_merge( $exp , array( '/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/iU', '/<script(.*?)src=(?:")([^"]+?)#(?:")/iU', '/<script(.*?)src="(.*?)"/iU', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/iU' ) );
            $sub = array_merge( $sub , array( '<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"' ) );
        }

        if( in_array( "link", self::$path_replace_list ) ){
            $exp = array_merge( $exp , array( '/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/iU', '/<link(.*?)href=(?:")([^"]+?)#(?:")/iU', '/<link(.*?)href="(.*?)"/iU', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/iU' ) );
            $sub = array_merge( $sub , array( '<link$1href=@$2://$3@', '<link$1href=@$2@' , '<link$1href="' . $path . '$2"', '<link$1href="$2"' ) );
        }

        if( in_array( "a", self::$path_replace_list ) ){
            $exp = array_merge( $exp , array( '/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/iU', '/<a(.*?)href="(.*?)"/iU', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/iU'  ) );
            $sub = array_merge( $sub , array( '<a$1href=@$2$3@', '<a$1href="' . self::$base_url . '$2"', '<a$1href="$2"' ) );
        }

        if( in_array( "input", self::$path_replace_list ) ){
            $exp = array_merge( $exp , array( '/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/iU', '/<input(.*?)src=(?:")([^"]+?)#(?:")/iU', '/<input(.*?)src="(.*?)"/iU', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/iU' ) );
            $sub = array_merge( $sub , array( '<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"' ) );
        }

@RainPHP https://github.com/rainphp : Do you know if there's any
special reason not to set the "i" flag on the third test (src="(.*?)" for
each $exp array ? I added it in the previous code.


Reply to this email directly or view it on GitHubhttps://github.com//issues/33#issuecomment-23864564
.

Solved in pull request #34. Waiting for an external review to merge it.