2012-05-24 16:22:33 +00: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 16:30:48 +00:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
2012-09-05 21:28:59 +00:00
|
|
|
require_once 'smb4php/smb.php';
|
2012-05-24 16:22:33 +00:00
|
|
|
|
2012-09-07 16:30:48 +00:00
|
|
|
class SMB extends \OC\Files\Storage\StreamWrapper{
|
2012-05-24 16:22:33 +00:00
|
|
|
private $password;
|
|
|
|
private $user;
|
|
|
|
private $host;
|
|
|
|
private $root;
|
2012-06-07 23:29:46 +00:00
|
|
|
private $share;
|
2012-05-24 16:22:33 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function __construct($params) {
|
2012-12-24 18:45:52 +00: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 00:12:14 +00:00
|
|
|
$this->share = substr($this->share, 0, -1);
|
2012-12-24 18:45:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-02-12 01:27:05 +00:00
|
|
|
throw new \Exception();
|
2012-06-07 23:29:46 +00:00
|
|
|
}
|
2012-11-16 11:14:29 +00:00
|
|
|
}
|
2012-05-24 16:22:33 +00:00
|
|
|
|
2012-10-11 21:06:57 +00:00
|
|
|
public function getId(){
|
|
|
|
return 'smb::' . $this->user . '@' . $this->host . '/' . $this->share . '/' . $this->root;
|
2012-05-24 16:22:33 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function constructUrl($path) {
|
2012-11-30 15:27:11 +00:00
|
|
|
if (substr($path, -1)=='/') {
|
2012-11-02 18:53:02 +00:00
|
|
|
$path=substr($path, 0, -1);
|
2012-05-24 16:22:33 +00:00
|
|
|
}
|
2013-02-11 20:25:29 +00:00
|
|
|
$path = urlencode($path);
|
2013-02-12 14:56:31 +00:00
|
|
|
$user = urlencode($this->user);
|
|
|
|
$pass = urlencode($this->password);
|
|
|
|
return 'smb://'.$user.':'.$pass.'@'.$this->host.$this->share.$this->root.$path;
|
2012-06-17 00:54:23 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function stat($path) {
|
2012-11-30 15:27:11 +00:00
|
|
|
if ( ! $path and $this->root=='/') {//mtime doesn't work for shares
|
2012-06-17 00:54:23 +00:00
|
|
|
$mtime=$this->shareMTime();
|
|
|
|
$stat=stat($this->constructUrl($path));
|
|
|
|
$stat['mtime']=$mtime;
|
|
|
|
return $stat;
|
2012-11-30 15:27:11 +00:00
|
|
|
} else {
|
2012-06-17 00:54:23 +00:00
|
|
|
return stat($this->constructUrl($path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a file or folder has been updated since $time
|
2012-10-11 21:17:59 +00:00
|
|
|
* @param string $path
|
2012-06-17 00:54:23 +00:00
|
|
|
* @param int $time
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
public function hasUpdated($path,$time) {
|
2012-10-21 20:04:45 +00:00
|
|
|
$this->init();
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!$path and $this->root=='/') {
|
2012-11-30 15:27:11 +00: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 00:54:23 +00:00
|
|
|
return true;
|
2012-11-30 15:27:11 +00:00
|
|
|
} else {
|
2012-06-17 00:54:23 +00:00
|
|
|
$actualTime=$this->filemtime($path);
|
|
|
|
return $actualTime>$time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the best guess for the modification time of the share
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
private function shareMTime() {
|
2012-06-17 00:54:23 +00:00
|
|
|
$dh=$this->opendir('');
|
|
|
|
$lastCtime=0;
|
2012-09-07 13:22:01 +00:00
|
|
|
while($file=readdir($dh)) {
|
2012-11-30 15:27:11 +00:00
|
|
|
if ($file!='.' and $file!='..') {
|
2012-06-17 00:54:23 +00:00
|
|
|
$ctime=$this->filemtime($file);
|
2012-11-30 15:27:11 +00:00
|
|
|
if ($ctime>$lastCtime) {
|
2012-06-17 00:54:23 +00:00
|
|
|
$lastCtime=$ctime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $lastCtime;
|
2012-05-24 16:22:33 +00:00
|
|
|
}
|
|
|
|
}
|