This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.remote_fsAccessWrapper.php
297 lines (261 loc) · 8.95 KB
/
class.remote_fsAccessWrapper.php
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
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio 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 Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* AJXP_Plugin to access a remote server that implements Pydio API
* @package AjaXplorer_Plugins
* @subpackage Access
*/
class remote_fsAccessWrapper implements AjxpWrapper
{
// Instance vars $this->
protected $host;
protected $port;
protected $secure;
protected $path;
protected $user;
protected $password;
protected $repositoryId;
protected $fp;
protected $crtMode;
protected $crtParameters;
protected $postFileData;
public static function getRealFSReference($path, $persistent = FALSE)
{
$tmpFile = AJXP_Utils::getAjxpTmpDir()."/".md5(time());
$tmpHandle = fopen($tmpFile, "wb");
self::copyFileInStream($path, $tmpHandle);
fclose($tmpHandle);
if (!$persistent) {
register_shutdown_function(array("AJXP_Utils", "silentUnlink"), $tmpFile);
}
return $tmpFile;
}
public static function isRemote()
{
return true;
}
public static function copyFileInStream($path, $stream)
{
$fake = new remote_fsAccessWrapper();
$parts = $fake->parseUrl($path);
$client = $fake->createHttpClient();
$client->writeContentToStream($stream);
$client->get($fake->path."?get_action=get_content&file=".AJXP_Utils::securePath($parts["path"]));
$client->clearContentDestStream();
}
public function stream_open($url, $mode, $options, &$context)
{
if ($mode == "w" || $mode == "rw") {
$this->crtMode = 'write';
$parts = $this->parseUrl($url);
$this->crtParameters = array(
"get_action"=>"put_content",
"encode" => "base64",
"file" => urldecode(AJXP_Utils::securePath($parts["path"]))
);
$tmpFileBuffer = realpath(AJXP_Utils::getAjxpTmpDir()).md5(time());
$this->postFileData = $tmpFileBuffer;
$this->fp = fopen($tmpFileBuffer, "w");
} else {
$this->crtMode = 'read';
$this->fp = tmpfile();
$this->copyFileInStream($url, $this->fp);
rewind($this->fp);
}
return ($this->fp !== false);
}
public function stream_stat()
{
return fstat($this->fp);
}
public function stream_seek($offset , $whence = SEEK_SET)
{
fseek($this->fp, $offset, SEEK_SET);
}
public function stream_tell()
{
return ftell($this->fp);
}
public function stream_read($count)
{
return fread($this->fp, $count);
}
public function stream_write($data)
{
fwrite($this->fp, $data, strlen($data));
return strlen($data);
}
public function stream_eof()
{
return feof($this->fp);
}
public function stream_close()
{
if (isSet($this->fp) && $this->fp!=-1 && $this->fp!==false) {
fclose($this->fp);
}
}
public function stream_flush()
{
if (isSet($this->fp) && $this->fp!=-1 && $this->fp!==false) {
if ($this->crtMode == 'write') {
rewind($this->fp);
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"Http_fput", array("target"=>$this->path));
$link = $this->createHttpClient();
$this->crtParameters["content"] = base64_encode(implode("", file($this->postFileData)));
$link->post($this->path, $this->crtParameters);
} else {
fflush($this->fp);
}
}
}
public function url_stat($path, $flags)
{
$parts = $this->parseUrl($path);
$client = $this->createHttpClient();
$client->get($this->path."?get_action=stat&file=".AJXP_Utils::securePath($parts["path"]));
$json = $client->getContent();
$decode = json_decode($json, true);
return $decode;
}
// NOT IMPLEMENTED
public static function changeMode($path, $chmodValue)
{
}
public function unlink($url)
{
}
public function rmdir($url, $options)
{
}
public function mkdir($url, $mode, $options)
{
}
public function rename($from, $to)
{
}
public function dir_opendir ($url , $options )
{
}
public function dir_closedir ()
{
}
public function dir_readdir ()
{
}
public function dir_rewinddir ()
{
}
protected function parseUrl($url)
{
// URL MAY BE ajxp.ftp://username:password@host/path
$urlParts = parse_url($url);
$this->repositoryId = $urlParts["host"];
$repository = ConfService::getRepositoryById($this->repositoryId);
// Get USER/PASS
// 1. Try from URL
if (isSet($urlParts["user"]) && isset($urlParts["pass"])) {
$this->user = $urlParts["user"];
$this->password = $urlParts["pass"];
}
// 2. Try from user wallet
if (!isSet($this->user) || $this->user=="") {
$loggedUser = AuthService::getLoggedUser();
if ($loggedUser != null) {
$wallet = $loggedUser->getPref("AJXP_WALLET");
if (is_array($wallet) && isSet($wallet[$this->repositoryId]["AUTH_USER"])) {
$this->user = $wallet[$this->repositoryId]["AUTH_USER"];
$this->password = AJXP_Utils::decypherStandardFormPassword($loggedUser->getId(), $wallet[$this->repositoryId]["AUTH_PASS"]);
}
}
}
// 3. Try from repository config
if (!isSet($this->user) || $this->user=="") {
$this->user = $repository->getOption("AUTH_USER");
$this->password = $repository->getOption("AUTH_PASS");
}
if (!isSet($this->user) || $this->user=="") {
throw new AJXP_Exception("Cannot find user/pass for Http access!");
}
$this->host = $repository->getOption("HOST");
$this->path = $repository->getOption("URI");
$this->auth_path = $repository->getOption("AUTH_URI");
$this->use_auth = $repository->getOption("USE_AUTH");
$urlParts["path"] = urlencode($urlParts["path"]);
return $urlParts;
}
/**
* Initialize and return the HttpClient
*
* @return HttpClient
*/
protected function createHttpClient()
{
require_once(AJXP_BIN_FOLDER."/class.HttpClient.php");
$httpClient = new HttpClient($this->host);
$httpClient->cookie_host = $this->host;
$httpClient->timeout = 50;
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"Creating Http client", array());
//$httpClient->setDebug(true);
if (!$this->use_auth) {
return $httpClient;
}
$uri = "";
if ($this->auth_path != "") {
$httpClient->setAuthorization($this->user, $this->password);
$uri = $this->auth_path;
}
if (!isSet($_SESSION["AJXP_REMOTE_SESSION"])) {
if ($uri == "") {
// Retrieve a seed!
$httpClient->get($this->path."?get_action=get_seed");
$seed = $httpClient->getContent();
$user = $this->user;
$pass = $this->password;
$pass = md5(md5($pass).$seed);
$uri = $this->path."?get_action=login&userid=".$user."&password=".$pass."&login_seed=$seed";
}
$httpClient->setHeadersOnly(true);
$httpClient->get($uri);
$httpClient->setHeadersOnly(false);
$cookies = $httpClient->getCookies();
if (isSet($cookies["AjaXplorer"])) {
$_SESSION["AJXP_REMOTE_SESSION"] = $cookies["AjaXplorer"];
$remoteSessionId = $cookies["AjaXplorer"];
}
} else {
$remoteSessionId = $_SESSION["AJXP_REMOTE_SESSION"];
$httpClient->setCookies(array("AjaXplorer"=>$remoteSessionId));
}
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"Http Client created", array());
return $httpClient;
}
/**
* Describe whether the current wrapper can rewind a stream or not.
* @static
* @return boolean
*/
public static function isSeekable()
{
return false;
}
}