Skip to content

Commit 781c3b0

Browse files
author
millsoft
committed
INIT
1 parent 7e1151e commit 781c3b0

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
##SQL Tunnel Soap Server/Client for PHP
2+
3+
### What is this?
4+
Not every hoster allows to connect directly to MYSQL but allows a direct Web connection (Port 80). This script is the middleman between your app and some mysql database.
5+
6+
### How To use?
7+
See tests/testsoap.php for an example how to use this
8+
9+
10+
### Thats all?
11+
yeah, for now thats all.. I don't want to make it complicated but I might add more features to it later. For now this is what I needed in one of my projects.
12+
13+
14+
--
15+
16+
***Have a nice day ;)***
17+
18+
`Michael`

sql.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
/**
4+
* Class SQL
5+
* @Author Michael Milawski
6+
*/
7+
8+
9+
class SQL
10+
{
11+
public $config = array();
12+
public $wokeUp = "no";
13+
private $DB = null;
14+
private $isConnected = false;
15+
16+
public function getConfig ()
17+
{
18+
return $_SESSION[ 'config' ];
19+
}
20+
21+
public function setConfig ($config)
22+
{
23+
$_SESSION[ 'config' ] = $config;
24+
$this->config = $config;
25+
}
26+
27+
28+
/**
29+
* Execute an SQL query
30+
*
31+
* @param $sql
32+
* @param array $params
33+
*
34+
* @return array|bool
35+
*/
36+
public function query ($sql, $params = array())
37+
{
38+
if (!$this->isConnected) {
39+
40+
$connected = $this->connect();
41+
42+
if (is_array($connected)) {
43+
return $connected;
44+
}
45+
46+
if (!$connected === true) {
47+
return $connected;
48+
}
49+
}
50+
51+
try {
52+
//connect as appropriate as above
53+
$stmt = $this->DB->prepare($sql);
54+
$stmt->execute($params);
55+
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
56+
57+
$re = array(
58+
"status" => "OK",
59+
"data" => $results,
60+
"count" => $stmt->rowCount(),
61+
);
62+
63+
//get count of rows when used limits
64+
if (stripos($sql, "SQL_CALC_FOUND_ROWS") !== false) {
65+
$cnt_stmt = $this->DB->prepare("SELECT FOUND_ROWS() as cnt");
66+
$cnt_stmt->execute();
67+
$cnt_res = $cnt_stmt->fetch(PDO::FETCH_ASSOC);
68+
69+
if (!empty($cnt_res) && isset($cnt_res[ 'cnt' ])) {
70+
$re[ 'FOUND_ROWS' ] = $cnt_res[ 'cnt' ];
71+
}
72+
}
73+
74+
return $re;
75+
76+
} catch (PDOException $ex) {
77+
return array(
78+
"status" => "ERROR",
79+
"error" => $ex->getMessage()
80+
);
81+
82+
}
83+
}
84+
85+
86+
/**
87+
* Connect to the database, config data is used from session
88+
* @return array|bool
89+
*/
90+
private function connect ()
91+
{
92+
extract($_SESSION[ 'config' ]);
93+
94+
$charset = isset($charset) ? $charset : "utf8mb4";
95+
if (isset($port)) {
96+
$host = $host . ":" . $port;
97+
}
98+
99+
try {
100+
$db = new PDO("mysql:host={$host};dbname={$dbname};charset={$charset}", $username, $password);
101+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
102+
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
103+
} catch (PDOException $ex) {
104+
return array(
105+
"status" => "ERROR",
106+
"error" => $ex->getMessage()
107+
);
108+
109+
}
110+
111+
$this->DB = $db;
112+
$this->isConnected = true;
113+
114+
return true;
115+
}
116+
}
117+
118+
119+
try {
120+
session_start();
121+
$myurl = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
122+
123+
$server = new SOAPServer(
124+
NULL,
125+
array(
126+
'uri' => $myurl
127+
)
128+
);
129+
130+
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
131+
$server->setClass('SQL');
132+
$server->handle();
133+
} catch (SOAPFault $f) {
134+
print $f->faultstring;
135+
}

tests/testsoap.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* Simple Test for the SOAP client
5+
*/
6+
7+
8+
/**
9+
* @param $tunnel_url
10+
* @param $config
11+
*
12+
* @return \SoapClient
13+
*/
14+
function getDbTunnel($tunnel_url, $config){
15+
16+
try {
17+
$db = new SoapClient(null, array(
18+
'location' => $tunnel_url,
19+
'uri' => $tunnel_url,
20+
'trace' => 1,
21+
'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP
22+
));
23+
24+
$db->setConfig($config);
25+
26+
} catch (SoapFault $fault) {
27+
trigger_error("SOAP-Error: (Error No: {$fault->faultcode}, "
28+
. "Error: {$fault->faultstring})", E_USER_ERROR);
29+
}
30+
return $db;
31+
}
32+
33+
$tunnel_url = "http://localhost/sqltunnel/sql.php";
34+
35+
$config = array(
36+
"host" => "YourHost",
37+
"dbname" => "db",
38+
"username" => "username",
39+
"password" => "password",
40+
);
41+
42+
$S = getDbTunnel($tunnel_url, $config);
43+
$data = $S->query("SELECT SQL_CALC_FOUND_ROWS id,name_en FROM events LIMIT 3");
44+
print_r($data);

0 commit comments

Comments
 (0)