-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.php
115 lines (102 loc) · 2.61 KB
/
fs.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
<?php
/**
* This class provides functionality for secure FileServer connection
* through SSH and SCP protocols. Also, there are used dedicated prepared
* statements using mysqli.
*
* For the SSH and SCP connections phpseclib is used.
* @link http://phpseclib.sourceforge.net/
*
* mysqli Prepared Statements:
* @link http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
*
* @param string $host, string $user, string $password
* @author Anastasios Glaros
* @version v1.0, 2015-3-10
*/
set_include_path( 'phpseclib0.3.10/');
require_once('Crypt/RSA.php');
require_once('Net/SSH2.php');
require_once('Net/SCP.php');
class FileServer
{
private $fs_host;
private $fs_user;
private $fs_pass;
//private $fs_port;
private $con = false; // Keeping track of if the connection is active or not
private $fsssh;
private $fsscp;
//private $results_mtx = array();
public function __construct($host, $user, $pass){
$this->fs_host = $host;
$this->fs_user = $user;
$this->fs_pass = $pass;
//$this->fs_port = $port;
}
/**
* This function establishes a secure SSH connection with the remote
* file server.
* @return boolean
*/
public function ssh_connection() {
if(!$this->con){
//echo "connection not existed.<br>";
$this->fsssh = new Net_SSH2($this->fs_host);
if(!$this->fsssh->login($this->fs_user, $this->fs_pass)){
echo "SSH: Fail to login.<br>";
return false;
}
if($this->fsssh){
$this->con = true;
//echo "true connect<br>";
return true;
}
else {
//echo "false connect<br>";
return false;
}
}
else {
echo "SSH connection exists.<br>";
return true;
}
}
/**
* This function transfers a file from the remote file server to a local
* folder using a SCP connection.
* @param string $remotePathAndFile, string $localPathAndFile
* @return boolean
*/
public function scp_bring($remoteFile, $localFile){
$this->fsscp = new Net_SCP($this->fsssh);
if(!$this->fsscp->get($remoteFile, $localFile)){
//echo "SCP: Fail retrieving file.<br>";
return false;
}
else {
//echo "Successful File Transfer. Path: " . $localFile;
return true;
}
}
/**
* This function closes an SSH connection.
* @return boolean
*/
public function disconnect() {
if($this->fsssh->isConnected()){
if($this->fsssh->disconnect()){
$this->con = false;
return true;
}
else return false;
}
else {
echo "No Connection to disconnect.<br>";
}
}
public function __destruct(){
//echo "destructed";
}
}
?>