cvs: pear /PECL/session_pgsql .cvsignore Makefile.in README config.m4 session_pgsql.c session_pgsql.h setup.stub
| From: | Yasuo Ohgaki | Date: | Sat, 22 Dec 2001 00:32:58 +0000 |
| Subject: | cvs: pear /PECL/session_pgsql .cvsignore Makefile.in README config.m4 session_pgsql.c session_pgsql.h setup.stub | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-1650@lists.php.net to get a copy of this message | ||
yohgaki Fri Dec 21 19:32:58 2001 EDT
Added files:
/pear/PECL/session_pgsql .cvsignore Makefile.in README config.m4
session_pgsql.c session_pgsql.h setup.stub
Log:
PostgreSQL session save handler added
# There are little problem in session module which is parent of this module
# This module does not work well until it is fixed ;)
@ PostgreSQL session save hanlder added
Index: pear/PECL/session_pgsql/.cvsignore +++ pear/PECL/session_pgsql/.cvsignore Makefile *.lo *.la libs deps libs.mk *.plg *.opt *.ncb Release Release_inline Debug Release_TS Release_TSDbg Release_TS_inline Debug_TS Index: pear/PECL/session_pgsql/Makefile.in +++ pear/PECL/session_pgsql/Makefile.in LTLIBRARY_NAME = libsession_pgsql.la LTLIBRARY_SOURCES = session_pgsql.c LTLIBRARY_SHARED_NAME = session_pgsql.la LTLIBRARY_SHARED_LIBADD = $(SESSION_SHARED_LIBADD) include $(top_srcdir)/build/dynlib.mk Index: pear/PECL/session_pgsql/config.m4 +++ pear/PECL/session_pgsql/config.m4 dnl dnl $Id: config.m4,v 1.1 2001/12/22 00:32:58 yohgaki Exp $ dnl PHP_ARG_WITH(session-pgsql,for pgsql sesssion support, [ --with-session-pgsql[=DIR] Include pgsql(PostgreSQL) support for session storage]) if test "$PHP_SESSION_PGSQL" != "no"; then if test "$PHP_SESSION_PGSQL" = "yes"; then PGSQL_SEARCH_PATHS="/usr /usr/local /usr/local/pgsql" else PGSQL_SEARCH_PATHS=$PHP_SESSION_PGSQL fi for i in $PGSQL_SEARCH_PATHS; do for j in include include/pgsql include/postgres include/postgresql ""; do if test -r "$i/$j/libpq-fe.h"; then PGSQL_INC_BASE=$i PGSQL_INCLUDE=$i/$j fi done for j in lib lib/pgsql lib/postgres lib/postgresql ""; do if test -f "$i/$j/libpq.so"; then PGSQL_LIBDIR=$i/$j fi done done if test -z "$PGSQL_INCLUDE"; then AC_MSG_ERROR(Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path) fi if test -z "$PGSQL_LIBDIR"; then AC_MSG_ERROR(Cannot find libpq.so. Please specify correct PostgreSQL installation path) fi if test -z "$PGSQL_INCLUDE" -a -z "$PGSQL_LIBDIR" ; then AC_MSG_ERROR([Unable to find libpq anywhere under $withval]) fi PHP_ADD_LIBRARY_WITH_PATH(pq, $PGSQL_LIBDIR, SESSION_SHARED_LIBADD) PHP_ADD_INCLUDE($PGSQL_INCLUDE) AC_DEFINE(HAVE_LIBPQ, 1, [Whether you have libpq]) PHP_MODULE_PTR(phpext_ps_pgsql_ptr) PHP_EXTENSION(session_pgsql,$ext_shared) fi Index: pear/PECL/session_pgsql/session_pgsql.c +++ pear/PECL/session_pgsql/session_pgsql.c /* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2002 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.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: Yasuo Ohgaki <yohgaki@php.net> | +----------------------------------------------------------------------+ */ /* $Id: session_pgsql.c,v 1.1 2001/12/22 00:32:58 yohgaki Exp $ */ #include <time.h> #include "config.h" #include "php.h" #include "ext/session/php_session.h" #ifdef HAVE_LIBPQ #include "session_pgsql.h" /* mod_data cannot be null. Just a dummy for mod_data. mod_pgsql don't use mod_data for thread safety. */ static char* dummy = "dummy"; static ps_module ps_mod_pgsql = { PS_MOD(pgsql) }; #ifdef ZTS int ps_pgsql_globals_id; #else php_ps_pgsql_globals ps_pgsql_globals; #endif #define PS_PGSQL_DATA ps_pgsql *data = PS_GET_MOD_DATA() #define QUERY_BUF_SIZE 250 PHP_MINIT_FUNCTION(session_pgsql); PHP_RINIT_FUNCTION(session_pgsql); PHP_MSHUTDOWN_FUNCTION(session_pgsql); PHP_RSHUTDOWN_FUNCTION(session_pgsql); PHP_MINFO_FUNCTION(session_pgsql); zend_module_entry session_pgsql_module_entry = { STANDARD_MODULE_HEADER, "session_pgsql", NULL, PHP_MINIT(session_pgsql),PHP_MSHUTDOWN(session_pgsql), NULL, PHP_RSHUTDOWN(session_pgsql), NULL, "0.1", STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_SESSION_PGSQL ZEND_GET_MODULE(session_pgsql) #endif PHP_MINIT_FUNCTION(session_pgsql) { #ifdef ZTS php_ps_pgsql_globals *ps_pgsql_globals; ts_allocate_id(&ps_pgsql_globals_id, sizeof(php_ps_pgsql_globals), NULL, NULL); ps_pgsql_globals = ts_resource(ps_pgsql_globals_id); #endif PS_PGSQL(pgsql_link) = PQconnectdb(PS(save_path)); if (!PS_PGSQL(pgsql_link) || PQstatus(PS_PGSQL(pgsql_link)) != CONNECTION_OK) { PQfinish(PS_PGSQL(pgsql_link)); php_error(E_NOTICE, "Session pgsql cannot connect to PostgreSQL server"); } php_error(E_NOTICE, "Session save handler registering: %s",ps_mod_pgsql.name); php_session_register_module(&ps_mod_pgsql); return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(session_pgsql) { /* link is closed at shutdown */ PQfinish(PS_PGSQL(pgsql_link)); PS_PGSQL(pgsql_link) = NULL; return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(session_pgsql) { PGresult *pg_result; int ret = SUCCESS; /* clean up link just in case */ pg_result = PQexec(PS_PGSQL(pgsql_link), "BEGIN;END;"); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { ret = FAILURE; } PQclear(pg_result); return ret; } static int ps_pgsql_valid_str(const char *key) { size_t len; const char *p; char c; int ret = 1; for (p = key; (c = *p); p++) { /* valid characters are a..z,A..Z,0..9,_ */ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_'))) { ret = 0; break; } } len = p - key; if (len != 32) ret = 0; return ret; } PS_OPEN_FUNC(pgsql) { PGresult *pg_result; int ret = SUCCESS; *mod_data = (void *) dummy; /* mod_data cannot be NULL */ if (!session_name && !ps_pgsql_valid_str(session_name)) { ret = FAILURE; } if (PQstatus(PS_PGSQL(pgsql_link)) != CONNECTION_OK) { /* link is bad... Try again */ PQreset(PS_PGSQL(pgsql_link)); if (PQstatus(PS_PGSQL(pgsql_link)) != CONNECTION_OK) { PQfinish((PS_PGSQL(pgsql_link))); ret = FAILURE; } } else { pg_result = PQexec(PS_PGSQL(pgsql_link), "BEGIN;"); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { ret = FAILURE; } PQclear(pg_result); } return ret; } PS_CLOSE_FUNC(pgsql) { PGresult *pg_result; int ret = SUCCESS; PS_SET_MOD_DATA(NULL); /* It's safe to assign NULL */ if (PQstatus(PS_PGSQL(pgsql_link)) == CONNECTION_BAD) { ret = FAILURE; } else { pg_result = PQexec(PS_PGSQL(pgsql_link), "END;"); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { ret = FAILURE; } PQclear(pg_result); } return SUCCESS; } PS_READ_FUNC(pgsql) { PGresult *pg_result; char query[QUERY_BUF_SIZE+1]; char *query_tpl = "SELECT sess_modified, sess_data FROM php_session WHERE sess_id = '%s';"; char *modified; size_t exp; size_t mod; int ret = FAILURE; ExecStatusType status; PS_PGSQL(sess_new) = 0; PS_PGSQL(sess_del) = 0; *vallen = 0; if (ps_pgsql_valid_str(key)) { snprintf(query, QUERY_BUF_SIZE, query_tpl, key); pg_result = PQexec(PS_PGSQL(pgsql_link), query); status = PQresultStatus(pg_result); if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) { if (PQntuples(pg_result) == 0) { PS_PGSQL(sess_new) = 1; } else { exp = time(NULL) - PS(gc_maxlifetime); modified = PQgetvalue(pg_result, 0, 0); mod = (size_t)atoi(modified); if ((mod < exp)) { PS_PGSQL(sess_new) = 1; PS_PGSQL(sess_del) = 1; } else { /* PQgetvalue reuturns "" for NULL */ *val = PQgetvalue(pg_result, 0, 1); if (*val) { *vallen = strlen(*val); *val = safe_estrndup(*val, *vallen); } } } ret = SUCCESS; } else { php_error(E_WARNING,"Session pgsql query filed: %s", PQerrorMessage(PS_PGSQL(pgsql_link))); ret = FAILURE; } PQclear(pg_result); } if (*vallen == 0) { *val = safe_estrndup("",0); } return ret; } PS_WRITE_FUNC(pgsql) { PGresult *pg_result; size_t query_len; char *query; char *query_delete = "DELETE FROM php_session WHERE sess_id = '%s';"; char *query_insert = "INSERT INTO php_session (sess_id, sess_created, sess_modified, sess_data) " "VALUES ('%s', %d, %d, '%s');"; char *query_update = "UPDATE php_session SET sess_data = '%s', sess_modified = %d " "WHERE sess_id = '%s';"; int ret = FAILURE; if (PS_PGSQL(sess_del)) { /* DELETE */ query_len = strlen(query_delete)+strlen(key); query = emalloc(query_len+1); sprintf(query, query_delete, key); pg_result = PQexec(PS_PGSQL(pgsql_link), query); PQclear(pg_result); efree(query); } if (PS_PGSQL(sess_new)) { /* INSERT */ query_len = strlen(query_insert)+vallen+strlen(key)+100; query = emalloc(query_len+1); sprintf(query, query_insert, key, time(NULL), time(NULL), val); pg_result = PQexec(PS_PGSQL(pgsql_link), query); if (PQresultStatus(pg_result) == PGRES_COMMAND_OK) { ret = SUCCESS; } } else { /* UPDATE */ query_len = strlen(query_update)+strlen(key)+vallen+100; query = emalloc(query_len+1); sprintf(query, query_update, val, time(NULL), key); pg_result = PQexec(PS_PGSQL(pgsql_link), query); if (PQresultStatus(pg_result) == PGRES_COMMAND_OK) { ret = SUCCESS; } } PQclear(pg_result); efree(query); return ret; } PS_DESTROY_FUNC(pgsql) { PGresult *pg_result; size_t query_len; char *query; char *query_update = "DELETE FROM php_session WHERE sess_id = '%s';"; int ret = FAILURE; if (ps_pgsql_valid_str(key)) { query_len = strlen(query_update)+strlen(key); query = (char *)emalloc(query_len+1); snprintf(query, query_len, key); pg_result = PQexec(PS_PGSQL(pgsql_link), query); if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) { ret = SUCCESS; } PQclear(pg_result); efree(query); } return ret; } PS_GC_FUNC(pgsql) { PGresult *pg_result; char query[QUERY_BUF_SIZE+1]; char *query_update = "DELETE FROM php_session WHERE sess_modified < %d"; int ret = FAILURE; sprintf(query, query_update, time(NULL) - maxlifetime); pg_result = PQexec(PS_PGSQL(pgsql_link), query); if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) { *nrdels = atoi(PQcmdTuples(pg_result)); ret = SUCCESS; } PQclear(pg_result); return ret; } #endif /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */ Index: pear/PECL/session_pgsql/session_pgsql.h +++ pear/PECL/session_pgsql/session_pgsql.h /* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2002 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.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: yohgaki@php.net | +----------------------------------------------------------------------+ */ #ifndef MOD_PGSQL_H #define MOD_PGSQL_H #ifdef HAVE_LIBPQ #include "config.h" #include "ext/session/php_session.h" #include <libpq-fe.h> extern ps_module ps_mod_pgsql; #define ps_pgsql_ptr &ps_mod_pgsql extern zend_module_entry php_session_pgsql_module; #define phpext_ps_pgsql_ptr &php_session_pgsql_module PS_FUNCS(pgsql); typedef struct _php_ps_pgsql_globals { PGconn *pgsql_link; int sess_new; int sess_del; } php_ps_pgsql_globals; #ifdef ZTS extern int ps_pgsql_globals_id; #else extern php_ps_pgsql_globals ps_pgsql_globals; #endif #ifdef ZTS #define PS_PGSQL(v) TSRMG(ps_pgsql_globals_id, php_ps_pgsql_globals *, v) #else #define PS_PGSQL(v) (ps_pgsql_globals.v) #endif #else #define ps_pgsql_ptr NULL #define phpext_ps_pgsql_ptr NULL #endif #endif Index: pear/PECL/session_pgsql/setup.stub +++ pear/PECL/session_pgsql/setup.stub # $Source: /usr/repository/pear/PECL/session_pgsql/setup.stub,v $ # $Id: setup.stub,v 1.1 2001/12/22 00:32:58 yohgaki Exp $ define_option with-session-pgsql 'session pgsql support?' yesnodir yes \ ' Whether to build the session pgsql extension.'
Index: pear/PECL/session_pgsql/.cvsignore +++ pear/PECL/session_pgsql/.cvsignore Makefile *.lo *.la libs deps libs.mk *.plg *.opt *.ncb Release Release_inline Debug Release_TS Release_TSDbg Release_TS_inline Debug_TS Index: pear/PECL/session_pgsql/Makefile.in +++ pear/PECL/session_pgsql/Makefile.in LTLIBRARY_NAME = libsession_pgsql.la LTLIBRARY_SOURCES = session_pgsql.c LTLIBRARY_SHARED_NAME = session_pgsql.la LTLIBRARY_SHARED_LIBADD = $(SESSION_SHARED_LIBADD) include $(top_srcdir)/build/dynlib.mk Index: pear/PECL/session_pgsql/config.m4 +++ pear/PECL/session_pgsql/config.m4 dnl dnl $Id: config.m4,v 1.1 2001/12/22 00:32:58 yohgaki Exp $ dnl PHP_ARG_WITH(session-pgsql,for pgsql sesssion support, [ --with-session-pgsql[=DIR] Include pgsql(PostgreSQL) support for session storage]) if test "$PHP_SESSION_PGSQL" != "no"; then if test "$PHP_SESSION_PGSQL" = "yes"; then PGSQL_SEARCH_PATHS="/usr /usr/local /usr/local/pgsql" else PGSQL_SEARCH_PATHS=$PHP_SESSION_PGSQL fi for i in $PGSQL_SEARCH_PATHS; do for j in include include/pgsql include/postgres include/postgresql ""; do if test -r "$i/$j/libpq-fe.h"; then PGSQL_INC_BASE=$i PGSQL_INCLUDE=$i/$j fi done for j in lib lib/pgsql lib/postgres lib/postgresql ""; do if test -f "$i/$j/libpq.so"; then PGSQL_LIBDIR=$i/$j fi done done if test -z "$PGSQL_INCLUDE"; then AC_MSG_ERROR(Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path) fi if test -z "$PGSQL_LIBDIR"; then AC_MSG_ERROR(Cannot find libpq.so. Please specify correct PostgreSQL installation path) fi if test -z "$PGSQL_INCLUDE" -a -z "$PGSQL_LIBDIR" ; then AC_MSG_ERROR([Unable to find libpq anywhere under $withval]) fi PHP_ADD_LIBRARY_WITH_PATH(pq, $PGSQL_LIBDIR, SESSION_SHARED_LIBADD) PHP_ADD_INCLUDE($PGSQL_INCLUDE) AC_DEFINE(HAVE_LIBPQ, 1, [Whether you have libpq]) PHP_MODULE_PTR(phpext_ps_pgsql_ptr) PHP_EXTENSION(session_pgsql,$ext_shared) fi Index: pear/PECL/session_pgsql/session_pgsql.c +++ pear/PECL/session_pgsql/session_pgsql.c /* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2002 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.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: Yasuo Ohgaki <yohgaki@php.net> | +----------------------------------------------------------------------+ */ /* $Id: session_pgsql.c,v 1.1 2001/12/22 00:32:58 yohgaki Exp $ */ #include <time.h> #include "config.h" #include "php.h" #include "ext/session/php_session.h" #ifdef HAVE_LIBPQ #include "session_pgsql.h" /* mod_data cannot be null. Just a dummy for mod_data. mod_pgsql don't use mod_data for thread safety. */ static char* dummy = "dummy"; static ps_module ps_mod_pgsql = { PS_MOD(pgsql) }; #ifdef ZTS int ps_pgsql_globals_id; #else php_ps_pgsql_globals ps_pgsql_globals; #endif #define PS_PGSQL_DATA ps_pgsql *data = PS_GET_MOD_DATA() #define QUERY_BUF_SIZE 250 PHP_MINIT_FUNCTION(session_pgsql); PHP_RINIT_FUNCTION(session_pgsql); PHP_MSHUTDOWN_FUNCTION(session_pgsql); PHP_RSHUTDOWN_FUNCTION(session_pgsql); PHP_MINFO_FUNCTION(session_pgsql); zend_module_entry session_pgsql_module_entry = { STANDARD_MODULE_HEADER, "session_pgsql", NULL, PHP_MINIT(session_pgsql),PHP_MSHUTDOWN(session_pgsql), NULL, PHP_RSHUTDOWN(session_pgsql), NULL, "0.1", STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_SESSION_PGSQL ZEND_GET_MODULE(session_pgsql) #endif PHP_MINIT_FUNCTION(session_pgsql) { #ifdef ZTS php_ps_pgsql_globals *ps_pgsql_globals; ts_allocate_id(&ps_pgsql_globals_id, sizeof(php_ps_pgsql_globals), NULL, NULL); ps_pgsql_globals = ts_resource(ps_pgsql_globals_id); #endif PS_PGSQL(pgsql_link) = PQconnectdb(PS(save_path)); if (!PS_PGSQL(pgsql_link) || PQstatus(PS_PGSQL(pgsql_link)) != CONNECTION_OK) { PQfinish(PS_PGSQL(pgsql_link)); php_error(E_NOTICE, "Session pgsql cannot connect to PostgreSQL server"); } php_error(E_NOTICE, "Session save handler registering: %s",ps_mod_pgsql.name); php_session_register_module(&ps_mod_pgsql); return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(session_pgsql) { /* link is closed at shutdown */ PQfinish(PS_PGSQL(pgsql_link)); PS_PGSQL(pgsql_link) = NULL; return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(session_pgsql) { PGresult *pg_result; int ret = SUCCESS; /* clean up link just in case */ pg_result = PQexec(PS_PGSQL(pgsql_link), "BEGIN;END;"); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { ret = FAILURE; } PQclear(pg_result); return ret; } static int ps_pgsql_valid_str(const char *key) { size_t len; const char *p; char c; int ret = 1; for (p = key; (c = *p); p++) { /* valid characters are a..z,A..Z,0..9,_ */ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_'))) { ret = 0; break; } } len = p - key; if (len != 32) ret = 0; return ret; } PS_OPEN_FUNC(pgsql) { PGresult *pg_result; int ret = SUCCESS; *mod_data = (void *) dummy; /* mod_data cannot be NULL */ if (!session_name && !ps_pgsql_valid_str(session_name)) { ret = FAILURE; } if (PQstatus(PS_PGSQL(pgsql_link)) != CONNECTION_OK) { /* link is bad... Try again */ PQreset(PS_PGSQL(pgsql_link)); if (PQstatus(PS_PGSQL(pgsql_link)) != CONNECTION_OK) { PQfinish((PS_PGSQL(pgsql_link))); ret = FAILURE; } } else { pg_result = PQexec(PS_PGSQL(pgsql_link), "BEGIN;"); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { ret = FAILURE; } PQclear(pg_result); } return ret; } PS_CLOSE_FUNC(pgsql) { PGresult *pg_result; int ret = SUCCESS; PS_SET_MOD_DATA(NULL); /* It's safe to assign NULL */ if (PQstatus(PS_PGSQL(pgsql_link)) == CONNECTION_BAD) { ret = FAILURE; } else { pg_result = PQexec(PS_PGSQL(pgsql_link), "END;"); if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { ret = FAILURE; } PQclear(pg_result); } return SUCCESS; } PS_READ_FUNC(pgsql) { PGresult *pg_result; char query[QUERY_BUF_SIZE+1]; char *query_tpl = "SELECT sess_modified, sess_data FROM php_session WHERE sess_id = '%s';"; char *modified; size_t exp; size_t mod; int ret = FAILURE; ExecStatusType status; PS_PGSQL(sess_new) = 0; PS_PGSQL(sess_del) = 0; *vallen = 0; if (ps_pgsql_valid_str(key)) { snprintf(query, QUERY_BUF_SIZE, query_tpl, key); pg_result = PQexec(PS_PGSQL(pgsql_link), query); status = PQresultStatus(pg_result); if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) { if (PQntuples(pg_result) == 0) { PS_PGSQL(sess_new) = 1; } else { exp = time(NULL) - PS(gc_maxlifetime); modified = PQgetvalue(pg_result, 0, 0); mod = (size_t)atoi(modified); if ((mod < exp)) { PS_PGSQL(sess_new) = 1; PS_PGSQL(sess_del) = 1; } else { /* PQgetvalue reuturns "" for NULL */ *val = PQgetvalue(pg_result, 0, 1); if (*val) { *vallen = strlen(*val); *val = safe_estrndup(*val, *vallen); } } } ret = SUCCESS; } else { php_error(E_WARNING,"Session pgsql query filed: %s", PQerrorMessage(PS_PGSQL(pgsql_link))); ret = FAILURE; } PQclear(pg_result); } if (*vallen == 0) { *val = safe_estrndup("",0); } return ret; } PS_WRITE_FUNC(pgsql) { PGresult *pg_result; size_t query_len; char *query; char *query_delete = "DELETE FROM php_session WHERE sess_id = '%s';"; char *query_insert = "INSERT INTO php_session (sess_id, sess_created, sess_modified, sess_data) " "VALUES ('%s', %d, %d, '%s');"; char *query_update = "UPDATE php_session SET sess_data = '%s', sess_modified = %d " "WHERE sess_id = '%s';"; int ret = FAILURE; if (PS_PGSQL(sess_del)) { /* DELETE */ query_len = strlen(query_delete)+strlen(key); query = emalloc(query_len+1); sprintf(query, query_delete, key); pg_result = PQexec(PS_PGSQL(pgsql_link), query); PQclear(pg_result); efree(query); } if (PS_PGSQL(sess_new)) { /* INSERT */ query_len = strlen(query_insert)+vallen+strlen(key)+100; query = emalloc(query_len+1); sprintf(query, query_insert, key, time(NULL), time(NULL), val); pg_result = PQexec(PS_PGSQL(pgsql_link), query); if (PQresultStatus(pg_result) == PGRES_COMMAND_OK) { ret = SUCCESS; } } else { /* UPDATE */ query_len = strlen(query_update)+strlen(key)+vallen+100; query = emalloc(query_len+1); sprintf(query, query_update, val, time(NULL), key); pg_result = PQexec(PS_PGSQL(pgsql_link), query); if (PQresultStatus(pg_result) == PGRES_COMMAND_OK) { ret = SUCCESS; } } PQclear(pg_result); efree(query); return ret; } PS_DESTROY_FUNC(pgsql) { PGresult *pg_result; size_t query_len; char *query; char *query_update = "DELETE FROM php_session WHERE sess_id = '%s';"; int ret = FAILURE; if (ps_pgsql_valid_str(key)) { query_len = strlen(query_update)+strlen(key); query = (char *)emalloc(query_len+1); snprintf(query, query_len, key); pg_result = PQexec(PS_PGSQL(pgsql_link), query); if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) { ret = SUCCESS; } PQclear(pg_result); efree(query); } return ret; } PS_GC_FUNC(pgsql) { PGresult *pg_result; char query[QUERY_BUF_SIZE+1]; char *query_update = "DELETE FROM php_session WHERE sess_modified < %d"; int ret = FAILURE; sprintf(query, query_update, time(NULL) - maxlifetime); pg_result = PQexec(PS_PGSQL(pgsql_link), query); if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) { *nrdels = atoi(PQcmdTuples(pg_result)); ret = SUCCESS; } PQclear(pg_result); return ret; } #endif /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */ Index: pear/PECL/session_pgsql/session_pgsql.h +++ pear/PECL/session_pgsql/session_pgsql.h /* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2002 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.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: yohgaki@php.net | +----------------------------------------------------------------------+ */ #ifndef MOD_PGSQL_H #define MOD_PGSQL_H #ifdef HAVE_LIBPQ #include "config.h" #include "ext/session/php_session.h" #include <libpq-fe.h> extern ps_module ps_mod_pgsql; #define ps_pgsql_ptr &ps_mod_pgsql extern zend_module_entry php_session_pgsql_module; #define phpext_ps_pgsql_ptr &php_session_pgsql_module PS_FUNCS(pgsql); typedef struct _php_ps_pgsql_globals { PGconn *pgsql_link; int sess_new; int sess_del; } php_ps_pgsql_globals; #ifdef ZTS extern int ps_pgsql_globals_id; #else extern php_ps_pgsql_globals ps_pgsql_globals; #endif #ifdef ZTS #define PS_PGSQL(v) TSRMG(ps_pgsql_globals_id, php_ps_pgsql_globals *, v) #else #define PS_PGSQL(v) (ps_pgsql_globals.v) #endif #else #define ps_pgsql_ptr NULL #define phpext_ps_pgsql_ptr NULL #endif #endif Index: pear/PECL/session_pgsql/setup.stub +++ pear/PECL/session_pgsql/setup.stub # $Source: /usr/repository/pear/PECL/session_pgsql/setup.stub,v $ # $Id: setup.stub,v 1.1 2001/12/22 00:32:58 yohgaki Exp $ define_option with-session-pgsql 'session pgsql support?' yesnodir yes \ ' Whether to build the session pgsql extension.'