From 8b2446075276f70b72a0836c34cfd4c551231d41 Mon Sep 17 00:00:00 2001 From: "Mark L. Woodward" Date: Mon, 30 Jan 2006 16:18:40 +0000 Subject: [PATCH] MSession now works with the new MCache server --- ext/msession/README | 4 +- ext/msession/config.m4 | 13 +- ext/msession/msession-test.php | 75 +- ext/msession/msession.c | 1338 +++++++++++++++++++------------- ext/msession/msessionapi.h | 366 +++++++++ ext/msession/php_msession.h | 46 +- ext/msession/reqclient.h | 238 +++--- 7 files changed, 1371 insertions(+), 709 deletions(-) create mode 100644 ext/msession/msessionapi.h diff --git a/ext/msession/README b/ext/msession/README index a4ed70faed..5442e3bbab 100644 --- a/ext/msession/README +++ b/ext/msession/README @@ -1,8 +1,8 @@ This is msession, it is an interface to a stand-alone session management system. The msession daemon can be found at -Mohawk Software's web site. +Mohawk Software's web site, under Project Phoenix -http://www.mohawksoft.com +http://www.mohawksoft.com/phoenix.html Requirements: Mohawk Software's Phoenix library. diff --git a/ext/msession/config.m4 b/ext/msession/config.m4 index f41d018c6d..c9b728f964 100644 --- a/ext/msession/config.m4 +++ b/ext/msession/config.m4 @@ -6,12 +6,12 @@ PHP_ARG_WITH(msession, for msession support, [ --with-msession[=DIR] Include msession support]) if test "$PHP_MSESSION" != "no"; then - if test -r $PHP_MSESSION/lib/libphoenix.a; then + if test -r $PHP_MSESSION/lib/libphoenix.so; then PHOENIX_DIR=$PHP_MSESSION else AC_MSG_CHECKING(for msession in default path) - for i in /usr/local/phoenix /usr/local /usr; do - if test -r $i/lib/libphoenix.a; then + for i in /opt/mohawk /usr/local/phoenix /usr/local /usr; do + if test -r $i/lib/libphoenix.so; then PHOENIX_DIR=$i AC_MSG_RESULT(found in $i) fi @@ -23,14 +23,13 @@ if test "$PHP_MSESSION" != "no"; then AC_MSG_ERROR(Please reinstall the Phoenix / msession distribution) fi - AC_DEFINE(HAVE_MSESSION, 1, [ ]) PHP_ADD_LIBRARY_WITH_PATH(phoenix, "$PHOENIX_DIR/lib", PHOENIX_LIB) - PHOENIX_INCLUDE="-I$PHOENIX_DIR" + PHOENIX_INCLUDE="-I$PHOENIX_DIR/src" PHP_SUBST(PHOENIX_LIB) - PHP_REQUIRE_CXX + PHP_REQUIRE_CXX PHP_NEW_EXTENSION(msession, msession.c, $ext_shared) - PHP_ADD_LIBRARY(stdc++) + PHP_ADD_LIBRARY(stdc++) fi diff --git a/ext/msession/msession-test.php b/ext/msession/msession-test.php index e591fcca70..6df6c9aaf6 100644 --- a/ext/msession/msession-test.php +++ b/ext/msession/msession-test.php @@ -15,50 +15,45 @@ # Start sessions, this will set a cookie. session_start(); +msession_connect('localhost','8086'); + # Now, optional, use msession_uniq() to create a guarenteed # uniq session name. # -if(!$HTTP_COOKIE_VARS["PHPSESSID"]) -{ - # Use uniq to create the session. This is guarenteed to be - # uniq in the server. - $sid = msession_uniq(32); - setcookie ("PHPSESSID", $sid); - session_id($sid); - $HTTP_COOKIE_VARS["PHPSESSID"] = $sid; - # New session, set some variables - if(0) // One at a time - { - echo "Set Variable: " . msession_set($sid, 'time',time()) ."

\n"; - echo "Set Variable: " . msession_set($sid, 'name1','test1') ."

\n"; - echo "Set Variable: " . msession_set($sid, 'name2','test2') ."

\n"; - echo "Set Variable: " . msession_set($sid, 'name3','test3') ."

\n"; - echo "Set Variable: " . msession_set($sid, 'name4','test4') ."

\n"; - echo "Set Variable: " . msession_set($sid, 'name5','test5') ."

\n"; - echo "Set Variable: " . msession_set($sid, 'name6','test6') ."

\n"; - echo "Set Variable: " . msession_set($sid, 'name7','test7') ."

\n"; - } - else // All at once in an array - { - $setarray = array(); - $setarray['time']=time(); - $setarray['name1'] = 'test1'; - $setarray['name2'] = 'test2'; - $setarray['name3'] = 'test3'; - $setarray['name4'] = 'test4'; - $setarray['name5'] = 'test5'; - $setarray['name6'] = 'test6'; - $setarray['name7'] = 'test7'; - msession_set_array($sid, $setarray); - } -} -else -{ +# Use uniq to create the session. This is guarenteed to be +# uniq in the server. + +if($HTTP_COOKIE_VARS["PHPSESSID"]) $sid = $HTTP_COOKIE_VARS["PHPSESSID"]; -} +else + $sid = msession_uniq(32, "phpsession"); +setcookie ("PHPSESSID", $sid); +session_id($sid); +$HTTP_COOKIE_VARS["PHPSESSID"] = $sid; +# New session, set some variables +echo "Set Variable: " . msession_set($sid, 'time',time()) ."

\n"; +echo "Set Variable: " . msession_set($sid, 'name1','test1') ."

\n"; +echo "Set Variable: " . msession_set($sid, 'name2','test2') ."

\n"; +echo "Set Variable: " . msession_set($sid, 'name3','test3') ."

\n"; +echo "Set Variable: " . msession_set($sid, 'name4','test4') ."

\n"; +echo "Set Variable: " . msession_set($sid, 'name5','test5') ."

\n"; +echo "Set Variable: " . msession_set($sid, 'name6','test6') ."

\n"; +echo "Set Variable: " . msession_set($sid, 'name7','test7') ."

