Move some code arround

This commit is contained in:
Robin Appelman 2013-05-01 00:03:10 +02:00
commit 78916cfc08
15 changed files with 161 additions and 382 deletions

View file

@ -1,94 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
abstract class Command {
/**
* @var \SMB\Connection $connection
*/
protected $connection;
/**
* @param \SMB\Connection $connection
*/
public function __construct($connection) {
$this->connection = $connection;
}
/**
* @param string $command
* @return array
*/
protected function execute($command) {
$this->connection->write($command . PHP_EOL);
$output = $this->connection->read();
return $output;
}
abstract public function run($arguments);
/**
* @param $lines
*
* @throws \SMB\NotFoundException
* @throws \SMB\AlreadyExistsException
* @throws \SMB\AccessDeniedException
* @throws \SMB\NotEmptyException
* @throws \Exception
* @return bool
*/
protected function parseOutput($lines) {
if (count($lines) === 0) {
return true;
} else {
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
case 'NT_STATUS_NO_SUCH_FILE':
throw new \SMB\NotFoundException();
case 'NT_STATUS_OBJECT_NAME_COLLISION':
throw new \SMB\AlreadyExistsException();
case 'NT_STATUS_ACCESS_DENIED':
throw new \SMB\AccessDeniedException();
case 'NT_STATUS_DIRECTORY_NOT_EMPTY':
throw new \SMB\NotEmptyException();
default:
throw new \Exception();
}
}
}
/**
* @param string $string
* @return string
*/
public function escape($string) {
return escapeshellarg($string);
}
/**
* @param string $path
* @return string
*/
public function escapePath($path) {
$path = str_replace('/', '\\', $path);
$path = str_replace('"', '^"', $path);
return '"' . $path . '"';
}
/**
* @param string $path
* @return string
*/
public function escapeLocalPath($path) {
$path = str_replace('"', '\"', $path);
return '"' . $path . '"';
}
}

View file

@ -1,16 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
class Del extends Simple {
public function __construct($connection) {
parent::__construct($connection);
$this->command = 'del';
}
}

View file

@ -1,48 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
class Dir extends Command {
public function __construct($share) {
parent::__construct($share);
}
public function run($arguments) {
$path = $this->escapePath($arguments['path']);
$this->execute('cd ' . $path);
$output = $this->execute('dir');
$this->execute('cd /');
return $this->parseOutput($output);
}
/**
* @param $lines
* @return array
*/
protected function parseOutput($lines) {
//last line is used space
array_pop($lines);
$regex = '/^\s*(.*?)\s\s\s\s+(?:([DHARS]*)\s+)?([0-9]+)\s+(.*)$/';
//2 spaces, filename, optional type, size, date
$content = array();
foreach ($lines as $line) {
if (preg_match($regex,$line,$matches)) {
list(,$name, $type, $size, $time)=$matches;
if ($name !== '.' and $name !== '..') {
$content[$name] = array(
'size' => intval(trim($size)),
'type' => (strpos($type,'D')!==false) ? 'dir' : 'file',
'time' => strtotime($time)
);
}
}
}
return $content;
}
}

View file

@ -1,27 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
/**
* run a command with two path parameter
*/
abstract class Double extends Command {
/**
* @var string $command
*/
protected $command;
public function run($arguments) {
$path1 = $this->escapePath($arguments['path1']);
$path2 = $this->escapePath($arguments['path2']);
$cmd = $this->command . ' ' . $path1 . ' ' . $path2;
$output = $this->execute($cmd);
return $this->parseOutput($output);
}
}

View file

@ -1,18 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
class Get extends Command {
public function run($arguments) {
$path1 = $this->escapePath($arguments['path1']);
$path2 = $this->escapeLocalPath($arguments['path2']); //second path is local, needs different escaping
$output = $this->execute('get ' . $path1 . ' ' . $path2);
return $this->parseOutput($output);
}
}

View file

