-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.hubicAccessWrapper.php
186 lines (168 loc) · 6.04 KB
/
class.hubicAccessWrapper.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
<?php
/*
* Copyright 2015 Cyril Aknine
* 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 <https://github.com/darylounet/access.hubic/>.
*
*/
defined('AJXP_EXEC') or die( 'Access not allowed');
require_once(AJXP_INSTALL_PATH .'/plugins/access.swift/class.swiftAccessWrapper.php');
require_once(AJXP_INSTALL_PATH .'/plugins/access.hubic/HubicBootstrap.php');
/**
* AJXP_Plugin to access a HubiC account
* @package AjaXplorer_Plugins
* @subpackage Access
*/
class hubicAccessWrapper extends swiftAccessWrapper
{
public static $lastException;
private static $cloudContext;
/**
* Initialize the stream from the given path.
* Concretely, transform ajxp.swiftfs:// into swiftfs://
*
* @param string $path
* @return mixed Real path or -1 if currentListing contains the listing : original path converted to real path
*/
protected static function initPath($path, $streamType, $storeOpenContext = false, $skipZip = false)
{
$url = parse_url($path);
$repoId = $url['host'];
$repoObject = ConfService::getRepositoryById($repoId);
if (!isset($repoObject)) {
$exception = new Exception('Cannot find repository with id '. $repoId);
self::$lastException = $exception;
throw $exception;
}
if (!in_array('swiftfs', stream_get_wrappers())) {
\OpenStack\HubicBootstrap::useStreamWrappers();
}
if (self::$cloudContext == null) {
self::$cloudContext = stream_context_create(
array('swiftfs' =>
array(
'token' => $_SESSION['PROP_HUBIC_account/credentials']['token'],
'swift_endpoint' => $_SESSION['PROP_HUBIC_account/credentials']['endpoint']
)
)
);
}
// Base container on HubiC is "default"
return 'swiftfs://'. $repoObject->getOption('CONTAINER') . str_replace('//', '/', $url['path']);
}
/**
* Opens the stream
* Diff with parent class : do not "securePath", as it removes double slash
*
* @param String $path Maybe in the form "ajxp.fs://repositoryId/pathToFile"
* @param String $mode
* @param unknown_type $options
* @param unknown_type $opened_path
* @return unknown
*/
public function stream_open($path, $mode, $options, &$context)
{
try {
$this->realPath = $this->initPath($path, "file");
} catch (Exception $e) {
AJXP_Logger::error(__CLASS__, 'stream_open', 'Error while opening stream ' .$path);
return false;
}
if ($this->realPath == -1) {
$this->fp = -1;
return true;
} else {
$this->fp = fopen($this->realPath, $mode, $options, self::$cloudContext);
return ($this->fp !== false);
}
}
/**
* Opens a handle to the dir
* Fix PEAR by being sure it ends up with "/", to avoid
* adding the current dir to the children list.
*
* @param unknown_type $path
* @param unknown_type $options
* @return unknown
*/
public function dir_opendir($path , $options )
{
$this->realPath = $this->initPath($path, "dir", true);
if ($this->realPath[strlen($this->realPath)-1] != "/") {
$this->realPath.="/";
}
if (is_string($this->realPath)) {
$this->dH = opendir($this->realPath, self::$cloudContext);
} elseif ($this->realPath == -1) {
$this->dH = -1;
}
return $this->dH !== false;
}
public function mkdir($path, $mode, $options)
{
$this->realPath = $this->initPath($path, "dir", true);
file_put_contents($this->realPath."/.marker", "tmpdata");
return true;
}
// DUPBLICATE STATIC FUNCTIONS TO BE SURE
// NOT TO MESS WITH self:: CALLS
public static function removeTmpFile($tmpDir, $tmpFile)
{
if(is_file($tmpFile)) unlink($tmpFile, self::$cloudContext);
if(is_dir($tmpDir)) rmdir($tmpDir, self::$cloudContext);
}
protected static function closeWrapper()
{
if (self::$crtZip != null) {
self::$crtZip = null;
self::$currentListing = null;
self::$currentListingKeys = null;
self::$currentListingIndex = null;
self::$currentFileKey = null;
}
}
public static function getRealFSReference($path, $persistent = false)
{
$tmpFile = AJXP_Utils::getAjxpTmpDir()."/".md5(time()).".".pathinfo($path, PATHINFO_EXTENSION);
$tmpHandle = fopen($tmpFile, "wb", null, self::$cloudContext);
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)
{
$fp = fopen($path, "r", null, self::$cloudContext);
while (!feof($fp)) {
$data = fread($fp, 4096);
fwrite($stream, $data, strlen($data));
}
fclose($fp);
}
public static function changeMode($path, $chmodValue)
{
// DO NOTHING!
//$realPath = self::initPath($path, "file");
//chmod($realPath, $chmodValue);
}
}