Skip to content

Commit 14e77cb

Browse files
committed
Added an example.
1 parent 0a37bec commit 14e77cb

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SQLTrees (php)
22
==============
33

4-
This is a PHP version of the Java library [SQLTrees](https://github.com/Orange-Cyberdefense/sqltrees) aiming at annihilating (possibility of) SQL injections and at the same time keeping the whole expressiveness of SQL. You can read (in French) [this introductory article](https://connect.ed-diamond.com/MISC/misc-111/zero-sqli-malgre-les-developpeurs), or read below a summary.
4+
This is a PHP version of the Java library [SQLTrees](https://github.com/Orange-Cyberdefense/sqltrees) aiming at annihilating (possibility of) SQL injections and at the same time keeping the whole expressiveness of SQL. You can read (in French) [this introductory article](https://connect.ed-diamond.com/MISC/misc-111/zero-sqli-malgre-les-developpeurs), check out [the example](https://githu.com/Seb35/sqltrees-php/tree/main/examples), or read below a summary.
55

66

77
Example

examples/test.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)