#include #include #include #include "../tlmpweb/tlmpweb.h" #include "tlmpwebsql.h" struct _F_websession_sql___v1_private{ _F_websession___v1 *cc; }; bool _F_websession_sql___v1::auth (WEBSESSION_SQL_INFO &info, const char *passwd) { return true; } void _F_websession_sql___v1::logout() { priv->cc->invalidate(); } void _F_websession_sql___v1::nosession(WEBSESSION_SQL_INFO &) { } void _F_websession_sql___v1::validsession(WEBSESSION_SQL_INFO &, bool &) { } void websession_sql___v1( _F_websession_sql___v1 &c, const char *realm, bool session_must_be_on) { glocal _F_websession_sql___v1 *c = &c; glocal WEBSESSION_SQL_INFO sinfo; _F_websession_sql___v1_private priv; c.priv = &priv; glocal.sinfo.user = ""; glocal.sinfo.lastaccess = 0; glocal.sinfo.since = 0; glocal.sinfo.realm = realm; (realm,session_must_be_on); glocal int ret = -1; glocal int *key = &info.key; glocal char *random = info.random; glocal.c->priv->cc = this; NSQL ns ("localhost","websession"); if (info.key != -1){ // Check if this is a valid session (ns,"select unix_timestamp(last_access) from sessions" " where num=%d and random='%s' and realm='%s'" ,info.key,info.random,info.realm); if (rownum > 0){ *glocal.key = -1; }else{ glocal.ret = 0; glocal.sinfo.lastaccess = atoi(row[0]); glocal.sinfo.since = time(NULL) - glocal.sinfo.lastaccess; } } if (glocal.ret == 0){ bool reject = false; glocal.c->validsession(glocal.sinfo,reject); if (reject){ delsession(info); glocal.ret = -1; } } return glocal.ret; glocal int ret = -1; glocal int *key = &info.key; glocal char *random = info.random; glocal.c->priv->cc = this; websession_setrandom (info.random); NSQL ns ("localhost","websession"); if (sql_action (ns,"insert into sessions (random,last_access,realm)" " values ('%s',now(),'%s')" ,info.random,info.realm) != -1){ // Trick to get the last auto-incremented value ? (ns,"select num from sessions" " where random='%s' and realm='%s'",info.random,info.realm); *glocal.key = atoi(row[0]); string cookie = string_f ("websession-%s",glocal.sinfo.realm); string val = string_f("%s-%s",row[0],glocal.random); websession_setcookie (cookie.c_str(),val.c_str(),0); glocal.ret = 0; glocal.sinfo.lastaccess = time(NULL); } return glocal.ret; NSQL ns ("localhost","websession"); sql_action (ns,"delete from sessions where num=%d",info.key); sql_action (ns,"delete from variables where num=%d",info.key); glocal NSQL *ns = NULL; glocal W_VAR **tbvar = tbvar; glocal int nbvar = nbvar; glocal int ret = 0; glocal int key = info.key; if (info.key != -1){ NSQL ns ("localhost","websession"); glocal.ns = &ns; (ns,"select count(*) from sessions" " where num=%d and random='%s'",info.key,info.random); int n = atoi(row[0]); if (n != 1){ // Unknown session or expired tlmpweb_error ("Unknown session???\n"); }else{ (*glocal.ns,"select variable,val from variables" " where num=%d",glocal.key); for (int i=0; igetname(),row[0])==0){ if (!v->isset()) v->setinitval (row[1]); break; } } } } return glocal.ret; NSQL ns ("localhost","websession"); glocal int key = info.key; glocal const char *name; glocal const char *val; glocal NSQL *ns = &ns; NSQL_ENCODE enc; for (int i=0; igetname(); string s = v->getvalstr(); glocal.val = enc.enc(s); (ns,"select count(*) from variables" " where num=%d and variable='%s'" ,info.key,glocal.name); if (row[0][0] == '1'){ sql_action (*glocal.ns,"update variables set val='%s'" " where num=%d and variable='%s'" ,glocal.val,glocal.key,glocal.name); }else{ sql_action (*glocal.ns,"insert into variables (num,variable,val)" " values" " (%d,'%s','%s')" ,glocal.key,glocal.name,glocal.val); } } sql_action (ns,"update sessions set last_access=now()" " where num=%d and random='%s'" ,info.key,info.random); return 0; glocal.c->nosession(glocal.sinfo); glocal.sinfo.user = info.user; bool ret = glocal.c->auth(glocal.sinfo,passwd); return ret; glocal.sinfo.user = info.user; glocal.c->exec(glocal.sinfo); } void websession_sql___v1(_F_websession_sql___v1 &c, const char *realm) { websession_sql___v1(c,realm,false); }