This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
StatementRetrievalManager.php
257 lines (238 loc) · 9 KB
/
StatementRetrievalManager.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
require_once 'Logger.php';
include_once("Encryption.php");
include_once("ConnectionManager.php");
include_once("HelperClass.php");
class StatementRetrievalManager
{
private $connManager;
private $encryption;
private $logger;
public function __construct($logID)
{
try
{
$this->encryption = new Encryption();
$this->logger = new Logger($logID);
}
catch (Exception $ex)
{
HelperClass::printResponseInCorrectEncodingAndCloseTunnelIfNeeded(null, $ex->getMessage());
}
}
/**
* The base function class that retrieves the specified SQL statement
* @param array $postData The post data that includes the connection details and the type of SQL statement that should be retrieved
* @throws Exception
*/
function getSqlStatement($postData)
{
include_once("ConnectionManager.php");
$helperClass = new HelperClass();
$this->connManager = new ConnectionManager($this->logger->getLogID());
$status = $this->connManager->connectToDBFromPostArray($postData);
if ($status[RESULT] != SUCCESS)
{
//print $this->encryption->encrypt(json_encode($status));
HelperClass::printResponseInCorrectEncodingAndCloseTunnelIfNeeded($postData, $status);
exit();
}
$query = "SHOW FIELDS FROM " . $postData["database"] . "." . $postData["table"];
$this->logger->writeToLog("Running Query: $query");
$result = $this->connManager->conn->query($query);
if ($result)
{
if ($postData['type'] == "GetSelectAllStatementCopy" ||
$postData['type'] == "GetSelectAllStatementEditor"
)
{
$data = $this->getSelectAllStatement($postData, $result);
}
else
{
if ($postData['type'] == "GetInsertStatementCopy" ||
$postData['type'] == "GetInsertStatementEditor"
)
{
$fields = array();
$i = 0;
while ($myrow = $result->fetch_array())
{
$fields[$i] = $myrow['Field'];
$i++;
}
$data = $this->getInsertStatement($postData, $fields);
}
else
{
if ($postData['type'] == "GetUpdateStatementCopy" ||
$postData['type'] == "GetUpdateStatementEditor"
)
{
$fields = array();
$i = 0;
while ($myrow = $result->fetch_array())
{
$fields[$i] = $myrow['Field'];
$i++;
}
$data = $this->getUpdateStatement($postData, $fields);
}
else
{
if ($postData['type'] == "GetDeleteStatementCopy"
|| $postData['type'] == "GetDeleteStatementEditor"
)
{
$data = $this->getDeleteStatement($postData);
}
else
{
if ($postData['type'] == "GetCreateStatementCopy"
|| $postData['type'] == "GetCreateStatementEditor"
)
{
$data = $this->getCreateStatement($postData);
}
}
}
}
}
$result = $helperClass->returnSuccessArray($data);
$result[LOCAL_TUNNEL_PORT] = $status[LOCAL_TUNNEL_PORT];
$this->logger->writeToLog("Successfully retrieved SQL statement");
HelperClass::printResponseInCorrectEncodingAndCloseTunnelIfNeeded($postData, $result);
//print $this->encryption->encrypt(json_encode($helperClass->returnSuccessArray($data)));
}
else
{
$mysqlError = mysqli_error($this->connManager->conn);
$this->logger->writeToLog("Failed to retrieve SQL Statement. MySQL Error: " . $mysqlError);
$returnSqlArray = $helperClass->returnMySQLErrorArray($mysqlError, mysqli_errno($this->connManager->conn));
$returnSqlArray[LOCAL_TUNNEL_PORT] = $status[LOCAL_TUNNEL_PORT];
HelperClass::printResponseInCorrectEncodingAndCloseTunnelIfNeeded($postData, $returnSqlArray);
//print $this->encryption->encrypt(json_encode($helperClass->returnMySQLErrorArray(mysqli_error($this->connManager->conn), mysqli_errno($this->connManager->conn))));
}
}
/**
* Retrieves the create statement back to the main function call
* @param type $postData The post data containing the connction details
* @return type Will either return the create statement or print JSON an error
*/
private function getCreateStatement($postData)
{
$helperClass = new HelperClass();
$query = "SHOW CREATE TABLE `" . $postData['database'] . "`.`" . $postData['table'] . "`";
$this->logger->writeToLog("Running query: $query");
$result = $this->connManager->conn->query($query);
if ($result)
{
while ($myrow = $result->fetch_array())
{
return $myrow['Create Table'];
}
}
else
{
$mysqlError = mysqli_error($this->connManager->conn);
$this->logger->writeToLog("Failed to get SQL statement. MySQL Error: $mysqlError");
print $this->encryption->encrypt(json_encode($helperClass->returnMySQLErrorArray($mysqlError, mysqli_errno($this->connManager->conn))));
exit();
}
}
/**
* Retrieves SQL to perform a select query
* @param type $postData The connection details
* @param type $result Result set of fields retrieved from database
* @return string The SELECT statement query
*/
private function getSelectAllStatement($postData, $result)
{
$line = "SELECT ";
$i = 0;
while ($myrow = $result->fetch_array())
{
if ($i < mysqli_num_rows($result) -1)
{
$line .= "`" . $postData["table"] . "`.`" . $myrow['Field'] . "`,\n\t";
}
else
{
$line .= "`" . $postData["table"] . "`.`" . $myrow['Field'] . "`\n";
}
$i++;
}
$line .= "FROM `" . $postData['database'] . "`.`" . $postData['table'] . "`;";
return $line;
}
/**
* Retrieves the INSERT INTO statement
* @param type $postData The connection details
* @param type $fields The fields that will make sure up the statement
* @return string The SQL statement
*/
private function getInsertStatement($postData, $fields)
{
$line = "INSERT INTO `" . $postData['database'] . "`.`" . $postData['table'] . "`\n(";
$fieldCount = count($fields);
for ($i = 0; $i < $fieldCount; $i++)
{
if ($i < $fieldCount - 1)
{
$line .= "`" . $fields[$i] . "`,\n";
}
else
{
$line .= "`" . $fields[$i] . "`)\n";
}
}
$line .= "VALUES\n(";
for ($i = 0; $i < $fieldCount; $i++)
{
if ($i < $fieldCount - 1)
{
$line .= "<" . $fields[$i] . ">,\n";
}
else
{
$line .= "<" . $fields[$i] . ">);";
}
}
return $line;
}
/**
* Returns the update statement
* @param type $postData The connection details
* @param type $fields The fields that will make up the statement
* @return string The update statement
*/
private function getUpdateStatement($postData, $fields)
{
$line = "UPDATE `" . $postData['database'] . "`.`" . $postData['table'] . "`\nSET\n";
$fieldCount = count($fields);
for ($i = 0; $i < $fieldCount; $i++)
{
if ($i < $fieldCount - 1)
{
$line .= "`" . $fields[$i] . "` = <" . $fields[$i] . ">,\n";
}
else
{
$line .= "`" . $fields[$i] . "` = <" . $fields[$i] . ">\n";
}
}
$line .= "WHERE `" . $fields[0] . "` = <{expr}>";
return $line;
}
/**
* Retrieves the SQL DELETE statement
* @param type $postData The connection details
* @return string The SQL Delete statement
*/
private function getDeleteStatement($postData)
{
$line = "DELETE FROM `" . $postData['database'] . "`.`" . $postData['table'] . "`\n";
$line .= "WHERE <{where_expression}>";
return $line;
}
}