smb.php 3.6 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.
 */

9
10
namespace OC\Files\Storage;

Thomas Müller's avatar
Thomas Müller committed
11
require_once __DIR__ . '/../3rdparty/smb4php/smb.php';
Robin Appelman's avatar
Robin Appelman committed
12

13
class SMB extends \OC\Files\Storage\StreamWrapper{
Robin Appelman's avatar
Robin Appelman committed
14
15
16
17
	private $password;
	private $user;
	private $host;
	private $root;
18
	private $share;
Robin Appelman's avatar
Robin Appelman committed
19

Thomas Mueller's avatar
Thomas Mueller committed
20
	public function __construct($params) {
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
		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)=='/') {
37
				$this->share = substr($this->share, 0, -1);
38
39
			}
		} else {
40
			throw new \Exception('Invalid configuration');
41
		}
Robin Appelman's avatar
Robin Appelman committed
42
	}
Robin Appelman's avatar
Robin Appelman committed
43

44
45
	public function getId(){
		return 'smb::' . $this->user . '@' . $this->host . '/' . $this->share . '/' . $this->root;
Robin Appelman's avatar
Robin Appelman committed
46
47
	}

Thomas Mueller's avatar
Thomas Mueller committed
48
	public function constructUrl($path) {
49
		if (substr($path, -1)=='/') {
50
			$path = substr($path, 0, -1);
Robin Appelman's avatar
Robin Appelman committed
51
		}
52
53
54
55
56
		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, '.');
57
		$path = urlencode($path);
58
59
60
		$user = urlencode($this->user);
		$pass = urlencode($this->password);
		return 'smb://'.$user.':'.$pass.'@'.$this->host.$this->share.$this->root.$path;
61
62
	}

Thomas Mueller's avatar
Thomas Mueller committed
63
	public function stat($path) {
64
		if ( ! $path and $this->root=='/') {//mtime doesn't work for shares
65
			$stat=stat($this->constructUrl($path));
66
67
68
69
			if (empty($stat)) {
				return false;
			}
			$mtime=$this->shareMTime();
70
71
			$stat['mtime']=$mtime;
			return $stat;
72
		} else {
73
74
75
76
77
78
79
80
			$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;
81
82
83
		}
	}

84
	/**
85
	 * Unlinks file or directory
86
	 * @param string $path
87
88
	 */
	public function unlink($path) {
89
90
91
92
93
94
95
96
		if ($this->is_dir($path)) {
			$this->rmdir($path);
		}
		else {
			$url = $this->constructUrl($path);
			unlink($url);
			clearstatcache(false, $url);
		}
97
98
99
100
101
		// smb4php still returns false even on success so
		// check here whether file was really deleted
		return !file_exists($path);
	}

102
103
	/**
	 * check if a file or folder has been updated since $time
104
	 * @param string $path
105
106
107
	 * @param int $time
	 * @return bool
	 */
Thomas Mueller's avatar
Thomas Mueller committed
108
109
	public function hasUpdated($path,$time) {
		if(!$path and $this->root=='/') {
110
111
			// mtime doesn't work for shares, but giving the nature of the backend,
			// doing a full update is still just fast enough
112
			return true;
113
		} else {
114
115
116
117
118
119
120
121
			$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
122
	private function shareMTime() {
123
124
		$dh=$this->opendir('');
		$lastCtime=0;
125
126
127
128
129
130
131
		if(is_resource($dh)) {
			while (($file = readdir($dh)) !== false) {
				if ($file!='.' and $file!='..') {
					$ctime=$this->filemtime($file);
					if ($ctime>$lastCtime) {
						$lastCtime=$ctime;
					}
132
133
134
135
				}
			}
		}
		return $lastCtime;
Robin Appelman's avatar
Robin Appelman committed
136
	}
137
138
139
140
141

	/**
	 * check if smbclient is installed
	 */
	public static function checkDependencies() {
142
143
		$smbClientExists = (bool) \OC_Helper::findBinaryPath('smbclient');
		return $smbClientExists ? true : array('smbclient');
144
145
	}

Robin Appelman's avatar
Robin Appelman committed
146
}