\n"; + + $setarray = array(); + $setarray['time']=time(); + $setarray['name1'] = 'test1'; + $setarray['name2'] = 'test2'; + $setarray['name3'] = 'test3'; + $setarray['name4'] = 'test4'; + $setarray['name5'] = 'test5'; + $setarray['name6'] = 'test6'; + $setarray['name7'] = 'test7'; + msession_set_array($sid, $setarray); #This makes a link between the variable $count and the # session variable "count" + session_register("count"); $count ++; @@ -77,6 +72,12 @@ echo "Random String: " . msession_randstr(32) . "
\n"; # newer versions create it by default) echo "Global Count: " . msession_inc(globals, "counter") . "
\n"; + +# Thread safe functions + +echo "Local Count: " . msession_add($sid, "counter", 2) . "
\n"; +echo "Muldiv counter * 2 / 3 :" . msession_muldiv($sid, "counter", "2", "3") . "
\n"; + # This gets a count of active sessions. echo "Total active sessions: " . msession_count() . "
\n"; diff --git a/ext/msession/msession.c b/ext/msession/msession.c index 9f218300e2..c271e7799c 100644 --- a/ext/msession/msession.c +++ b/ext/msession/msession.c @@ -1,13 +1,13 @@ /* +----------------------------------------------------------------------+ - | msession 1.0 | + | msession 1.0 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-2006 The PHP Group | + | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | + | 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 through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | + | 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. | @@ -16,7 +16,6 @@ | Portions copyright the PHP group. | +----------------------------------------------------------------------+ */ - #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -24,11 +23,10 @@ #include "php.h" #include "php_ini.h" #include "php_msession.h" -#include "reqclient.h" +#include "msessionapi.h" #include "ext/standard/info.h" #include "ext/session/php_session.h" - /* Macros and such */ #ifndef TRUE #define TRUE 1 @@ -40,26 +38,43 @@ #define SID_LEN 32 #endif - /* Uncomment to get debugging messages */ -/* #define ERR_DEBUG */ +// #define ERR_DEBUG #ifdef ERR_DEBUG -#define ELOG( str ) php_log_err( str ) +#define ELOG( str ) php_log_err( (char *)(str) ) #else #define ELOG( str ) #endif /* Test if system is OK fror use. */ /* Macros may be ugly, but I can globaly add debuging when needed. */ -#define IFCONNECT_BEGIN if(s_reqb && s_conn) { -#define IFCONNECT_ENDVAL(V) } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Msession not initialized"); return V; } -#define IFCONNECT_END } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Msession not initialized"); RETURN_FALSE; } +#define IFCONNECT_BEGIN { void *t_conn; REQB t_reqb; if(MSessionGetBuffers(&t_conn, &t_reqb TSRMLS_CC)) { + +/* End IFCONNECT block with return FALSE */ +#define IFCONNECT_END MSessionReleaseBuffers(t_conn, &t_reqb TSRMLS_CC);} else { php_error(E_WARNING, s_szNoInit, get_active_function_name(TSRMLS_C)); RETURN_FALSE; }} + +/* End IFCONNECT block without return */ +#define IFCONNECT_ENDNR MSessionReleaseBuffers(t_conn, &t_reqb TSRMLS_CC);} else { php_error(E_WARNING, s_szNoInit, get_active_function_name(TSRMLS_C)); }} #ifndef HAVE_PHP_SESSION #error HAVE_PHP_SESSION not defined #endif +static int msession_split_host_port(char *host, int cbhost, char *save_path, int def_port); +static int MSessionGetBuffers(void **pconn, REQB *preq TSRMLS_DC); +static void MSessionReleaseBuffers(void *pconn, REQB *preq TSRMLS_DC); +static void msession_logf(char *fmt, ...); +static void php_msession_init_globals(zend_msession_globals *msession_globals); +static int PHPDoRequest(void **pconn, REQB *preq TSRMLS_DC); +static void *PHPMSessionConnect(const char *szhost, int nport TSRMLS_DC); +static void PHPMSessionDisconnect(TSRMLS_DC); +static char * PHPMSessionGetString(int req, const char *session, char *name, char *value, int param TSRMLS_DC); +static int PHPMSessionGetInt(int req, char *session, char *name, char *value, int param TSRMLS_DC); +static int PHPMSessionGetArray(zval *return_value, int req, char *session, char *name, char *value, int param TSRMLS_DC); +static int PHPMSessionGetArrayPairs(zval *return_value, int req, char *session, char *name, char *value, int param TSRMLS_DC); +static int PHPMSessionExec(int req, char *session, char *name, char *value, int param TSRMLS_DC); + /* Test if session module contains custom sesson ID patch */ #ifdef PHP_SESSION_API #if (PHP_SESSION_API >= 20020330) @@ -80,9 +95,6 @@ #warning Backward compatible msession extension requires PHP sessions. If PHP compiles and links, you can ignore this warning. #elif (ZEND_MODULE_API_NO >= 20010901) #define PHP_4_1 -#else -#warning ZEND_MODULE_API_NO not defined -#define PHP_4_1 #endif /* @@ -113,14 +125,19 @@ ps_module ps_mod_msession = { #endif #endif +ZEND_DECLARE_MODULE_GLOBALS(msession) + +/* Static strings */ +static char s_szNoInit[]="%s(): MSession not initialized"; +static char s_szErrFmt[]="%s(): %s"; + /* Per-process variables need by msession */ static char s_szdefhost[]="localhost"; -static char * s_szhost=s_szdefhost; -static int s_port=8086; -static void * s_conn=NULL; -static REQB * s_reqb=NULL; -zend_function_entry msession_functions[] = { +#define MAX_HOST 1024 +#define DEF_PORT 8086 + +function_entry msession_functions[] = { PHP_FE(msession_connect,NULL) PHP_FE(msession_disconnect,NULL) PHP_FE(msession_lock,NULL) @@ -132,11 +149,15 @@ zend_function_entry msession_functions[] = { PHP_FE(msession_get,NULL) PHP_FE(msession_find,NULL) PHP_FE(msession_get_array,NULL) + PHP_FE(msession_save_object,NULL) + PHP_FE(msession_get_object,NULL) PHP_FE(msession_set_array,NULL) PHP_FE(msession_timeout,NULL) PHP_FE(msession_inc,NULL) - PHP_FE(msession_set_data,NULL) - PHP_FE(msession_get_data,NULL) + PHP_FE(msession_add,NULL) + PHP_FE(msession_muldiv,NULL) + PHP_FE(msession_setdata,NULL) + PHP_FE(msession_getdata,NULL) PHP_FE(msession_listvar,NULL) PHP_FE(msession_list,NULL) PHP_FE(msession_uniq,NULL) @@ -144,8 +165,6 @@ zend_function_entry msession_functions[] = { PHP_FE(msession_plugin,NULL) PHP_FE(msession_call,NULL) PHP_FE(msession_ctl,NULL) - PHP_FE(msession_exec,NULL) - PHP_FE(msession_ping,NULL) {NULL, NULL, NULL} }; @@ -156,236 +175,413 @@ zend_module_entry msession_module_entry = { "msession", msession_functions, PHP_MINIT(msession), - NULL, - NULL, - NULL, + PHP_MSHUTDOWN(msession), + NULL, /* PHP_RINIT(msession),*/ + NULL, /* PHP_RSHUTDOWN(msession), */ PHP_MINFO(msession), #ifdef PHP_4_1 NO_VERSION_YET, #endif STANDARD_MODULE_PROPERTIES }; - #ifdef COMPILE_DL_MSESSION ZEND_GET_MODULE(msession) #endif +// Split host port syntax: host:port +// Warning, make sure buffer is big enough! +static int msession_split_host_port(char *host, int cbhost, char *save_path, int def_port) +{ + int port; + char * szport; + strncpy(host, save_path, cbhost); + szport = strchr(host, ':'); + if(szport) + { + *szport++=0; + port = atoi(szport); + } + else + { + port = def_port; + } + return port; +} +// Called by internal functions to get buffers and connection +static int MSessionGetBuffers(void **pconn, REQB *preqb TSRMLS_DC) +{ + *pconn = (void *)MSESSION_G(conn); + AllocateRequestBuffer(preqb,2048); + if(!*pconn) + *pconn = PHPMSessionConnect(MSESSION_G(host), MSESSION_G(port) TSRMLS_CC); + return (int) *pconn; +} + +// Called after GetBuffers, updates preqb if nessisary. +// The syntax of this function, along with allows for +// flexibility of the nature of the interface changes. +static void MSessionReleaseBuffers(void *pconn, REQB *preqb TSRMLS_DC) +{ + MSESSION_G(conn) = pconn; + FreeRequestBuffer(preqb); +} +/* Uses printf syntax to write to php_log_err + * Must remain in ERR_DEBUG ifdef to ensure it + * does not get compiled into release code + */ +static void msession_logf(char *fmt, ...) +{ + va_list arglist; + char buffer[2048]; + va_start(arglist, fmt); + vsnprintf(buffer, sizeof(buffer), fmt, arglist); + va_end(arglist); + php_log_err(buffer); +} + +// Initialize global structure +static void php_msession_init_globals(zend_msession_globals *msession_globals) +{ + char host[MAX_HOST]; + int global_variable; + char *init = INI_STR("session.save_path"); + msession_globals->conn = NULL; + msession_globals->reqb = NULL; + + if(!init) + { + ELOG("Could not get session.save_path from INI_STR"); + msession_globals->host = pestrdup(s_szdefhost,1); + msession_globals->port = DEF_PORT; + } + else + { + msession_globals->port = msession_split_host_port(host, sizeof(host), init, DEF_PORT); + msession_globals->host = pestrdup(host,1); + } +} + PHP_MINIT_FUNCTION(msession) { - s_conn = NULL; - s_szhost = s_szdefhost; - + ZEND_INIT_MODULE_GLOBALS(msession, php_msession_init_globals, NULL); #ifdef HAVE_PHP_SESSION php_session_register_module(&ps_mod_msession); #endif return SUCCESS; } -PHP_MINFO_FUNCTION(msession) +PHP_MSHUTDOWN_FUNCTION(msession) { - php_info_print_table_start(); - php_info_print_table_header(2, "msession support", "enabled"); - php_info_print_table_end(); + return SUCCESS; } -#define RPT_CONN_ERR -#ifdef ERR_DEBUG -#undef RPT_CONN_ERR -#define RPT_CONN_ERR if(s_reqb->req.stat == REQ_ERR && (result == REQE_BADCONN || result == REQE_NOSEND)) \ - ELOG("Socket reports closed"); \ - else if(s_reqb->req.stat == REQ_ERR)\ - ELOG("MSession call failed");\ -#else -#endif -#define CONN_ERR(reqb)\ - (reqb->req.stat == REQ_ERR) && (reqb->req.param == REQE_BADCONN || reqb->req.param == REQE_NOSEND) - -int PHPExecRequest(int req, char *session, char *d1, char *d2,int param) +PHP_RINIT_FUNCTION(msession) { - int result; - FormatRequest(&s_reqb, req, session, d1, d2, param); - result = DoRequest(s_conn, &s_reqb); + return SUCCESS; +} - /* Server closed the connection! */ - if(CONN_ERR(s_reqb)) +PHP_RSHUTDOWN_FUNCTION(msession) +{ + TSRMLS_FETCH(); + void *t_conn = (void *)MSESSION_G(conn); + if(t_conn) { - ELOG("Connection was closed by server"); - if(ReopenReqConn(s_conn)) - { - FormatRequest(&s_reqb, req, session, d1, d2, param); - result = DoRequest(s_conn, &s_reqb); - } + MSESSION_G(conn) = NULL; + CloseReqConn(t_conn); + t_conn = NULL; } - RPT_CONN_ERR - return result; + return SUCCESS; } -int PHPExecRequestMulti(int req, char *session, int n, char **pairs, int param) -{ - int result; - FormatRequestMulti(&s_reqb, req, session, n, pairs, param); - result = DoRequest(s_conn, &s_reqb); - /* Server closed the connection! */ - if(CONN_ERR(s_reqb)) - { - ELOG("Connection was closed by server"); - if(ReopenReqConn(s_conn)) - { - FormatRequestMulti(&s_reqb, req, session, n, pairs, param); - result = DoRequest(s_conn, &s_reqb); - } - } - RPT_CONN_ERR - return result; +PHP_MINFO_FUNCTION(msession) +{ + ELOG("PHP_MINFO"); + php_info_print_table_start(); + php_info_print_table_header(2, "msession support", "enabled"); + php_info_print_table_end(); } -int PHPExecRequestStrings(int req, char *session, int n, char **strings) + +/* The PHP Version of DoRequest */ +/* Manages connections that have been disconnected */ +static int PHPDoRequest(void **pconn, REQB *preq TSRMLS_DC) { - int result; - FormatRequestStrings(&s_reqb, req, session, n, strings); - result = DoRequest(s_conn, &s_reqb); + int stat = preq->req->stat; + int param = preq->req->param; + int result = DoRequest(*pconn, preq); + ELOG("PHPDoRequest"); /* Server closed the connection! */ - if(CONN_ERR(s_reqb)) + if(preq->req->stat == MREQ_ERR && (result == REQE_BADCONN || result == REQE_NOSEND)) { + /* In the case of a bad socket, this should be */ + /* all that changs! */ + preq->req->stat = stat; + preq->req->param = param; ELOG("Connection was closed by server"); - if(ReopenReqConn(s_conn)) - { - FormatRequestStrings(&s_reqb, req, session, n, strings); - result = DoRequest(s_conn, &s_reqb); - } + if(ReopenReqConn(*pconn)) + result = DoRequest(*pconn, preq); } - RPT_CONN_ERR +#ifdef ERR_DEBUG + if(preq->req->stat == MREQ_ERR && (result == REQE_BADCONN || result == REQE_NOSEND)) + ELOG("Socket reports closed"); +#endif return result; } - - -int PHPMsessionConnect(const char *szhost, int nport) +static void *PHPMSessionConnect(const char *szhost, int nport TSRMLS_DC) { int fNewHost=FALSE; - - if(!s_reqb) - s_reqb = AllocateRequestBuffer(2048); + int fNewPort=FALSE; + int fResult=FALSE; + void *t_conn; + REQB *preqb; + TSRMLS_FETCH(); - if(!s_reqb) /* no buffer, it won't work! */ - return 0; + ELOG("PHPMSessionConnect"); + + t_conn = (void *)MSESSION_G(conn); + + char *gszhost = MSESSION_G(host); + int gport = MSESSION_G(port); - if(strcmp(s_szhost, szhost)) + if(nport == 0) + nport = DEF_PORT; + + if(strcmp(gszhost, szhost)) { - if(s_szhost != s_szdefhost) - free(s_szhost); - s_szhost = strdup(szhost); + if(gszhost != s_szdefhost) + free(gszhost); + gszhost = strdup(szhost); + MSESSION_G(host) = gszhost; fNewHost = TRUE; } - if(nport && nport != s_port) + if(nport != gport) { - fNewHost = TRUE; - s_port = nport; + fNewPort = TRUE; + gport = nport; + MSESSION_G(port) = gport; } + if(!fNewHost && !fNewPort && t_conn) + return t_conn; // Nothing to do, already there - ELOG("PHPMsessionConnect:new"); - if(s_conn && fNewHost) + if(t_conn) { - ELOG("Closing old connection, opening new"); - CloseReqConn(s_conn); - s_conn = NULL; + MSESSION_G(conn) = NULL; + CloseReqConn(t_conn); + php_log_err("Call to PHPMSessionConnect with non-null t_conn" TSRMLS_CC); + t_conn = NULL; } - if(!s_conn) - s_conn = OpenReqConn(s_szhost, s_port); -#ifdef ERR_DEBUG - else + + t_conn = OpenReqConn(gszhost, gport); + + MSESSION_G(conn) = t_conn; + return t_conn; +} + +static void PHPMSessionDisconnect(TSRMLS_DC) +{ + TSRMLS_FETCH(); + void *t_conn = (void *)MSESSION_G(conn); + if(t_conn) { - ELOG("Reusing old connection"); + CloseReqConn(t_conn); + MSESSION_G(conn) = NULL; + t_conn = NULL; } -#endif +} + +static char * PHPMSessionGetString(int req, const char *session, char *name, char *value, int param TSRMLS_DC) +{ + char *szvalue = NULL; + TSRMLS_FETCH(); + ELOG("PHPMSessionGetString"); + IFCONNECT_BEGIN + FormatRequest(&t_reqb, req, (char *)session, name, value, param); + PHPDoRequest(&t_conn, &t_reqb); + + if(t_reqb.req->stat == MREQ_OK && t_reqb.req->datum[0]) + szvalue = safe_estrdup(t_reqb.req->datum); + else + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); + IFCONNECT_ENDNR #ifdef ERR_DEBUG -{ - char buffer[256]; - sprintf(buffer,"Connect: %s [%d] = %d (%X)\n", - s_szhost, s_port, (s_conn != NULL), (unsigned)s_conn); - php_log_err(buffer); -} + if(szvalue) + ELOG(szvalue); + else + ELOG("GetString returned NULL"); #endif - return (s_conn) ? 1 : 0; + return szvalue; } -void PHPMsessionDisconnect() +static int PHPMSessionGetInt(int req, char *session, char *name, char *value, int param TSRMLS_DC) { -} + TSRMLS_FETCH(); + ELOG("PHPMSessionGetInt"); + IFCONNECT_BEGIN + FormatRequest(&t_reqb, req, session, name, value, param); + PHPDoRequest(&t_conn, &t_reqb); -char *PHPMsessionGetData(char *session TSRMLS_DC) -{ - char *ret = NULL; - ELOG("PHPMsessionGetData"); + param = t_reqb.req->param; + if(t_reqb.req->stat != MREQ_OK) + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); + + IFCONNECT_ENDNR + return param; +} +static int StringsToFixedArray(char *strings, char **array, int ceArray) +{ + int i; + char *p = strings; + int term=ceArray-1; + for(i=0; i < term; i++) + { + if(!*p) + break; + array[i] = p; + p += strlen(p)+1; + } + array[i]=0; + return i; +} +static int PHPMSessionGetArray(zval *return_value, int req, char *session, char *name, char *value, int param TSRMLS_DC) +{ + int stat=0; + TSRMLS_FETCH(); + ELOG("PHPMSessionGetArray"); IFCONNECT_BEGIN + FormatRequest(&t_reqb, req, session, name, value, param); + PHPDoRequest(&t_conn,&t_reqb); - PHPExecRequest(REQ_DATAGET, (char *)session,"","",0); + if(t_reqb.cmd==MREQ_OK && t_reqb.param) + { + int i=0; + int term = 0; + int string_count = t_reqb.param + 4; + char ** strings = (char **)emalloc(sizeof(char *) * (t_reqb.param + 4)); + term = StringsToFixedArray(t_reqb.req->datum, strings, string_count); - if(s_reqb->req.stat==REQ_OK) - ret = safe_estrdup(s_reqb->req.datum); - IFCONNECT_ENDVAL(0) - - return ret; + array_init(return_value); + for(i=0; i < term; i++) + { + ELOG(strings[i]); + add_index_string(return_value, i, safe_estrdup(strings[i]), 0); + } + efree(strings); + } + else if(t_reqb.req->stat != MREQ_OK) + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); + + stat = t_reqb.req->stat; + + IFCONNECT_ENDNR + return stat; } -int PHPMsessionSetData(char *session, char *data TSRMLS_DC) + +static int PHPMSessionGetArrayPairs(zval *return_value, int req, char *session, char *name, char *value, int param TSRMLS_DC) { - ELOG("PHPMsessionSetData"); + int stat=0; + TSRMLS_FETCH(); + ELOG("PHPMSessionGetArrayPairs"); IFCONNECT_BEGIN - int ret=0; + FormatRequest(&t_reqb, req, session, name, value, param); + PHPDoRequest(&t_conn,&t_reqb); - PHPExecRequest(REQ_DATASET, session,"",data,0); - ret = (s_reqb->req.stat==REQ_OK); - if(s_reqb->req.stat!=REQ_OK) - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); + if(t_reqb.cmd==MREQ_OK && t_reqb.param) + { + int ndx=0; + int term = 0; + int string_count = t_reqb.param + 4; + char ** strings = (char **)emalloc(sizeof(char *) * (t_reqb.param + 4)); + term = StringsToFixedArray(t_reqb.req->datum, strings, string_count); + array_init(return_value); + if(req == MREQ_GETOBJECT) + { + ELOG("Using MREQ_GETOBJECT classname and classdata"); + add_assoc_string(return_value, safe_estrdup("classname"), safe_estrdup(strings[ndx]), 0); + ELOG(strings[ndx]); + ndx++; + add_assoc_string(return_value, safe_estrdup("classdata"), safe_estrdup(strings[ndx]), 0); + ELOG(strings[ndx]); + ndx++; + } + for(; ndx < term; ndx+=2) + { + ELOG(strings[ndx]); + ELOG(strings[ndx+1]); + add_assoc_string(return_value, safe_estrdup(strings[ndx]), + safe_estrdup(strings[ndx+1]), 0); + } + + efree(strings); + } + else + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); - return ret; + stat = t_reqb.req->stat; + IFCONNECT_ENDNR - IFCONNECT_ENDVAL(0) + return stat; } -int PHPMsessionDestroy(char *session TSRMLS_DC) +static int PHPMSessionExec(int req, char *session, char *name, char *value, int param TSRMLS_DC) { - ELOG("PHPMsessionDestroy"); + int stat=0; + TSRMLS_FETCH(); + ELOG("PHPMSessionExec"); IFCONNECT_BEGIN + FormatRequest(&t_reqb, req, session, name, value, param); + PHPDoRequest(&t_conn, &t_reqb); - int ret=0; - PHPExecRequest( REQ_DROP, session, "","",0); - ret = (s_reqb->req.stat==REQ_OK); - if(s_reqb->req.stat!=REQ_OK) - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - return ret; - - IFCONNECT_ENDVAL(0) + stat = t_reqb.req->stat; + + if(t_reqb.req->stat != MREQ_OK) + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); + + IFCONNECT_ENDNR + return stat; } +static int PHPMSessionExecData(int req, char *session, char *name, char *value, void *data, int cb, int param TSRMLS_DC) +{ + int stat=0; + TSRMLS_FETCH(); + ELOG("PHPMSessionExec"); + IFCONNECT_BEGIN + FormatRequestData(&t_reqb, req, session, name, value, data, cb, param); + PHPDoRequest(&t_conn, &t_reqb); + + stat = t_reqb.req->stat; + if(t_reqb.req->stat != MREQ_OK) + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); + + IFCONNECT_ENDNR + return stat; +} /* {{{ proto bool msession_connect(string host, string port) Connect to msession sever */ PHP_FUNCTION(msession_connect) { char *szhost; int nport; - zval **zhost; zval **zport; - + ELOG("msession_connect"); if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &zhost, &zport) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_string_ex(zhost); convert_to_string_ex(zport); - szhost = Z_STRVAL_PP(zhost); nport = atoi(Z_STRVAL_PP(zport)); - if(PHPMsessionConnect(szhost,nport)) + if(PHPMSessionConnect(szhost,nport)) { RETURN_TRUE; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "MSession connect failed"); + php_error(E_WARNING, "%s(): MSession connect failed", get_active_function_name(TSRMLS_C)); RETURN_FALSE; } } @@ -395,7 +591,8 @@ PHP_FUNCTION(msession_connect) Disconnect from msession server */ PHP_FUNCTION(msession_disconnect) { - PHPMsessionDisconnect(); + ELOG("msession_disconnect"); + PHPMSessionDisconnect(); RETURN_NULL(); } /* }}} */ @@ -404,62 +601,199 @@ PHP_FUNCTION(msession_disconnect) Get session count */ PHP_FUNCTION(msession_count) { - IFCONNECT_BEGIN - - int count; - PHPExecRequest( REQ_COUNT, "", "","",0); - - count = (s_reqb->req.stat == REQ_OK) ? s_reqb->req.param : 0; - + int count=0; + ELOG("msession_count"); + count = PHPMSessionGetInt(MREQ_COUNT, "","","",0 TSRMLS_CC); RETURN_LONG(count); - - IFCONNECT_END } /* }}} */ -/* {{{ proto bool msession_create(string session) - Create a session */ +/* {{{ proto bool msession_create(string session[,string classname][,string data]) + Create a session cacheid */ PHP_FUNCTION(msession_create) { - IFCONNECT_BEGIN -/* int stat; */ + int stat=0; + int ret; char *szsession; zval **session; - - if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &session) == FAILURE) + zval **zclassname; + zval **zdata; + char *classname="phpsession"; + char *data = NULL; + int nargs = ZEND_NUM_ARGS(); + + ELOG("msession_create"); + if(nargs < 1) { WRONG_PARAM_COUNT; } - convert_to_string_ex(session); - szsession = Z_STRVAL_PP(session); - PHPExecRequest( REQ_CREATE, szsession, "","",0); - if(s_reqb->req.stat==REQ_OK) + + if(nargs == 2) + { + ret = zend_get_parameters_ex(2, &session, &zclassname); + convert_to_string_ex(session); + convert_to_string_ex(zclassname); + szsession = Z_STRVAL_PP(session); + classname = Z_STRVAL_PP(zclassname); + } + else if(nargs == 3) + { + ret = zend_get_parameters_ex(3, &session, &zclassname, &zdata); + convert_to_string_ex(session); + convert_to_string_ex(zclassname); + convert_to_string_ex(zdata); + szsession = Z_STRVAL_PP(session); + classname = Z_STRVAL_PP(zclassname); + data = Z_STRVAL_PP(zdata); + } + if(ret = FAILURE) + { + WRONG_PARAM_COUNT; + } + + stat = PHPMSessionExec(MREQ_CREATE, szsession,classname ,data,0 TSRMLS_CC); + + if(stat==MREQ_OK) { RETURN_TRUE; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); RETURN_FALSE; } - IFCONNECT_END } /* }}} */ +/* {{{ proto bool msession_save_object(string session, string data[, array tuples[,string classname]]) + Create a session cacheid + This function really needs to be written to handle objects and not arrays. +*/ +PHP_FUNCTION(msession_save_object) +{ + zval **session=NULL; + zval **zdata=NULL; + zval **tuples=NULL; + zval **zclassname=NULL; + HashPosition pos; + zval **entry; + char *key; + uint keylen; + ulong numndx; + int ndx=0; + char **pairs; + HashTable *htTuples; + int i; + int nargs = ZEND_NUM_ARGS(); + int res; + int countpair; + TSRMLS_FETCH(); + char *szsession; + char *classname; + char *data; + int ret; + + ELOG("msession_save_object"); + if(nargs < 2 ) + { + WRONG_PARAM_COUNT; + } + if(nargs == 2) + { + ret = zend_get_parameters_ex(2, &session, &zdata); + convert_to_string_ex(session); + convert_to_string_ex(zclassname); + szsession = Z_STRVAL_PP(session); + classname = Z_STRVAL_PP(zclassname); + } + else if(nargs == 3) + { + ret = zend_get_parameters_ex(3, &session, &zdata, &tuples); + convert_to_string_ex(session); + convert_to_string_ex(zclassname); + convert_to_string_ex(zdata); + szsession = Z_STRVAL_PP(session); + classname = Z_STRVAL_PP(zclassname); + data = Z_STRVAL_PP(zdata); + } + else if(nargs == 4) + { + ret = zend_get_parameters_ex(4, &session, &zdata, &tuples, &zclassname); + convert_to_string_ex(session); + convert_to_string_ex(zclassname); + convert_to_string_ex(zdata); + szsession = Z_STRVAL_PP(session); + classname = Z_STRVAL_PP(zclassname); + data = Z_STRVAL_PP(zdata); + } + + htTuples = Z_ARRVAL_PP(tuples); + + countpair = zend_hash_num_elements(htTuples); + + pairs = (char **)emalloc(sizeof(char *) * (countpair+1) * 2); + + if(!pairs) + { + ELOG("no pairs"); + RETURN_FALSE; + } + + ELOG("have pairs"); + + /* Initializes pos */ + zend_hash_internal_pointer_reset_ex(htTuples, &pos); + + ELOG("reset pointer"); + + for(i=0; i < countpair; i++) + { + if(zend_hash_get_current_data_ex(htTuples, (void **)&entry, &pos) != SUCCESS) + break; + + if(entry) + { + char *szentry; + convert_to_string_ex(entry); + szentry = Z_STRVAL_PP(entry); + + if(zend_hash_get_current_key_ex(htTuples,&key,&keylen,&numndx,0,&pos)== HASH_KEY_IS_STRING) + { + pairs[ndx++] = key; + pairs[ndx++] = szentry; + } + } + zend_hash_move_forward_ex(htTuples, &pos); + } + + IFCONNECT_BEGIN + ELOG("FormatMulti"); + FormatRequestStrings(&t_reqb, MREQ_CREATE | MSTAT_TYPE_OBJECT, + szsession, classname, data, ndx, pairs); + PHPDoRequest(&t_conn,&t_reqb); + + if(t_reqb.req->stat != MREQ_OK) + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); + efree((void *)pairs); + + IFCONNECT_END + + RETURN_TRUE; +} +/* }}} */ /* {{{ proto bool msession_destroy(string name) Destroy a session */ PHP_FUNCTION(msession_destroy) { char *szsession; zval **session; + ELOG("msession_destroy"); if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &session) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(session); szsession = Z_STRVAL_PP(session); - PHPMsessionDestroy(szsession TSRMLS_CC); - + PHPMSessionExec(MREQ_DROP, szsession, "", "", 0 TSRMLS_CC); RETURN_TRUE; } /* }}} */ @@ -468,33 +802,27 @@ PHP_FUNCTION(msession_destroy) Lock a session */ PHP_FUNCTION(msession_lock) { - IFCONNECT_BEGIN + int stat; + int param; + char *szsession; zval **session; + ELOG("msession_lock"); if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &session) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(session); szsession = Z_STRVAL_PP(session); - PHPExecRequest( REQ_SLOCK, szsession, "","",0); - if(s_reqb->req.stat==REQ_OK) - { - RETURN_LONG(s_reqb->req.param); - } - else - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - RETURN_FALSE; - } - - IFCONNECT_END + param = PHPMSessionGetInt(MREQ_SLOCK, szsession, "","",0 TSRMLS_CC); + RETURN_LONG(param); } /* }}} */ -/* {{{ proto int msession_ctl(string name) + +/* {{{ proto int msession_stat(string name) Lock a session */ PHP_FUNCTION(msession_ctl) { @@ -508,25 +836,25 @@ PHP_FUNCTION(msession_ctl) "NOW", NULL }; - IFCONNECT_BEGIN + int stat = 0; + int param; + char *szsession; zval **session; zval **which; - int fn = REQ_STAT_EXIST; - + int fn = MREQ_CTL_EXIST; int n = ZEND_NUM_ARGS(); + ELOG("msession_ctl"); if(n != 1 && n != 2) { WRONG_PARAM_COUNT; } - if(zend_get_parameters_ex(n,&session,&which) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_string_ex(session); szsession = Z_STRVAL_PP(session); @@ -540,44 +868,28 @@ PHP_FUNCTION(msession_ctl) { if(strcasecmp(parray[i], szwhich) == 0) { - ELOG(szwhich); - fn = i; break; } } } - - PHPExecRequest( REQ_CTL, szsession, "","",fn); - - if(s_reqb->req.stat==REQ_OK) - { -#ifdef ERR_DEBUG - char buffer[128]; - sprintf(buffer, "ret:%s", s_reqb->req.datum); - ELOG(buffer); -#endif - s_reqb->req.param = atoi(s_reqb->req.datum); - RETURN_LONG(s_reqb->req.param); - } - else - { - ELOG("msession_ctl failed"); - RETURN_FALSE; - } - IFCONNECT_END + param = PHPMSessionGetInt(MREQ_CTL, szsession, "", "", fn TSRMLS_CC); + RETURN_LONG(param); } /* {{{ proto int msession_unlock(string session, int key) Unlock a session */ PHP_FUNCTION(msession_unlock) { - IFCONNECT_BEGIN + int stat; + int param; + char *szsession; long lkey; zval **session; zval **key; + ELOG("msession_unlock"); if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &session, &key) == FAILURE) { WRONG_PARAM_COUNT; @@ -586,18 +898,9 @@ PHP_FUNCTION(msession_unlock) szsession = Z_STRVAL_PP(session); convert_to_long_ex(key); lkey = Z_LVAL_PP(key); - PHPExecRequest( REQ_SUNLOCK, szsession, "","",lkey); - if(s_reqb->req.stat==REQ_OK) - { - RETURN_LONG(s_reqb->req.param); - } - else - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - RETURN_FALSE; - } - IFCONNECT_END + param = PHPMSessionGetInt(MREQ_SUNLOCK, szsession, "", "", lkey TSRMLS_CC); + RETURN_LONG(param); } /* }}} */ @@ -605,14 +908,14 @@ PHP_FUNCTION(msession_unlock) Set value in session */ PHP_FUNCTION(msession_set) { - IFCONNECT_BEGIN - + int stat; char *szsession; char *szname; char *szvalue; zval **session; zval **name; zval **value; + ELOG("msession_get"); if(ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3,&session,&name,&value) == FAILURE) { @@ -626,18 +929,16 @@ PHP_FUNCTION(msession_set) szname = Z_STRVAL_PP(name); szvalue = Z_STRVAL_PP(value); - PHPExecRequest( REQ_SETVAL, szsession, szname, szvalue, 0); + stat = PHPMSessionExec(MREQ_SETVAL, szsession, szname, szvalue, 0 TSRMLS_CC); - if(s_reqb->req.stat==REQ_OK) + if(stat==MREQ_OK) { RETURN_TRUE; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); RETURN_FALSE; } - IFCONNECT_END } /* }}} */ @@ -645,13 +946,14 @@ PHP_FUNCTION(msession_set) Get value from session */ PHP_FUNCTION(msession_get) { - IFCONNECT_BEGIN + int stat; + char *szvalue=NULL; char *szsession; char *szname; - char *szvalue; zval **session; zval **name; zval **value; + ELOG("msession_get"); if(ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3,&session,&name,&value) == FAILURE) { @@ -664,52 +966,79 @@ PHP_FUNCTION(msession_get) szname = Z_STRVAL_PP(name); szvalue = Z_STRVAL_PP(value); - PHPExecRequest( REQ_GETVAL, szsession, szname, szvalue,0); + szvalue = PHPMSessionGetString(MREQ_GETVAL, szsession,szname, szvalue, 0 TSRMLS_CC); - if(s_reqb->req.stat==REQ_OK) + if(szvalue) { - szvalue = safe_estrdup(s_reqb->req.datum); - RETURN_STRING(szvalue, 0) + RETURN_STRING(szvalue,0) } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); RETURN_NULL(); } - IFCONNECT_END } /* }}} */ -/* {{{ proto string msession_uniq(int num_chars) +/* {{{ proto string msession_uniq(int num_chars[, string classname[, string data]]) Get uniq id */ PHP_FUNCTION(msession_uniq) { - IFCONNECT_BEGIN + int stat=0; + int ret = FAILURE; + int cbname; + zval **session; + zval **zclassname; + zval **zdata; + char *szsession; + char *classname="phpsession"; + char *data = NULL; + int nargs = ZEND_NUM_ARGS(); - long val; - zval **param; - - if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1,¶m) == FAILURE) + ELOG("msession_uniq"); + if(nargs < 1) { WRONG_PARAM_COUNT; } - convert_to_long_ex(param); - val = Z_LVAL_PP(param); + if(nargs == 1) + { + ret = zend_get_parameters_ex(1, &session); + convert_to_string_ex(session); + szsession = Z_STRVAL_PP(session); + } + else if(nargs == 2) + { + ret = zend_get_parameters_ex(2, &session, &zclassname); + convert_to_string_ex(session); + convert_to_string_ex(zclassname); + szsession = Z_STRVAL_PP(session); + classname = Z_STRVAL_PP(zclassname); + } + else if(nargs == 3) + { + ret = zend_get_parameters_ex(3, &session, &zclassname, &zdata); + convert_to_string_ex(session); + convert_to_string_ex(zclassname); + convert_to_string_ex(zdata); + szsession = Z_STRVAL_PP(session); + classname = Z_STRVAL_PP(zclassname); + data = Z_STRVAL_PP(zdata); + } + else + { + WRONG_PARAM_COUNT; + } - PHPExecRequest( REQ_UNIQ,"", "", "",val); + cbname = atoi(szsession); + szsession = PHPMSessionGetString(MREQ_UNIQ, NULL, classname, data,MPARM_CREATE(0,cbname) TSRMLS_CC); - if(s_reqb->req.stat==REQ_OK) + if(szsession) { - char *szval = safe_estrdup(s_reqb->req.datum); - RETURN_STRING(szval, 0) + RETURN_STRING(szsession,0) } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); RETURN_NULL(); } - IFCONNECT_END - } /* }}} */ @@ -717,9 +1046,11 @@ PHP_FUNCTION(msession_uniq) Get random string */ PHP_FUNCTION(msession_randstr) { - IFCONNECT_BEGIN + int stat; + char *szvalue=NULL; long val; zval **param; + ELOG("msession_randstr"); if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1,¶m) == FAILURE) { @@ -728,19 +1059,16 @@ PHP_FUNCTION(msession_randstr) convert_to_long_ex(param); val = Z_LVAL_PP(param); - PHPExecRequest( REQ_RANDSTR,"", "", "",val); + szvalue = PHPMSessionGetString(MREQ_RANDSTR, "","","",val TSRMLS_CC); - if(s_reqb->req.stat==REQ_OK) + if(szvalue) { - char *szval = safe_estrdup(s_reqb->req.datum); - RETURN_STRING(szval, 0) + RETURN_STRING(szvalue,0) } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); RETURN_NULL(); } - IFCONNECT_END } /* }}} */ @@ -748,12 +1076,12 @@ PHP_FUNCTION(msession_randstr) Find all sessions with name and value */ PHP_FUNCTION(msession_find) { - IFCONNECT_BEGIN - + int stat; char *szname; char *szvalue; zval **name; zval **value; + ELOG("msession_find"); if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &name, &value) == FAILURE) { @@ -764,28 +1092,8 @@ PHP_FUNCTION(msession_find) szname = Z_STRVAL_PP(name); szvalue = Z_STRVAL_PP(value); - PHPExecRequest( REQ_FIND, "", szname, szvalue,0); + stat = PHPMSessionGetArray(return_value, MREQ_FIND, "", szname, szvalue, 0 TSRMLS_CC); - if(s_reqb->req.stat==REQ_OK && s_reqb->req.param) - { - int i; - char *str = s_reqb->req.datum; - array_init(return_value); - - for(i=0; i < s_reqb->req.param; i++) - { - int cbstr = strlen(str); - char *data = safe_estrdup(str); - add_index_string(return_value, i, data, 0); - str += (cbstr+1); - } - } - else if(s_reqb->req.stat != REQ_OK) - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - RETURN_NULL(); - } - IFCONNECT_END } /* }}} */ @@ -793,31 +1101,8 @@ PHP_FUNCTION(msession_find) List all sessions */ PHP_FUNCTION(msession_list) { - IFCONNECT_BEGIN - PHPExecRequest( REQ_LIST, "", "", "",0); - - if(s_reqb->req.stat==REQ_OK && s_reqb->req.param) - { - int i; - char *str = s_reqb->req.datum; - array_init(return_value); - - for(i=0; i < s_reqb->req.param; i++) - { - int cbstr = strlen(str); - char *data = safe_estrdup(str); - add_index_string(return_value, i, data, 0); - str += (cbstr+1); - } - } - else if(s_reqb->req.stat != REQ_OK) - { - /* May this should be an error? */ - if(s_reqb->req.param != REQE_NOSESSION) - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - RETURN_NULL(); - } - IFCONNECT_END + ELOG("msession_list"); + int stat = PHPMSessionGetArray(return_value, MREQ_LIST, "", "","", 0 TSRMLS_CC); } /* }}} */ @@ -825,57 +1110,42 @@ PHP_FUNCTION(msession_list) Get array of msession variables */ PHP_FUNCTION(msession_get_array) { - IFCONNECT_BEGIN + int stat; char *szsession; zval **session; + ELOG("msession_getarray"); if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &session) == FAILURE) { WRONG_PARAM_COUNT; - } + } convert_to_string_ex(session); szsession = Z_STRVAL_PP(session); - PHPExecRequest( REQ_GETALL, szsession, "", "",0); - - array_init(return_value); - - if(s_reqb->req.stat == REQ_OK) - { - int i; - char *str = s_reqb->req.datum; - int num = s_reqb->req.param*2; - for(i=0; i < num; i+=2) - { - int cbvalue; - int cbname; - char *szvalue; - char *szname; - - cbname = strlen(str); - szname = safe_estrndup(str,cbname); - str += (cbname+1); - - cbvalue = strlen(str); - szvalue = safe_estrndup(str,cbvalue); - str += (cbvalue+1); - add_assoc_string(return_value, szname, szvalue, 0); - } - } - else - { - if(s_reqb->req.param != REQE_NOSESSION) - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - RETURN_NULL(); - } - IFCONNECT_END + stat = PHPMSessionGetArrayPairs(return_value, MREQ_GETALL, szsession, "","", 0 TSRMLS_CC); } /* }}} */ +/* {{{ proto object msession_get_object(string object) + Get msession variable object */ +PHP_FUNCTION(msession_get_object) +{ +// FIXME FIXME FIXME + int stat; + char *szsession; + zval **session; + if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &session) == FAILURE) + { + WRONG_PARAM_COUNT; + } + convert_to_string_ex(session); + szsession = Z_STRVAL_PP(session); + stat = PHPMSessionGetArrayPairs(return_value, MREQ_GETOBJECT, szsession, NULL, NULL, 0 TSRMLS_CC); +} +/* }}} */ /* {{{ proto bool msession_set_array(string session, array tuples) Set msession variables from an array*/ PHP_FUNCTION(msession_set_array) { - IFCONNECT_BEGIN zval **session; zval **tuples; HashPosition pos; @@ -892,8 +1162,10 @@ PHP_FUNCTION(msession_set_array) char **pairs; HashTable *htTuples; int i; - int countpair; + + ELOG("msession_set_array"); + TSRMLS_FETCH(); if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &session, &tuples) == FAILURE) { @@ -903,7 +1175,13 @@ PHP_FUNCTION(msession_set_array) countpair = zend_hash_num_elements(htTuples); - pairs = (char **)safe_emalloc(sizeof(char *), countpair * 2, 0); + pairs = (char **)emalloc(sizeof(char *) * countpair * 2); + + if(!pairs) + { + ELOG("no pairs"); + RETURN_FALSE; + } ELOG("have pairs"); @@ -939,12 +1217,16 @@ PHP_FUNCTION(msession_set_array) zend_hash_move_forward_ex(htTuples, &pos); } - PHPExecRequestMulti(REQ_SETVAL, Z_STRVAL_PP(session), countpair, pairs,0); + IFCONNECT_BEGIN + ELOG("FormatMulti"); + FormatRequestStrings(&t_reqb, MREQ_SETVAL, Z_STRVAL_PP(session), NULL, NULL, ndx, pairs); + PHPDoRequest(&t_conn,&t_reqb); - if(s_reqb->req.stat != REQ_OK) - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); + if(t_reqb.req->stat != MREQ_OK) + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); efree((void *)pairs); IFCONNECT_END + } /* }}} */ @@ -952,46 +1234,15 @@ PHP_FUNCTION(msession_set_array) return associative array of value:session for all sessions with a variable named 'name' */ PHP_FUNCTION(msession_listvar) { - IFCONNECT_BEGIN + int stat; zval **name; if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &name) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(name); - PHPExecRequest( REQ_LISTVAR, "", Z_STRVAL_PP(name), "",0); - - array_init(return_value); - - if(s_reqb->req.stat == REQ_OK) - { - int i; - char *str = s_reqb->req.datum; - int num = s_reqb->req.param*2; - for(i=0; i < num; i+=2) - { - int cbvalue; - int cbname; - char *szvalue; - char *szname; - - cbname= strlen(str); - szname= safe_estrndup(str,cbname); - str += (cbname+1); - - cbvalue = strlen(str); - szvalue = safe_estrndup(str,cbvalue); - str += (cbvalue+1); - add_assoc_string(return_value, szname, szvalue, 0); - } - } - else - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - RETURN_NULL(); - } - IFCONNECT_END + stat = PHPMSessionGetArrayPairs(return_value, MREQ_LISTVAR, "", Z_STRVAL_PP(name),"",0 TSRMLS_CC); } /* }}} */ @@ -999,11 +1250,13 @@ PHP_FUNCTION(msession_listvar) Set/get session timeout */ PHP_FUNCTION(msession_timeout) { - IFCONNECT_BEGIN + int stat; + int param; zval **session; int ac = ZEND_NUM_ARGS(); int zstat = FAILURE; int timeout = 0; + ELOG("msession_timeout"); if(ac == 1) { zstat = zend_get_parameters_ex(1, &session); @@ -1020,60 +1273,127 @@ PHP_FUNCTION(msession_timeout) WRONG_PARAM_COUNT; } convert_to_string_ex(session); - PHPExecRequest( REQ_TIMEOUT, Z_STRVAL_PP(session), "","",timeout); - if(s_reqb->req.stat == REQ_OK) + param = PHPMSessionGetInt(MREQ_TIMEOUT, Z_STRVAL_PP(session),"","",timeout TSRMLS_CC); + + RETURN_LONG(param); +} +/* }}} */ + +/* {{{ proto string msession_inc(string session, string name) + Increment value in session */ +PHP_FUNCTION(msession_inc) +{ + int stat; + char *szvalue; + zval **session; + zval **name; + + if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &session, &name) == FAILURE) { - RETURN_LONG( s_reqb->req.param); + WRONG_PARAM_COUNT; + } + convert_to_string_ex(session); + convert_to_string_ex(name); + + szvalue = PHPMSessionGetString(MREQ_ADD, Z_STRVAL_PP(session), Z_STRVAL_PP(name),"1",0 TSRMLS_CC); + + if(szvalue) + { + RETURN_STRING(szvalue,0) } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); RETURN_NULL(); } - IFCONNECT_END } /* }}} */ +/* {{{ proto string msession_add(string session, string name, string value) + Increment value in session */ +PHP_FUNCTION(msession_add) +{ + int stat; + char *szvalue; + zval **session; + zval **name; + zval **value; + ELOG("msession_add"); + + if(ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &session, &name, &value) == FAILURE) + { + WRONG_PARAM_COUNT; + } + convert_to_string_ex(session); + convert_to_string_ex(name); + convert_to_string_ex(value); -/* {{{ proto string msession_inc(string session, string name) + szvalue = PHPMSessionGetString(MREQ_ADD, Z_STRVAL_PP(session), Z_STRVAL_PP(name),Z_STRVAL_PP(value),0 TSRMLS_CC); + + if(szvalue) + { + RETURN_STRING(szvalue,0) + } + else + { + RETURN_NULL(); + } +} +/* }}} */ +/* {{{ proto string msession_muldiv(string session, string name, string mul, string div) Increment value in session */ -PHP_FUNCTION(msession_inc) +PHP_FUNCTION(msession_muldiv) { - IFCONNECT_BEGIN - char *val; + int stat; + char *szvalue=NULL; zval **session; zval **name; + zval **mul; + zval **div; + char *pdata; + ELOG("msession_muldiv"); - if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &session, &name) == FAILURE) + if(ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &session, &name, &mul, &div) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(session); convert_to_string_ex(name); + convert_to_string_ex(mul); + convert_to_string_ex(div); + + pdata = Z_STRVAL_PP(div); + + IFCONNECT_BEGIN + FormatRequestData(&t_reqb, MREQ_MULDIV, + Z_STRVAL_PP(session), Z_STRVAL_PP(name), Z_STRVAL_PP(mul), pdata, strlen(pdata)+1, 0); - PHPExecRequest( REQ_INC, Z_STRVAL_PP(session), Z_STRVAL_PP(name),0,0); + PHPDoRequest(&t_conn, &t_reqb); - if(s_reqb->req.stat==REQ_OK) + if(t_reqb.req->stat == MREQ_OK) + szvalue = safe_estrdup(t_reqb.req->datum); + else + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); + + IFCONNECT_ENDNR + + if(szvalue) { - val = safe_estrdup(s_reqb->req.datum); - RETURN_STRING(val, 0) + RETURN_STRING(szvalue,0) } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - RETURN_FALSE; + RETURN_NULL(); } - IFCONNECT_END } /* }}} */ -/* {{{ proto string msession_get_data(string session) +/* {{{ proto string msession_getdata(string session) Get data session unstructured data. (PHP sessions use this) */ -PHP_FUNCTION(msession_get_data) +PHP_FUNCTION(msession_getdata) { - IFCONNECT_BEGIN char *val; zval **session; + ELOG("msession_getdata"); if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &session) == FAILURE) { @@ -1081,7 +1401,7 @@ PHP_FUNCTION(msession_get_data) } convert_to_string_ex(session); - val = PHPMsessionGetData(Z_STRVAL_PP(session) TSRMLS_CC); + val = PHPMSessionGetString(MREQ_DATAGET, Z_STRVAL_PP(session), "", "", 0 TSRMLS_CC); if(val) { @@ -1091,17 +1411,17 @@ PHP_FUNCTION(msession_get_data) { RETURN_NULL(); } - IFCONNECT_END } /* }}} */ -/* {{{ proto bool msession_set_data(string session, string value) +/* {{{ proto bool msession_setdata(string session, string value) Set data session unstructured data. (PHP sessions use this) */ -PHP_FUNCTION(msession_set_data) +PHP_FUNCTION(msession_setdata) { - IFCONNECT_BEGIN + int stat; zval **session; zval **value; + ELOG("msession_setdata"); if(ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &session, &value) == FAILURE) { @@ -1109,8 +1429,10 @@ PHP_FUNCTION(msession_set_data) } convert_to_string_ex(session); convert_to_string_ex(value); + char *szdata = Z_STRVAL_PP(value); - if(PHPMsessionSetData(Z_STRVAL_PP(session), Z_STRVAL_PP(value) TSRMLS_CC)) + stat = PHPMSessionExecData(MREQ_DATASET, Z_STRVAL_PP(session),NULL,NULL,szdata,strlen(szdata),0 TSRMLS_CC); + if(stat==MREQ_OK) { RETURN_TRUE; } @@ -1118,7 +1440,6 @@ PHP_FUNCTION(msession_set_data) { RETURN_FALSE; } - IFCONNECT_END } /* }}} */ @@ -1126,13 +1447,14 @@ PHP_FUNCTION(msession_set_data) Call the personality plugin escape function */ PHP_FUNCTION(msession_plugin) { - IFCONNECT_BEGIN int ret; - char *retval; + char *retval=NULL; zval **session; zval **val; zval **param=NULL; + char *szparam; + ELOG("msession_plugin"); if(ZEND_NUM_ARGS() == 3) { ret = zend_get_parameters_ex(3, &session, &val, ¶m); @@ -1154,45 +1476,43 @@ PHP_FUNCTION(msession_plugin) convert_to_string_ex(val); ret = atoi(Z_STRVAL_PP(val)); + szparam = (param) ? Z_STRVAL_PP(param) : ""; - PHPExecRequest( REQ_PLUGIN, Z_STRVAL_PP(session), Z_STRVAL_PP(val), param ? Z_STRVAL_PP(param) : "",ret); + retval =PHPMSessionGetString(MREQ_PLUGIN, Z_STRVAL_PP(session), Z_STRVAL_PP(val), szparam, ret TSRMLS_CC); - if(s_reqb->req.stat==REQ_OK && s_reqb->req.len) - { - retval = safe_estrdup(s_reqb->req.datum); + if(retval) + { RETURN_STRING(retval, 0) } - else if(s_reqb->req.stat != REQ_OK) + else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); RETURN_FALSE; - } - IFCONNECT_END } /* }}} */ -#define MAX_EXT_VAL 5 +#define MAX_EXT_VAL 6 /* {{{ proto string msession_call(string fn_name, [, string param1 ], ... [,string param4]) Call the plugin function named fn_name */ PHP_FUNCTION(msession_call) { - IFCONNECT_BEGIN int n; int i; int ret; - char *retval; + char *retval=NULL; zval **val[MAX_EXT_VAL]; char *strings[MAX_EXT_VAL+1]; /* zval **param=NULL; */ + TSRMLS_FETCH(); n = ZEND_NUM_ARGS(); + ELOG("msession_call"); if((n < 1) || (n > MAX_EXT_VAL)) { WRONG_PARAM_COUNT; } - ret = zend_get_parameters_ex(ZEND_NUM_ARGS(), &val[0],&val[1],&val[2],&val[3],&val[4]); + ret = zend_get_parameters_ex(n, &val[0],&val[1],&val[2],&val[3],&val[4],&val[5],&val[6]); if(ret == FAILURE) { @@ -1203,69 +1523,27 @@ PHP_FUNCTION(msession_call) convert_to_string_ex(val[i]); strings[i] = Z_STRVAL_PP(val[i]); } + strings[i]=0; - PHPExecRequestStrings(REQ_CALL, NULL , n, strings); - - if(s_reqb->req.stat==REQ_OK && s_reqb->req.len) - { - retval = safe_estrdup(s_reqb->req.datum); - RETURN_STRING(retval, 0) - } - else if(s_reqb->req.stat != REQ_OK) - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - RETURN_FALSE; - } - IFCONNECT_END -} -/* }}} */ -/* {{{ proto string msession_exec(string cmdline) - executes a program on msession system */ -PHP_FUNCTION(msession_exec) -{ IFCONNECT_BEGIN - char *val; - zval **cmdline; - - if(ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &cmdline) == FAILURE) - { - WRONG_PARAM_COUNT; - } - convert_to_string_ex(cmdline); + FormatRequestStrings(&t_reqb, MREQ_CALL, NULL , strings[0], NULL, n-1, (n>1) ? &strings[1] : NULL); + PHPDoRequest(&t_conn, &t_reqb); - PHPExecRequest( REQ_EXEC, Z_STRVAL_PP(cmdline), "","",0); - - if(s_reqb->req.stat == REQ_OK) - { - val = safe_estrdup(s_reqb->req.datum); - RETURN_STRING(val, 0) - } + if(t_reqb.req->stat==MREQ_OK && t_reqb.req->len) + retval = safe_estrdup(t_reqb.req->datum); else - { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - RETURN_NULL(); - } - IFCONNECT_END -} -/* }}} */ -/* {{{ proto bool msession_ping(void) - returns non-zero if msession is alive*/ -PHP_FUNCTION(msession_ping) -{ - IFCONNECT_BEGIN + php_error(E_WARNING, s_szErrFmt, get_active_function_name(TSRMLS_C), ReqbErr(&t_reqb)); - PHPExecRequest(REQ_PING,"","","", 0); + IFCONNECT_ENDNR - if(s_reqb->req.stat==REQ_OK) - { - RETURN_TRUE; + if(retval) + { + RETURN_STRING(retval, 0) } - else + else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); RETURN_FALSE; } - IFCONNECT_END } /* }}} */ @@ -1273,32 +1551,19 @@ PHP_FUNCTION(msession_ping) PS_OPEN_FUNC(msession) { - int port; - int len = strlen(save_path)+1; - char * path = emalloc(len); - char * szport; + char *szport; + char host[MAX_HOST]; + int port = msession_split_host_port(host, sizeof(host), (char *)save_path, DEF_PORT); - strcpy(path, save_path); + ELOG("PS_OPEN_FUNC"); - szport = strchr(path, ':'); - - if(szport) - { - *szport++=0; - port = atoi(szport); - } - else - { - port = s_port; - } - ELOG( "ps_open_msession"); PS_SET_MOD_DATA((void *)1); /* session.c needs a non-zero here! */ - if (PHPMsessionConnect(path, port)) { - efree(path); + if (PHPMSessionConnect(host, port)) { + ELOG("Success"); return SUCCESS; } else { - efree(path); + ELOG("Failure"); return FAILURE; } } @@ -1306,18 +1571,20 @@ PS_OPEN_FUNC(msession) PS_CLOSE_FUNC(msession) { ELOG( "ps_close_msession"); - PHPMsessionDisconnect(); + PHPMSessionDisconnect(); + PS_SET_MOD_DATA((void *)0); return SUCCESS; } PS_READ_FUNC(msession) { ELOG( "ps_read_msession"); - *val = PHPMsessionGetData((char *)key TSRMLS_CC); + *val = PHPMSessionGetString(MREQ_DATAGET, (char *)key, "", "", 0 TSRMLS_CC); if(*val) - { - *vallen = strlen(*val); - } + { + *vallen = strlen(*val); + ELOG( *val ); + } else { *val = emalloc(1); @@ -1329,14 +1596,18 @@ PS_READ_FUNC(msession) PS_WRITE_FUNC(msession) { - ELOG( "ps_write_msession"); - return (PHPMsessionSetData((char *)key, (char *)val TSRMLS_CC)) ? SUCCESS : FAILURE; + ELOG("PS_WRITE_FUNC"); + ELOG(key); + ELOG(val); + int stat = PHPMSessionExecData(MREQ_DATASET, key, NULL, NULL, val, strlen(val), 0 TSRMLS_CC); + return (stat == MREQ_OK) ? SUCCESS : FAILURE; } PS_DESTROY_FUNC(msession) { ELOG( "ps_destroy_msession"); - return (PHPMsessionDestroy((char *)key TSRMLS_CC)) ? SUCCESS : FAILURE; + int stat = PHPMSessionExec(MREQ_DROP, (char *)key, "", "", 0 TSRMLS_CC); + return (stat == MREQ_OK) ? SUCCESS : FAILURE; } PS_GC_FUNC(msession) @@ -1348,26 +1619,19 @@ PS_GC_FUNC(msession) #ifdef HAVE_PHP_SESSION_CREATESID PS_CREATE_SID_FUNC(msession) { - if(s_reqb && s_conn) - { - ELOG("Should create a new session"); - PHPExecRequest( REQ_UNIQ,"", "", "",SID_LEN); + ELOG("PS_CREATE_SID_FUNCx"); + char * sessid = PHPMSessionGetString(MREQ_UNIQ, "","phpsess","",MPARM_CREATE(0,SID_LEN) TSRMLS_CC); - if(s_reqb->req.stat==REQ_OK) - { - ELOG("Reports UNIQ success"); - if(newlen) - *newlen = SID_LEN; - return safe_estrdup(s_reqb->req.datum); - } - else - { - ELOG("Reports that UNIQ failed"); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ReqbErr(s_reqb)); - } - } - ELOG("Yikes, could not get sid from msession 2003"); - return php_session_create_id(mod_data, newlen TSRMLS_CC); + if(!sessid || !strlen(sessid)) + ELOG("No SID returned from UNIQ"); + else + ELOG(sessid); + + if(!sessid) + sessid = php_session_create_id(mod_data, newlen TSRMLS_CC); + + ELOG(sessid); + return sessid; } #endif /* HAVE_PHP_SESSION_CREATESID */ #endif /* HAVE_PHP_SESSION */ diff --git a/ext/msession/msessionapi.h b/ext/msession/msessionapi.h new file mode 100644 index 0000000000..fba66d54ce --- /dev/null +++ b/ext/msession/msessionapi.h @@ -0,0 +1,366 @@ +/* + Mohawk Software Framework by Mohawk Software + Copyright (C) 1998-2001 Mark L. Woodward + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA + + If you want support or to professionally license this library, the author + can be reached at info@mohawksoft.com + + When used as part of the PHP project this file, as present in the PHP + CVS and original source distribution is to be considered part of the + PHP package and subject to version 2.02 of the PHP license. + http://www.php.net/license/2_02.txt. + +*/ +#ifndef _MCACHE_API_ +#define _MCACHE_API_ + +#include "reqclient.h" + +#ifdef timeout +#undef timeout +#endif + +#ifdef __cplusplus +class MCache_conn : public MSock +{ + protected: + REQB m_reqb; + int m_errno; + public: + MCache_conn(char *host, int port); + ~MCache_conn(); + int Request(int stat, char *session, char *name, char *value, int param=0); + int RequestData(int stat, char *session, char *name, char *value, char *data, int cbdata, int param=0); + int RequestStrings(int stat, char *session, char *name, char *value, int n, char **strings); + char * ResultToStr(); + char ** ResultToArray(); + char ** ResultToPairs(); + Boolean create(char *session, char *classname, char *strdata); + Boolean create_pairs(char *session, char *classname, char *strdata, int count, char **pairs); + Boolean destroy(char *session); + int set(char *session, char *name, char *value); + int set_array(char *session, int count, char **pairs); + int get_int(char *session, char *name, int def); + char *get_text(char *session, char *name, char *def); + char * uniq(int cbstr, char *classname, char *strdata); + char * randstr(int cbstr); + char **get_array(char *session); + char **get_object(char *session); + char **listvar(char *name); + char **find(char *name, char *value); + char **list(); + int count(); + int lock(char *session); + int unlock(char *session, int key); + Boolean timeout(char *session, int timeout); + int inc_int(char *session, char *name); + char *inc_text(char *session, char *name); + int setdata(char *session, char *data); + char *getdata(char *session); + int setdata(char *session, char *data, int len); + char *getdata(char *session, int *len); + int call_int(char *fn,int n, char **strings); + char *call_text(char *fn,int n, char **strings); + int ctl(char *session,int type); + int plugin_int(char *session, char *nparam, char *sparam, int param); + char *plugin_text(char *session, char *nparam, char *sparam, int param); + int Sessionflush(int seconds); + int admin(int fn, char *opt); + int serialize(char *szparam); + char *exec(char *szcommand); + char *add(char *session, char *name, char *value); + char *muldiv(char *session, char *name, char *mul, char *div); + int ping(void); + static void free(void *result); + static int ctl_type(char *str); + inline Boolean StatOK(void) {return (m_reqb.req->stat == REQ_OK) ? TRUE : FALSE; } + inline int param() {return m_reqb.param; } + inline int merrno() {return m_errno;} + inline char *errtext() {return ReqbErr(&m_reqb); } + inline char *reqtext(void) {return (m_reqb.req->len > 1) ? m_reqb.req->datum : NULL; } + +}; + +typedef MCache_conn *MSCONN; + +extern "C" { +#else +typedef void * MSCONN; +#endif + +#define MAX_REQ_STRINGS 1024 +#define MREQ_TOPHANDLER -42 + +enum REQ_TYPES +{ + MREQ_CTL=2, + MREQ_SETVAL, + MREQ_GETVAL, + MREQ_CREATE, + MREQ_DROP, + MREQ_GETALL, + MREQ_FIND, + MREQ_COUNT, + MREQ_FLUSH, + MREQ_SLOCK, + MREQ_SUNLOCK, + MREQ_TIMEOUT, + MREQ_ADD, + MREQ_DATAGET, + MREQ_DATASET, + MREQ_LIST, + MREQ_LISTVAR, + MREQ_UNIQ, + MREQ_RANDSTR, + MREQ_PLUGIN, + MREQ_CALL, + MREQ_SERIALIZE, + MREQ_RESTORE, + MREQ_EXEC, + MREQ_FIND_CACHEID, + MREQ_SET_CACHEID, + MREQ_RENAME, + MREQ_GETOBJECT, + MREQ_MULDIV, + MREQ_LAST, +// These are handled by the server, not the library. + MREQ_ADMIN = REQ_INTERNAL_END, +}; +#define MREQ_OK REQ_OK +#define MREQ_ERR REQ_ERR + +#ifdef PLUGMSG_START +enum MREQ_PLUGIN_MSG +{ + MREQ_PLUGIN_PULSE=PLUGMSG_START, + MREQ_PLUGIN_SESSION_FLUSH, + MREQ_PLUGIN_SESSION_CREATE, + MREQ_PLUGIN_SESSION_INIT, + MREQ_PLUGIN_SESSION_DESTROY, + MREQ_PLUGIN_ESCAPE, + MREQ_PLUGIN_RELAY, + MREQ_PLUGIN_SERIALIZE, + MREQ_PLUGIN_RESTORE, + MREQ_PLUGIN_SESSION_SETDATA, + MREQ_PLUGIN_SESSION_SETVALUE, + MREQ_PLUGIN_SESSION_SETBLOB, + MREQ_PLUGIN_SESSION_RENAME, + MREQ_PLUGIN_LAST +}; + +#define MREQ_SESSION_INIT_FAILED -1 +#define MREQ_SESSION_INIT_HANDLED 1 +#define MREQ_SESSION_INIT_NOTHANDLED 0 +#endif + +enum MCACHE_ADMIN +{ + MADMIN_SHUTDOWN, + MADMIN_LOADDLL, + MADMIN_SETSERIAL, + MADMIN_SETVERBOSE, + MADMIN_SETMAXTHREADS, + MADMIN_SERIALIZE, + MADMIN_AUTOSAVE, + MADMIN_AUTODROP, + MADMIN_STATUS +}; + +#define MREQ_CTL_EXIST 0 +#define MREQ_CTL_TTL 1 +#define MREQ_CTL_AGE 2 +#define MREQ_CTL_TLA 3 +#define MREQ_CTL_CTIM 4 +#define MREQ_CTL_TOUCH 5 +#define MREQ_CTL_NOW 6 + + +#define MPARM_TIME_MASK 0x0000FFFF +#define MPARM_TIME_FIXED 0x0000FFFF +#define MPARAM_CBSTR_MASK 0x00FF0000 + + +#define MPARM_TIME(parm) ((parm)&MPARM_TIME_MASK) +#define MPARM_CBSTR(parm) (((parm) >> 16) & 0xFF) + +#define MPARM_CREATE(TIME, CBSTR) \ + ((TIME) | (CBSTR << 16)) + +/* Added for MCache objects */ +#define MSTAT_TYPE_SESSION 0x00000000 +#define MSTAT_TYPE_OBJECT 0x00100000 +#define MSTAT_TYPE_FAST 0x00200000 +#define MSTAT_TYPE_FIXED 0x00400000 +#define MSTAT_TYPE_MASK 0x00F00000 +#define MSTAT_CMD_MASK 0x000FFFFF + +#define MSTAT_TYPE_TEST(stat, TY) (((stat) & MSTAT_TYPE_MASK)==(TY)) +#define MSTAT_CMD(stat) ((stat) & (MSTAT_CMD_MASK)) + +// Private session flags +#define MCACHE_FIXED MSTAT_TYPE_FIXED // Fixed object, no expiration +#define MCACHE_NORMAL MSTAT_TYPE_SESSION +#define MCACHE_OBJECT MSTAT_TYPE_OBJECT +#define MCACHE_FAST MSTAT_TYPE_FAST + +#ifdef WIN32 +#define EXTAPI extern "C" __declspec(dllexport) +#else +#define EXTAPI extern "C" +#endif + +#ifndef MCACHEAPI_INTERNAL +MSCONN mcache_connect(char *host, int port); + +/** Disconnects a session from an mcache daemon. Returns nothing. */ +void mcache_disconnect(MSCONN conn); + +/** Creates a new session. Returns TRUE is successful, or FALSE if an error occured. +An error occurs if the session already exists. mcache_errno() will return REQE_DUPSESSION +if the session exists */ + +Boolean mcache_create(MSCONN conn, char *session, char *classname, char *strdata); + +/** Behaves exactly like mcache_create, except that initial data is passed to populate the session. +"pairs" is an array of string data. pairs[0] contains a name, pairs[1] contains its value, pairs[2] +contains the second name, pairs[3] containes the second value, and so on. */ + +Boolean mcache_create_pairs(MSCONN conn, char *session, char *classname, char *strdata, int count, char **pairs); + +/** Destroys a session. Returns TRUE on success, FALSE on error. */ +Boolean mcache_destroy(MSCONN conn, char *session); + +/** Sets a value in a session. Returns TRUE on success, FALSE on error */ +int mcache_set(MSCONN conn, char *session, char *name, char *value); + +int mcache_set_array(MSCONN conn, char *session, int count, char **pairs); +/** Gets a value in a session as an integer. Returns the value from the session, or +def from the argument list, if no value was found. */ +int mcache_get_int(MSCONN conn, char *session, char *name, int def); + + +/** Gets a value in a session as a string. Returns the value from the session, or +def from the argument list, if no value was found. */ +char * mcache_get_text(MSCONN conn, char *session, char *name, char *def); + + +/** Create a new session with a guaranteed unique name. Returns a string of the name */ +char * mcache_uniq(MSCONN conn, int cbstr, char *classname, char *strdata); + +/** Create a pseudo-random alphanumeric string */ +char * mcache_randstr(MSCONN conn, int cbstr); + +/** Gets all the name/value pairs from a session into a character array. The array is +formatted as an array of string data. array[0] contains a name, array[1] contains its +value, array[2] contains the second name, array[3] containes the second value, and so on. +Returns NULL on error. */ + +char ** mcache_get_array(MSCONN conn, char *session); + +char **mcache_get_object(MSCONN conn, char *session); + +/** Finds sessions which have the particular name/value property. Returns NULL +if none were found. */ +char ** mcache_find(MSCONN conn, char *name, char *value); + +int mcache_inc_int(MSCONN conn, char *session, char *name); +char *mcache_inc_text(MSCONN conn, char *session, char *name); + +char *mcache_add(MSCONN conn, char *session, char *name, char *value); +char *mcache_muldiv(MSCONN conn,char *sn,char *nam,char *mul,char *div); + +/** Lists all active sessions. */ +char ** mcache_list(MSCONN conn); + +/** Returns an associative array of session:var for all sessions which have +a setting of name. */ +char **mcache_listvar(MSCONN conn, char *name); + +/** Returns the number of active sessions */ +int mcache_count(MSCONN conn); + +/** Locks a session. Returns an integer key for the lock. +Locks remain exclusive for 30 seconds, after which someone else +can lock the session. Locks are coperative, no enforcement +is attempted. */ + +int mcache_lock(MSCONN conn, char *session); + +/** Unlocks a session */ +int mcache_unlock(MSCONN conn, char *session, int key); + + +/** Sets the timeout of a session. + * timeout = 0 means never timeout + * timeout > 0 sets session expire time in seconds + * timeout < 0 resets to default timeout +*/ +Boolean mcache_timeout(MSCONN conn, char *session, int timeout); + +/** Sets the session data string, creates a session if one does not exist */ +int mcache_setdata(MSCONN conn, char *session, char *data); + +/** Gets the session data string from a session */ +char *mcache_getdata(MSCONN conn, char *session); + +/** Returns the last error. */ +int mcache_errno(MSCONN conn); + +/** Call a function plugin, return an int */ +int mcache_call_int(MSCONN conn, char *fn,int n, char **strings); + +/** Call a function plugin, return a char */ +char *mcache_call_text(MSCONN conn, char *fn,int n, char **strings); + +/** Call to get info about a session */ +int mcache_ctl(MSCONN conn, char *session, int type); + +/** Convert a string ctl to a type for mcache_ctl */ +int mcache_ctl_type(char *str); + +/** Call the personality plugin escape function, return int */ +int mcache_plugin_int(MSCONN conn, char *session, char *nparam, char *sparam, int param); + +/** Call the personality plugin escape function, return char * */ +char *mcache_plugin_text(MSCONN conn, char *session, char *nparam, char *sparam, int param); + +int mcache_flush(MSCONN conn, int seconds); +int mcache_loaddll(MSCONN conn, char *dll); +int mcache_shutdown(MSCONN conn, char *timeout); +int mcache_serialize(MSCONN conn, char *szparam); +char * mcache_exec(MSCONN conn, char *szparam); +int mcache_setserial(MSCONN conn, char *serialize); +int mcache_setmaxthreads(MSCONN conn, char *maxt); +int mcache_setverbose(MSCONN conn, char *verbose); +int mcache_setautosave(MSCONN conn, char *autsave); +int mcache_setautodrop(MSCONN conn, char *autdrop); +int mcache_ping(MSCONN conn); +int mcache_status(MSCONN conn); +Boolean mcache_statok(MSCONN conn); + +char *mcache_text(MSCONN conn); + +/** Returns a static character string describing an error. */ +char * mcache_errtext(MSCONN conn); +#endif + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/ext/msession/php_msession.h b/ext/msession/php_msession.h index b41df742f6..976bb9de2a 100644 --- a/ext/msession/php_msession.h +++ b/ext/msession/php_msession.h @@ -2,12 +2,12 @@ +----------------------------------------------------------------------+ | msession 1.0 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-2006 The PHP Group | + | Copyright (c) 1997-2003 The PHP Group | +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | + | 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 through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | + | 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. | @@ -35,6 +35,9 @@ extern zend_module_entry msession_module_entry; #endif PHP_MINIT_FUNCTION(msession); +PHP_MSHUTDOWN_FUNCTION(msession); +PHP_RINIT_FUNCTION(msession); +PHP_RSHUTDOWN_FUNCTION(msession); PHP_MINFO_FUNCTION(msession); PHP_FUNCTION(msession_connect); @@ -45,50 +48,41 @@ PHP_FUNCTION(msession_ctl); PHP_FUNCTION(msession_count); PHP_FUNCTION(msession_create); PHP_FUNCTION(msession_destroy); -PHP_FUNCTION(msession_exec); PHP_FUNCTION(msession_set); PHP_FUNCTION(msession_get); PHP_FUNCTION(msession_find); PHP_FUNCTION(msession_get_array); +PHP_FUNCTION(msession_save_object); +PHP_FUNCTION(msession_get_object); PHP_FUNCTION(msession_set_array); PHP_FUNCTION(msession_timeout); PHP_FUNCTION(msession_inc); -PHP_FUNCTION(msession_set_data); -PHP_FUNCTION(msession_get_data); +PHP_FUNCTION(msession_add); +PHP_FUNCTION(msession_muldiv); +PHP_FUNCTION(msession_setdata); +PHP_FUNCTION(msession_getdata); PHP_FUNCTION(msession_listvar); PHP_FUNCTION(msession_list); PHP_FUNCTION(msession_uniq); PHP_FUNCTION(msession_randstr); PHP_FUNCTION(msession_plugin); PHP_FUNCTION(msession_call); -PHP_FUNCTION(msession_ping); - -/* - Declare any global variables you may need between the BEGIN - and END macros here: +PHP_FUNCTION(msession_find_cacheid); +PHP_FUNCTION(msession_set_cacheid); ZEND_BEGIN_MODULE_GLOBALS(msession) - int global_variable; - void *conn; + int port; char *host; + void *conn; + void *reqb; ZEND_END_MODULE_GLOBALS(msession) -*/ -/* In every function that needs to use variables in php_msession_globals, - do call MSESSIONLS_FETCH(); after declaring other variables used by - that function, and always refer to them as MSESSIONG(variable). - You are encouraged to rename these macros something shorter, see - examples in any other php module directory. -*/ #ifdef ZTS -#define MSESSIONG(v) (msession_globals->v) -#define MSESSIONLS_FETCH() php_msession_globals *msession_globals = ts_resource(msession_globals_id) +#define MSESSION_G(v) TSRMG(msession_globals_id, zend_msession_globals *, v) #else -#define MSESSIONG(v) (msession_globals.v) -#define MSESSIONLS_FETCH() +#define MSESSION_G(v) (msession_globals.v) #endif - #else #define phpext_msession_ptr NULL diff --git a/ext/msession/reqclient.h b/ext/msession/reqclient.h index c8ff1c979b..414854f14a 100644 --- a/ext/msession/reqclient.h +++ b/ext/msession/reqclient.h @@ -1,76 +1,45 @@ /* - +----------------------------------------------------------------------+ - | msession 1.0 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2006 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: Mark Woodward | - | Portions copyright the PHP group. | - +----------------------------------------------------------------------+ - */ - -/* --------------------------------------------------------------------------- - These are definitions are pulled from Phoenix. - It would probably be easier to maintain one file, - but some phoenix header files conflict with other - project header files common to PHP. Besides, this - allows this header to be PHP license and Phoenix to - be LGPL with no conflicts. - - MAKE NO CHANGES TO THIS FILE IT. MUST REMAIN CONSTANT - WITH PHOENIX OR IT WILL NOT WORK. --------------------------------------------------------------------------- -*/ -/* Required to use the new version of Phoenix 1.2 */ + Mohawk Software Framework by Mohawk Software + Copyright (C) 1998-2001 Mark L. Woodward + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA + + If you want support or to professionally license this library, the author + can be reached at info@mohawksoft.com +*/ +#ifndef _REQCLIENT_ +#define _REQCLIENT_ #ifndef REQCLIENT_VER -#define REQCLIENT_VER 030113 +#define REQCLIENT_VER 051222 #endif -enum REQ_TYPES -{ - REQ_ERR, - REQ_OK, - REQ_CTL, - REQ_SETVAL, - REQ_GETVAL, - REQ_CREATE, - REQ_DROP, - REQ_GETALL, - REQ_FIND, - REQ_COUNT, - REQ_FLUSH, - REQ_SLOCK, - REQ_SUNLOCK, - REQ_TIMEOUT, - REQ_INC, - REQ_DATAGET, - REQ_DATASET, - REQ_LIST, - REQ_LISTVAR, - REQ_UNIQ, - REQ_RANDSTR, - REQ_PLUGIN, - REQ_CALL, - REQ_SERIALIZE, - REQ_RESTORE, - REQ_EXEC, - REQ_LAST, - REQ_INTERNAL_BEGIN=1023, - REQ_INTERNALLAST, -}; +#ifndef _PHOENIX_ +typedef unsigned char Boolean; +#endif + +#if defined (__cplusplus) +extern "C" { +#endif + +// Standard request transport errors. enum REQ_ERRORS { REQE_NOERROR=0, + REQE_ERR, REQE_NOSESSION, REQE_DUPSESSION, REQE_NOWLOCK, @@ -85,14 +54,47 @@ enum REQ_ERRORS REQE_BUFFER, REQE_DENIED, REQE_NOFN, - REQE_UNKNOWN + REQE_UNKNOWN, + REQE_PLUGIN1, + REQE_PLUGIN2, + REQE_PLUGIN3, + REQE_PLUGIN4 }; +#define REQ_OK 0 +#define REQ_ERR 1 +#define REQ_UNHANDLED 3 + +#define REQ_INTERNAL_BEGIN 1024 +#define REQ_POPEN 1024 +#define REQ_PCLOSE 1025 +#define REQ_PING 1026 +#define REQ_INTERNAL_END 2048 + +/** Packet version */ +#define REQV_VERSION 0x42 +#define REQV_VERMASK 0xFF + +/** Packet has array of strings */ +#define REQVF_STRINGS 0x0200 +/** Packet has binary information */ +#define REQVF_BVAL 0x0400 + + +#define REQV_ISVER(V) (((V) & REQV_VERMASK) == REQV_VERSION) +#define REQVF_TEST(V,F) (((V) & (F)) == (F)) +#define REQVSZ_VERSION "MV42" +#define REQVSZ_STRINGS "MS42" +#define REQVSZ_BVAL "MB42" + +#define REQFMT_BINARY 0 +#define REQFMT_ASCII 1 -#define REQ_POPEN 1024 -#define REQ_PCLOSE 1025 -#define REQ_PING 1026 +// An application can use this to change the default +extern unsigned int g_defReqFmt; +/** Binary request packet */ +/** If sizeof(int) > 4 this will need to be fixed. */ typedef struct _requestPacket { int version; @@ -102,70 +104,106 @@ typedef struct _requestPacket int name; int value; int param; + int data; char datum[0]; }REQ; +/*** Ascii string version of request packet */ +typedef struct _REQSZ +{ + char version[4]; + char stat[4]; + char len[4]; + char session[4]; + char name[4]; + char value[4]; + char param[4]; + char misc[4]; + char datum[0]; +}REQSZ; typedef struct _requestBuf { - unsigned int type; /* Type of packet, dynamic/static */ - unsigned int size; /* size of memory block */ + unsigned int type; // Type of packet, dynamic/static + unsigned int size; // size of memory block #if (REQCLIENT_VER >= 030113) - unsigned int fmt; /* format, binary/ascii */ - unsigned int reserved; /* Just in case */ + unsigned int fmt; // format, binary/ascii + unsigned int reserved; // Just in case #else -#warning You are using an old Phoenix definition, this will have problems with a newer version +#warning You are using an old Phoenix definition (pre 030113), this will have problems with a newer version +#endif +#if (REQCLIENT_VER >= 051222) + int cmd; + int param; + char * session; + char * name; + char * value; + char * data; + int cbdata; + + // To be used by library layer for bookeeping + int cb; + int count; #endif - REQ req; + REQ *req; }REQB; +typedef struct ListWalkDataStruct +{ + REQB *reqb; + char *Key; + char *Data; + int cb; + int count; +}ListWalkData; + #define MAX_REQ 16384 #define REQB_STATIC 1 #define REQB_DYNAMIC 2 -#define REQ_STAT_EXIST 0 -#define REQ_STAT_TTL 1 -#define REQ_STAT_AGE 2 -#define REQ_STAT_TLA 3 -#define REQ_STAT_CTIM 4 -#define REQ_STAT_TOUCH 5 -#define REQ_STAT_NOW 6 - #define STATIC_REQB( len ) \ char buffer [ len ]; \ REQB *preq = StaticRequestBuffer(buffer, len); -#if defined (__cplusplus) -extern "C" { -#endif +#define ASCII_REQB(reqb) (REQB)->fmt = REQFMT_ASCII +#define BINARY_REQB(reqb) (REQB)->fmt = REQFMT_BINARY -#define SIZEREQB(REQB,SIZE) (((REQB)->size >= (unsigned int) SIZE) ? REQB : SizeRequestBuffer(REQB,SIZE)) -REQB *AllocateRequestBuffer(unsigned size); +REQB *AllocateRequestBuffer(REQB *reqb, unsigned size); void FreeRequestBuffer(REQB *req); REQB *SizeRequestBuffer(REQB *req, unsigned int size); REQB *StaticRequestBuffer(char *buffer, unsigned int cb); -int FormatRequest(REQB **buffer, int stat, const char *session, const char *name, const char *value, int param); -int FormatRequestMulti(REQB **buffer, int stat, char *session, int n, char **pairs, int param); -int FormatRequestStrings(REQB **ppreq, int stat, char *session, int n, char **strings); -int DoSingleRequest(char *hostname, int port, REQB **preq); +int FilterRequest(REQB *preq); +int FormatRequest(REQB *preq, int stat, char *session, char *name, char *value, int param); +int FormatRequestData(REQB *preq, int stat, char *session, char *name, char *value, void *data, int cbdata, int param); +int FormatRequestStrings(REQB *preq, int stat, char *session, char *name, char *value, int n, char **strings); +int FormatRequestf(REQB *preq, int stat, char *session, char *name, char *value, int param, char *fmt, ...); + +void *GetReqbDatumPtr(REQB *preq); + +int DoSingleRequest(char *hostname, int port, REQB *preq); void *OpenReqConn(char *hostname, int port); +Boolean ReopenReqConn(void *conn); void CloseReqConn(void *conn); -void DeleteReqConn(void *conn); -unsigned char ReopenReqConn(void *conn); -int DoRequest(void *conn, REQB **preq); +int DoRequest(void *conn, REQB *preq); char *ReqbErr(REQB *reqb); +int LWDaddNameVal(ListWalkData *plw, char *name, char *value); +int LWDaddValue(ListWalkData *plw, char *value); + #define ASSERT_STAT(PREQ) if(PREQ->stat != REQ_OK) \ {fprintf(stderr, "Error in Request %s %d %s\n", \ __FILE__,__LINE__, ReqErr(PREQ->param)); exit(-1); } #if defined (__cplusplus) - /* C API but with class definitions */ - int ReadRequestTimeout(REQB **ppreq, MSock *sock, int timeout); - int ReadRequest(REQB **preq, MSock *sock); - int WriteRequest(REQB *preq, MSock *sock); + // C API but with class definitions + int ReadRequestTimeout(REQB *preq, MSock *sock, int timeout); + int ReadRequest(REQB *reqb, MSock *sock); + int WriteRequest(REQB *reqb, MSock *sock); + Boolean OpenReqSock(REQB *reqb, MSock *sock, char *hostname); + void CloseReqSock(REQB *reqb, MSock *sock); } -#endif +#endif +#endif -- 2.50.1