commit | author | age
|
9200ad
|
1 |
<?php |
T |
2 |
/* |
|
3 |
Copyright (c) 2005, Till Brehm, projektfarm Gmbh |
|
4 |
All rights reserved. |
|
5 |
|
|
6 |
Redistribution and use in source and binary forms, with or without modification, |
|
7 |
are permitted provided that the following conditions are met: |
|
8 |
|
|
9 |
* Redistributions of source code must retain the above copyright notice, |
|
10 |
this list of conditions and the following disclaimer. |
|
11 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
12 |
this list of conditions and the following disclaimer in the documentation |
|
13 |
and/or other materials provided with the distribution. |
|
14 |
* Neither the name of ISPConfig nor the names of its contributors |
|
15 |
may be used to endorse or promote products derived from this software without |
|
16 |
specific prior written permission. |
|
17 |
|
|
18 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
19 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
21 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
22 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
23 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
24 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
25 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
26 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
27 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
28 |
*/ |
|
29 |
|
|
30 |
class db |
|
31 |
{ |
|
32 |
var $dbHost = ""; // hostname of the MySQL server |
|
33 |
var $dbName = ""; // logical database name on that server |
|
34 |
var $dbUser = ""; // database authorized user |
|
35 |
var $dbPass = ""; // user's password |
|
36 |
var $linkId = 0; // last result of mysql_connect() |
|
37 |
var $queryId = 0; // last result of mysql_query() |
|
38 |
var $record = array(); // last record fetched |
|
39 |
var $autoCommit = 1; // Autocommit Transactions |
|
40 |
var $currentRow; // current row number |
|
41 |
var $errorNumber = 0; // last error number |
|
42 |
var $errorMessage = ""; // last error message |
|
43 |
var $errorLocation = "";// last error location |
|
44 |
var $show_error_messages = false; |
|
45 |
|
|
46 |
// constructor |
|
47 |
function db() |
|
48 |
{ |
|
49 |
|
|
50 |
global $conf; |
20218c
|
51 |
$this->dbHost = $conf["mysql"]["host"]; |
M |
52 |
//$this->dbName = $conf["mysql"]["database"]; |
|
53 |
$this->dbUser = $conf["mysql"]["admin_user"]; |
|
54 |
$this->dbPass = $conf["mysql"]["admin_password"]; |
9200ad
|
55 |
//$this->connect(); |
T |
56 |
} |
|
57 |
|
|
58 |
// error handler |
|
59 |
function updateError($location) |
|
60 |
{ |
|
61 |
$this->errorNumber = mysql_errno(); |
|
62 |
$this->errorMessage = mysql_error(); |
|
63 |
$this->errorLocation = $location; |
|
64 |
if($this->errorNumber && $this->show_error_messages) |
|
65 |
{ |
|
66 |
echo('<br /><b>'.$this->errorLocation.'</b><br />'.$this->errorMessage); |
|
67 |
flush(); |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
function connect() |
|
72 |
{ |
|
73 |
if($this->linkId == 0) |
|
74 |
{ |
|
75 |
$this->linkId = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); |
239ce8
|
76 |
|
9200ad
|
77 |
if(!$this->linkId) |
T |
78 |
{ |
|
79 |
$this->updateError('DB::connect()<br />mysql_connect'); |
|
80 |
return false; |
|
81 |
} |
|
82 |
} |
|
83 |
return true; |
|
84 |
} |
|
85 |
|
|
86 |
function query($queryString) |
|
87 |
{ |
|
88 |
if(!$this->connect()) |
|
89 |
{ |
|
90 |
return false; |
|
91 |
} |
|
92 |
if($this->dbName != '') { |
|
93 |
if(!mysql_select_db($this->dbName, $this->linkId)) |
|
94 |
{ |
|
95 |
$this->updateError('DB::connect()<br />mysql_select_db'); |
|
96 |
return false; |
|
97 |
} |
|
98 |
} |
|
99 |
$this->queryId = @mysql_query($queryString, $this->linkId); |
|
100 |
$this->updateError('DB::query('.$queryString.')<br />mysql_query'); |
|
101 |
if(!$this->queryId) |
|
102 |
{ |
|
103 |
return false; |
|
104 |
} |
|
105 |
$this->currentRow = 0; |
|
106 |
return $this->queryId; |
|
107 |
} |
|
108 |
|
|
109 |
// returns all records in an array |
|
110 |
function queryAllRecords($queryString) |
|
111 |
{ |
|
112 |
if(!$this->query($queryString)) |
|
113 |
{ |
|
114 |
return false; |
|
115 |
} |
|
116 |
$ret = array(); |
|
117 |
while($line = $this->nextRecord()) |
|
118 |
{ |
|
119 |
$ret[] = $line; |
|
120 |
} |
|
121 |
return $ret; |
|
122 |
} |
|
123 |
|
|
124 |
// returns one record in an array |
|
125 |
function queryOneRecord($queryString) |
|
126 |
{ |
|
127 |
if(!$this->query($queryString) || $this->numRows() == 0) |
|
128 |
{ |
|
129 |
return false; |
|
130 |
} |
|
131 |
return $this->nextRecord(); |
|
132 |
} |
|
133 |
|
|
134 |
// returns the next record in an array |
|
135 |
function nextRecord() |
|
136 |
{ |
|
137 |
$this->record = mysql_fetch_assoc($this->queryId); |
|
138 |
$this->updateError('DB::nextRecord()<br />mysql_fetch_array'); |
|
139 |
if(!$this->record || !is_array($this->record)) |
|
140 |
{ |
|
141 |
return false; |
|
142 |
} |
|
143 |
$this->currentRow++; |
|
144 |
return $this->record; |
|
145 |
} |
|
146 |
|
|
147 |
// returns number of rows returned by the last select query |
|
148 |
function numRows() |
|
149 |
{ |
|
150 |
return mysql_num_rows($this->queryId); |
|
151 |
} |
|
152 |
|
|
153 |
function affectedRows() |
|
154 |
{ |
|
155 |
return mysql_affected_rows($this->linkId); |
|
156 |
} |
|
157 |
|
|
158 |
// returns mySQL insert id |
|
159 |
function insertID() |
|
160 |
{ |
|
161 |
return mysql_insert_id($this->linkId); |
|
162 |
} |
|
163 |
|
|
164 |
// Check der variablen |
|
165 |
// deprecated, now use quote |
|
166 |
function check($formfield) |
|
167 |
{ |
|
168 |
return $this->quote($formfield); |
|
169 |
} |
|
170 |
|
|
171 |
// Check der variablen |
|
172 |
function quote($formfield) |
|
173 |
{ |
|
174 |
return addslashes($formfield); |
|
175 |
} |
|
176 |
|
|
177 |
// Check der variablen |
|
178 |
function unquote($formfield) |
|
179 |
{ |
|
180 |
return stripslashes($formfield); |
|
181 |
} |
|
182 |
|
|
183 |
function toLower($record) { |
|
184 |
if(is_array($record)) { |
|
185 |
foreach($record as $key => $val) { |
|
186 |
$key = strtolower($key); |
|
187 |
$out[$key] = $val; |
|
188 |
} |
|
189 |
} |
|
190 |
return $out; |
|
191 |
} |
|
192 |
|
|
193 |
|
|
194 |
function insert($tablename,$form,$debug = 0) |
|
195 |
{ |
|
196 |
if(is_array($form)){ |
|
197 |
foreach($form as $key => $value) |
|
198 |
{ |
|
199 |
$sql_key .= "$key, "; |
|
200 |
$sql_value .= "'".$this->check($value)."', "; |
|
201 |
} |
|
202 |
$sql_key = substr($sql_key,0,strlen($sql_key) - 2); |
|
203 |
$sql_value = substr($sql_value,0,strlen($sql_value) - 2); |
|
204 |
|
|
205 |
$sql = "INSERT INTO $tablename (" . $sql_key . ") VALUES (" . $sql_value .")"; |
|
206 |
|
|
207 |
if($debug == 1) echo "SQL-Statement: ".$sql."<br><br>"; |
|
208 |
$this->query($sql); |
|
209 |
if($debug == 1) echo "mySQL Error Message: ".$this->errorMessage; |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
213 |
function update($tablename,$form,$bedingung,$debug = 0) |
|
214 |
{ |
|
215 |
|
|
216 |
if(is_array($form)){ |
|
217 |
foreach($form as $key => $value) |
|
218 |
{ |
|
219 |
$insql .= "$key = '".$this->check($value)."', "; |
|
220 |
} |
|
221 |
$insql = substr($insql,0,strlen($insql) - 2); |
|
222 |
$sql = "UPDATE $tablename SET " . $insql . " WHERE $bedingung"; |
|
223 |
if($debug == 1) echo "SQL-Statement: ".$sql."<br><br>"; |
|
224 |
$this->query($sql); |
|
225 |
if($debug == 1) echo "mySQL Error Message: ".$this->errorMessage; |
|
226 |
} |
|
227 |
} |
|
228 |
|
|
229 |
function closeConn() { |
|
230 |
|
|
231 |
} |
|
232 |
|
|
233 |
function freeResult() { |
|
234 |
|
|
235 |
|
|
236 |
} |
|
237 |
|
|
238 |
function delete() { |
|
239 |
|
|
240 |
} |
|
241 |
|
|
242 |
function Transaction($action) { |
|
243 |
//action = begin, commit oder rollback |
|
244 |
|
|
245 |
} |
|
246 |
|
|
247 |
/* |
|
248 |
$columns = array(action => add | alter | drop |
|
249 |
name => Spaltenname |
|
250 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
251 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
252 |
typeValue => Wert z.B. bei Varchar |
|
253 |
defaultValue => Default Wert |
|
254 |
notNull => true | false |
|
255 |
autoInc => true | false |
|
256 |
option => unique | primary | index) |
|
257 |
|
|
258 |
|
|
259 |
*/ |
|
260 |
|
|
261 |
function createTable($table_name,$columns) { |
|
262 |
$index = ""; |
|
263 |
$sql = "CREATE TABLE $table_name ("; |
|
264 |
foreach($columns as $col){ |
|
265 |
$sql .= $col["name"]." ".$this->mapType($col["type"],$col["typeValue"])." "; |
|
266 |
|
|
267 |
if($col["defaultValue"] != "") { |
|
268 |
if($col["defaultValue"] == "NULL" or $col["defaultValue"] == "NOT NULL") { |
|
269 |
$sql .= "DEFAULT ".$col["defaultValue"]." "; |
|
270 |
} else { |
|
271 |
$sql .= "DEFAULT '".$col["defaultValue"]."' "; |
|
272 |
} |
|
273 |
|
|
274 |
} elseif($col["defaultValue"] != false) { |
|
275 |
$sql .= "DEFAULT '' "; |
|
276 |
} |
|
277 |
if($col["defaultValue"] != "NULL" && $col["defaultValue"] != "NOT NULL") { |
|
278 |
if($col["notNull"] == true) { |
|
279 |
$sql .= "NOT NULL "; |
|
280 |
} else { |
|
281 |
$sql .= "NULL "; |
|
282 |
} |
|
283 |
} |
|
284 |
if($col["autoInc"] == true) $sql .= "auto_increment "; |
|
285 |
$sql.= ","; |
|
286 |
// key Definitionen |
|
287 |
if($col["option"] == "primary") $index .= "PRIMARY KEY (".$col["name"]."),"; |
|
288 |
if($col["option"] == "index") $index .= "INDEX (".$col["name"]."),"; |
|
289 |
if($col["option"] == "unique") $index .= "UNIQUE (".$col["name"]."),"; |
|
290 |
} |
|
291 |
$sql .= $index; |
|
292 |
$sql = substr($sql,0,-1); |
|
293 |
$sql .= ")"; |
|
294 |
|
|
295 |
$this->query($sql); |
|
296 |
return true; |
|
297 |
} |
|
298 |
|
|
299 |
/* |
|
300 |
$columns = array(action => add | alter | drop |
|
301 |
name => Spaltenname |
|
302 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
303 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
304 |
typeValue => Wert z.B. bei Varchar |
|
305 |
defaultValue => Default Wert |
|
306 |
notNull => true | false |
|
307 |
autoInc => true | false |
|
308 |
option => unique | primary | index) |
|
309 |
|
|
310 |
|
|
311 |
*/ |
|
312 |
function alterTable($table_name,$columns) { |
|
313 |
$index = ""; |
|
314 |
$sql = "ALTER TABLE $table_name "; |
|
315 |
foreach($columns as $col){ |
|
316 |
if($col["action"] == 'add') { |
|
317 |
$sql .= "ADD ".$col["name"]." ".$this->mapType($col["type"],$col["typeValue"])." "; |
|
318 |
} elseif ($col["action"] == 'alter') { |
|
319 |
$sql .= "CHANGE ".$col["name"]." ".$col["name_new"]." ".$this->mapType($col["type"],$col["typeValue"])." "; |
|
320 |
} elseif ($col["action"] == 'drop') { |
|
321 |
$sql .= "DROP ".$col["name"]." "; |
|
322 |
} |
|
323 |
if($col["action"] != 'drop') { |
|
324 |
if($col["defaultValue"] != "") $sql .= "DEFAULT '".$col["defaultValue"]."' "; |
|
325 |
if($col["notNull"] == true) { |
|
326 |
$sql .= "NOT NULL "; |
|
327 |
} else { |
|
328 |
$sql .= "NULL "; |
|
329 |
} |
|
330 |
if($col["autoInc"] == true) $sql .= "auto_increment "; |
|
331 |
$sql.= ","; |
|
332 |
// key Definitionen |
|
333 |
if($col["option"] == "primary") $index .= "PRIMARY KEY (".$col["name"]."),"; |
|
334 |
if($col["option"] == "index") $index .= "INDEX (".$col["name"]."),"; |
|
335 |
if($col["option"] == "unique") $index .= "UNIQUE (".$col["name"]."),"; |
|
336 |
} |
|
337 |
} |
|
338 |
$sql .= $index; |
|
339 |
$sql = substr($sql,0,-1); |
|
340 |
|
|
341 |
//die($sql); |
|
342 |
$this->query($sql); |
|
343 |
return true; |
|
344 |
} |
|
345 |
|
|
346 |
function dropTable($table_name) { |
|
347 |
$this->check($table_name); |
|
348 |
$sql = "DROP TABLE '". $table_name."'"; |
|
349 |
return $this->query($sql); |
|
350 |
} |
|
351 |
|
b84d47
|
352 |
// gibt Array mit Tabellennamen zur�ck |
9200ad
|
353 |
function getTables($database_name = '') { |
T |
354 |
|
b84d47
|
355 |
if($database_name == ''){ |
P |
356 |
$database_name = $this->dbName; |
|
357 |
} |
|
358 |
$result = mysql_query("SHOW TABLES FROM `$database_name`"); |
facccb
|
359 |
$tb_names = array(); |
9200ad
|
360 |
for ($i = 0; $i < mysql_num_rows($result); $i++) { |
T |
361 |
$tb_names[$i] = mysql_tablename($result, $i); |
|
362 |
} |
|
363 |
return $tb_names; |
|
364 |
} |
|
365 |
|
b84d47
|
366 |
// gibt Feldinformationen zur Tabelle zur�ck |
9200ad
|
367 |
/* |
T |
368 |
$columns = array(action => add | alter | drop |
|
369 |
name => Spaltenname |
|
370 |
name_new => neuer Spaltenname, nur bei 'alter' belegt |
|
371 |
type => 42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob |
|
372 |
typeValue => Wert z.B. bei Varchar |
|
373 |
defaultValue => Default Wert |
|
374 |
notNull => true | false |
|
375 |
autoInc => true | false |
|
376 |
option => unique | primary | index) |
|
377 |
|
|
378 |
|
|
379 |
*/ |
|
380 |
|
|
381 |
function tableInfo($table_name) { |
|
382 |
|
|
383 |
global $go_api,$go_info; |
|
384 |
// Tabellenfelder einlesen |
|
385 |
|
|
386 |
if($rows = $go_api->db->queryAllRecords("SHOW FIELDS FROM ".$table_name)){ |
|
387 |
foreach($rows as $row) { |
|
388 |
$name = $row[0]; |
|
389 |
$default = $row[4]; |
|
390 |
$key = $row[3]; |
|
391 |
$extra = $row[5]; |
|
392 |
$isnull = $row[2]; |
|
393 |
$type = $row[1]; |
|
394 |
|
|
395 |
|
|
396 |
$column = array(); |
|
397 |
|
|
398 |
$column["name"] = $name; |
|
399 |
//$column["type"] = $type; |
|
400 |
$column["defaultValue"] = $default; |
|
401 |
if(stristr($key,"PRI")) $column["option"] = "primary"; |
|
402 |
if(stristr($isnull,"YES")) { |
|
403 |
$column["notNull"] = false; |
|
404 |
} else { |
|
405 |
$column["notNull"] = true; |
|
406 |
} |
|
407 |
if($extra == 'auto_increment') $column["autoInc"] = true; |
|
408 |
|
|
409 |
|
|
410 |
// Type in Metatype umsetzen |
|
411 |
|
|
412 |
if(stristr($type,"int(")) $metaType = 'int32'; |
|
413 |
if(stristr($type,"bigint")) $metaType = 'int64'; |
|
414 |
if(stristr($type,"char")) { |
|
415 |
$metaType = 'char'; |
|
416 |
$tmp_typeValue = explode('(',$type); |
|
417 |
$column["typeValue"] = substr($tmp_typeValue[1],0,-1); |
|
418 |
} |
|
419 |
if(stristr($type,"varchar")) { |
|
420 |
$metaType = 'varchar'; |
|
421 |
$tmp_typeValue = explode('(',$type); |
|
422 |
$column["typeValue"] = substr($tmp_typeValue[1],0,-1); |
|
423 |
} |
|
424 |
if(stristr($type,"text")) $metaType = 'text'; |
|
425 |
if(stristr($type,"double")) $metaType = 'double'; |
|
426 |
if(stristr($type,"blob")) $metaType = 'blob'; |
|
427 |
|
|
428 |
|
|
429 |
$column["type"] = $metaType; |
|
430 |
|
|
431 |
$columns[] = $column; |
|
432 |
} |
|
433 |
return $columns; |
|
434 |
} else { |
|
435 |
return false; |
|
436 |
} |
|
437 |
|
|
438 |
|
|
439 |
//$this->createTable('tester',$columns); |
|
440 |
|
|
441 |
/* |
|
442 |
$result = mysql_list_fields($go_info["server"]["db_name"],$table_name); |
|
443 |
$fields = mysql_num_fields ($result); |
|
444 |
$i = 0; |
|
445 |
$table = mysql_field_table ($result, $i); |
|
446 |
while ($i < $fields) { |
|
447 |
$name = mysql_field_name ($result, $i); |
|
448 |
$type = mysql_field_type ($result, $i); |
|
449 |
$len = mysql_field_len ($result, $i); |
|
450 |
$flags = mysql_field_flags ($result, $i); |
|
451 |
print_r($flags); |
|
452 |
|
|
453 |
$columns = array(name => $name, |
|
454 |
type => "", |
|
455 |
defaultValue => "", |
|
456 |
isnull => 1, |
|
457 |
option => ""); |
|
458 |
$returnvar[] = $columns; |
|
459 |
|
|
460 |
$i++; |
|
461 |
} |
|
462 |
*/ |
|
463 |
|
|
464 |
|
|
465 |
|
|
466 |
} |
|
467 |
|
|
468 |
function mapType($metaType,$typeValue) { |
|
469 |
global $go_api; |
|
470 |
$metaType = strtolower($metaType); |
|
471 |
switch ($metaType) { |
|
472 |
case 'int16': |
|
473 |
return 'smallint'; |
|
474 |
break; |
|
475 |
case 'int32': |
|
476 |
return 'int'; |
|
477 |
break; |
|
478 |
case 'int64': |
|
479 |
return 'bigint'; |
|
480 |
break; |
|
481 |
case 'double': |
|
482 |
return 'double'; |
|
483 |
break; |
|
484 |
case 'char': |
|
485 |
return 'char'; |
|
486 |
break; |
|
487 |
case 'varchar': |
b84d47
|
488 |
if($typeValue < 1) die("Datenbank Fehler: F�r diesen Datentyp ist eine L�ngenangabe notwendig."); |
9200ad
|
489 |
return 'varchar('.$typeValue.')'; |
T |
490 |
break; |
|
491 |
case 'text': |
|
492 |
return 'text'; |
|
493 |
break; |
|
494 |
case 'blob': |
|
495 |
return 'blob'; |
|
496 |
break; |
|
497 |
} |
|
498 |
} |
|
499 |
|
|
500 |
} |
|
501 |
|
20218c
|
502 |
?> |