Skip to content

Commit

Permalink
Adding a security option that prevents output in require_once_dir ($p…
Browse files Browse the repository at this point in the history
…revents_output is true by default)
  • Loading branch information
Fabrice Luraine committed Dec 5, 2009
1 parent 79c6838 commit 2962931
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/limonade.php
Expand Up @@ -1761,16 +1761,17 @@ function v($value, $default)
*
* @param string $path Path in which are the file to load
* @param string $pattern a regexp pattern that filter files to load
* @param bool $prevents_output security option that prevents output
* @return array paths of loaded files
*/
function require_once_dir($path, $pattern = "*.php")
function require_once_dir($path, $pattern = "*.php", $prevents_output = true)
{
if($path[strlen($path) - 1] != "/") $path .= "/";
$filenames = glob($path.$pattern);
if(!is_array($filenames)) $filenames = array();
ob_start();
if($prevents_output) ob_start();
foreach($filenames as $filename) require_once $filename;
ob_end_clean();
if($prevents_output) ob_end_clean();
return $filenames;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/main.php
Expand Up @@ -128,11 +128,17 @@ function test_main_require_once_dir()
ob_start();
assert_empty(require_once_dir($root));
$files = require_once_dir($root, "AUTHORS");
assert_empty(ob_get_contents());
ob_clean();

assert_length_of($files, 1);
assert_match('/AUTHORS$/', $files[0]);

ob_start();
$files = require_once_dir($root, "CHANGES", false);
assert_not_empty(ob_get_contents());
ob_clean();

$lib = $root.'/lib';
$limonade = $lib.'/limonade';

Expand Down

1 comment on commit 2962931

@sofadesign
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also avoid unwanted output, like white chars, in order to prevent corruption when you want to render binary files.

Please sign in to comment.