2012-05-24 20:22:33 +04:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2012-09-07 20:30:48 +04:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
2013-08-18 21:14:14 +04:00
|
|
|
require_once __DIR__ . '/../3rdparty/smb4php/smb.php';
|
2012-05-24 20:22:33 +04:00
|
|
|
|
2012-09-07 20:30:48 +04:00
|
|
|
class SMB extends \OC\Files\Storage\StreamWrapper{
|
2012-05-24 20:22:33 +04:00
|
|
|
private $password;
|
|
|
|
private $user;
|
|
|
|
private $host;
|
|
|
|
private $root;
|
2012-06-08 03:29:46 +04:00
|
|
|
private $share;
|
2012-05-24 20:22:33 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function __construct($params) {
|
2012-12-24 22:45:52 +04:00
|
|
|
if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) {
|
|
|
|
$this->host=$params['host'];
|
|
|
|
$this->user=$params['user'];
|
|
|
|
$this->password=$params['password'];
|
|
|
|
$this->share=$params['share'];
|
|
|
|
$this->root=isset($params['root'])?$params['root']:'/';
|
|
|
|
if ( ! $this->root || $this->root[0]!='/') {
|
|
|
|
$this->root='/'.$this->root;
|
|
|
|
}
|
|
|
|
if (substr($this->root, -1, 1)!='/') {
|
|
|
|
$this->root.='/';
|
|
|
|
}
|
|
|
|
if ( ! $this->share || $this->share[0]!='/') {
|
|
|
|
$this->share='/'.$this->share;
|
|
|
|
}
|
|
|
|
if (substr($this->share, -1, 1)=='/') {
|
2013-02-20 04:12:14 +04:00
|
|
|
$this->share = substr($this->share, 0, -1);
|
2012-12-24 22:45:52 +04:00
|
|
|
}
|
|
|
|
} else {
|
2014-03-19 00:15:11 +04:00
|
|
|
throw new \Exception('Invalid configuration');
|
2012-06-08 03:29:46 +04:00
|
|
|
}
|
2012-11-16 15:14:29 +04:00
|
|
|
}
|
2012-05-24 20:22:33 +04:00
|
|
|
|
2012-10-12 01:06:57 +04:00
|
|
|
public function getId(){
|
|
|
|
return 'smb::' . $this->user . '@' . $this->host . '/' . $this->share . '/' . $this->root;
|
2012-05-24 20:22:33 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function constructUrl($path) {
|
2012-11-30 19:27:11 +04:00
|
|
|
if (substr($path, -1)=='/') {
|
2013-11-14 19:52:00 +04:00
|
|
|
$path = substr($path, 0, -1);
|
2012-05-24 20:22:33 +04:00
|
|
|
}
|
2013-11-14 19:52:00 +04:00
|
|
|
if (substr($path, 0, 1)=='/') {
|
|
|
|
$path = substr($path, 1);
|
|
|
|
}
|
|
|
|
// remove trailing dots which some versions of samba don't seem to like
|
|
|
|
$path = rtrim($path, '.');
|
2013-02-12 00:25:29 +04:00
|
|
|
$path = urlencode($path);
|
2013-02-12 18:56:31 +04:00
|
|
|
$user = urlencode($this->user);
|
|
|
|
$pass = urlencode($this->password);
|
|
|
|
return 'smb://'.$user.':'.$pass.'@'.$this->host.$this->share.$this->root.$path;
|
2012-06-17 04:54:23 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function stat($path) {
|
2012-11-30 19:27:11 +04:00
|
|
|
if ( ! $path and $this->root=='/') {//mtime doesn't work for shares
|
2012-06-17 04:54:23 +04:00
|
|
|
$stat=stat($this->constructUrl($path));
|
2013-06-01 02:06:23 +04:00
|
|
|
if (empty($stat)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$mtime=$this->shareMTime();
|
2012-06-17 04:54:23 +04:00
|
|
|
$stat['mtime']=$mtime;
|
|
|
|
return $stat;
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
2013-06-01 02:06:23 +04:00
|
|
|
$stat = stat($this->constructUrl($path));
|
|
|
|
|
|
|
|
// smb4php can return an empty array if the connection could not be established
|
|
|
|
if (empty($stat)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $stat;
|
2012-06-17 04:54:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 21:39:39 +04:00
|
|
|
/**
|
2013-11-29 15:58:57 +04:00
|
|
|
* Unlinks file or directory
|
2014-05-15 16:19:32 +04:00
|
|
|
* @param string $path
|
2013-11-14 21:39:39 +04:00
|
|
|
*/
|
|
|
|
public function unlink($path) {
|
2013-11-29 15:58:57 +04:00
|
|
|
if ($this->is_dir($path)) {
|
|
|
|
$this->rmdir($path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$url = $this->constructUrl($path);
|
|
|
|
unlink($url);
|
|
|
|
clearstatcache(false, $url);
|
|
|
|
}
|
2013-11-14 21:39:39 +04:00
|
|
|
// smb4php still returns false even on success so
|
|
|
|
// check here whether file was really deleted
|
|
|
|
return !file_exists($path);
|
|
|
|
}
|
|
|
|
|
2012-06-17 04:54:23 +04:00
|
|
|
/**
|
|
|
|
* check if a file or folder has been updated since $time
|
2012-10-12 01:17:59 +04:00
|
|
|
* @param string $path
|
2012-06-17 04:54:23 +04:00
|
|
|
* @param int $time
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function hasUpdated($path,$time) {
|
|
|
|
if(!$path and $this->root=='/') {
|
2012-11-30 19:27:11 +04:00
|
|
|
// mtime doesn't work for shares, but giving the nature of the backend,
|
|
|
|
// doing a full update is still just fast enough
|
2012-06-17 04:54:23 +04:00
|
|
|
return true;
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
2012-06-17 04:54:23 +04:00
|
|
|
$actualTime=$this->filemtime($path);
|
|
|
|
return $actualTime>$time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the best guess for the modification time of the share
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
private function shareMTime() {
|
2012-06-17 04:54:23 +04:00
|
|
|
$dh=$this->opendir('');
|
|
|
|
$lastCtime=0;
|
2013-09-04 15:06:04 +04:00
|
|
|
if(is_resource($dh)) {
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
if ($file!='.' and $file!='..') {
|
|
|
|
$ctime=$this->filemtime($file);
|
|
|
|
if ($ctime>$lastCtime) {
|
|
|
|
$lastCtime=$ctime;
|
|
|
|
}
|
2012-06-17 04:54:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $lastCtime;
|
2012-05-24 20:22:33 +04:00
|
|
|
}
|
2013-08-02 17:44:56 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* check if smbclient is installed
|
|
|
|
*/
|
|
|
|
public static function checkDependencies() {
|
2014-11-25 13:06:26 +03:00
|
|
|
$smbClientExists = (bool) \OC_Helper::findBinaryPath('smbclient');
|
|
|
|
return $smbClientExists ? true : array('smbclient');
|
2013-08-02 17:44:56 +04:00
|
|
|
}
|
|
|
|
|
2012-05-24 20:22:33 +04:00
|
|
|
}
|