smb.php 2.38 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
12
13
namespace OC\Files\Storage;

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) {
Robin Appelman's avatar
Robin Appelman committed
21
22
23
		$this->host=$params['host'];
		$this->user=$params['user'];
		$this->password=$params['password'];
24
		$this->share=$params['share'];
Robin Appelman's avatar
Robin Appelman committed
25
		$this->root=isset($params['root'])?$params['root']:'/';
Thomas Mueller's avatar
Thomas Mueller committed
26
		if(!$this->root || $this->root[0]!='/') {
27
28
			$this->root='/'.$this->root;
		}
Thomas Mueller's avatar
Thomas Mueller committed
29
		if(substr($this->root,-1,1)!='/') {
30
31
			$this->root.='/';
		}
Thomas Mueller's avatar
Thomas Mueller committed
32
		if(!$this->share || $this->share[0]!='/') {
33
34
			$this->share='/'.$this->share;
		}
Thomas Mueller's avatar
Thomas Mueller committed
35
		if(substr($this->share,-1,1)=='/') {
36
37
			$this->share=substr($this->share,0,-1);
		}
Robin Appelman's avatar
Robin Appelman committed
38
39

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

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

Thomas Mueller's avatar
Thomas Mueller committed
52
53
	public function stat($path) {
		if(!$path and $this->root=='/') {//mtime doesn't work for shares
54
55
56
57
58
59
60
61
62
			$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
63
	public function filetype($path) {
64
		return (bool)@$this->opendir($path);//using opendir causes the same amount of requests and caches the content of the folder in one go
65
66
	}

67
68
69
70
71
	/**
	 * check if a file or folder has been updated since $time
	 * @param int $time
	 * @return bool
	 */
Thomas Mueller's avatar
Thomas Mueller committed
72
73
	public function hasUpdated($path,$time) {
		if(!$path and $this->root=='/') {
74
75
76
77
78
79
80
81
82
83
84
			//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
85
	private function shareMTime() {
86
87
		$dh=$this->opendir('');
		$lastCtime=0;
Thomas Mueller's avatar
Thomas Mueller committed
88
89
		while($file=readdir($dh)) {
			if($file!='.' and $file!='..') {
90
				$ctime=$this->filemtime($file);
Thomas Mueller's avatar
Thomas Mueller committed
91
				if($ctime>$lastCtime) {
92
93
94
95
96
					$lastCtime=$ctime;
				}
			}
		}
		return $lastCtime;
Robin Appelman's avatar
Robin Appelman committed
97
98
	}
}