2 SQLite3 user authorizer callback
4 <?php require_once(__DIR__ . '/skipif.inc'); ?>
8 $db = new SQLite3(':memory:');
9 $db->enableExceptions(true);
11 $db->setAuthorizer(function (int $action) {
12 if ($action == SQLite3::SELECT) {
19 // This query should be accepted
20 var_dump($db->querySingle('SELECT 1;'));
23 // This one should fail
24 var_dump($db->querySingle('CREATE TABLE test (a, b);'));
25 } catch (\Exception $e) {
26 echo $e->getMessage() . "\n";
29 // Test disabling the authorizer
30 $db->setAuthorizer(null);
32 // This should now succeed
33 var_dump($db->exec('CREATE TABLE test (a); INSERT INTO test VALUES (42);'));
34 var_dump($db->querySingle('SELECT a FROM test;'));
36 // Test if we are getting the correct arguments
37 $db->setAuthorizer(function (int $action) {
38 $constants = (new ReflectionClass('SQLite3'))->getConstants();
39 $constants = array_flip($constants);
41 var_dump($constants[$action], implode(',', array_slice(func_get_args(), 1)));
45 var_dump($db->exec('SELECT * FROM test WHERE a = 42;'));
46 var_dump($db->exec('DROP TABLE test;'));
48 // Try to return something invalid from the authorizer
49 $db->setAuthorizer(function () {
54 var_dump($db->querySingle('SELECT 1;'));
55 } catch (\Exception $e) {
56 echo $e->getMessage() . "\n";
57 echo $e->getPrevious()->getMessage() . "\n";
60 $db->setAuthorizer(function () {
65 var_dump($db->querySingle('SELECT 1;'));
66 } catch (\Exception $e) {
67 echo $e->getMessage() . "\n";
68 echo $e->getPrevious()->getMessage() . "\n";
74 Unable to prepare statement: 23, not authorized
80 string(12) "test,a,main,"
82 string(12) "test,a,main,"
85 string(20) "sqlite_master,,main,"
86 string(10) "DROP_TABLE"
87 string(11) "test,,main,"
89 string(11) "test,,main,"
91 string(20) "sqlite_master,,main,"
93 string(28) "sqlite_master,tbl_name,main,"
95 string(24) "sqlite_master,type,main,"
97 string(28) "sqlite_master,rootpage,main,"
99 string(28) "sqlite_master,rootpage,main,"
101 Unable to prepare statement: 23, not authorized
102 The authorizer callback returned an invalid type: expected int
103 Unable to prepare statement: 23, not authorized
104 The authorizer callback returned an invalid value