php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #53713
Patch session_sqlite3 revision 2011-08-01 05:13 UTC by crrodriguez at opensuse dot org
Patch bugs-53713-sqlite3-session-win32-plus-test revision 2011-05-02 12:07 UTC by jinmoku at hotmail dot com
revision 2011-05-02 12:03 UTC by jinmoku at hotmail dot com
Patch bugs-53713-sqlite3-session.patch revision 2011-05-02 12:05 UTC by jinmoku at hotmail dot com
revision 2011-04-30 02:04 UTC by jinmoku at hotmail dot com

Patch session_sqlite3 for SQLite related Bug #53713

Patch version 2011-08-01 05:13 UTC

Return to Bug #53713 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: crrodriguez@opensuse.org

Index: ext/sqlite3/config.w32
===================================================================
--- ext/sqlite3/config.w32	(revisión: 313991)
+++ ext/sqlite3/config.w32	(copia de trabajo)
@@ -5,7 +5,7 @@
 
 if (PHP_SQLITE3 != "no") {
 	ADD_FLAG("CFLAGS_SQLITE3", "/D SQLITE_THREADSAFE=" + (PHP_ZTS == "yes" ? "1" : "0") + " /D SQLITE_ENABLE_FTS3=1 /D SQLITE_ENABLE_COLUMN_METADATA=1 /D SQLITE_CORE=1 ");
-	EXTENSION("sqlite3", "sqlite3.c", null, "/I" + configure_module_dirname + "/libsqlite /I" + configure_module_dirname);
+	EXTENSION("sqlite3", "sqlite3.c sess_sqlite3.c", null, "/I" + configure_module_dirname + "/libsqlite /I" + configure_module_dirname);
 
 	ADD_SOURCES(configure_module_dirname + "/libsqlite", "sqlite3.c", "sqlite3");
 
Index: ext/sqlite3/sess_sqlite3.c
===================================================================
--- ext/sqlite3/sess_sqlite3.c	(revisión: 0)
+++ ext/sqlite3/sess_sqlite3.c	(revisión: 0)
@@ -0,0 +1,190 @@
+/* 
+   +----------------------------------------------------------------------+
+   | PHP Version 5                                                        |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-2011 The PHP Group                                |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 3.01 of the PHP license,      |
+   | that is bundled with this package in the file LICENSE, and is        |
+   | available through the world-wide-web at the following url:           |
+   | http://www.php.net/license/3_01.txt                                  |
+   | If you did not receive a copy of the PHP license and are unable to   |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@php.net so we can mail you a copy immediately.               |
+   +----------------------------------------------------------------------+
+   | Authors: John Coggeshall <john@php.net>                              |
+   |          Wez Furlong <wez@thebrainroom.com>                          |
+   |          Stealth35 <jinmoku@hotmail.com>                             |
+   +----------------------------------------------------------------------+
+ */
+
+/* $Id$ */
+
+#include "php.h"
+
+#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
+
+#include "ext/session/php_session.h"
+#include "ext/standard/php_lcg.h"
+#include <sqlite3.h>
+#define SQLITE_RETVAL(__r) ((__r) == SQLITE_OK ? SUCCESS : FAILURE)
+#define PS_SQLITE_DATA sqlite3 *db = (sqlite3*)PS_GET_MOD_DATA()
+
+PS_FUNCS(sqlite3);
+
+extern ps_module ps_mod_sqlite3 = {
+	PS_MOD(sqlite3)
+};
+
+PS_OPEN_FUNC(sqlite3) 
+{
+	int rc;
+	sqlite3 *db;
+
+	/* TODO: do we need a safe_mode check here? */
+	rc = sqlite3_open(save_path, &db);
+	if (rc) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, 
+				"SQLite: failed to open/create session database `%s' - %s", save_path, sqlite3_errmsg(db));
+		return FAILURE;
+	}
+
+	/* allow up to 1 minute when busy */
+	sqlite3_busy_timeout(db, 60000);
+
+	sqlite3_exec(db, "PRAGMA default_synchronous = OFF", NULL, NULL, NULL);
+	sqlite3_exec(db, "PRAGMA count_changes = OFF", NULL, NULL, NULL);
+
+	/* This will fail if the table already exists, but that's not a big problem. I'm
+	   unclear as to how to check for a table's existence in SQLite -- that would be better here. */
+	sqlite3_exec(db, 
+	    "CREATE TABLE IF NOT EXISTS session_data ("
+	    "    sess_id PRIMARY KEY," 
+	    "    value TEXT, "
+	    "    updated INTEGER "
+	    ")", NULL, NULL, NULL);
+
+	PS_SET_MOD_DATA(db);
+
+	return SUCCESS;
+}
+
+PS_CLOSE_FUNC(sqlite3) 
+{
+	PS_SQLITE_DATA;
+
+	sqlite3_close(db);
+
+	return SUCCESS;
+}
+
+PS_READ_FUNC(sqlite3) 
+{
+	PS_SQLITE_DATA;
+	char *query;
+	const char *tail;
+	sqlite3_stmt *stmt;
+	int result;
+	const char *rowdata;
+
+	*val = NULL;
+	
+	query = sqlite3_mprintf("SELECT value FROM session_data WHERE sess_id='%q' LIMIT 1", key);
+	if (query == NULL) {
+		/* no memory */
+		return FAILURE;
+	}
+
+	if (sqlite3_prepare(db, query, strlen(query) + 1, &stmt, &tail) != SQLITE_OK) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLite: Could not compile session read query: %s", sqlite3_errmsg(db));
+		sqlite3_free(query);
+		return FAILURE;
+	}
+
+	result = sqlite3_step(stmt);
+	if(result == SQLITE_ROW) {
+		*vallen = sqlite3_column_bytes(stmt, 0);
+		rowdata  = sqlite3_column_text(stmt, 0);
+		if (*vallen) {
+			*val = emalloc(*vallen);
+			memcpy(*val, rowdata, *vallen);
+			(*val)[*vallen] = '\0';
+		} else {
+			*val = STR_EMPTY_ALLOC();
+		}
+	}
+	
+	if (SQLITE_OK != sqlite3_finalize(stmt)) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLite: session read: error %s", sqlite3_errmsg(db));
+	}
+
+	sqlite3_free(query);
+	
+	return *val == NULL ? FAILURE : SUCCESS;
+}
+
+PS_WRITE_FUNC(sqlite3) 
+{
+	PS_SQLITE_DATA;
+	char *error;
+	char *query;
+	time_t t;
+	int rv;
+	
+	t = time(NULL);
+	
+	query = sqlite3_mprintf("REPLACE INTO session_data VALUES('%q', '%q', %d)", key, val, t);
+	rv = sqlite3_exec(db, query, NULL, NULL, &error);
+	if (rv != SQLITE_OK) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLite: session write query failed: %s", error);
+		sqlite3_free(error);
+	}
+	sqlite3_free(query);
+
+	return SQLITE_RETVAL(rv);
+}
+
+PS_DESTROY_FUNC(sqlite3) 
+{
+	char *query;
+	int rv;
+	PS_SQLITE_DATA;
+
+	query = sqlite3_mprintf("DELETE FROM session_data WHERE sess_id='%q'", key);
+	rv = sqlite3_exec(db, query, NULL, NULL, NULL);
+sqlite3_free(query);
+	
+	return SQLITE_RETVAL(rv);
+}
+
+PS_GC_FUNC(sqlite3) 
+{
+	PS_SQLITE_DATA;
+	char *query;
+	int rv;
+	time_t t = time(NULL);
+
+	query = sqlite3_mprintf("DELETE FROM session_data WHERE (%d - updated) > %d", t, maxlifetime);
+	rv = sqlite3_exec(db, query,	NULL, NULL, NULL);
+	sqlite3_free(query);
+
+	/* because SQLite does not actually clear the deleted data from the database 
+	 * we need to occassionaly do so manually to prevent the sessions database 
+	 * from growing endlessly.
+	 */
+	if ((int) ((float) PS(gc_divisor) * PS(gc_divisor) * php_combined_lcg(TSRMLS_C)) < PS(gc_probability)) {
+		rv = sqlite3_exec(db, "VACUUM", NULL, NULL, NULL);
+	}
+	return SQLITE_RETVAL(rv);
+}
+
+#endif /* HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION) */
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
\ No hay ningún carácter de nueva línea al final del fichero
Index: ext/sqlite3/config0.m4
===================================================================
--- ext/sqlite3/config0.m4	(revisión: 313991)
+++ ext/sqlite3/config0.m4	(copia de trabajo)
@@ -85,7 +85,7 @@
 
   AC_DEFINE(HAVE_SQLITE3,1,[ ])
 
