improve escaping

This commit is contained in:
Robin Appelman 2012-12-18 00:37:21 +01:00
commit adf5ec0e6d
3 changed files with 15 additions and 5 deletions

View file

@ -53,7 +53,8 @@ abstract class Command {
*/
public function escapePath($path) {
$path = str_replace('/', '\\', $path);
return '"' . trim(escapeshellarg($path), "'") . '"';
$path = str_replace('"', '^"', $path);
return '"' . $path . '"';
}
/**
@ -61,6 +62,7 @@ abstract class Command {
* @return string
*/
public function escapeLocalPath($path) {
return '"' . trim(escapeshellarg($path), "'") . '"';
$path = str_replace('"', '\"', $path);
return '"' . $path . '"';
}
}

View file

@ -152,7 +152,7 @@ class Share {
* @return array
*/
public function read() {
fgets($this->pipes[1]);//first line is promt
fgets($this->pipes[1]);//first line is prompt
$output = array();
$line = fgets($this->pipes[1]);
while (substr($line, 0, 4) !== 'smb:') { //next prompt functions as delimiter
@ -160,6 +160,5 @@ class Share {
$line = fgets($this->pipes[1]);
}
return $output;
// return explode(PHP_EOL, stream_get_contents($this->pipes[1]));
}
}

View file

@ -76,7 +76,8 @@ class Test extends PHPUnit_Framework_TestCase {
}
public function testEscaping() {
$names = array('simple', 'with spaces');
// / ? < > \ : * | ” are illegal characters in path on windows, no use trying to get them working
$names = array('simple', 'with spaces', "single'quote'");
$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua';
$tmpFile1 = tempnam('/tmp', 'smb_test_');
@ -96,6 +97,7 @@ class Test extends PHPUnit_Framework_TestCase {
$tmpFile2 = tempnam('/tmp', 'smb_test_');
$this->share->get($this->root . '/' . $name . '/foo.txt', $tmpFile2);
$this->assertEquals($text, file_get_contents($tmpFile2));
unlink($tmpFile2);
$this->share->rename($this->root . '/' . $name . '/foo.txt', $this->root . '/' . $name . '/bar.txt');
$dir = $this->share->dir($this->root . '/' . $name);
@ -113,9 +115,16 @@ class Test extends PHPUnit_Framework_TestCase {
$tmpFile2 = tempnam('/tmp', 'smb_test_');
$this->share->get($this->root . '/' . $name, $tmpFile2);
$this->assertEquals($text, file_get_contents($tmpFile2));
unlink($tmpFile2);
$this->share->del($this->root . '/' . $name);
$tmpFile2 = tempnam('/tmp', 'smb_test_' . $name);
$this->share->put($tmpFile2, $this->root . '/' . $name);
$this->assertArrayHasKey($name, $this->share->dir($this->root));
$this->share->del($this->root . '/' . $name);
unlink($tmpFile2);
$this->assertEquals(array(), $this->share->dir($this->root));
}