smb.php 2.39 KB
Newer Older
Robin Appelman's avatar
Robin Appelman committed
1
2
3
4
5
6
7
8
<?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.
 */

Thomas Mueller's avatar
Thomas Mueller committed
9
require_once 'smb4php/smb.php';
Robin Appelman's avatar
Robin Appelman committed
10

11
class OC_FileStorage_SMB extends OC_FileStorage_StreamWrapper{
Robin Appelman's avatar
Robin Appelman committed
12
13
14
15
	private $password;
	private $user;
	private $host;
	private $root;
16
	private $share;
Robin Appelman's avatar
Robin Appelman committed
17

Thomas Mueller's avatar
Thomas Mueller committed
18
	public function __construct($params) {
Robin Appelman's avatar
Robin Appelman committed
19
20
21
		$this->host=$params['host'];
		$this->user=$params['user'];
		$this->password=$params['password'];
22
		$this->share=$params['share'];
Robin Appelman's avatar
Robin Appelman committed
23
		$this->root=isset($params['root'])?$params['root']:'/';
Thomas Mueller's avatar
Thomas Mueller committed
24
		if(!$this->root || $this->root[0]!='/') {
25
26
			$this->root='/'.$this->root;
		}
Felix Moeller's avatar
Felix Moeller committed
27
		if(substr($this->root,-1, 1)!='/') {
28
29
			$this->root.='/';
		}
Thomas Mueller's avatar
Thomas Mueller committed
30
		if(!$this->share || $this->share[0]!='/') {
31
32
			$this->share='/'.$this->share;
		}
Felix Moeller's avatar
Felix Moeller committed
33
34
		if(substr($this->share, -1, 1)=='/') {
			$this->share=substr($this->share, 0, -1);
35
		}
Robin Appelman's avatar
Robin Appelman committed
36
37

		//create the root folder if necesary
Thomas Mueller's avatar
Thomas Mueller committed
38
		if(!$this->is_dir('')) {
39
40
			$this->mkdir('');
		}
Robin Appelman's avatar
Robin Appelman committed
41
42
	}

Thomas Mueller's avatar
Thomas Mueller committed
43
44
	public function constructUrl($path) {
		if(substr($path,-1)=='/') {
Felix Moeller's avatar
Felix Moeller committed
45
			$path=substr($path, 0, -1);
Robin Appelman's avatar
Robin Appelman committed
46
		}
47
		return 'smb://'.$this->user.':'.$this->password.'@'.$this->host.$this->share.$this->root.$path;
48
49
	}

Thomas Mueller's avatar
Thomas Mueller committed
50
51
	public function stat($path) {
		if(!$path and $this->root=='/') {//mtime doesn't work for shares
52
53
54
55
56
57
58
59
60
			$mtime=$this->shareMTime();
			$stat=stat($this->constructUrl($path));
			$stat['mtime']=$mtime;
			return $stat;
		}else{
			return stat($this->constructUrl($path));
		}
	}

Thomas Mueller's avatar
Thomas Mueller committed
61
	public function filetype($path) {
62
		return (bool)@$this->opendir($path) ? 'dir' : 'file';//using opendir causes the same amount of requests and caches the content of the folder in one go
63
64
	}

65
66
67
68
69
	/**
	 * check if a file or folder has been updated since $time
	 * @param int $time
	 * @return bool
	 */
Felix Moeller's avatar
Felix Moeller committed
70
	public function hasUpdated($path, $time) {
Thomas Mueller's avatar
Thomas Mueller committed
71
		if(!$path and $this->root=='/') {
72
73
74
75
76
77
78
79
80
81
82
			//mtime doesn't work for shares, but giving the nature of the backend, doing a full update is still just fast enough
			return true;
		}else{
			$actualTime=$this->filemtime($path);
			return $actualTime>$time;
		}
	}

	/**
	 * get the best guess for the modification time of the share
	 */
Thomas Mueller's avatar
Thomas Mueller committed
83
	private function shareMTime() {
84
85
		$dh=$this->opendir('');
		$lastCtime=0;
Thomas Mueller's avatar
Thomas Mueller committed
86
87
		while($file=readdir($dh)) {
			if($file!='.' and $file!='..') {
88
				$ctime=$this->filemtime($file);
Thomas Mueller's avatar
Thomas Mueller committed
89
				if($ctime>$lastCtime) {
90
91
92
93
94
					$lastCtime=$ctime;
				}
			}
		}
		return $lastCtime;
Robin Appelman's avatar
Robin Appelman committed
95
96
	}
}