-
Notifications
You must be signed in to change notification settings - Fork 2
/
sql.php
135 lines (107 loc) · 3.03 KB
/
sql.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
<?php
/**
* Class SQL
* @Author Michael Milawski
*/
class SQL
{
public $config = array();
public $wokeUp = "no";
private $DB = null;
private $isConnected = false;
public function getConfig ()
{
return $_SESSION[ 'config' ];
}
public function setConfig ($config)
{
$_SESSION[ 'config' ] = $config;
$this->config = $config;
}
/**
* Execute an SQL query
*
* @param $sql
* @param array $params
*
* @return array|bool
*/
public function query ($sql, $params = array())
{
if (!$this->isConnected) {
$connected = $this->connect();
if (is_array($connected)) {
return $connected;
}
if (!$connected === true) {
return $connected;
}
}
try {
//connect as appropriate as above
$stmt = $this->DB->prepare($sql);
$stmt->execute($params);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$re = array(
"status" => "OK",
"data" => $results,
"count" => $stmt->rowCount(),
);
//get count of rows when used limits
if (stripos($sql, "SQL_CALC_FOUND_ROWS") !== false) {
$cnt_stmt = $this->DB->prepare("SELECT FOUND_ROWS() as cnt");
$cnt_stmt->execute();
$cnt_res = $cnt_stmt->fetch(PDO::FETCH_ASSOC);
if (!empty($cnt_res) && isset($cnt_res[ 'cnt' ])) {
$re[ 'FOUND_ROWS' ] = $cnt_res[ 'cnt' ];
}
}
return $re;
} catch (PDOException $ex) {
return array(
"status" => "ERROR",
"error" => $ex->getMessage()
);
}
}
/**
* Connect to the database, config data is used from session
* @return array|bool
*/
private function connect ()
{
extract($_SESSION[ 'config' ]);
$charset = isset($charset) ? $charset : "utf8mb4";
if (isset($port)) {
$host = $host . ":" . $port;
}
try {
$db = new PDO("mysql:host={$host};dbname={$dbname};charset={$charset}", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $ex) {
return array(
"status" => "ERROR",
"error" => $ex->getMessage()
);
}
$this->DB = $db;
$this->isConnected = true;
return true;
}
}
try {
session_start();
$myurl = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$server = new SOAPServer(
NULL,
array(
'uri' => $myurl
)
);
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
$server->setClass('SQL');
$server->handle();
} catch (SOAPFault $f) {
print $f->faultstring;
}