|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Library; |
|
4
|
|
|
|
|
5
|
|
|
define('NET_SFTP_LOGGING', 2); |
|
6
|
|
|
|
|
7
|
|
|
class Sftp |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
protected $connectionName; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Constructor |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $connectionName |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct($connectionName) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->connectionName = $connectionName; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Gets all XML files from a remote folder |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $remoteDir |
|
26
|
|
|
* @param string $localDir |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getFiles($remoteDir, $localDir) |
|
30
|
|
|
{ |
|
31
|
|
|
$files = $this->remoteListPackaged($remoteDir); |
|
32
|
|
|
|
|
33
|
|
|
foreach ($files as $file) { |
|
34
|
|
|
$this->getFile($file['fullFileName'], $localDir . '/' . $file['fileName']); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $files; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Gets all XML files from a remote folder |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $dir |
|
44
|
|
|
* @param bool $sorted |
|
45
|
|
|
* @param string $sortOrder |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function remoteListPackaged($dir, $sorted = true, $sortOrder = 'desc') |
|
49
|
|
|
{ |
|
50
|
|
|
$files = []; |
|
51
|
|
|
$all = $this->remoteList($dir, $sorted, $sortOrder); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($all as $file=>$val) { |
|
54
|
|
|
if ($this->isValidExtension($val['filename'])) { |
|
55
|
|
|
$files[] = [ |
|
56
|
|
|
'fileName' => $val['filename'], |
|
57
|
|
|
'fullFileName' => $dir . '/' . $val['filename'], |
|
58
|
|
|
'size' => $val['size'], |
|
59
|
|
|
'modified' => gmdate("m/d/Y H:i:s", $val['mtime']), |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $files; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Retrieves a file from a SSH/SFTP server |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $remoteFile |
|
71
|
|
|
* @param string $localFile |
|
72
|
|
|
* @return \Symfony\Component\Console\Output\OutputInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getFile($remoteFile, $localFile) |
|
75
|
|
|
{ |
|
76
|
|
|
return \SSH::into($this->connectionName)->get($remoteFile, $localFile); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Places a file on a remote SSH/SFTP server |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $remoteFile |
|
83
|
|
|
* @param string $localFile |
|
84
|
|
|
* @return \Symfony\Component\Console\Output\OutputInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
public function putFile($remoteFile, $localFile) |
|
87
|
|
|
{ |
|
88
|
|
|
return \SSH::into($this->connectionName)->put($localFile, $remoteFile); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Retrieves a listing from directory |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $dir |
|
95
|
|
|
* @param bool $sorted |
|
96
|
|
|
* @param string $sortOrder |
|
97
|
|
|
* @return array array of files and folders |
|
98
|
|
|
*/ |
|
99
|
|
|
public function remoteList($dir, $sorted = true, $sortOrder = 'desc') |
|
100
|
|
|
{ |
|
101
|
|
|
$conn = $this->getConnection(); |
|
102
|
|
|
$conn->chdir($dir); |
|
103
|
|
|
$list = $conn->rawlist(); |
|
104
|
|
|
|
|
105
|
|
|
if ($sorted) { |
|
106
|
|
|
$list = $this->sortList($list, $sortOrder); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $list; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Retrieves a listing from directory |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $list |
|
116
|
|
|
* @param string $sortOrder |
|
117
|
|
|
* @return array sorted array of a folder |
|
118
|
|
|
*/ |
|
119
|
|
|
public function sortList($list, $sortOrder = 'desc') |
|
120
|
|
|
{ |
|
121
|
|
|
usort($list, function($a, $b) { return $a['mtime'] - $b['mtime']; }); |
|
122
|
|
|
|
|
123
|
|
|
return $sortOrder == 'asc' ? $list : array_reverse($list); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Retrieves a listing from directory |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $remoteFileFrom |
|
130
|
|
|
* @param string $remoteFileTo |
|
131
|
|
|
* @return boolean success |
|
132
|
|
|
*/ |
|
133
|
|
|
public function rename($remoteFileFrom, $remoteFileTo) |
|
134
|
|
|
{ |
|
135
|
|
|
$conn = $this->getConnection(); |
|
136
|
|
|
//print $conn->getSFTPLog(); |
|
|
|
|
|
|
137
|
|
|
return $conn->rename($remoteFileFrom, $remoteFileTo); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Gets a Net_SFTP connection as Laravels SSH doesn't have all the functionality |
|
142
|
|
|
* |
|
143
|
|
|
* @return Net_SFTP connection |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function getConnection() |
|
146
|
|
|
{ |
|
147
|
|
|
return \SSH::into($this->connectionName)->getGateway()->getConnection(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Gets a Net_SFTP connection as Laravels SSH doesn't have all the functionality |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $fileName |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function isValidExtension($fileName) |
|
157
|
|
|
{ |
|
158
|
|
|
return substr(strrchr($fileName, '.'), 1) === 'xml' || substr(strrchr($fileName, '.'), 1) === 'err'; |
|
159
|
|
|
} |
|
160
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.