elFinderVolumeFTPIIS.class.php
3.17 KB
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
<?php
/**
* Simple elFinder driver for IIS FTP
*
**/
class elFinderVolumeFTPIIS extends elFinderVolumeFTP {
/**
* Connect to ftp server
*
* @return bool
**/
protected function connect() {
if (!($this->connect = ftp_connect($this->options['host'], $this->options['port'], $this->options['timeout']))) {
return $this->setError('Unable to connect to FTP server '.$this->options['host']);
}
if (!ftp_login($this->connect, $this->options['user'], $this->options['pass'])) {
$this->umount();
return $this->setError('Unable to login into '.$this->options['host']);
}
// switch off extended passive mode - may be usefull for some servers
//@ftp_exec($this->connect, 'epsv4 off' );
// enter passive mode if required
$this->options['mode'] = 'active';
ftp_pasv($this->connect, $this->options['mode'] == 'passive');
// enter root folder
if (!ftp_chdir($this->connect, $this->root))
{
$this->umount();
return $this->setError('Unable to open root folder.');
}
$stat = array();
$stat['name'] = $this->root;
$stat['mime'] = 'directory';
$this->filesCache[$this->root] = $stat;
$this->cacheDir($this->root);
return true;
}
/**
* Parse line from ftp_rawlist() output and return file stat (array)
*
* @param string $raw line from ftp_rawlist() output
* @return array
**/
protected function parseRaw($raw) {
$info = preg_split("/\s+/", $raw, 9);
$stat = array();
$stat['name'] = join(" ", array_slice($info, 3, 9));
$stat['read'] = true;
if ($info[2] == '<DIR>')
{
$stat['size'] = 0;
$stat['mime'] = 'directory';
}
else
{
$stat['size'] = $info[2];
$stat['mime'] = $this->mimetype($stat['name']);
}
return $stat;
}
/**
* Cache dir contents
*
* @param string $path dir path
* @return void
**/
protected function cacheDir($path) {
$this->dirsCache[$path] = array();
if (preg_match('/\'|\"/', $path)) {
foreach (ftp_nlist($this->connect, $path) as $p) {
if (($stat = $this->_stat($p)) &&empty($stat['hidden'])) {
// $files[] = $stat;
$this->dirsCache[$path][] = $p;
}
}
return;
}
foreach (ftp_rawlist($this->connect, $path) as $raw) {
if (($stat = $this->parseRaw($raw))) {
$p = $path.DIRECTORY_SEPARATOR.$stat['name'];
// $files[] = $stat;
$this->dirsCache[$path][] = $p;
//$stat['name'] = $p;
$this->filesCache[$p] = $stat;
}
}
}
protected function _stat($path) {
$stat = array();
$stat = $this->filesCache[$path];
if (empty($stat))
{
$this->cacheDir($this->_dirname($path));
$stat = $this->filesCache[$path];
}
return $stat;
}
protected function ftp_scan_dir($remote_directory)
{
$buff = ftp_rawlist($this->connect, $remote_directory, true);
$items = array();
foreach ($buff as $str) {
$info = preg_split("/\s+/", $str, 9);
$remote_file_path = $remote_directory . DIRECTORY_SEPARATOR . join(" ", array_slice($info, 3, 9));
$item = array();
$item['type'] = $info[2] == '<DIR>' ? 'd' : 'f';
$item['path'] = $remote_file_path;
$items[] = $item;
if ($item['type'] == 'd')
$items = array_merge($items, $this->ftp_scan_dir($item['path']));
}
return $items;
}
} // END class