From 2103275301f0f18f5db6c3e9951b93a807f92f15 Mon Sep 17 00:00:00 2001 From: Anton Batenev Date: Tue, 21 May 2013 20:21:17 +0400 Subject: [PATCH] Fix build script and add usage example php script --- build.sh | 49 ++++++++++++++++++++++------------ config.m4 | 4 +-- example.php | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 example.php diff --git a/build.sh b/build.sh index 1f87206..49f7d5e 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,26 @@ #!/bin/sh +# +# SQLite3 compile options +# + +CFLAGS=" \ + -DSQLITE_HAS_CODEC \ + -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT \ + -DSQLITE_ENABLE_COLUMN_METADATA \ + -DSQLITE_ENABLE_STAT3 \ + -DSQLITE_ENABLE_RTREE \ + -DSQLITE_ENABLE_FTS3 \ + -DSQLITE_ENABLE_FTS3_PARENTHESIS \ + -DSQLITE_ENABLE_FTS4 \ + -DSQLITE_SECURE_DELETE \ + -DSQLITE_ENABLE_ICU \ + -DSQLITE_SOUNDEX \ + -DSQLITE_DEFAULT_FOREIGN_KEYS=1 \ + -I/usr/local/include" + +LDFLAGS="-lcrypto -licuuc -licui18n -L/usr/local/lib" + # # Get PHP source code (installed version) # @@ -62,21 +83,8 @@ if [ ! -f "${SQLCIPHER_SRC}/sqlite3.c" ]; then ./configure \ --disable-shared \ --enable-tempstore=yes \ - CFLAGS=" \ - -DSQLITE_HAS_CODEC \ - -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT \ - -DSQLITE_ENABLE_COLUMN_METADATA \ - -DSQLITE_ENABLE_STAT3 \ - -DSQLITE_ENABLE_RTREE \ - -DSQLITE_ENABLE_FTS3 \ - -DSQLITE_ENABLE_FTS3_PARENTHESIS \ - -DSQLITE_ENABLE_FTS4 \ - -DSQLITE_SECURE_DELETE \ - -DSQLITE_ENABLE_ICU \ - -DSQLITE_SOUNDEX \ - -DSQLITE_DEFAULT_FOREIGN_KEYS=1 \ - -I/usr/local/include" \ - LDFLAGS="-lcrypto -licuuc -licui18n -L/usr/local/lib" + CFLAGS="${CFLAGS}" \ + LDFLAGS="${LDFLAGS}" if [ $? -ne 0 ]; then exit $? fi @@ -148,7 +156,12 @@ done # Build pdo_sqlcipher # -cp -r "${SQLCIPHER_SRC}" "${BUILD_DIR}/sqlcipher" +cp "${SQLCIPHER_SRC}/sqlite3.c" "${BUILD_DIR}/sqlite3.c" +if [ $? -ne 0 ]; then + exit $? +fi + +cp "${SQLCIPHER_SRC}/sqlite3.h" "${BUILD_DIR}/sqlite3.h" if [ $? -ne 0 ]; then exit $? fi @@ -170,7 +183,9 @@ if [ $? -ne 0 ]; then exit $? fi -./configure +./configure \ + CFLAGS="${CFLAGS}" \ + LDFLAGS="${LDFLAGS}" if [ $? -ne 0 ]; then exit $? fi diff --git a/config.m4 b/config.m4 index 5201228..55c541e 100644 --- a/config.m4 +++ b/config.m4 @@ -25,9 +25,9 @@ if test "$PHP_PDO_SQLCIPHER" != "no"; then fi AC_MSG_RESULT($pdo_inc_path) - php_pdo_sqlcipher_sources_core="pdo_sqlcipher.c sqlite_driver.c sqlite_statement.c sqlcipher/sqlite3.c" + php_pdo_sqlcipher_sources_core="pdo_sqlcipher.c sqlite_driver.c sqlite_statement.c sqlite3.c" - PHP_NEW_EXTENSION(pdo_sqlcipher, $php_pdo_sqlcipher_sources_core, $ext_shared,,-I$pdo_inc_path -Isqlcipher) + PHP_NEW_EXTENSION(pdo_sqlcipher, $php_pdo_sqlcipher_sources_core, $ext_shared,,-I$pdo_inc_path) ifdef([PHP_ADD_EXTENSION_DEP], [ diff --git a/example.php b/example.php new file mode 100644 index 0000000..6ce9f64 --- /dev/null +++ b/example.php @@ -0,0 +1,75 @@ +getMessage() . PHP_EOL); + } + + return $pdo; +} + +// +// Create new example database with one table +// (it can be done through sqlcipher command line client) +// + +$sql = "PRAGMA key = '$password'; +PRAGMA encoding = \"UTF-8\"; +PRAGMA auto_vacuum = 2; +PRAGMA incremental_vacuum(10); + +CREATE TABLE `test` ( + `id` INTEGER NOT NULL PRIMARY KEY, + `value` TEXT NOT NULL +);"; + +$pdo = getPDO($database); + +if ($pdo->exec($sql) === false) { + $error = $pdo->errorInfo(); + die($error[0] . ": " . $error[2] . PHP_EOL); +} + +// +// Use example database +// + +// set encryption key before any sql command +$sql = "PRAGMA key = '$password'"; +$pdo = getPDO($database); + +if ($pdo->exec($sql) === false) { + $error = $pdo->errorInfo(); + die($error[0] . ": " . $error[2] . PHP_EOL); +} + +// insert rows +$sql = "INSERT INTO `test` VALUES (1, 'value1'); +INSERT INTO `test` VALUES (2, 'value2'); +INSERT INTO `test` VALUES (3, 'value3');"; + +if ($pdo->exec($sql) === false) { + $error = $pdo->errorInfo(); + die($error[0] . ": " . $error[2] . PHP_EOL); +} + +// select rows +$result = $pdo->query("SELECT * FROM `test`"); + +if ($result === false) { + $error = $pdo->errorInfo(); + die($error[0] . ": " . $error[2] . PHP_EOL); +} + +foreach ($result as $row) { + print_r($row); +}