Back to index

php5  5.3.10
sess_sqlite.c
Go to the documentation of this file.
00001 /* 
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: John Coggeshall <john@php.net>                              |
00016    |          Wez Furlong <wez@thebrainroom.com>                          |
00017    +----------------------------------------------------------------------+
00018  */
00019 
00020 /* $Id: sess_sqlite.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "php.h"
00023 
00024 #if HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION)
00025 
00026 #include "ext/session/php_session.h"
00027 #include "ext/standard/php_lcg.h"
00028 #include <sqlite.h>
00029 #define SQLITE_RETVAL(__r) ((__r) == SQLITE_OK ? SUCCESS : FAILURE)
00030 #define PS_SQLITE_DATA sqlite *db = (sqlite*)PS_GET_MOD_DATA()
00031 extern int sqlite_encode_binary(const unsigned char *in, int n, unsigned char *out);
00032 extern int sqlite_decode_binary(const unsigned char *in, unsigned char *out);
00033 
00034 PS_FUNCS(sqlite);
00035 
00036 ps_module ps_mod_sqlite = {
00037        PS_MOD(sqlite)
00038 };
00039 
00040 PS_OPEN_FUNC(sqlite) 
00041 {
00042        char *errmsg = NULL;
00043        sqlite *db;
00044 
00045        /* TODO: do we need a safe_mode check here? */
00046        db = sqlite_open(save_path, 0666, &errmsg);
00047        if (db == NULL) {
00048               php_error_docref(NULL TSRMLS_CC, E_WARNING, 
00049                             "SQLite: failed to open/create session database `%s' - %s", save_path, errmsg);
00050               sqlite_freemem(errmsg);
00051               return FAILURE;
00052        }
00053 
00054        /* allow up to 1 minute when busy */
00055        sqlite_busy_timeout(db, 60000);
00056 
00057        sqlite_exec(db, "PRAGMA default_synchronous = OFF", NULL, NULL, NULL);
00058        sqlite_exec(db, "PRAGMA count_changes = OFF", NULL, NULL, NULL);
00059 
00060        /* This will fail if the table already exists, but that's not a big problem. I'm
00061           unclear as to how to check for a table's existence in SQLite -- that would be better here. */
00062        sqlite_exec(db, 
00063            "CREATE TABLE session_data ("
00064            "    sess_id PRIMARY KEY," 
00065            "    value TEXT, "
00066            "    updated INTEGER "
00067            ")", NULL, NULL, NULL);
00068 
00069        PS_SET_MOD_DATA(db);
00070 
00071        return SUCCESS;
00072 }
00073 
00074 PS_CLOSE_FUNC(sqlite) 
00075 {
00076        PS_SQLITE_DATA;
00077 
00078        sqlite_close(db);
00079 
00080        return SUCCESS;
00081 }
00082 
00083 PS_READ_FUNC(sqlite) 
00084 {
00085        PS_SQLITE_DATA;
00086        char *query;
00087        const char *tail;
00088        sqlite_vm *vm;
00089        int colcount, result;
00090        const char **rowdata, **colnames;
00091        char *error;
00092 
00093        *val = NULL;
00094        *vallen = 0;
00095        
00096        query = sqlite_mprintf("SELECT value FROM session_data WHERE sess_id='%q' LIMIT 1", key);
00097        if (query == NULL) {
00098               /* no memory */
00099               return FAILURE;
00100        }
00101 
00102        if (sqlite_compile(db, query, &tail, &vm, &error) != SQLITE_OK) {
00103               php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLite: Could not compile session read query: %s", error);
00104               sqlite_freemem(error);
00105               sqlite_freemem(query);
00106               return FAILURE;
00107        }
00108 
00109        switch ((result = sqlite_step(vm, &colcount, &rowdata, &colnames))) {
00110               case SQLITE_ROW:
00111                      if (rowdata[0] != NULL) {
00112                             *vallen = strlen(rowdata[0]);
00113                             if (*vallen) {
00114                                    *val = emalloc(*vallen);
00115                                    *vallen = sqlite_decode_binary(rowdata[0], *val);
00116                                    (*val)[*vallen] = '\0';
00117                             } else {
00118                                    *val = STR_EMPTY_ALLOC();
00119                             }
00120                      }
00121                      break;
00122               default:
00123                      sqlite_freemem(error);
00124                      error = NULL;
00125        }
00126        
00127        if (SQLITE_OK != sqlite_finalize(vm, &error)) {
00128               php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLite: session read: error %s", error);
00129               sqlite_freemem(error);
00130               error = NULL;
00131        }
00132 
00133        sqlite_freemem(query);
00134        
00135        return *val == NULL ? FAILURE : SUCCESS;
00136 }
00137 
00138 PS_WRITE_FUNC(sqlite) 
00139 {
00140        PS_SQLITE_DATA;
00141        char *error;
00142        time_t t;
00143        char *binary;
00144        int binlen;
00145        int rv;
00146        
00147        t = time(NULL);
00148 
00149        binary = safe_emalloc(1 + vallen / 254, 257, 3);
00150        binlen = sqlite_encode_binary((const unsigned char*)val, vallen, binary);
00151        
00152        rv = sqlite_exec_printf(db, "REPLACE INTO session_data VALUES('%q', '%q', %d)", NULL, NULL, &error, key, binary, t);
00153        if (rv != SQLITE_OK) {
00154               php_error_docref(NULL TSRMLS_CC, E_WARNING, "SQLite: session write query failed: %s", error);
00155               sqlite_freemem(error);
00156        }
00157        efree(binary);
00158 
00159        return SQLITE_RETVAL(rv);
00160 }
00161 
00162 PS_DESTROY_FUNC(sqlite) 
00163 {
00164        int rv;
00165        PS_SQLITE_DATA;
00166 
00167        rv = sqlite_exec_printf(db, "DELETE FROM session_data WHERE sess_id='%q'", NULL, NULL, NULL, key);
00168        
00169        return SQLITE_RETVAL(rv);
00170 }
00171 
00172 PS_GC_FUNC(sqlite) 
00173 {
00174        PS_SQLITE_DATA;
00175        int rv;
00176        time_t t = time(NULL);
00177 
00178        rv = sqlite_exec_printf(db, 
00179                      "DELETE FROM session_data WHERE (%d - updated) > %d", 
00180                      NULL, NULL, NULL, t, maxlifetime);
00181 
00182        /* because SQLite does not actually clear the deleted data from the database 
00183         * we need to occassionaly do so manually to prevent the sessions database 
00184         * from growing endlessly.
00185         */
00186        if ((int) ((float) PS(gc_divisor) * PS(gc_divisor) * php_combined_lcg(TSRMLS_C)) < PS(gc_probability)) {
00187               rv = sqlite_exec_printf(db, "VACUUM", NULL, NULL, NULL);
00188        }
00189        return SQLITE_RETVAL(rv);
00190 }
00191 
00192 #endif /* HAVE_PHP_SESSION && !defined(COMPILE_DL_SESSION) */
00193 
00194 /*
00195  * Local variables:
00196  * tab-width: 4
00197  * c-basic-offset: 4
00198  * End:
00199  * vim600: sw=4 ts=4 fdm=marker
00200  * vim<600: sw=4 ts=4
00201  */