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;
|
|
|
|
|
2012-10-12 01:17:59 +04:00
|
|
|
require_once 'smb4php/smb.php';
|
|
|
|
|
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-05-24 20:22:33 +04:00
|
|
|
$this->host=$params['host'];
|
|
|
|
$this->user=$params['user'];
|
|
|
|
$this->password=$params['password'];
|
2012-06-08 03:29:46 +04:00
|
|
|
$this->share=$params['share'];
|
2012-05-24 20:22:33 +04:00
|
|
|
$this->root=isset($params['root'])?$params['root']:'/';
|
2012-11-30 19:27:11 +04:00
|
|
|
if ( ! $this->root || $this->root[0]!='/') {
|
2012-06-08 23:23:25 +04:00
|
|
|
$this->root='/'.$this->root;
|
|
|
|
}
|
2012-11-30 19:27:11 +04:00
|
|
|
if (substr($this->root, -1, 1)!='/') {
|
2012-06-08 03:29:46 +04:00
|
|
|
$this->root.='/';
|
|
|
|
}
|
2012-11-30 19:27:11 +04:00
|
|
|
if ( ! $this->share || $this->share[0]!='/') {
|
2012-06-08 03:29:46 +04:00
|
|
|
$this->share='/'.$this->share;
|
|
|
|
}
|
2012-11-02 22:53:02 +04:00
|
|
|
if(substr($this->share, -1, 1)=='/') {
|
2012-11-07 20:18:56 +04:00
|
|
|
$this->share = substr($this->share,0,-1);
|
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-09-07 17:22:01 +04:00
|
|
|
public function constructUrl($path) {
|
2012-11-30 19:27:11 +04:00
|
|
|
if (substr($path, -1)=='/') {
|
2012-11-02 22:53:02 +04:00
|
|
|
$path=substr($path, 0, -1);
|
2012-05-24 20:22:33 +04:00
|
|
|
}
|
2012-06-08 03:29:46 +04:00
|
|
|
return 'smb://'.$this->user.':'.$this->password.'@'.$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
|
|
|
$mtime=$this->shareMTime();
|
|
|
|
$stat=stat($this->constructUrl($path));
|
|
|
|
$stat['mtime']=$mtime;
|
|
|
|
return $stat;
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
2012-06-17 04:54:23 +04:00
|
|
|
return stat($this->constructUrl($path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function filetype($path) {
|
2012-11-30 19:27:11 +04:00
|
|
|
// using opendir causes the same amount of requests and caches the content of the folder in one go
|
|
|
|
return (bool)@$this->opendir($path) ? 'dir' : 'file';
|
2012-06-17 04:59:38 +04:00
|
|
|
}
|
|
|
|
|
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) {
|
2012-10-22 00:04:45 +04:00
|
|
|
$this->init();
|
2012-09-07 17:22:01 +04:00
|
|
|
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;
|
2012-09-07 17:22:01 +04:00
|
|
|
while($file=readdir($dh)) {
|
2012-11-30 19:27:11 +04:00
|
|
|
if ($file!='.' and $file!='..') {
|
2012-06-17 04:54:23 +04:00
|
|
|
$ctime=$this->filemtime($file);
|
2012-11-30 19:27:11 +04:00
|
|
|
if ($ctime>$lastCtime) {
|
2012-06-17 04:54:23 +04:00
|
|
|
$lastCtime=$ctime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $lastCtime;
|
2012-05-24 20:22:33 +04:00
|
|
|
}
|
|
|
|
}
|