2020-03-24 23:22:07 +01:00
#!/bin/php
< ? php
/*
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2020-04-07 19:39:06 +02:00
require ( __DIR__ . '/../include/mb_ucfirst.php' );
2020-03-24 23:22:07 +01:00
define ( 'N' , " \n " );
use function mysqli_real_escape_string as myesc ;
$inifp = __DIR__ . '/../sec/mastostartadmin.ini' ;
$iniarr =@ parse_ini_file ( $inifp )
or mexit ( 'Impossibile aprire il file di configurazione «' . $inifp . '»' . N , 1 );
$link =@ mysqli_connect ( $iniarr [ 'db_host' ], $iniarr [ 'db_admin_name' ], $iniarr [ 'db_admin_password' ], $iniarr [ 'db_name' ], $iniarr [ 'db_port' ], $iniarr [ 'db_socket' ])
or mexit ( 'Impossibile connettersi al server MySQL: ' . mysqli_connect_error () . N , 1 );
mysqli_set_charset ( $link , 'utf8mb4' )
or mexit ( mysqli_error ( $link ) . N , 1 );
$tables = array ();
$res = mysqli_query ( $link , 'SHOW TABLES' )
or mexit ( mysqli_error ( $link ) . N , 1 );
while ( $row = mysqli_fetch_row ( $res )) {
$resb = mysqli_query ( $link , 'SHOW COLUMNS FROM ' . $row [ 0 ])
or mexit ( mysqli_error ( $link ) . N , 1 );
$fields = array ();
// lo uso solo per alcuni tipi, quindi non sto a cercare completezza
while ( $rowb = mysqli_fetch_assoc ( $resb )) {
if ( preg_match ( '/(\w+)\((.*)\)( unsigned)?/' , $rowb [ 'Type' ], $buf ) === 1 ) {
switch ( $buf [ 1 ]) {
case 'char' :
case 'varchar' :
$fields [ $rowb [ 'Field' ]] = $buf [ 2 ];
break ;
case 'tinyint' :
if ( array_key_exists ( 3 , $buf ))
$fields [ $rowb [ 'Field' ]] = array ( 'min' => 0 , 'max' => 255 );
else
$fields [ $rowb [ 'Field' ]] = array ( 'min' =>- 128 , 'max' => 127 );
break ;
case 'smallint' :
if ( array_key_exists ( 3 , $buf ))
$fields [ $rowb [ 'Field' ]] = array ( 'min' => 0 , 'max' => 65535 );
else
$fields [ $rowb [ 'Field' ]] = array ( 'min' =>- 32768 , 'max' => 32767 );
break ;
case 'mediumint' :
if ( array_key_exists ( 3 , $buf ))
$fields [ $rowb [ 'Field' ]] = array ( 'min' => 0 , 'max' => 16777215 );
else
$fields [ $rowb [ 'Field' ]] = array ( 'min' =>- 8388608 , 'max' => 8388607 );
break ;
case 'int' :
if ( array_key_exists ( 3 , $buf ))
$fields [ $rowb [ 'Field' ]] = array ( 'min' => 0 , 'max' => 4294967295 );
else
$fields [ $rowb [ 'Field' ]] = array ( 'min' =>- 2147483648 , 'max' => 2147483647 );
break ;
// bigint non ci sta in php a meno di usare bcmath o gmp che non è detto siano abilitate sul server, in ogni caso poco importa perché valori bigint vengono usati solo internamente al db, non "vengono da fuori"
case 'bigint' :
if ( array_key_exists ( 3 , $buf ))
$fields [ $rowb [ 'Field' ]] = array ( 'min' => '0' , 'max' => '18446744073709551615' );
else
$fields [ $rowb [ 'Field' ]] = array ( 'min' => '-9223372036854775808' , 'max' => '9223372036854775807' );
break ;
case 'decimal' :
// questo è da testare contro un decimale vero
// fatto, il risultato è che in mysql devo usare decimal(14,4)
if ( preg_match ( '/,/' , $buf [ 2 ]) === 1 ) {
$lim = explode ( ',' , $buf [ 2 ]);
} else {
$lim [ 0 ] = $buf [ 2 ];
$lim [ 1 ] = 0 ;
}
$int = $lim [ 0 ] - $lim [ 1 ];
$sint = '' ;
for ( $i = 0 ; $i < $int ; $i ++ )
$sint .= '9' ;
$sdec = '' ;
for ( $i = 0 ; $i < $lim [ 1 ]; $i ++ )
$sdec .= '9' ;
$max = $sint . '.' . $sdec ;
if ( array_key_exists ( 3 , $buf ))
$fields [ $rowb [ 'Field' ]] = array ( 'min' => 0 , 'max' => floatval ( $max ));
else
$fields [ $rowb [ 'Field' ]] = array ( 'min' => floatval ( '-' . $max ), 'max' => floatval ( $max ));
break ;
default :
$fields [ $rowb [ 'Field' ]] = $rowb [ 'Type' ];
break ;
}
} elseif ( $rowb [ 'Type' ] == 'text' ) {
$fields [ $rowb [ 'Field' ]] = 65535 ;
} else {
$fields [ $rowb [ 'Field' ]] = $rowb [ 'Type' ];
}
}
$tables [ $row [ 0 ]] = $fields ;
}
$localesfp = 'locales.json' ;
$locales =@ file_get_contents ( $localesfp );
if ( $locales === false )
mexit ( 'Non ho potuto aprire il file «' . $localesfp . '».' . N , 1 );
$locales = json_decode ( $locales , true );
print_r ( $locales );
foreach ( $locales as $key => $val ) {
$code = myesc ( $link , $key );
2020-04-07 19:39:06 +02:00
$NameIt = myesc ( $link , mb_ucfirst ( locale_get_display_name ( $key , 'it' )));
$NameEn = myesc ( $link , mb_ucfirst ( locale_get_display_name ( $key , 'en' )));
$NameEs = myesc ( $link , mb_ucfirst ( locale_get_display_name ( $key , 'es' )));
$NameFr = myesc ( $link , mb_ucfirst ( locale_get_display_name ( $key , 'fr' )));
$NameOrig = myesc ( $link , mb_ucfirst ( locale_get_display_name ( $key , $key )));
2020-04-03 10:59:02 +02:00
$que = 'INSERT INTO Languages (ID, Code, NameIT, NameEN, NameES, NameFR, NameOrig) VALUES (NULL, \'' . $code . '\', \'' . $NameIt . '\', \'' . $NameEn . '\', \'' . $NameEs . '\', \'' . $NameFr . '\', \'' . $NameOrig . '\')' ;
2020-03-24 23:22:07 +01:00
echo ( $que . N );
mysqli_query ( $link , $que )
or mexit ( mysqli_error ( $link ) . N , 2 );
}
mysqli_close ( $link );
exit ( 0 );
function mexit ( $msg , $rv ) {
global $link ;
if ( $link )
mysqli_close ( $link );
echo ( $msg );
exit ( $rv );
}
function truncs ( $str , $tab , $col , $ctx ) {
global $tables , $tronconi , $iswin ;
if ( $iswin )
$tab = strtolower ( $tab );
$size = $tables [ $tab ][ $col ];
$len = mb_strlen ( $str , 'UTF-8' );
if ( $len > $size ) {
$tronconi [] = array ( 'id' => null , 'tab' => $tab , 'col' => $col , 'ctx' => $ctx , 'len' => $len , 'size' => $size );
$str = mb_substr ( $str , 0 , $size - 1 , 'UTF-8' ) . '…' ;
}
return ( $str );
}
function truncn ( $num , $tab , $col , $ctx ) {
global $tables , $iswin ;
if ( $iswin )
$tab = strtolower ( $tab );
if ( is_numeric ( $num )) {
if ( $num > $tables [ $tab ][ $col ][ 'max' ]) {
notify ( $ctx . ': ho dovuto troncare «' . $num . '» al valore massimo «' . $tables [ $tab ][ $col ][ 'max' ] . '» che può avere nella colonna «' . $col . '» della tabella «' . $tab . '»).' , 2 );
$num = $tables [ $tab ][ $col ][ 'max' ];
} elseif ( $num < $tables [ $tab ][ $col ][ 'min' ]) {
notify ( $ctx . ': ho dovuto troncare «' . $num . '» al valore minimo «' . $tables [ $tab ][ $col ][ 'min' ] . '» che può avere nella colonna «' . $col . '» della tabella «' . $tab . '»).' , 2 );
$num = $tables [ $tab ][ $col ][ 'min' ];
}
} else {
notify ( $ctx . ': truncn(): mi aspettavo un numero, invece non lo era; ritorno «0».' , 3 );
$num = 0 ;
}
return ( $num );
}
?>