@ -1,48 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
class ListShares {
/**
* @var \SMB\Server $server
*/
private $server;
/**
* @param \SMB\Server $server
*/
public function __construct($server){
$this->server=$server;
}
public function run() {
$auth = escapeshellarg($this->server->getAuthString()); //TODO: don't pass password as shell argument
$command = \SMB\Server::CLIENT . ' -N -U ' . $auth . ' ' . '-gL ' . escapeshellarg($this->server->getHost()) . ' 2> /dev/null';
exec($command, $output);
return $this->parseOutput($output);
}
/**
* @param $lines
* @return array
*/
protected function parseOutput($lines) {
$shares = array();
foreach ($lines as $line) {
if (strpos($line, '|')) {
list($type, $name, $description) = explode('|', $line);
if (strtolower($type) === 'disk') {
$shares[$name] = $description;
}
}
}
return $shares;
}
}

View file

@ -1,16 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
class Mkdir extends Simple {
public function __construct($connection) {
parent::__construct($connection);
$this->command = 'mkdir';
}
}

View file

@ -1,37 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
class Put extends Command {
public function run($arguments) {
$path1 = $this->escapeLocalPath($arguments['path1']); //first path is local, needs different escaping
$path2 = $this->escapePath($arguments['path2']);
$output = $this->execute('put ' . $path1 . ' ' . $path2);
return $this->parseOutput($output);
}
/**
* @param $lines
*
* @throws \SMB\NotFoundException
* @return bool
*/
protected function parseOutput($lines) {
if (count($lines) === 0) {
return true;
} else {
if (strpos($lines[0], 'does not exist')) {
throw new \SMB\NotFoundException();
} else {
parent::parseOutput($lines);
}
return false;
}
}
}

View file

@ -1,16 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
class Rename extends Double {
public function __construct($connection) {
parent::__construct($connection);
$this->command = 'rename';
}
}

View file

@ -1,16 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
class Rmdir extends Simple {
public function __construct($connection) {
parent::__construct($connection);
$this->command = 'rmdir';
}
}

View file

@ -1,26 +0,0 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace SMB\Command;
/**
* run a command with one path parameter
*/
abstract class Simple extends Command {
/**
* @var string $command
*/
protected $command;
public function run($arguments) {
$path = $this->escapePath($arguments['path']);
$cmd = $this->command . ' ' . $path;
$output = $this->execute($cmd);
return $this->parseOutput($output);
}
}

View file

@ -110,7 +110,13 @@ class Connection {
} }
} }
public function close() {
$this->write('close' . PHP_EOL);
}
public function __destruct() { public function __destruct() {
$this->close();
proc_terminate($this->process);
proc_close($this->process); proc_close($this->process);
} }
} }

View file