-  sqlite3_sources="sqlite3.c $sqlite3_extra_sources"
+  sqlite3_sources="sqlite3.c sess_sqlite3.c $sqlite3_extra_sources"
 
   PHP_NEW_EXTENSION(sqlite3, $sqlite3_sources, $ext_shared,,$PHP_SQLITE3_CFLAGS)
   PHP_ADD_BUILD_DIR([$ext_builddir/libsqlite])
Index: ext/sqlite3/sqlite3.c
===================================================================
--- ext/sqlite3/sqlite3.c	(revisión: 313991)
+++ ext/sqlite3/sqlite3.c	(copia de trabajo)
@@ -25,6 +25,10 @@
 #include "php.h"
 #include "php_ini.h"
 #include "ext/standard/info.h"
+#include "ext/standard/info.h"
+#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
+#include "ext/session/php_session.h"
+#endif
 #include "php_sqlite3.h"
 #include "php_sqlite3_structs.h"
 #include "main/SAPI.h"
@@ -38,6 +42,12 @@
 ZEND_DECLARE_MODULE_GLOBALS(sqlite3)
 
 static PHP_GINIT_FUNCTION(sqlite3);
+
+#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
+extern ps_module ps_mod_sqlite3;
+#define ps_sqlite3_ptr &ps_mod_sqlite3
+#endif
+
 static int php_sqlite3_authorizer(void *autharg, int access_type, const char *arg3, const char *arg4, const char *arg5, const char *arg6);
 static void sqlite3_param_dtor(void *data);
 static int php_sqlite3_compare_stmt_zval_free(php_sqlite3_free_list **free_list, zval *statement);
@@ -2120,6 +2130,10 @@
 	php_sqlite3_result_entry = zend_register_internal_class(&ce TSRMLS_CC);
 
 	REGISTER_INI_ENTRIES();
+	
+#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
+	php_session_register_module(ps_sqlite3_ptr);
+#endif
 
 	REGISTER_LONG_CONSTANT("SQLITE3_ASSOC", PHP_SQLITE3_ASSOC, CONST_CS | CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("SQLITE3_NUM", PHP_SQLITE3_NUM, CONST_CS | CONST_PERSISTENT);
@@ -2171,6 +2185,16 @@
 }
 /* }}} */
 
+/* {{{ sqlite3_module_dep
+*/
+static const zend_module_dep sqlite3_deps[] = {
+#if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
+	ZEND_MOD_REQUIRED("session")
+#endif
+	{NULL, NULL, NULL}
+};
+/* }}} */
+
 /* {{{ sqlite3_module_entry
 */
 zend_module_entry sqlite3_module_entry = {
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Dec 06 14:01:25 2019 UTC