New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The url tag produce an extra directory separator before the result #625

Closed
frkinta opened this Issue Dec 30, 2016 · 14 comments

Comments

Projects
None yet
3 participants
@frkinta
Contributor

frkinta commented Dec 30, 2016

Hello,

I have a problem using Url Tags When i add "<%/ image.jpg %>" I have good result in localhost but on production server I have a directory separator before the url; resulting "\/image.jpg" on IIS

@frkinta frkinta changed the title from The url tag produce an directory separator before the result to The url tag produce an extra directory separator before the result Dec 30, 2016

@ctrlaltca

This comment has been minimized.

Member

ctrlaltca commented Dec 31, 2016

Can you please try to patch framework/Web/UI/TTemplate.php at line 665 so that the line reads:

OLD

$tpl[$c++]=array($container,array(TCompositeLiteral::TYPE_EXPRESSION,"rtrim(dirname(\$this->getApplication()->getRequest()->getApplicationUrl()), '/').'/$literal'"));

NEW

$tpl[$c++]=array($container,array(TCompositeLiteral::TYPE_EXPRESSION,"rtrim(dirname(\$this->getApplication()->getRequest()->getApplicationUrl()), '/\\').'/$literal'"));

If it works, i'll be glad to commit the fix here and in a few other places where the same issue is present in the framework. Thank you

@frkinta

This comment has been minimized.

Contributor

frkinta commented Dec 31, 2016

I changed it in framework/Web/UI/TTemplateManager.php but it didn't work

@ctrlaltca

This comment has been minimized.

Member

ctrlaltca commented Jan 1, 2017

So, i took the time of installing a fresh Windows 7 vm, install IIS7.5 with php5.6 (fastcgi) and load up prado and prado-demos.
I've modified the "helloworld" demo like this:

<html>

<head>
<title>Hello World Demo - Prado</title>
</head>

<body>

<com:TForm>
<com:TButton Text="Click me" OnClick="buttonClicked" />
<br/>
<com:TTextBox Text="<%/ welcome.png %>" Style="width:40em"/>
<com:TImage ImageUrl="<%/ welcome.png %>" />
</com:TForm>

</body>
</html>

And here's the result:

schermata 2016-01-01 alle 15 26 52

So, it looks that in my test this is working. Can you try to replicate my test and post the result?

@frkinta

This comment has been minimized.

Contributor

frkinta commented Jan 2, 2017

This is the code

<html>
    <head>
        <title>Hello World Demo - Prado</title>
    </head>
    <body>
    <com:TForm>
        <com:TButton Text="Click me" OnClick="buttonClicked" />
        <br/>
        <com:TTextBox Text="<%/ logoONAB.jpg %>" Style="width:40em"/>
        <com:TImage ImageUrl="<%/ logoONAB.jpg %>" AlternateText="<%/ logoONAB.jpg %>" />
        <com:TImage ImageUrl="<%/ assets/logoONAB.jpg %>" AlternateText="<%/ assets/logoONAB.jpg %>" />
    </com:TForm>
</body>
</html>

helloword.txt

You can see the result here (Windows server 2008, IIS7.5, php 5.4.45 CGI/FastCGI)

image

@frkinta

This comment has been minimized.

Contributor

frkinta commented Jan 3, 2017

Another error occured when trying to change the "helloword"

MasterLayout.tpl

<!DOCTYPE html>
<html lang="fr">
    <com:THead Title="Accueil">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="<%/ assets/images/favicon.png %>" rel="icon" type="image/png" />
        <link href="<%/ assets/bootstrap-334/css/bootstrap.min.css %>" rel="stylesheet" type="text/css" />
    </com:THead>
    <body>
    <com:TForm>
        <com:TContentPlaceHolder ID="Main" />
    </com:TForm>
</body>
</html>

MasterLayout.php


class MasterLayout extends TTemplateControl {
    public function onLoad($param) {
        parent::onLoad($param);
    }
}

helloword2.page

<%@ MasterClass="Application.pages.MasterLayout" %>

<com:TContent ID="Main">
    <com:TButton Text="Click me" OnClick="buttonClicked" />
    <br/>
    <com:TTextBox Text="<%/ logoONAB.jpg %>" Style="width:40em"/>
    <com:TImage ImageUrl="<%/ logoONAB.jpg %>" AlternateText="<%/ logoONAB.jpg %>" />
    <com:TImage ImageUrl="<%/ assets/logoONAB.jpg %>" AlternateText="<%/ assets/logoONAB.jpg %>" />
</com:TContent>

The result is an error

image

@frkinta

This comment has been minimized.

Contributor

frkinta commented Jan 11, 2017

Finally I corrupt framework/TComponent.php on line 1232

	public function evaluateExpression($expression)
	{
		$expression=$this->dyEvaluateExpressionFilter($expression);
		try
		{
                    $expression=str_replace("rtrim(", "",$expression);
                    $lst = explode('.',$expression);
                    $tmp = explode(',',$lst[0]);
                    $lst[0]=$tmp[0];
                    $expression=implode('.',$lst);
			if(eval("\$result=$expression;")===false)
				throw new Exception('');
                        $result = str_replace('\\','',$result);
			return $result;
		}
		catch(Exception $e)
		{
			throw new TInvalidOperationException('component_expression_invalid',get_class($this),$expression,$e->getMessage());
		}
	}

