smb.php 2.75 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;

11
require_once '../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();
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)=='/') {
Felix Moeller's avatar
Felix Moeller committed
50
			$path=substr($path, 0, -1);
Robin Appelman's avatar
Robin Appelman committed
51
		}
52
		$path = urlencode($path);
53
54
55
		$user = urlencode($this->user);
		$pass = urlencode($this->password);
		return 'smb://'.$user.':'.$pass.'@'.$this->host.$this->share.$this->root.$path;
56
57
	}

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

	/**
	 * check if a file or folder has been updated since $time
81
	 * @param string $path
82
83
84
	 * @param int $time
	 * @return bool
	 */
Thomas Mueller's avatar
Thomas Mueller committed
85
86
	public function hasUpdated($path,$time) {
		if(!$path and $this->root=='/') {
87
88
			// mtime doesn't work for shares, but giving the nature of the backend,
			// doing a full update is still just fast enough
89
			return true;
90
		} else {
91
92
93
94
95
96
97
98
			$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
99
	private function shareMTime() {
100
101
		$dh=$this->opendir('');
		$lastCtime=0;
Thomas Mueller's avatar
Thomas Mueller committed
102
		while($file=readdir($dh)) {
103
			if ($file!='.' and $file!='..') {
104
				$ctime=$this->filemtime($file);
105
				if ($ctime>$lastCtime) {
106
107
108
109
110
					$lastCtime=$ctime;
				}
			}
		}
		return $lastCtime;
Robin Appelman's avatar
Robin Appelman committed
111
112
	}
}