Req #62745 [Wfx]: Extend echo and print possiblity
| From: | kjelkenes at gmail dot com | Date: | Mon, 06 Aug 2012 18:59:35 +0000 |
| Subject: | Req #62745 [Wfx]: Extend echo and print possiblity | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-172475@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62745&edit=1
ID: 62745
User updated by: kjelkenes at gmail dot com
Reported by: kjelkenes at gmail dot com
Summary: Extend echo and print possiblity
Status: Wont fix
Type: Feature/Change Request
Package: Output Control
Operating System: *
PHP Version: 5.4.5
Block user comment: N
Private report: N
New Comment:
Just stating the obvious.
OOP, MVC, Templating engine, you sir have greatly missed the point of this feature request.
Are you talking about OOP? OOP has _nothing_ to do with this question, of course you would use OOP:
classes, namespaces and traits.. But this is not the place to talk about OOP design.
Are you talking about MVC? Because this addition will make every currently existing MVC frameworks
such as Symfony 2 and Codeigniter more secure.
You separate controller / business logic / presentation logic, but you are now starting a discussion
of using a 3rdparty library against php itself as a templating-engine. Does not really make sense at
all regarding this feature request.
You don't USE a templating framework if you implement this change, this change will make it
easier then never to CREATE the fastest templating engine on the market without having to parse code
to php code and then use the php code to make php safe. Note, php is a language, you can also
template with it because of it's easy syntax HTML<?php echo $var?>MORE_HTML.
In Symfony 2 you have to either use
- TWIG syntax (Templating engine)
- echo $this->escape("<script></script"); ( Symfony way .. )
Now, this didn't have to be needed if this feature request was implemented.
- echo "<script>.."; ( This is not possible at this time )
Previous Comments:
------------------------------------------------------------------------
[2012-08-06 18:27:49] phpmpan at mpan dot pl
When making general statements, first make sure that by writing "all people do" you
don't mean "I do". Also please take a look at the calendar. Mine says 2012, not the
end of XX century. Maybe 10 years ago it was acceptable, but it's no longer a good habit to mix
business logic and presentation layer, virtually any bigger application is written in OOP (which
doesn't mix well with exiting PHP mode) and the presentation layer is handled by template
engines or frameworks.
As I said before: you're trying to solve a problem that doesn't really exist. If it can be
observed anywhere, it's just a symptom of a different problem that lies between keyboard and
chair, not in PHP.
Also it's not a good idea to call PHP devs ignorants and raging because your idea is not
accepted. If you believe that it's really needed, try to convince people it is so! Insulting
will not get you anywhere.
------------------------------------------------------------------------
[2012-08-06 13:07:30] kjelkenes at gmail dot com
Ignorance, gotta love it. There is really a difference of output buffering functions and this, I do
of course know of the output buffering functions, this is NOT RELATED. Read on to really see what I
mean about this.
This is how people in most cases write their VIEW logic. Meaning ending and starting <?php echo
..?> every time they echo stuff.. That also means you should ESCAPE ALL data that comes to echo,
else you are just not SAFE.
index.php:
<?php
// Web page title.
$title = 'My website';
// A item from the database.
$item = array('title' =>
'<script>alert("Hi!")</script>');
?>
<?php ob_start() ?>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>
<div><p><?php echo $item['title']?></p></div>
</body>
</html>
<?php
$content = htmlspecialchars(ob_get_flush(),ENT_QUOTES,'UTF-8');
echo $content;
/* Returns:
<html>
<head>
<title>My website</title>
</head>
<body>
<div><p><script>alert("Hi!")</script></p></div>
</body>
</html>
FAIL?! We NEED a echo handler....
*/
?>
Obviously this won't work if people wrote their template like this (but that's up to
them...):
index.php
echo "
<html>
<head>
<title>{$title}</title>
</head>
<body>
<div><p>{$item['title']}</p></div>
</body>
</html>
";
This is wanted behaviour (The use of htmlspecialchars wouldn't be necessary if we had a handler
that intercepted the echo statement.):
<?php
// Web page title.
$title = 'My website';
// A item from the database.
$item = array('title' =>
'<script>alert("Hi!")</script>');
?>
<html>
<head>
<title><?php echo
htmlspecialchars($title,ENT_QUOTES,'UTF-8')?></title>
</head>
<body>
<div><p><?php echo
htmlspecialchars($item['title'],ENT_QUOTES,'UTF-8')?></p></div>
</body>
</html>
<?php
/* Returns:
<html>
<head>
<title>My website</title>
</head>
<body>
<div><p><script>alert("Hi!")</script></p></div>
</body>
</html>
Perfect!
*/
?>
------------------------------------------------------------------------
[2012-08-06 01:01:42] aharvey@php.net
The commenters are right: output buffering already deals with the feature as requested, and as
Laruence points out, the taint extension is available for the underlying issue if you want to go
down that road.
Closing.
------------------------------------------------------------------------
[2012-08-05 06:55:18] laruence@php.net
1. if you want taint mode, refer to : http://pecl.php.net/taint
2. if you want escape output: refer to http://www.php.net/manual/en/function.ob-
start.php
thanks
------------------------------------------------------------------------
[2012-08-05 05:43:07] phpmpan at mpan dot pl
From my point of view, keithm provides a solution that does exactly the thing you have asked for.
Output buffering wasn't created for this purpose, but it can be easily used for it without
breaking anything. If this is not what you wanted, maybe your description is not clear enough?
However this is a solution for a problem that doesn't exist in reality.
1. Most of the data output by scripts should never be escaped.
Yet your idea causes ALL data to be escaped, producing garbage.
2. In few specific cases there are small portions of data from untrusted
sources that should be escaped. In such cases a single call is enough.
What you want to be introduced requires 3 lines of code (enable escaping,
echo, disable escaping) just to make same thing a single function call
could do.
3. Even worse: the concept is perpendicular to echo by design,
but not perpendicular to echo by behaviour. Hence it's a design error.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=62745
--
Edit this bug report at https://bugs.php.net/bug.php?id=62745&edit=1