This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.sftpAccessDriver.php
189 lines (174 loc) · 6.6 KB
/
class.sftpAccessDriver.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
<?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/>.
*
*/
namespace Pydio\Access\Driver\StreamProvider\SFTP;
use DOMNode;
use PclZip;
use Pydio\Access\Core\AJXP_MetaStreamWrapper;
use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Access\Core\RecycleBinManager;
use Pydio\Access\Core\Model\Repository;
use Pydio\Access\Driver\StreamProvider\FS\fsAccessDriver;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Controller\Controller;
use Pydio\Core\Exception\PydioException;
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* Plugin to access an ftp server over SSH
* @package AjaXplorer_Plugins
* @subpackage Access
*/
class sftpAccessDriver extends fsAccessDriver
{
/**
* @var Repository
*/
public $repository;
public $driverConf;
protected $wrapperClassName;
protected $urlBase;
public function initRepository()
{
if (is_array($this->pluginConf)) {
$this->driverConf = $this->pluginConf;
} else {
$this->driverConf = array();
}
if (!function_exists('ssh2_connect')) {
throw new \Exception("You must have the php ssh2 extension active!");
}
ConfService::setConf("PROBE_REAL_SIZE", false);
$path = $this->repository->getOption("PATH");
$recycle = $this->repository->getOption("RECYCLE_BIN");
$this->detectStreamWrapper(true);
$this->urlBase = "pydio://".$this->repository->getId();
restore_error_handler();
if (!file_exists($this->urlBase)) {
if ($this->repository->getOption("CREATE")) {
$test = @mkdir($this->urlBase);
if (!$test) {
throw new PydioException("Cannot create path ($path) for your repository! Please check the configuration.");
}
} else {
throw new PydioException("Cannot find base path ($path) for your repository! Please check the configuration!");
}
}
if ($recycle != "") {
RecycleBinManager::init($this->urlBase, "/".$recycle);
}
}
/**
* @inheritdoc
*/
protected function parseSpecificContributions(ContextInterface $ctx, \DOMNode &$contribNode)
{
parent::parseSpecificContributions($ctx, $contribNode);
if($contribNode->nodeName != "actions") return ;
$this->disableArchiveBrowsingContributions($contribNode);
}
protected function filecopy($srcFile, $destFile)
{
if (AJXP_MetaStreamWrapper::nodesUseSameWrappers($srcFile, $destFile)) {
$srcFilePath = str_replace($this->urlBase, "", $srcFile);
$destFilePath = str_replace($this->urlBase, "", $destFile);
$destDirPath = dirname($destFilePath);
list($connection, $remote_base_path) = sftpAccessWrapper::getSshConnection($srcFile);
$remoteSrc = $remote_base_path.$srcFilePath;
$remoteDest = $remote_base_path.$destDirPath;
$this->logDebug("SSH2 CP", array("cmd" => 'cp '.$remoteSrc.' '.$remoteDest));
ssh2_exec($connection, 'cp '.$remoteSrc.' '.$remoteDest);
Controller::applyHook("node.change", array(new AJXP_Node($srcFile), new AJXP_Node($destFile), true));
} else {
parent::filecopy($srcFile, $destFile);
}
}
/**
* @param $src
* @param $dest
* @param $basedir
* @throws \Exception
* @return PclZip Zip Archive
*/
public function makeZip ($src, $dest, $basedir)
{
@set_time_limit(60);
require_once(AJXP_BIN_FOLDER."/lib/pclzip.lib.php");
$filePaths = array();
$uniqid = uniqid();
$uniqfolder = '/tmp/ajaxplorer-zip-'.$uniqid;
mkdir($uniqfolder);
foreach ($src as $item) {
$basedir = trim(dirname($item));
$basename = basename($item);
$uniqpath = $uniqfolder.'/'.$basename;
$this->full_copy($this->urlBase.$item, $uniqpath);
$filePaths[] = array(PCLZIP_ATT_FILE_NAME => $uniqpath,
PCLZIP_ATT_FILE_NEW_SHORT_NAME => $basename);
}
$this->logDebug("Pathes", $filePaths);
$this->logDebug("Basedir", array($basedir));
$archive = new PclZip($dest);
$vList = $archive->create($filePaths, PCLZIP_OPT_REMOVE_PATH, $uniqfolder, PCLZIP_OPT_NO_COMPRESSION);
$this->recursiveRmdir($uniqfolder);
if (!$vList) {
throw new \Exception("Zip creation error : ($dest) ".$archive->errorInfo(true));
}
return $vList;
}
public function full_copy( $source, $destination )
{
if ( is_dir( $source ) ) {
@mkdir( $destination );
$directory = dir( $source );
while ( FALSE !== ( $readdirectory = $directory->read() ) ) {
if ($readdirectory == '.' || $readdirectory == '..') {
continue;
}
$PathDir = $source . '/' . $readdirectory;
if ( is_dir( $PathDir ) ) {
$this->full_copy( $PathDir, $destination . '/' . $readdirectory );
continue;
}
copy( $PathDir, $destination . '/' . $readdirectory );
}
$directory->close();
} else {
copy( $source, $destination );
}
}
public function recursiveRmdir($path)
{
if (is_dir($path)) {
$path = rtrim($path, '/');
$subdir = dir($path);
while (($file = $subdir->read()) !== false) {
if ($file != '.' && $file != '..') {
(!is_link("$path/$file") && is_dir("$path/$file")) ? $this->recursiveRmdir("$path/$file") : unlink("$path/$file");
}
}
$subdir->close();
rmdir($path);
return true;
}
return false;
}
}