Is there a way to attach a stream filter to echo so that any data that is echoed goes through the stream filter? My idea is to write an output escaping filter to protect against XSS.
I found this bug report http://bugs.php.net/bug.php?id=30583 but it is from 2004 and I didn't know if this is now possible or not.
class strtoupper_filter extends php_user_filter
{
function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = strtoupper($bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
stream_filter_register("strtoupper", "strtoupper_filter");
$fp = fopen("php://output", "w");
stream_filter_append($fp, "strtoupper");
echo "echo: testing 123<br>";
print("print: testing 123<br>");
fwrite($fp, "fwrite: testing 123<br>");