-
Notifications
You must be signed in to change notification settings - Fork 126
/
ftp.controller.php
145 lines (125 loc) · 4.21 KB
/
ftp.controller.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
<?php
class FTPStorage implements StorageController
{
private $connection;
private $login;
function __destruct()
{
if($this->connection)
ftp_close($this->connection);
}
function connect()
{
if(!$this->connection)
{
if(defined('FTP_SSL') && FTP_SSL === true)
$this->connection = ftp_ssl_connect(FTP_SERVER, ((defined('FTP_SSL') && is_numeric(FTP_PORT))?FTP_PORT:21) );
else
$this->connection = ftp_connect(FTP_SERVER, ((defined('FTP_SSL') && is_numeric(FTP_PORT))?FTP_PORT:21) );
}
if($this->connection && !$this->login)
{
$this->login = ftp_login($this->connection, FTP_USER, FTP_PASS);
if( (defined('FTP_PASSIVEMODE') && FTP_PASSIVEMODE === true) || !defined('FTP_PASSIVEMODE') )
{
ftp_set_option($this->connection, FTP_USEPASVADDRESS, false);
ftp_pasv($this->connection, TRUE);
}
else
ftp_pasv($this->connection, false);
}
// Was the connection successful?
if ((!$this->connection) || (!$this->login)) {
$this->connection = false;
return false;
}
return true;
}
function isEnabled()
{
return (defined('FTP_SERVER') && FTP_SERVER &&
defined('FTP_USER') && FTP_USER &&
defined('FTP_PASS') && FTP_PASS);
}
function hashExists($hash)
{
if(!$this->connect()) return null;
$subdir = $this->hashToDir($hash);
$ftpfilepath = FTP_BASEDIR.$subdir.'/'.$hash;
if(@ftp_chdir($this->connection, FTP_BASEDIR.$subdir))
return (ftp_size($this->connection,$ftpfilepath)>0?true:false);
return false;
}
function getItems($dev=false)
{
if(!$this->connect()) return false;
return $this->ftp_list_files_recursive(FTP_BASEDIR,$dev);
}
function pullFile($hash,$location)
{
if(!$this->connect()) return false;
$subdir = $this->hashToDir($hash);
$ftpfilepath = FTP_BASEDIR.$subdir.'/'.$hash;
return ftp_get($this->connection, $location, $ftpfilepath, FTP_BINARY);
}
function pushFile($source,$hash)
{
if(!$this->connect()) return false;
$subdir = $this->hashToDir($hash);
$ftpfilepath = FTP_BASEDIR.$subdir.'/'.$hash;
$this->ftp_mksubdirs($subdir);
return ftp_put($this->connection, $ftpfilepath, $source, FTP_BINARY);
}
function deleteFile($hash)
{
if(!$this->connect()) return false;
$subdir = $this->hashToDir($hash);
$ftpfilepath = FTP_BASEDIR.$subdir.'/'.$hash;
return (ftp_delete($this->connection,$ftpfilepath)?true:false);
}
function hashToDir($hash)
{
$md5 = md5($hash);
$dir = $md5[0].'/'.$md5[1].'/'.$md5[2];
return $dir;
}
function ftp_mksubdirs($ftpath)
{
if(!$this->connect()) return false;
@ftp_chdir($this->connection, FTP_BASEDIR);
$parts = array_filter(explode('/',$ftpath), function($value) {
return ($value !== null && $value !== false && $value !== '');
});
foreach($parts as $part){
$part = strval($part);
if(!@ftp_chdir($this->connection, $part)){
ftp_mkdir($this->connection, $part);
ftp_chdir($this->connection, $part);
}
}
}
function ftp_list_files_recursive($path,$dev=false)
{
if(!$this->connect()) return false;
$items = ftp_mlsd($this->connection, $path);
$result = array();
if(is_array($items))
foreach ($items as $item)
{
$name = $item['name'];
$type = $item['type'];
$filepath = $path.'/'. $name;
if ($type == 'dir')
{
$result =
array_merge($result, $this->ftp_list_files_recursive($filepath,$dev));
}
else if(mightBeAHash($name) || endswith($name,'.enc'))
{
$result[] = $name;
if($dev===true) echo " Got $name \r";
}
}
return $result;
}
}