Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lib_remotestorage.php « inc - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f8835c1641a4cafe50476bbc0a02414478c968a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php

/**
* ownCloud
*
* @author Frank Karlitschek 
* @copyright 2010 Frank Karlitschek karlitschek@kde.org 
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either 
* version 3 of the License, or any later version.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*  
* You should have received a copy of the GNU Lesser General Public 
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
* 
*/


class OC_FILESTORAGE_REMOTE extends OC_FILESTORAGE{
	private $url;
	private $username;
	private $password;
	private $remote=false;
	private $statCache;
	private $statCacheDir=false;
	private $changed=array();
	
	private function cacheDir($dir){
		if($this->statCacheDir!=$dir or $this->statCacheDir===false){
			$this->statCache=$this->remote->getFiles($dir);
			$keys=array_keys($this->statCache);
			$this->statCacheDir=$dir;
		}
	}
	
	public function __construct($arguments){
		$this->url=$arguments['url'];
		$this->username=$arguments['username'];
		$this->password=$arguments['password'];
	}
	private function connect(){
		if($this->remote===false){
			$this->remote=OC_CONNECT::connect($this->url,$this->username,$this->password);
		}
	}
	public function mkdir($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$return=$this->remote->newFile($parent,$name,'dir');
		if($return){
			$this->notifyObservers($path,OC_FILEACTION_CREATE);
		}
		return $return;
	}
	public function rmdir($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$return=$this->remote->delete($parent,$name);
		if($return){
			$this->notifyObservers($path,OC_FILEACTION_DELETE);
		}
		return $return;
	}
	public function opendir($path){
		$this->connect();
		$this->cacheDir($path);
		$dirs=array_keys($this->statCache);
		$id=uniqid();
		global $FAKEDIRS;
		$FAKEDIRS[$id]=$dirs;
		if($return=opendir("fakedir://$id")){
			$this->notifyObservers($path,OC_FILEACTION_READ);
		}
		return $return;
	}
	public function is_dir($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($path);
		if($path=='' or $path=='/'){
			return true;
		}
		if(!isset($this->statCache[$name])){
			return false;
		}
		return ($this->statCache[$name]['type'=='dir']);
	}
	public function is_file($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return false;
		}
		return ($this->statCache[$name]['type'!='dir']);
	}
	public function stat($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return $false;
		}
		return $this->statCache[$name];
	}
	public function filetype($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return false;
		}
		return $this->statCache[$name]['type'];
	}
	public function filesize($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return $false;
		}
		return $this->statCache[$name]['size'];
	}
	public function is_readable($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return false;
		}
		return $this->statCache[$name]['readable'];
	}
	public function is_writeable($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return false;
		}
		return $this->statCache[$name]['writeable'];
	}
	public function file_exists($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		return isset($this->statCache[$name]);
	}
	public function readfile($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$file=$this->remote->getFile($parent,$name);
		readfile($file);
		unlink($file);
	}
	public function filectime($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return false;
		}
		return $this->statCache[$name]['ctime'];
	}
	public function filemtime($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return false;
		}
		return $this->statCache[$name]['mtime'];
	}
	public function fileatime($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return false;
		}
		return $this->statCache[$name]['atime'];
	}
	public function file_get_contents($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$file=$this->remote->getFile($parent,$name);
		file_get_contents($file);
		unlink($file);
	}
	public function file_put_contents($path,$data){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$file=$this->remote->getFile($parent,$name);
		$file=tempnam(sys_get_temp_dir(),'oc_');
		file_put_contents($file,$data);
		if($return=$this->remote->sendTmpFile($file,$parent,$name)){
			$this->notifyObservers($path,OC_FILEACTION_WRITE);
		}
	}
	public function unlink($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		if($return=$this->remote->delete($paren,$name)){
			$this->notifyObservers($path,OC_FILEACTION_DELETE);
		}
		return $return;
	}
	public function rename($path1,$path2){
		$this->connect();
		$parent1=dirname($path1);
		$name1=substr($path1,strlen($parent1)+1);
		$parent2=dirname($path2);
		$name2=substr($path2,strlen($parent2)+1);
		if($return=$this->remote->move($parent1,$name1,$parent2,$name2)){
			$this->notifyObservers($path1.'->'.$path2,OC_FILEACTION_RENAME);
		}
		return $return;
	}
	public function copy($path1,$path2){
		$this->connect();
		$parent1=dirname($path1);
		$name1=substr($path1,strlen($parent1)+1);
		$parent2=dirname($path2);
		$name2=substr($path2,strlen($parent2)+1);
		if($return=$this->copy->rename($parent1,$name1,$parent2,$name2)){
			$this->notifyObservers($path1.'->'.$path2,OC_FILEACTION_RENAME);
		}
		return $return;
	}
	public function fopen($path,$mode){
		$this->connect();
		$changed=false;
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		$file=$this->remote->getFile($parent,$name);
		if($return=fopen($file,$mode)){
			switch($mode){
				case 'r':
					$this->notifyObservers($path,OC_FILEACTION_READ);
					break;
				case 'r+':
				case 'w+':
				case 'x+':
				case 'a+':
					$this->notifyObservers($path,OC_FILEACTION_READ | OC_FILEACTION_WRITE);
					$this->changed[]=array('dir'=>$parent,'file'=>$name,'tmp'=>$file);
					break;
				case 'w':
				case 'x':
				case 'a':
					$this->notifyObservers($path,OC_FILEACTION_WRITE);
					$this->changed[]=array('dir'=>$parent,'file'=>$name,'tmp'=>$file);
					break;
			}
		}
		return $return;
	}
	
	public function getMimeType($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		if(substr($name,0,1)=='/'){
			$name=substr($name,1);
		}
		$this->cacheDir($parent);
		if(!isset($this->statCache[$name])){
			return false;
		}
		return $this->statCache[$name]['mime'];
	}
	
	public function toTmpFile($path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		if(substr($name,0,1)=='/'){
			$name=substr($name,1);
		}
		$filename=$this->remote->getFile($parent,$name);
		if($filename){
			$this->notifyObservers($path,OC_FILEACTION_READ);
			return $filename;
		}else{
			return false;
		}
	}
	
	public function fromTmpFile($tmpFile,$path){
		$this->connect();
		$parent=dirname($path);
		$name=substr($path,strlen($parent)+1);
		if($this->remote->sendTmpFile($tmpFile,$parent,$name)){
			$this->notifyObservers($path,OC_FILEACTION_CREATE);
			return true;
		}else{
			return false;
		}
	}
	
	public function delTree($dir) {
		$this->connect();
		$parent=dirname($dir);
		$name=substr($dir,strlen($parent)+1);
		$return=$this->remote->delete($parent,$name);
		if($return=rmdir($dir)){
			$this->notifyObservers($dir,OC_FILEACTION_DELETE);
		}
		return $return;
	}
	
	public function find($path){
		return $this->getTree($path);
	}
	
	public function getTree($dir) {
		$this->connect();
		if($return=$this->remote->getTree($dir)){
			$this->notifyObservers($dir,OC_FILEACTION_READ);
		}
		return $return;
	}
	
	public function __destruct(){
		foreach($this->changed as $changed){
			$this->remote->sendTmpFile($changed['tmp'],$changed['dir'],$changed['file']);
		}
	}
}

?>