forked from jmccoskery/php-mysql-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSHMysql.php
More file actions
128 lines (108 loc) · 5.54 KB
/
SSHMysql.php
File metadata and controls
128 lines (108 loc) · 5.54 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
<?php
/********************************************************************************
* @author jmccoskery
* PHP class for running SQL Queries on remote MySQL Databases over an SSH connection
* requires openssl and SSH2 PHP modules to be installed on server
*
* @usage:
* $mysql = new SSHMysql(...server creds...);
* $result = $mysql->query(...sql string...);
********************************************************************************/
class SSHMysql
{
// variable definition(s)
private $_server;
/********************************************************************************
* @method __construct
* @param $server_sshipaddress
* @param $server_sshport
* @param $server_sshusername
* @param $server_sshpassword
* @param $server_mysqlipaddress
* @param $server_mysqlusername
* @param $server_mysqlpassword
* @param $server_mysqlport
*
* class construct, takes server credentials as parameters for use in subsequent
* queries
********************************************************************************/
function __construct( $server_sshipaddress, $server_sshport, $server_sshusername, $server_sshpassword, $server_mysqlipaddress, $server_mysqlusername, $server_mysqlpassword, $server_mysqlport )
{
$this->_server = array();
$this->_server['sshipaddress'] = $server_sshipaddress;
$this->_server['sshport'] = $server_sshport;
$this->_server['sshusername'] = $server_sshusername;
$this->_server['sshpassword'] = $server_sshpassword;
$this->_server['mysqlipaddress'] = $server_mysqlipaddress;
$this->_server['mysqlusername'] = $server_mysqlusername;
$this->_server['mysqlpassword'] = $server_mysqlpassword;
$this->_server['mysqlport'] = $server_mysqlport;
}
/********************************************************************************
* @method query
* @param $sql
* @return stdObject
*
* executes the query on the mysql server, parses it and returns the results or
* error details
********************************************************************************/
public function query($sql)
{
// if !ssh2_connect, exit because the SSH2 module is not installed for PHP //
if (function_exists("ssh2_connect") )
{
$connection = ssh2_connect($_server['sshipaddress'], $_server['sshport']);
// if the SSH username and password are correct, try to run the query via ssh2_exec; if it's not correct return authentication failure to user //
if (ssh2_auth_password($connection, $_server['sshusername'], $_server['sshpassword'])) {
// set up a shell script. use port-forwarding over 3307 to tunnel into the remote database via SSH, this COULD CHANGE depending on your server's SSH configuration. 3307 is most common.//
// clean up the SQL query so things like slashes, single quotes and double quotes don't cause errors //
$ssh_query ='ssh -L 3307:'.$_server['sshipaddress'].':'.$_server['mysqlport'].'; echo "' . str_replace( '"', '\'', stripslashes( $query ) ) . '" | mysql -u '.$_server['mysqlusername'].' -h '.$_server['mysqlipaddress'].' --password='.$_server['mysqlpassword'];
// execute the query over a secure connection //
$result = ssh2_exec($connection, $ssh_query);
// catch any stream errors that might occur. This will return the command line's MySQL errors to help with query debugging if there's an error in the SQL statement
$error_result = ssh2_fetch_stream($result, SSH2_STREAM_STDERR);
// turn on stream blocking to save the query results and errors to variables
stream_set_blocking($result, true);
stream_set_blocking($error_result, true);
// DEBUG, print the sql result:
// print_r( stream_get_contents($result) );
// print_r( stream_get_contents($error_result) );
// parse the sql query. all results come back as strings within a standard, tab delimited format that can be split into result sets
$arr_1 = explode( "\n", stream_get_contents($result) );
$keys = explode( "\t", $arr_1[0] ); // get the column names
$results = array();
for($i=1;$i< ( sizeof($arr_1) -1 );$i++) // parse the results
{
$values = explode( "\t", $arr_1[$i] );
$return = new stdClass;
$index = 0;
foreach( $values as $v )
{
$return->{$keys[$index]} = $v;
$index++;
}
$results[] = $return;
}
if(sizeof($results) > 0)
{
return array('status'=>'success','msg'=>'DB Query was successful.', 'dataset'=>$results, 'type'=>'ssh');
} else {
return array('status'=>'error', 'msg'=>'There is an error in your SQL statement, or your sql returned no results', 'errorset'=>stream_get_contents($error_result), 'dataset'=>array(), 'type'=>'ssh');
}
// close the SSH tunnel
fclose($result);
if ( function_exists ( 'ssh2_disconnect' ) ) {
ssh2_disconnect ( $connection );
} else { // if no disconnect func is available, close conn, unset var
fclose ( $connection );
$connection = false;
}
} else {
return array('status'=>'error', 'msg'=>'SSH Authentication Failed because of a bad username or password. Please check the SSH authentication settings and try again.','dataset'=>array(), 'type'=>'ssh');
}
} else {
return array('status'=>'error', 'msg'=>'SSH Authentication Failed because SSH2 Library is not installed on this server.<br/><br/><b>SSH2 Library is required for making SSH connections to remote servers.</b>','dataset'=>array(), 'type'=>'ssh');
}
}
}
?>