diff options
author | Robin Appelman <icewind@owncloud.com> | 2014-08-26 18:46:07 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-02-16 13:52:11 +0100 |
commit | d2255a1d304aa5e537e8aa097add2a51aa0e5d89 (patch) | |
tree | 79801d54149e6c8037e717a1d5d6ac43327eb6db /apps/files_external/3rdparty/icewind/smb/src/Server.php | |
parent | 78febb2ee594bac5d483f7c8534ed5eb33c2c528 (diff) | |
download | nextcloud-server-d2255a1d304aa5e537e8aa097add2a51aa0e5d89.tar.gz nextcloud-server-d2255a1d304aa5e537e8aa097add2a51aa0e5d89.zip |
New SMB storage backend
Diffstat (limited to 'apps/files_external/3rdparty/icewind/smb/src/Server.php')
-rw-r--r-- | apps/files_external/3rdparty/icewind/smb/src/Server.php | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/apps/files_external/3rdparty/icewind/smb/src/Server.php b/apps/files_external/3rdparty/icewind/smb/src/Server.php new file mode 100644 index 00000000000..f7227d4baef --- /dev/null +++ b/apps/files_external/3rdparty/icewind/smb/src/Server.php @@ -0,0 +1,141 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Licensed under the MIT license: + * http://opensource.org/licenses/MIT + */ + +namespace Icewind\SMB; + +use Icewind\SMB\Exception\AuthenticationException; +use Icewind\SMB\Exception\InvalidHostException; + +class Server { + const CLIENT = 'smbclient'; + const LOCALE = 'en_US.UTF-8'; + + /** + * @var string $host + */ + protected $host; + + /** + * @var string $user + */ + protected $user; + + /** + * @var string $password + */ + protected $password; + + /** + * Check if the smbclient php extension is available + * + * @return bool + */ + public static function NativeAvailable() { + return function_exists('smbclient_state_new'); + } + + /** + * @param string $host + * @param string $user + * @param string $password + */ + public function __construct($host, $user, $password) { + $this->host = $host; + $this->user = $user; + $this->password = $password; + } + + /** + * @return string + */ + public function getAuthString() { + return $this->user . '%' . $this->password; + } + + /** + * @return string + */ + public function getUser() { + return $this->user; + } + + /** + * @return string + */ + public function getPassword() { + return $this->password; + } + + /** + * return string + */ + public function getHost() { + return $this->host; + } + + /** + * @return \Icewind\SMB\IShare[] + * + * @throws \Icewind\SMB\Exception\AuthenticationException + * @throws \Icewind\SMB\Exception\InvalidHostException + */ + public function listShares() { + $command = Server::CLIENT . ' --authentication-file=/proc/self/fd/3' . + ' -gL ' . escapeshellarg($this->getHost()); + $connection = new RawConnection($command); + $connection->writeAuthentication($this->getUser(), $this->getPassword()); + $output = $connection->readAll(); + + $line = $output[0]; + + $line = rtrim($line, ')'); + if (substr($line, -23) === ErrorCodes::LogonFailure) { + throw new AuthenticationException(); + } + if (substr($line, -26) === ErrorCodes::BadHostName) { + throw new InvalidHostException(); + } + if (substr($line, -22) === ErrorCodes::Unsuccessful) { + throw new InvalidHostException(); + } + if (substr($line, -28) === ErrorCodes::ConnectionRefused) { + throw new InvalidHostException(); + } + + $shareNames = array(); + foreach ($output as $line) { + if (strpos($line, '|')) { + list($type, $name, $description) = explode('|', $line); + if (strtolower($type) === 'disk') { + $shareNames[$name] = $description; + } + } + } + + $shares = array(); + foreach ($shareNames as $name => $description) { + $shares[] = $this->getShare($name); + } + return $shares; + } + + /** + * @param string $name + * @return \Icewind\SMB\IShare + */ + public function getShare($name) { + return new Share($this, $name); + } + + /** + * @return string + */ + public function getTimeZone() { + $command = 'net time zone -S ' . escapeshellarg($this->getHost()); + return exec($command); + } +} |