2014-03-21 14:22:48 +00: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 11:04:13 +00:00
|
|
|
require_once __DIR__ . '/../3rdparty/smb4php/smb.php';
|
|
|
|
|
2014-03-25 13:54:54 +00:00
|
|
|
class SMB_OC extends \OC\Files\Storage\SMB {
|
2014-03-26 11:04:13 +00:00
|
|
|
private $username_as_share;
|
|
|
|
|
2014-03-21 14:22:48 +00:00
|
|
|
public function __construct($params) {
|
2014-07-16 17:40:22 +00:00
|
|
|
if (isset($params['host']) && \OC::$server->getSession()->exists('smb-credentials')) {
|
2014-03-21 14:22:48 +00:00
|
|
|
$host=$params['host'];
|
2014-03-26 11:04:13 +00:00
|
|
|
$this->username_as_share = ($params['username_as_share'] === 'true');
|
2014-03-21 14:22:48 +00:00
|
|
|
|
2014-07-16 17:40:22 +00:00
|
|
|
$params_auth = \OC::$server->getSession()->get('smb-credentials');
|
|
|
|
$user = \OC::$server->getSession()->get('loginname');
|
2014-03-21 14:22:48 +00:00
|
|
|
$password = $params_auth['password'];
|
|
|
|
|
|
|
|
$root=isset($params['root'])?$params['root']:'/';
|
|
|
|
$share = '';
|
|
|
|
|
2014-03-26 11:04:13 +00:00
|
|
|
if ($this->username_as_share) {
|
2014-03-21 14:22:48 +00:00
|
|
|
$share = '/'.$user;
|
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function login( $params ) {
|
2014-07-16 17:40:22 +00:00
|
|
|
\OC::$server->getSession()->set('smb-credentials', $params);
|
2014-03-21 14:22:48 +00:00
|
|
|
}
|
2014-03-24 15:15:15 +00:00
|
|
|
|
|
|
|
public function isSharable($path) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-26 11:04:13 +00:00
|
|
|
|
|
|
|
public function test($isPersonal = true) {
|
|
|
|
if ($isPersonal) {
|
2014-03-26 15:11:09 +00:00
|
|
|
if ($this->stat('')) {
|
2014-03-26 11:04:13 +00:00
|
|
|
return true;
|
2014-03-26 15:11:09 +00:00
|
|
|
}
|
2014-03-26 11:04:13 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$smb = new \smb();
|
|
|
|
$pu = $smb->parse_url($this->constructUrl(''));
|
|
|
|
|
|
|
|
// Attempt to connect anonymously
|
|
|
|
$pu['user'] = '';
|
|
|
|
$pu['pass'] = '';
|
|
|
|
|
|
|
|
// Share cannot be checked if dynamic
|
|
|
|
if ($this->username_as_share) {
|
2014-03-26 15:11:09 +00:00
|
|
|
if ($smb->look($pu)) {
|
2014-03-26 11:04:13 +00:00
|
|
|
return true;
|
2014-03-26 15:11:09 +00:00
|
|
|
} else {
|
2014-03-26 11:04:13 +00:00
|
|
|
return false;
|
2014-03-26 15:11:09 +00:00
|
|
|
}
|
2014-03-26 11:04:13 +00:00
|
|
|
}
|
2014-03-26 15:11:09 +00:00
|
|
|
if (!$pu['share']) {
|
2014-03-26 11:04:13 +00:00
|
|
|
return false;
|
2014-03-26 15:11:09 +00:00
|
|
|
}
|
2014-03-26 11:04:13 +00:00
|
|
|
|
|
|
|
// The following error messages are expected due to anonymous login
|
|
|
|
$regexp = array(
|
|
|
|
'(NT_STATUS_ACCESS_DENIED)' => 'skip'
|
|
|
|
) + $smb->getRegexp();
|
|
|
|
|
2014-03-26 15:11:09 +00:00
|
|
|
if ($smb->client("-d 0 " . escapeshellarg('//' . $pu['host'] . '/' . $pu['share']) . " -c exit", $pu, $regexp)) {
|
2014-03-26 11:04:13 +00:00
|
|
|
return true;
|
2014-03-26 15:11:09 +00:00
|
|
|
} else {
|
2014-03-26 11:04:13 +00:00
|
|
|
return false;
|
2014-03-26 15:11:09 +00:00
|
|
|
}
|
2014-03-26 11:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-21 14:22:48 +00:00
|
|
|
}
|