This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.FilesystemMounter.php
executable file
·208 lines (193 loc) · 7.52 KB
/
class.FilesystemMounter.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
<?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');
/**
* Dynamically mount a remote folder when switching to the repository
* @package AjaXplorer_Plugins
* @subpackage Meta
*
*/
class FilesystemMounter extends AJXP_AbstractMetaSource
{
/**
* @var Repository
*/
protected $repository;
/**
* @param AbstractAccessDriver $accessDriver
* @param Repository $repository
*/
public function beforeInitMeta($accessDriver, $repository)
{
$this->accessDriver = $accessDriver;
$this->repository = $repository;
if($this->isAlreadyMounted()) return;
$this->mountFS();
}
/**
* @param AbstractAccessDriver $accessDriver
*/
public function initMeta($accessDriver)
{
parent::initMeta($accessDriver);
$this->repository = $this->accessDriver->repository;
/*
if($this->isAlreadyMounted()) return;
$this->mountFS();
*/
}
protected function getCredentials()
{
// 1. Try from plugin config
$user = $this->options["USER"];
$password = $this->options["PASS"];
// 1BIS : encoded?
if ($user == "" && isSet($this->options["ENCODED_CREDENTIALS"])) {
list($user,$password) = AJXP_Safe::getCredentialsFromEncodedString($this->options["ENCODED_CREDENTIALS"]);
}
// 2. Try from session
if ($user=="" && isSet($this->options["USE_SESSION_CREDENTIALS"]) ) {
$safeCred = AJXP_Safe::loadCredentials();
if ($safeCred !== false) {
$user = $safeCred["user"];
$password = $safeCred["password"];
} else {
throw new Exception("Session credential are empty! Did you forget to check the Set Session Credential in the Authentication configuration panel?");
}
}
return array($user, $password);
}
protected function getOption($name, $user="", $pass="", $escapePass = true)
{
$opt = $this->options[$name];
$opt = str_replace("AJXP_USER", $user, $opt);
if($escapePass) $opt = str_replace("AJXP_PASS", "'$pass'", $opt);
else $opt = str_replace("AJXP_PASS", $pass, $opt);
$opt = str_replace("AJXP_SERVER_UID", posix_getuid(), $opt);
$opt = str_replace("AJXP_SERVER_GID", posix_getgid(), $opt);
if (stristr($opt, "AJXP_REPOSITORY_PATH") !== false) {
$repo = $this->repository;
$path = $repo->getOption("PATH");
$opt = str_replace("AJXP_REPOSITORY_PATH", $path, $opt);
}
$opt = AJXP_VarsFilter::filter($opt);
return $opt;
}
protected function isAlreadyMounted()
{
list($user, $password) = $this->getCredentials();
$MOUNT_POINT = $this->getOption("MOUNT_POINT", $user, $password);
if( is_dir($MOUNT_POINT) ){
$statParent = stat(dirname($MOUNT_POINT));
$statMount = stat($MOUNT_POINT);
// Compare device id's
if( $statParent[0] == $statMount[0] ){
return false;
}else{
return true;
}
}else{
return false;
}
}
public function mountFS()
{
list($user, $password) = $this->getCredentials();
$this->logDebug("FSMounter::mountFS Should mount" . $user);
$repo = $this->repository;
if(isset($this->options["MOUNT_DEVIL"]) && !empty($this->options["MOUNT_DEVIL"]) && $this->options["MOUNT_DEVIL"]) {
$udevil = "udevil ";
}else{
$udevil = "";
}
$MOUNT_TYPE = $this->options["FILESYSTEM_TYPE"];
$MOUNT_POINT = $this->getOption("MOUNT_POINT", $user, $password);
$MOUNT_POINT_ROOT = $this->getOption("MOUNT_POINT", "", "");
$create = $repo->getOption("CREATE");
if ( $MOUNT_POINT != $MOUNT_POINT_ROOT && !is_dir($MOUNT_POINT_ROOT) && $create) {
@mkdir($MOUNT_POINT_ROOT, 0755);
}
$recycle = false;
if (!is_dir($MOUNT_POINT) && $create) {
@mkdir($MOUNT_POINT, 0755);
} else {
if ($repo->getOption("RECYCLE_BIN") != "") {
// Make sure the recycle bin was not mounted inside the mount point!
$recycle = $repo->getOption("PATH")."/".$repo->getOption("RECYCLE_BIN");
if (@is_dir($recycle)) {
@rmdir($recycle);
}
}
}
$UNC_PATH = $this->getOption("UNC_PATH", $user, $password, false);
$MOUNT_OPTIONS = $this->getOption("MOUNT_OPTIONS", $user, $password, false);
$cmd = $udevil."mount -t " .$MOUNT_TYPE. (empty( $MOUNT_OPTIONS )? " " : " -o " .escapeshellarg($MOUNT_OPTIONS). " " ) .escapeshellarg($UNC_PATH). " " .escapeshellarg($MOUNT_POINT);
$res = null;
if($this->getOption("MOUNT_ENV_PASSWD") == true){
putenv("PASSWD=$password");
}
system($cmd, $res);
if($this->getOption("MOUNT_ENV_PASSWD") == true){
putenv("PASSWD=");
}
$resultsOptions = str_replace(" ", "", $this->getOption("MOUNT_RESULT_SUCCESS"));
$acceptedResults = array(0);
if(!empty($resultsOptions)){
$acceptedResults = array_merge($acceptedResults, array_map("intval", explode(",", $resultsOptions)));
}
if($res === null){
// Check it is correctly mounted now!
// Could not get the output return code
$cmd1 = "mount | grep ".escapeshellarg($MOUNT_POINT);
$output = shell_exec($cmd1);
$success = !empty($output);
}else{
$success = (in_array($res, $acceptedResults));
}
if (!$success) {
throw new Exception("Error while mounting file system!");
} else {
if ($recycle !== false && !is_dir($recycle)) {
@mkdir($recycle, 0755);
}
}
}
public function umountFS()
{
$this->logDebug("FSMounter::unmountFS");
list($user, $password) = $this->getCredentials();
$MOUNT_POINT = $this->getOption("MOUNT_POINT", $user, $password);
if(isset($this->options["MOUNT_DEVIL"]) && !empty($this->options["MOUNT_DEVIL"]) && $this->options["MOUNT_DEVIL"]) {
$udevil = "udevil ";
}else{
$udevil = "";
}
system($udevil."umount ".escapeshellarg($MOUNT_POINT), $res);
if($this->getOption("REMOVE_MOUNTPOINT_ON_UNMOUNT") == true && $res == 0 && !$this->isAlreadyMounted() ){
// Remove mount point
$testRm = @rmdir($MOUNT_POINT);
if($testRm === false){
$this->logError("[umount]", "Error while trying to delete mount point on unmount");
}
}
return true;
}
}