2014-03-21 18:22:48 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
2014-03-26 15:04:13 +04:00
|
|
|
|
2014-08-26 20:46:07 +04:00
|
|
|
use Icewind\SMB\Exception\AccessDeniedException;
|
|
|
|
use Icewind\SMB\Exception\Exception;
|
|
|
|
use Icewind\SMB\Server;
|
|
|
|
|
|
|
|
class SMB_OC extends SMB {
|
2014-03-26 15:04:13 +04:00
|
|
|
private $username_as_share;
|
|
|
|
|
2014-10-23 22:17:08 +04:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2014-03-21 18:22:48 +04:00
|
|
|
public function __construct($params) {
|
2014-07-16 21:40:22 +04:00
|
|
|
if (isset($params['host']) && \OC::$server->getSession()->exists('smb-credentials')) {
|
2014-08-26 20:46:07 +04:00
|
|
|
$host = $params['host'];
|
2014-03-26 15:04:13 +04:00
|
|
|
$this->username_as_share = ($params['username_as_share'] === 'true');
|
2014-03-21 18:22:48 +04:00
|
|
|
|
2014-10-23 22:17:08 +04:00
|
|
|
$params_auth = json_decode(\OC::$server->getCrypto()->decrypt(\OC::$server->getSession()->get('smb-credentials')), true);
|
2014-07-16 21:40:22 +04:00
|
|
|
$user = \OC::$server->getSession()->get('loginname');
|
2014-03-21 18:22:48 +04:00
|
|
|
$password = $params_auth['password'];
|
|
|
|
|
2014-08-26 20:46:07 +04:00
|
|
|
$root = isset($params['root']) ? $params['root'] : '/';
|
2014-03-21 18:22:48 +04:00
|
|
|
$share = '';
|
|
|
|
|
2014-03-26 15:04:13 +04:00
|
|
|
if ($this->username_as_share) {
|
2014-08-26 20:46:07 +04:00
|
|
|
$share = '/' . $user;
|
2014-03-21 18:22:48 +04:00
|
|
|
} elseif (isset($params['share'])) {
|
|
|
|
$share = $params['share'];
|
|
|
|
} else {
|
|
|
|
throw new \Exception();
|
|
|
|
}
|
|
|
|
parent::__construct(array(
|
|
|
|
"user" => $user,
|
|
|
|
"password" => $password,
|
|
|
|
"host" => $host,
|
|
|
|
"share" => $share,
|
|
|
|
"root" => $root
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
throw new \Exception();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 22:17:08 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Intercepts the user credentials on login and stores them
|
|
|
|
* encrypted inside the session if SMB_OC storage is enabled.
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public static function login($params) {
|
|
|
|
$mountpoints = \OC_Mount_Config::getAbsoluteMountPoints($params['uid']);
|
|
|
|
$mountpointClasses = array();
|
|
|
|
foreach($mountpoints as $mountpoint) {
|
|
|
|
$mountpointClasses[$mountpoint['class']] = true;
|
|
|
|
}
|
|
|
|
if(isset($mountpointClasses['\OC\Files\Storage\SMB_OC'])) {
|
|
|
|
\OC::$server->getSession()->set('smb-credentials', \OC::$server->getCrypto()->encrypt(json_encode($params)));
|
|
|
|
}
|
2014-03-21 18:22:48 +04:00
|
|
|
}
|
2014-03-24 19:15:15 +04:00
|
|
|
|
2014-10-23 22:17:08 +04:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-03-24 19:15:15 +04:00
|
|
|
public function isSharable($path) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-26 15:04:13 +04:00
|
|
|
|
2014-10-23 22:17:08 +04:00
|
|
|
/**
|
|
|
|
* @param bool $isPersonal
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-03-26 15:04:13 +04:00
|
|
|
public function test($isPersonal = true) {
|
|
|
|
if ($isPersonal) {
|
2014-03-26 19:11:09 +04:00
|
|
|
if ($this->stat('')) {
|
2014-03-26 15:04:13 +04:00
|
|
|
return true;
|
2014-03-26 19:11:09 +04:00
|
|
|
}
|
2014-03-26 15:04:13 +04:00
|
|
|
return false;
|
|
|
|
} else {
|
2014-08-26 20:46:07 +04:00
|
|
|
$server = new Server($this->server->getHost(), '', '');
|
2014-03-26 15:04:13 +04:00
|
|
|
|
2014-08-26 20:46:07 +04:00
|
|
|
try {
|
|
|
|
$server->listShares();
|
2014-03-26 15:04:13 +04:00
|
|
|
return true;
|
2014-08-26 20:46:07 +04:00
|
|
|
} catch (AccessDeniedException $e) {
|
|
|
|
// expected due to anonymous login
|
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
2014-03-26 15:04:13 +04:00
|
|
|
return false;
|
2014-03-26 19:11:09 +04:00
|
|
|
}
|
2014-03-26 15:04:13 +04:00
|
|
|
}
|
|
|
|
}
|
2014-03-21 18:22:48 +04:00
|
|
|
}
|