forked from SusoGym/SEST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.connect.php
338 lines (281 loc) · 8.79 KB
/
class.connect.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
require_once("MySQLException.php");
/**
* Database Connection Klasse
* erstellt die Verbindung zur Datenbank
*/
class Connection {
/**
* @var string configuration file
*/
public static $configFile = "cfg.ini";
/**
* @var string database ip
*/
private $server;
/**
* @var string database user
*/
private $user;
/**
* @var string database password
*/
private $pass;
/**
* @var string filesystem path
*/
private $basepath;
/**
* @var string download folder
*/
private $download;
/**
* @var string ics filename
*/
private $icsfile;
/**
* @var string ldap link
*/
private $ldap;
/**
* @var \mysqli database connection instance
*/
private $connID;
/**
* @var string database name
*/
private $database;
/**
* Konstruktor der automatisch die Datenbankverbindung
* bei Instanzierung aufbaut
*/
public function __construct() {
$this->getCredentials();
$this->connect();
}
/**
* Ermittelt die Serververbindung, benutzer und Passwort zur Basisdatenbank
*/
private function getCredentials() {
$f = fopen(self::$configFile, "r");
while (!feof($f)) {
$line = fgets($f);
$larr = explode("=", $line);
switch ($larr[0]) {
case "SERVER":
$this->server = trim($larr[1]);
break;
case "USER":
$this->user = trim($larr[1]);
break;
case "PASS":
$this->pass = trim($larr[1]);
break;
case "DB":
$this->database = trim($larr[1]);
break;
case "DOWNLOAD":
$this->download = trim($larr[1]);
break;
case "FILEBASE":
$this->basepath = trim($larr[1]);
break;
case "ICS":
$this->icsfile = trim($larr[1]);
break;
case "ldap":
$this->ldap = trim($larr[1]);
break;
}
}
fclose($f);
}
/**
* returns values from ini
*
* @return array(string)
*/
public function getIniParams() {
return array("download" => $this->download, "basepath" => $this->basepath, "icsfile" => $this->icsfile, "ldap"=> $this->ldap); // could be much more versatile
}
/**
* Verbindet mit der jeweils benutzten Datenbank
*/
private function connect() {
$reporting = error_reporting(0);
ChromePhp::info("Connecting to " . $this->user . "@" . $this->server . ":" . $this->database);
$mysqli = $this->connID = new \mysqli($this->server, $this->user, $this->pass, $this->database);
error_reporting($reporting);
if ($mysqli->connect_errno) {
printf("Connection to database failed: %s\n", $mysqli->connect_error);
ChromePhp::error("Connection to database failed: " . $mysqli->connect_error);
exit();
}
//ChromePhp::logSQL("Connection to database " . $this->user . "@" . $this->server . "/" . $this->database . " successful!");
mysqli_set_charset($mysqli, 'utf8');
}
/**
* universelle Methode zur Rückgabe von Abfrageergebnissen
*
* @param string $query SQL Abfrage
* @return array[][] welches im ersten Index die Zeile, im Zweiten Index die Spalte bezeichnet
* zur Abfrage kann also jeweils im zweiten Index auf die einzelnen Ergebnisse in angegebener
* Reihenfolge zurückgegriffen werden
* @throws MySQLException
*/
public function selectValues($query) {
ChromePhp::logSQL("Selecting values: \"$query\" from " . $this->getCaller());
$mysqli = $this->connID;
$result = $mysqli->query($query);
if ($result === false) {
throw new MySQLException($mysqli, $query);
}
$value = null;
$anz = $result->field_count;
$valCount = 0;
while ($row = $result->fetch_array(MYSQLI_NUM)) {
for ($x = 0; $x < $anz; $x++) {
$value[$valCount][$x] = $row[$x];
}
$valCount++;
}
$result->free();
return $value;
}
/**
* Gibt assoziative Arrays zurück
* Indizes mit Feldnamen
*
* @param $query String SQL Query
* @return array[Feldname][] | null
* @throws MySQLException
*/
public function selectAssociativeValues($query) {
ChromePhp::logSQL("Selecting values: \"$query\" from " . $this->getCaller());
$mysqli = $this->connID;
$result = $mysqli->query($query);
if ($result === false) {
throw new MySQLException($mysqli, $query);
}
$assocValue = null;
$fieldName = null;
$anz = $result->field_count;
$valCount = 0;
while ($row = $result->fetch_array(MYSQLI_NUM)) {
for ($x = 0; $x < $anz; $x++) {
$fieldInfo = $result->fetch_field_direct($x);
$assocValue[$valCount][$fieldInfo->name] = $row[$x];
}
$valCount++;
}
$result->free();
return $assocValue;
}
/**
* gibt ein Array mit den Feldnamen zurück
*
* @param string $query
* @return array
*/
public function selectFieldNames($query) {
ChromePhp::logSQL("Selecting FieldNames: \"$query\" from " . $this->getCaller());
$fieldNames = array();
$mysqli = $this->connID;
if ($result = $mysqli->query($query)) {
$finfo = $result->fetch_fields();
foreach ($finfo as $f) {
$fieldNames[] = $f->name;
}
return $fieldNames;
} else {
return null;
}
}
/**
* Universelle Methode zum Einfügen von Daten
*
* @param string $query SQL Abfrage
* @return int Insert ID
* @throws MySQLException
*/
public function insertValues($query) {
ChromePhp::logSQL("Inserting: \"$query\" from " . $this->getCaller());
$mysqli = $this->connID;
$result = $mysqli->query($query);
if ($result === false) {
throw new MySQLException($mysqli, $query);
}
return $mysqli->insert_id;
}
/**
* Führt eine SQL Query durch
*
* @param $query String SQL Query
* @throws MySQLException
*/
function straightQuery($query) {
ChromePhp::logSQL("Query: \"$query\" from " . $this->getCaller());
$mysqli = $this->connID;
$result = $mysqli->query($query);
if ($result === false) {
throw new MySQLException($mysqli, $query);
}
}
/**
* Führt eine multi SQL Query durch (Statement1 ; Statement2 ; ...)
*
* @param $query
* @throws MySQLException
*/
function straightMultiQuery($query) {
$mysqli = $this->connID;
$result = $mysqli->multi_query($query);
if ($result === false) {
throw new MySQLException($mysqli, $query);
}
while ($mysqli->more_results() && $mysqli->next_result()) ;
}
/**
* Schließt die Datenbank Verbindung
*/
public function close() {
mysqli_close($this->connID);
}
/**
* Gibt die genutzte mysqli Klasse zurück
*
* @return \mysqli
*/
public function getConnection() {
return $this->connID;
}
/**
* Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection
*
* @param $string
* @return string
*/
function escape_string($string) {
return $this->connID->real_escape_string($string);
}
/**
* Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection
* Will change the given String
*
* @param $string
*/
function escape_stringDirect(&$string) {
$string = $this->escape_string($string);
}
/**
* @return string location the caller method was called from
*/
function getCaller() {
$info = debug_backtrace();
$file = $info[1]['file'];
$line = $info[1]['line'];
$method = $info[2]['function'];
return "$file:$line / $method()";
}
}
?>