|
| 1 | +<?php |
| 2 | + |
| 3 | +use SQLTrees\SQLDSL; |
| 4 | +use SQLTrees\CompiledStatement; |
| 5 | +use function SQLTrees\{select,from,where,order_by,limit,tupleArray,tuple,eq,operator,andExpr,orExp,id,str,num}; |
| 6 | + |
| 7 | +if( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) { |
| 8 | + exit( 1 ); |
| 9 | +} |
| 10 | + |
| 11 | +# TODO add a MySQL user and password with access to databases mysql and information_schema |
| 12 | +$dbuser = 'root'; |
| 13 | +$dbpass = ''; |
| 14 | + |
| 15 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 16 | + |
| 17 | +function showSQLStatement( CompiledStatement $stmt ) : string { |
| 18 | + $sql = $stmt->getPreparedStatement(); |
| 19 | + $parameters = count( $sql['parameters'] ) |
| 20 | + ? ' % ( ' . implode( ', ', array_map( function( $x ) { return '(' . $x[0] . ') "' . $x[1] . '"'; }, $sql['parameters'] ) ) . ' )' |
| 21 | + : ''; |
| 22 | + return '"' . $sql['template'] . '"' . $parameters; |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); |
| 27 | + |
| 28 | +### |
| 29 | +# Example given in https://connect.ed-diamond.com/MISC/misc-111/zero-sqli-malgre-les-developpeurs |
| 30 | +$a = select( SQLDSL::$star, from( id( 'songs' ) ), |
| 31 | + where( andExpr( eq( id( 'yearofsong' ), num( 1964 ) ), |
| 32 | + eq( id( 'songtitle' ), str( 'I\'m a loser' ) ), |
| 33 | + eq( id( 'author' ), str( 'Lennon' ) ) ) ) ); |
| 34 | + |
| 35 | +$stmt = $a->compile(); |
| 36 | +echo showSQLStatement( $stmt ) . "\n"; |
| 37 | + |
| 38 | + |
| 39 | +### |
| 40 | +# Another example that should work on any MySQL server |
| 41 | +# You have to adapt the credentials below |
| 42 | +$a = select( tuple( id( 'db' ) ), from( id( 'db' ) ) ); |
| 43 | +$stmt = $a->compile(); |
| 44 | +echo showSQLStatement( $stmt ) . "\n"; |
| 45 | + |
| 46 | +# Add password for root in third argument or a MySQL account with access to mysql.db |
| 47 | +$mysqli = new mysqli( 'localhost', $dbuser, $dbpass, 'mysql' ); |
| 48 | +$res = $stmt->run_mysqli( $mysqli ); |
| 49 | +$array = $res->get_result(); |
| 50 | +$mysqli->close(); |
| 51 | + |
| 52 | +var_dump( $res ); |
| 53 | +var_dump( $array->fetch_all() ); |
| 54 | + |
| 55 | +function getCompiledSQL( $columns, $parameter ) : CompiledStatement { |
| 56 | + return select( tupleArray( array_map( function( $column ) { return id( $column ); }, $columns ) ), |
| 57 | + from( id( 'tables' ) ), |
| 58 | + where( andExpr( operator( id( 'table_name' ), '>=', str( $parameter ) ) ) ), |
| 59 | + order_by( tuple( id( 'table_name' ), id( 'create_time' ) ) ), |
| 60 | + limit( tuple( num( 0 ), num( 10 ) ) ) |
| 61 | + )->compile(); |
| 62 | +} |
| 63 | +$mysqli = new mysqli( 'localhost', $dbuser, $dbpass, 'information_schema' ); |
| 64 | +$columns = [ 'table_schema', 'table_name', 'engine', 'table_rows', 'create_time' ]; |
| 65 | +$parameter = 'h'; |
| 66 | +$stmt = getCompiledSQL( $columns, $parameter ); |
| 67 | +echo showSQLStatement( $stmt ) . "\n"; |
| 68 | +$res = $stmt->run_mysqli( $mysqli ); |
| 69 | +$array = $res->get_result(); |
| 70 | +$mysqli->close(); |
| 71 | + |
| 72 | +var_dump( $res ); |
| 73 | +var_dump( $array->fetch_all() ); |
| 74 | + |
| 75 | +unset( $dbuser ); |
| 76 | +unset( $dbpass ); |
0 commit comments