Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
foreach($rows as $row) {
$name = $row['Field'];
$default = $row['Default'];
$key = $row['Key'];
$extra = $row['Extra'];
$isnull = $row['Null'];
$type = $row['Type'];
$column = array();
$column['name'] = $name;
//$column['type'] = $type;
$column['defaultValue'] = $default;
if(stristr($key, 'PRI')) $column['option'] = 'primary';
if(stristr($isnull, 'YES')) {
$column['notNull'] = false;
} else {
$column['notNull'] = true;
}
if($extra == 'auto_increment') $column['autoInc'] = true;
// Type in Metatype umsetzen
if(stristr($type, 'int(')) $metaType = 'int32';
if(stristr($type, 'bigint')) $metaType = 'int64';
if(stristr($type, 'char')) {
$metaType = 'char';
$tmp_typeValue = explode('(', $type);
$column['typeValue'] = substr($tmp_typeValue[1], 0, -1);
}
if(stristr($type, 'varchar')) {
$metaType = 'varchar';
$tmp_typeValue = explode('(', $type);
$column['typeValue'] = substr($tmp_typeValue[1], 0, -1);
}
if(stristr($type, 'text')) $metaType = 'text';
if(stristr($type, 'double')) $metaType = 'double';
if(stristr($type, 'blob')) $metaType = 'blob';
$column['type'] = $metaType;
$columns[] = $column;
}
return $columns;
} else {
return false;
}
//$this->createTable('tester',$columns);
/*
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
$result = mysql_list_fields($go_info["server"]["db_name"],$table_name);
$fields = mysql_num_fields ($result);
$i = 0;
$table = mysql_field_table ($result, $i);
while ($i < $fields) {
$name = mysql_field_name ($result, $i);
$type = mysql_field_type ($result, $i);
$len = mysql_field_len ($result, $i);
$flags = mysql_field_flags ($result, $i);
print_r($flags);
$columns = array(name => $name,
type => "",
defaultValue => "",
isnull => 1,
option => "");
$returnvar[] = $columns;
$i++;
}
*/
}
public function mapType($metaType, $typeValue) {
$metaType = strtolower($metaType);
switch ($metaType) {
case 'int16':
return 'smallint';
break;
case 'int32':
return 'int';
break;
case 'int64':
return 'bigint';
break;
case 'double':
return 'double';
break;
case 'char':
return 'char';
break;
case 'varchar':
if($typeValue < 1) die('Database failure: Length required for these data types.');
return 'varchar('.$typeValue.')';
break;
case 'text':
return 'text';
break;
case 'blob':
return 'blob';
break;

Till Brehm
committed
case 'date':
return 'date';
break;
$app->error('Unknown meta type: '.$metaType);
return false;
/**
* Get the database type (mariadb or mysql)
*
* @access public
* @return string 'mariadb' or string 'mysql'
*/
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
public function getDatabaseType() {
$tmp = $this->queryOneRecord('SELECT VERSION() as version');
if(stristr($tmp['version'],'mariadb')) {
return 'mariadb';
} else {
return 'mysql';
}
}
/**
* Get the database version
*
* @access public
* @param bool $major_version_only = true will return the major version only, e.g. 8 for MySQL 8
* @return string version number
*/
public function getDatabaseVersion($major_version_only = false) {
$tmp = $this->queryOneRecord('SELECT VERSION() as version');
$version = explode('-', $tmp['version']);
if($major_version_only == true) {
$version_parts = explode('.', $version[0]);
return $version_parts[0];
} else {
return $version[0];
}
}
/**
* Get a mysql password hash
*
* @access public
* @param string $password cleartext password
* @param string $hash_type MySQL hash type to use. either mysql_native_password or caching_sha2_password
public function getPasswordHash($password, $hash_type = 'mysql_native_password') {
if($hash_type == 'caching_sha2_password') {
$password_hash = $this->mysqlSha256Crypt($password, $this->genSalt(20), 5000);
} else {
$password_hash = '*' . strtoupper(sha1(sha1($password, true)));
}
return $password_hash;
}
/**
* @param $size int length of salt in bytes
*
* @return string
*/
private function genSalt($size) {
$salt = random_bytes($size);
if($salt === false) {
throw new Exception('Cannot generate salt.');
}
for($i = 0; $i < $size; $i++) {
$ord = ord($salt[$i]) & 0x7f;
$ord += 32;
if($ord == 36 /* $ */) {
$ord += 1;
}
$salt[$i] = chr($ord);
return $salt;
}
/**
* this is the SHA256 algorithm of the crypt unix call – the only difference is that we do not truncate the salt to 16 chars
* @see https://www.akkadia.org/drepper/SHA-crypt.txt
* @see https://github.com/mysql/mysql-server/blob/trunk/mysys/crypt_genhash_impl.cc
*
* @param string $plaintext the plain text password
* @param string $salt the raw salt (needs to be 20 bytes long)
* @param int $rounds number of rounds. MySQL default is 5000. Must be between 1000 and 4095000 (0xFFF * 1000)
*
* @return string hashed password in MySQL format
*/
private function mysqlSha256Crypt($plaintext, $salt, $rounds) {
$plaintext_len = strlen($plaintext);
$salt_len = strlen($salt);
// 1
$ctxA = hash_init('sha256');
// 2
hash_update($ctxA, $plaintext);
// 3
hash_update($ctxA, $salt);
// 4
$ctxB = hash_init('sha256');
// 5
hash_update($ctxB, $plaintext);
// 6
hash_update($ctxB, $salt);
// 7
hash_update($ctxB, $plaintext);
// 8
$B = hash_final($ctxB, true);
// 9
for($i = $plaintext_len; $i > 32; $i -= 32) {
hash_update($ctxA, $B);
// 10
hash_update($ctxA, substr($B, 0, $i));
// 11
for($i = $plaintext_len; $i > 0; $i >>= 1) {
if(($i & 1) != 0) {
hash_update($ctxA, $B);
} else {
hash_update($ctxA, $plaintext);
}
}
// 12
$A = hash_final($ctxA, true);
// 13
$ctxDP = hash_init('sha256');
// 14
for($i = 0; $i < $plaintext_len; $i++) {
hash_update($ctxDP, $plaintext);
}
// 15
$DP = hash_final($ctxDP, true);
// 16
$P = "";
for($i = $plaintext_len; $i > 32; $i -= 32) {
$P .= $DP;
}
$P .= substr($DP, 0, $i);
// 17
$ctxDS = hash_init('sha256');
// 18
for($i = 0; $i < 16 + ord($A[0]); $i++) {
hash_update($ctxDS, $salt);
}
// 19
$DS = hash_final($ctxDS, true);
// 20
$S = "";
for($i = $salt_len; $i >= 32; $i -= 32) {
$S .= $DS;
}
$S .= substr($DS, 0, $i);
// 21
$C = "";
for($i = 0; $i < $rounds; $i++) {
$ctxC = hash_init('sha256');
if(($i & 1) != 0) {
hash_update($ctxC, $P);
} else {
hash_update($ctxC, $i == 0 ? $A : $C);
}
hash_update($ctxC, $S);
}
hash_update($ctxC, $P);
}
if(($i & 1) != 0) {
hash_update($ctxC, $i == 0 ? $A : $C);
} else {
hash_update($ctxC, $P);
}
$C = hash_final($ctxC, true);
}
// 22
$b64result = str_repeat(' ', 43);
$p = 0;
$b64_from_24bit = function($B2, $B1, $B0, $N) use (&$b64result, &$p) {
$b64_alphabet = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$w = ($B2 << 16) | ($B1 << 8) | $B0;
$n = $N;
while(--$n >= 0) {
$b64result[$p++] = $b64_alphabet[$w & 0x3f];
$w = $w >> 6;
}
};
$b64_from_24bit(ord($C[0]), ord($C[10]), ord($C[20]), 4);
$b64_from_24bit(ord($C[21]), ord($C[1]), ord($C[11]), 4);
$b64_from_24bit(ord($C[12]), ord($C[22]), ord($C[2]), 4);
$b64_from_24bit(ord($C[3]), ord($C[13]), ord($C[23]), 4);
$b64_from_24bit(ord($C[24]), ord($C[4]), ord($C[14]), 4);
$b64_from_24bit(ord($C[15]), ord($C[25]), ord($C[5]), 4);
$b64_from_24bit(ord($C[6]), ord($C[16]), ord($C[26]), 4);
$b64_from_24bit(ord($C[27]), ord($C[7]), ord($C[17]), 4);
$b64_from_24bit(ord($C[18]), ord($C[28]), ord($C[8]), 4);
$b64_from_24bit(ord($C[9]), ord($C[19]), ord($C[29]), 4);
$b64_from_24bit(0, ord($C[31]), ord($C[30]), 3);
// we do not truncate $salt to 16 chars since MySQL does not do that and uses 20 bytes salts
return sprintf('$A$%03x$%s%s', $rounds / 1000, $salt, $b64result);
}
/**
* database query result class
*
* @package pxFramework
*
*/
class db_result {
/**
*
* @var mysqli_result|null
* @access private
*/
private $_iResId = null;
/** @var mysqli|null */
private $_iConnection = null;
/**
*
*
* @access private
*/

Marius Burkard
committed
public function __construct($iResId, $iConnection) {
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
$this->_iResId = $iResId;
$this->_iConnection = $iConnection;
}
/**
* get count of result rows
*
* Returns the amount of rows in the result set
*
* @access public
* @return int amount of rows
*/
public function rows() {
if(!is_object($this->_iResId)) return 0;
$iRows = mysqli_num_rows($this->_iResId);
if(!$iRows) $iRows = 0;
return $iRows;
}
/**
* Get number of affected rows
*
* Returns the amount of rows affected by the previous query
*
* @access public
* @return int amount of affected rows
*/
public function affected() {
if(!is_object($this->_iConnection)) return 0;
$iRows = mysqli_affected_rows($this->_iConnection);
if(!$iRows) $iRows = 0;
return $iRows;
}
/**
* Frees the result set
*
* @access public
*/
public function free() {
if(!is_object($this->_iResId)) return;
mysqli_free_result($this->_iResId);
return;
}
/**
* Get a result row (associative)
*
* Returns the next row in the result set. To be used in a while loop like while($currow = $result->get()) { do something ... }
*
* @access public
* @return array result row
*/
public function get() {
$aItem = null;
if(is_object($this->_iResId)) {
$aItem = mysqli_fetch_assoc($this->_iResId);
if(!$aItem) $aItem = null;
}
return $aItem;
}
/**
* Get a result row (array with numeric index)
*
* @access public
* @return array result row
*/
public function getAsRow() {
$aItem = null;
if(is_object($this->_iResId)) {
$aItem = mysqli_fetch_row($this->_iResId);
if(!$aItem) $aItem = null;
}
return $aItem;
}
}
/**
* database query result class
*
* emulates a db result set out of an array so you can use array results and db results the same way
*
* @package pxFramework
* @see db_result
*
*
*/
class fakedb_result {
/**
*
*
* @access private
*/
private $aResultData = array();
/**
*
*
* @access private
*/
private $aLimitedData = array();
/**
*
*
* @access private
*/

Marius Burkard
committed
public function __construct($aData) {
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
$this->aResultData = $aData;
$this->aLimitedData = $aData;
reset($this->aLimitedData);
}
/**
* get count of result rows
*
* Returns the amount of rows in the result set
*
* @access public
* @return int amount of rows
*/
// Gibt die Anzahl Zeilen zurück
public function rows() {
return count($this->aLimitedData);
}
/**
* Frees the result set
*
* @access public
*/
// Gibt ein Ergebnisset frei
public function free() {
$this->aResultData = array();
$this->aLimitedData = array();
return;
}
/**
* Get a result row (associative)
*
* Returns the next row in the result set. To be used in a while loop like while($currow = $result->get()) { do something ... }
*
* @access public
* @return array result row
*/
// Gibt eine Ergebniszeile zurück
public function get() {
$aItem = null;
if(!is_array($this->aLimitedData)) return $aItem;
foreach($this->aLimitedData as $vKey => $aItem) {
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
if(!$aItem) $aItem = null;
}
return $aItem;
}
/**
* Get a result row (array with numeric index)
*
* @access public
* @return array result row
*/
public function getAsRow() {
return $this->get();
}
/**
* Limit the result (like a LIMIT x,y in a SQL query)
*
* @access public
* @param int $iStart offset to start read
* @param int $iLength amount of datasets to read
*/
public function limit_result($iStart, $iLength) {
$this->aLimitedData = array_slice($this->aResultData, $iStart, $iLength, true);
}
}