@ -1,6 +1,6 @@
<?php <?php
/** /**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com> * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or * This file is licensed under the Affero General Public License version 3 or
* later. * later.
* See the COPYING-README file. * See the COPYING-README file.
@ -39,8 +39,9 @@ class Server {
* @param string $host * @param string $host
* @param string $user * @param string $user
* @param string $password * @param string $password
* @param bool $caching
*/ */
public function __construct($host, $user, $password, $caching = self::CACHING_ENABLED) { public function __construct($host, $user, $password, $caching = self::CACHING_DISABLED) {
$this->host = $host; $this->host = $host;
$this->user = $user; $this->user = $user;
$this->password = $password; $this->password = $password;
@ -79,8 +80,20 @@ class Server {
* @return Share[] * @return Share[]
*/ */
public function listShares() { public function listShares() {
$cmd = new Command\ListShares($this); $auth = escapeshellarg($this->getAuthString()); //TODO: don't pass password as shell argument
$shareNames = $cmd->run(null); $command = self::CLIENT . ' -N -U ' . $auth . ' ' . '-gL ' . escapeshellarg($this->getHost());// . ' 2> /dev/null';
exec($command, $output);
$shareNames = array();
foreach ($output as $line) {
if (strpos($line, '|')) {
list($type, $name, $description) = explode('|', $line);
if (strtolower($type) === 'disk') {
$shareNames[$name] = $description;
}
}
}
$shares = array(); $shares = array();
foreach ($shareNames as $name => $description) { foreach ($shareNames as $name => $description) {
$shares[] = $this->getShare($name); $shares[] = $this->getShare($name);

View file

@ -32,7 +32,12 @@ class Share {
public function __construct($server, $name) { public function __construct($server, $name) {
$this->server = $server; $this->server = $server;
$this->name = $name; $this->name = $name;
}
public function connect() {
if ($this->connection and $this->connection->isValid()) {
return;
}
$command = Server::CLIENT . ' -U ' . escapeshellarg($this->server->getUser()) . $command = Server::CLIENT . ' -U ' . escapeshellarg($this->server->getUser()) .
' //' . $this->server->getHost() . '/' . $this->name; ' //' . $this->server->getHost() . '/' . $this->name;
$this->connection = new Connection($command); $this->connection = new Connection($command);
@ -42,17 +47,17 @@ class Share {
} }
} }
public function connect() {
$command = Server::CLIENT . ' -U ' . escapeshellarg($this->server->getUser()) .
' //' . $this->server->getHost() . '/' . $this->name;
$this->connection = new Connection($command);
$this->connection->write($this->server->getPassword());
}
public function getName() { public function getName() {
return $this->name; return $this->name;
} }
protected function simpleCommand($command, $path) {
$path = $this->escapePath($path);
$cmd = $command . ' ' . $path;
$output = $this->execute($cmd);
return $this->parseOutput($output);
}
/** /**
* List the content of a remote folder * List the content of a remote folder
* *
@ -60,7 +65,29 @@ class Share {
* @return array * @return array
*/ */
public function dir($path) { public function dir($path) {
return (new Command\Dir($this->connection))->run(array('path' => $path)); $path = $this->escapePath($path);
$this->execute('cd ' . $path);
$output = $this->execute('dir');
$this->execute('cd /');
//last line is used space
array_pop($output);
$regex = '/^\s*(.*?)\s\s\s\s+(?:([DHARS]*)\s+)?([0-9]+)\s+(.*)$/';
//2 spaces, filename, optional type, size, date
$content = array();
foreach ($output as $line) {
if (preg_match($regex, $line, $matches)) {
list(, $name, $type, $size, $time) = $matches;
if ($name !== '.' and $name !== '..') {
$content[$name] = array(
'size' => intval(trim($size)),
'type' => (strpos($type, 'D') !== false) ? 'dir' : 'file',
'time' => strtotime($time)
);
}
}
}
return $content;
} }
/** /**
@ -70,7 +97,7 @@ class Share {
* @return bool * @return bool
*/ */
public function mkdir($path) { public function mkdir($path) {
return (new Command\Mkdir($this->connection))->run(array('path' => $path)); return $this->simpleCommand('mkdir', $path);
} }
/** /**
@ -80,7 +107,7 @@ class Share {
* @return bool * @return bool
*/ */
public function rmdir($path) { public function rmdir($path) {
return (new Command\Rmdir($this->connection))->run(array('path' => $path)); return $this->simpleCommand('rmdir', $path);
} }
/** /**
@ -90,7 +117,7 @@ class Share {
* @return bool * @return bool
*/ */
public function del($path) { public function del($path) {
return (new Command\Del($this->connection))->run(array('path' => $path)); return $this->simpleCommand('del', $path);
} }
/** /**
@ -101,7 +128,11 @@ class Share {
* @return bool * @return bool
*/ */
public function rename($from, $to) { public function rename($from, $to) {
return (new Command\Rename($this->connection))->run(array('path1' => $from, 'path2' => $to)); $path1 = $this->escapePath($from);
$path2 = $this->escapePath($to);
$cmd = 'rename ' . $path1 . ' ' . $path2;
$output = $this->execute($cmd);
return $this->parseOutput($output);
} }
/** /**
@ -112,7 +143,10 @@ class Share {
* @return bool * @return bool
*/ */
public function put($source, $target) { public function put($source, $target) {
return (new Command\Put($this->connection))->run(array('path1' => $source, 'path2' => $target)); $path1 = $this->escapeLocalPath($source); //first path is local, needs different escaping
$path2 = $this->escapePath($target);
$output = $this->execute('put ' . $path1 . ' ' . $path2);
return $this->parseOutput($output);
} }
/** /**
@ -123,7 +157,10 @@ class Share {
* @return bool * @return bool
*/ */
public function get($source, $target) { public function get($source, $target) {
return (new Command\Get($this->connection))->run(array('path1' => $source, 'path2' => $target)); $path1 = $this->escapePath($source);
$path2 = $this->escapeLocalPath($target); //second path is local, needs different escaping
$output = $this->execute('get ' . $path1 . ' ' . $path2);
return $this->parseOutput($output);
} }
/** /**
@ -132,4 +169,81 @@ class Share {
public function getServer() { public function getServer() {
return $this->server; return $this->server;
} }
/**
* @param string $command
* @return array
*/
protected function execute($command) {
$this->connect();
$this->connection->write($command . PHP_EOL);
$output = $this->connection->read();
return $output;
}
/**
* @param $lines
*
* @throws \SMB\NotFoundException
* @throws \SMB\AlreadyExistsException
* @throws \SMB\AccessDeniedException
* @throws \SMB\NotEmptyException
* @throws \Exception
* @return bool
*/
protected function parseOutput($lines) {
if (count($lines) === 0) {
return true;
} else {
if (strpos($lines[0], 'does not exist')) {
throw new NotFoundException();
}
list($error,) = explode(' ', $lines[0]);
switch ($error) {
case 'NT_STATUS_OBJECT_PATH_NOT_FOUND':
case 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
case 'NT_STATUS_NO_SUCH_FILE':
throw new NotFoundException();
case 'NT_STATUS_OBJECT_NAME_COLLISION':
throw new AlreadyExistsException();
case 'NT_STATUS_ACCESS_DENIED':
throw new AccessDeniedException();
case 'NT_STATUS_DIRECTORY_NOT_EMPTY':
throw new NotEmptyException();
default:
throw new \Exception();
}
}
}
/**
* @param string $string
* @return string
*/
protected function escape($string) {
return escapeshellarg($string);
}
/**
* @param string $path
* @return string
*/
protected function escapePath($path) {
$path = str_replace('/', '\\', $path);
$path = str_replace('"', '^"', $path);
return '"' . $path . '"';
}
/**
* @param string $path
* @return string
*/
protected function escapeLocalPath($path) {
$path = str_replace('"', '\"', $path);
return '"' . $path . '"';
}
public function __destruct() {
unset($this->connection);
}
} }

View file

@ -18,18 +18,20 @@ class Test extends PHPUnit_Framework_TestCase {
private $config; private $config;
private $needsCleanup = false;
public function setUp() { public function setUp() {
$this->config = json_decode(file_get_contents(__DIR__ . '/config.json')); $this->config = json_decode(file_get_contents(__DIR__ . '/config.json'));
$this->server = new SMB\Server($this->config->host, $this->config->user, $this->config->password); $this->server = new SMB\Server($this->config->host, $this->config->user, $this->config->password);
$this->share = $this->server->getShare($this->config->share); $this->share = $this->server->getShare($this->config->share);
$this->root = '/' . uniqid(); $this->root = '/' . uniqid();
$this->share->mkdir($this->root);
} }
public function tearDown() { public function tearDown() {
if ($this->share) { if ($this->share and $this->needsCleanup) {
$this->share->rmdir($this->root); $this->share->rmdir($this->root);
} }
unset($this->share);
} }
public function testListShares() { public function testListShares() {
@ -43,6 +45,8 @@ class Test extends PHPUnit_Framework_TestCase {
} }
public function testDirectory() { public function testDirectory() {
$this->share->mkdir($this->root);
$this->needsCleanup = true;
$this->assertEquals(array(), $this->share->dir($this->root)); $this->assertEquals(array(), $this->share->dir($this->root));
$this->share->mkdir($this->root . '/foo'); $this->share->mkdir($this->root . '/foo');
@ -61,6 +65,8 @@ class Test extends PHPUnit_Framework_TestCase {
} }
public function testFile() { public function testFile() {
$this->share->mkdir($this->root);
$this->needsCleanup = true;
$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'; $text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua';
$size = strlen($text); $size = strlen($text);
$tmpFile1 = tempnam('/tmp', 'smb_test_'); $tmpFile1 = tempnam('/tmp', 'smb_test_');
@ -91,6 +97,8 @@ class Test extends PHPUnit_Framework_TestCase {
} }
public function testEscaping() { public function testEscaping() {
$this->share->mkdir($this->root);
$this->needsCleanup = true;
// / ? < > \ : * | ” are illegal characters in path on windows, no use trying to get them working // / ? < > \ : * | ” are illegal characters in path on windows, no use trying to get them working
$names = array('simple', 'with spaces', "single'quote'", '$as#d', '€', '££Ö€ßœĚęĘĞĜΣΥΦΩΫΫ'); $names = array('simple', 'with spaces', "single'quote'", '$as#d', '€', '££Ö€ßœĚęĘĞĜΣΥΦΩΫΫ');