Req->Bug #74472 [Opn->Nab]: readdir strips leading and trailing quotes in a filename
| From: | requinix@php.net | Date: | Wed, 19 Apr 2017 05:13:44 +0000 |
| Subject: | Req->Bug #74472 [Opn->Nab]: readdir strips leading and trailing quotes in a filename | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-208652@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74472&edit=1
ID: 74472
Updated by: requinix@php.net
Reported by: james at workinout dot com
Summary: readdir strips leading and trailing quotes in a
filename
-Status: Open
+Status: Not a bug
-Type: Feature/Change Request
+Type: Bug
Package: Filesystem function related
Operating System: osx 10.12
PHP Version: 7.1.4
Block user comment: N
Private report: N
New Comment:
The files don't have quotes.
http://cgit.drupalcode.org/omega/tree/omega/starterkits/default/js?h=7.x-4.x
The filename must be escaped in the shell command, like with escapeshellarg. Not doing so will cause
problems for files that contain spaces or other potentially unsafe characters (such as
'{'). For example,
exec("stat --format=%G " . escapeshellarg($entry));
The fact that the escaped version has quotes is simply because that's the easiest way to make a
string safe.
The quotes you're seeing mentioned somewhere are probably indicative of either PHP code (quotes
form a string) or an actual shell command (author escaped the filename).
Previous Comments:
------------------------------------------------------------------------
[2017-04-19 04:36:34] james at workinout dot com
Description:
------------
in drupal, some files named with leading and trailing quotes
filename === '{{ THEME SANITIZED }}.behaviors.js'
$entry = readdir() --- > $entry === -> {{ THEME SANITIZED }}.behaviors.js (NO QUOTES)
stat will not work because the stripped quotes are needed: stat
complains like below:
stat --format=%G (command used..)
stat: cannot stat '{{': No such file or directory
stat: cannot stat 'THEME': No such file or directory
stat: cannot stat 'SANITIZED': No such file or directory
stat: cannot stat '}}.behaviors.js': No such file or directory
stat: cannot stat '{{': No such file or directory
stat: cannot stat 'THEME': No such file or directory
stat: cannot stat 'SANITIZED': No such file or directory
stat: cannot stat '}}.behaviors.js': No such file or directory
Test script:
---------------
Solution is to put the quotes back in filename.
$entry = "'" . $entry . "'".
stat works ok then...
if ($handle = opendir($name)) {
while (false !== ($entry = readdir($handle))) {
if (preg_match('/\.$|\..$/', $entry)) {
continue;
}
//special case for filenames with single quotes (they are stripped by readdir() )
// used in omega themes with {{ in the filename..
if(preg_match('/\{/', $entry)) {
$entry = "'" . $entry . "'";
}
<after this, stat will work....
Expected result:
----------------
providing an optional argument in readdir() to not strip leading,trailing quotes would be useful
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74472&edit=1