It is solution for the initial problem. I don't know if it have a negative impact elsewhere.

You can see the "helloword" result with framework version 332 (modified) and 324

@frkinta frkinta closed this Jan 11, 2017

@ctrlaltca

This comment has been minimized.

Member

ctrlaltca commented Jan 11, 2017

That's more a workaround than a fix, and it will have a negative impact on every expression that contains a \ character, removing it. A real fix would be to understand what adds the character in the expression in the first place.

@frkinta frkinta reopened this Jan 11, 2017

@frkinta

This comment has been minimized.

Contributor

frkinta commented Jan 11, 2017

So I'm really in trouble

@frkinta

This comment has been minimized.

Contributor

frkinta commented Jan 11, 2017

The problem is the return of dirname php function.

On server root (windows),

  • $this->getApplication()->getRequest()->getApplicationUrl() return /index.php or /helloword/index.php.
  • dirname($this->getApplication()->getRequest()->getApplicationUrl()) will return\ or/helloword (The \ prefix only appear when index.php is on $_SERVER["DOCUMENT_ROOT"] this is why @ctrlaltca didn't have error on localhost).

rtrim(dirname($this->getApplication()->getRequest()->getApplicationUrl()), '/').'/logoONAB.jpg' or rtrim(dirname($this->getApplication()->getRequest()->getApplicationUrl()), '/\').'/logoONAB.jpg' always return \/logoONAB.jpg.

When I change rtrim(dirname($this->getApplication()->getRequest()->getApplicationUrl()), '......') to
str_replace('\\', '', dirname($this->getApplication()->getRequest()->getApplicationUrl())) it work both on windows and linux.

@ctrlaltca

This comment has been minimized.

Member

ctrlaltca commented Jan 11, 2017

Thank you for the update, will test later today

@LCSKJ

This comment has been minimized.

Member

LCSKJ commented Jan 11, 2017

EDIT:
Just ignore this post, stupid me didn't read answers carefully, don't slap me, it was late 😄. My proposal was similar to the first answer from @ctrlaltca that is even more correct if your entry script resides within in subfolder - so all credit to @ctrlaltca. Seems like I overread the rtrim(..., '/\\') at the end of the line which is superior to using DIRECTORY_SEPERATOR. I leave my original answer here though since it (tries to) explain why it happens. Since it doesn't seem to help with the original problem though I'm out of proposals either for now.

EDIT#2:
I just tested the fix from @ctrlaltca first answer on a virtual windows server and it's working for me just fine, w/o it I had the same issues. Be sure to escape the backslash properly!

IGNORE (just for reference):
This may also be a problem with platform specific DIRECTORY_SEPERATORs ... dirname() (normally) works with file paths but in TTemplate it gets passed an URL. Under linux only / (slashes) are valid in file paths and under windows both / (slashes) and \ (backslashes) are valid, but URLs always only contain / (slashes). Now, if your entry script (which $this->getApplication()->getRequest()->getApplicationUrl() returns) gets passed to dirname() it may return a \ (backslash) if it is top level which will not get trimmed by rtrim(..., '/').

Example:

$url = '/index.php';
$dirname = dirname($url);
$trim = rtrim($dirname, '/');

This will result in $dirname containing a / (slash) under linux and a \ (backslash) under windows and thus $trim will be empty under linux and contain a \ (backslash) under windows, which is exactly the behavior @frkinta is facing.

So a fix could be to change framework/Web/UI/TTemplate.php at line 665 to use DIRECTORY_SEPERATOR instead of '/' for trimming like

$tpl[$c++]=array($container,array(TCompositeLiteral::TYPE_EXPRESSION,"rtrim(dirname(\$this->getApplication()->getRequest()->getApplicationUrl()), DIRECTORY_SEPERATOR).'/$literal'"));

I do not have a windows server at hand, would be nice if some can test it ... I can guarantee nothing though, it's just a proposal 😄

ctrlaltca added a commit that referenced this issue Jan 15, 2017

@ctrlaltca ctrlaltca closed this in 9ef9e7e Jan 15, 2017

@ctrlaltca

This comment has been minimized.

Member

ctrlaltca commented Jan 15, 2017

So, i finally managed to reproduce the problem and test a fix for it.
The original patch proposed in #625 (comment) was working, but we need to patch the code in another point to fix the same issue when the url tag was used inside a component property, eg:

<com:TTextBox Text="<%/ image.jpg %>" />
@frkinta

This comment has been minimized.

Contributor

frkinta commented Jan 23, 2017

It look like not fixed in TTemplate class in pradolite.php file

@ctrlaltca

This comment has been minimized.

Member

ctrlaltca commented Jan 23, 2017

Updated in 5dd4b52, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment