Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.10.12

Bug #10185 problem with addslashs in container "PHPArray" / method "toString"
Submitted: 2007-02-25 22:54 UTC Modified: 2007-03-26 20:00 UTC
From: cwiedmann Assigned: aashley
Status: Closed Package: Config (version 1.10.9)
PHP Version: 5.2.0 OS: Windows NT
Roadmaps: 1.10.10    
Subscription  


 [2007-02-25 22:54 UTC] cwiedmann (Wiedmann)
Description: ------------ Hello, at the moment I'm working with Apache config files. Some directives can have a doublequote in the parameter string. If I parse a config file and write it to a PHPArray, all doublequotes have a unwanted slash. Now if I parse this PHPArray and write it back to a Apache config file, the result is not correct. I think you shoud only add slashs to singlequotes and backslashs. I use addcslashes() instead of addslashes() in my patch, to prevent this problem. Regards, Carsten Test script: --------------- test.php -------- <?php header('Content-Type: text/plain'); require_once 'Config.php'; $conf = 'LogFormat "%h %l %u %t \"%r\" %>s %b" common'.PHP_EOL; $conf .= '<Directory "/foo/bar">'.PHP_EOL; $conf .= ' CustomLog "/foo/bar/access.log" common'.PHP_EOL; $conf .= '</Directory>'.PHP_EOL; $conf .= '<Directory "\foo\bar\">'.PHP_EOL; $conf .= ' CustomLog "\foo\bar\access.log" common'.PHP_EOL; $conf .= '</Directory>'.PHP_EOL; $conf .= '<Directory "\\\foo\\\bar">'.PHP_EOL; $conf .= ' CustomLog "\\\foo\\\bar\\\access.log" common'.PHP_EOL; $conf .= '</Directory>'.PHP_EOL; echo '======== out1 ========'.PHP_EOL; file_put_contents('httpd.conf.old', $conf); readfile('httpd.conf.old'); echo '======================'.PHP_EOL; echo PHP_EOL; $conf1 = new Config; $root1 =& $conf1->parseConfig('httpd.conf.old', 'apache'); echo '======== out2 ========'.PHP_EOL; print_r($root1->toArray()); echo '======== out3 ========'.PHP_EOL; print_r($root1->toString('apache')); echo '======== out4 ========'.PHP_EOL; $conf1->writeConfig('httpd.conf.new.php', 'phparray', array('name' => 'test')); readfile('httpd.conf.new.php'); echo PHP_EOL.'======================'.PHP_EOL; echo PHP_EOL; $conf2 = new Config; $root2 =& $conf2->parseConfig('httpd.conf.new.php', 'phparray', array('name' => 'test')); echo '======== out5 ========'.PHP_EOL; print_r($root2->toArray()); echo '======== out6 ========'.PHP_EOL; print_r($root2->toString('apache')); echo '======== out7 ========'.PHP_EOL; $conf2->writeConfig('httpd.conf.new', 'apache'); readfile('httpd.conf.new'); echo '======================'.PHP_EOL; ?> PHPArray.php.diff ----------------- --- PHPArray.php.v1.29 Sun Feb 25 18:33:30 2007 +++ PHPArray.php Mon Feb 26 03:22:29 2007 @@ -163,14 +163,15 @@ $string .= $parentString."['#']"; foreach ($attributes as $attr => $val) { $attrString .= $parentString."['@']" - ."['".$attr."'] = '".addslashes($val)."';\n"; + ."['".$attr."'] = '".addcslashes($val, "\\'")."';\n"; + } } else { $string .= $parentString; } $string .= ' = '; if (is_string($obj->content)) { - $string .= "'".addslashes($obj->content)."'"; + $string .= "'".addcslashes($obj->content, "\\'")."'"; } elseif (is_int($obj->content) || is_float($obj->content)) { $string .= $obj->content; } elseif (is_bool($obj->content)) { @@ -186,7 +187,7 @@ $parentString = $this->_getParentString($obj); foreach ($attributes as $attr => $val) { $attrString .= $parentString."['@']" - ."['".$attr."'] = '".addslashes($val)."';\n"; + ."['".$attr."'] = '".addcslashes($val, "\\'")."';\n"; } } $string .= $attrString; Expected result: ---------------- ======== out4 ======== <?php $test['LogFormat'] = '"%h %l %u %t \\"%r\\" %>s %b" common'; $test['Directory'][0]['@']['0'] = '"/foo/bar"'; $test['Directory'][0]['CustomLog'] = '"/foo/bar/access.log" common'; $test['Directory'][1]['@']['0'] = '"\\foo\\bar\\"'; $test['Directory'][1]['CustomLog'] = '"\\foo\\bar\\access.log" common'; $test['Directory'][2]['@']['0'] = '"\\\\foo\\\\bar"'; $test['Directory'][2]['CustomLog'] = '"\\\\foo\\\\bar\\\\access.log" common'; ?> ====================== ======== out7 ======== LogFormat "%h %l %u %t \"%r\" %>s %b" common <Directory "/foo/bar"> CustomLog "/foo/bar/access.log" common </Directory> <Directory "\foo\bar\"> CustomLog "\foo\bar\access.log" common </Directory> <Directory "\\foo\\bar"> CustomLog "\\foo\\bar\\access.log" common </Directory> ====================== Actual result: -------------- ======== out4 ======== <?php $test['LogFormat'] = '\"%h %l %u %t \\\"%r\\\" %>s %b\" common'; $test['Directory'][0]['@']['0'] = '\"/foo/bar\"'; $test['Directory'][0]['CustomLog'] = '\"/foo/bar/access.log\" common'; $test['Directory'][1]['@']['0'] = '\"\\foo\\bar\\\"'; $test['Directory'][1]['CustomLog'] = '\"\\foo\\bar\\access.log\" common'; $test['Directory'][2]['@']['0'] = '\"\\\\foo\\\\bar\"'; $test['Directory'][2]['CustomLog'] = '\"\\\\foo\\\\bar\\\\access.log\" common'; ?> ====================== ======== out7 ======== LogFormat \"%h %l %u %t \\"%r\\" %>s %b\" common <Directory \"/foo/bar\"> CustomLog \"/foo/bar/access.log\" common </Directory> <Directory \"\foo\bar\\"> CustomLog \"\foo\bar\access.log\" common </Directory> <Directory \"\\foo\\bar\"> CustomLog \"\\foo\\bar\\access.log\" common </Directory> ======================

Comments

 [2007-03-26 20:00 UTC] aashley (Adam Ashley)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.
 [2007-05-31 04:26 UTC] tomdesp (Thomas Despoix)
The bug is not fixed, but created the exact same problem now with simgle quotes. You should have followed Wiedmann's suggestion to use addcslashes() instead of delimiting PHP strings with double quotes. I'm on the way to report the new deplicate bug.
 [2007-05-31 05:04 UTC] tomdesp (Thomas Despoix)
See new bug report : Bug #11184