summaryrefslogtreecommitdiffstats
path: root/common/eurephiadb_session_common.c
blob: f9fc795c40d216f6ba93084009145fafb0170b3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eurephiadb_session_common.c  --  Common function for handling sessions
 *
 *  GPLv2 only - Copyright (C) 2008
 *               David Sommerseth <dazo@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; version 2
 *  of the License.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

/**
 * @file   eurephiadb_session_common.c
 * @author David Sommerseth <dazo@users.sourceforge.net>
 * @date   2008-11-28
 *
 * @brief  Common functions for handling eurephia sessions.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <eurephia_nullsafe.h>
#include <eurephia_context.h>
#include <eurephia_log.h>
#include <eurephia_values.h>
#include <eurephiadb_session_struct.h>


#ifndef DOXYGEN
#ifndef DRIVER_MODE   // Use the right declaration, depending on if we compile driver or plug-in/exec
// Function needed to be found in the database driver
int (*eDBstore_session_value) (eurephiaCTX *ctx, eurephiaSESSION *session, int mode,
                               const char *key, const char *val);
#else
int eDBstore_session_value (eurephiaCTX *ctx, eurephiaSESSION *session, int mode,
			    const char *key, const char *val);
#endif /* !DRIVER_MODE */
#endif /* !DOXYGEN */
/**
 * Adds or updates a key in the eurephiaVALUES stack.  Database is updated before the stack is updated.
 * If database fails, the stack is not updated.
 *
 * @param ctx     eurephiaCTX
 * @param session eurephiaSESSION, which contains information about the current user session
 * @param key     key name of the value to be stored
 * @param val     value to be stored
 *
 * @return Returns 1 on success, otherwise 0.
 */
int eDBset_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, const char *key, const char *val) {
        eurephiaVALUES *svals = NULL;

        if( (session == NULL) || (key == NULL) ) {
                return 0;
        }

        DEBUG(ctx, 30, "Function call: eDBset_session_value(ctx, '%s','%s','%s')",
              session->sessionkey, key, val);

        // Create a new session value buffer if it does not exist
        if( session->sessvals == NULL ) {
                session->sessvals = eCreate_value_space(ctx, 10);
                if( session->sessvals == NULL ) {
                        eurephia_log(ctx, LOG_PANIC, 0, "Could not allocate memory for session values");
                        return 0;
                }
        }

        // Check if the session value exists already.  If it does update it, or else add it
        svals = eGet_valuestruct(session->sessvals, key);
        if( (svals == NULL) && (val != NULL) ) {
                DEBUG(ctx, 32, "eDBset_session_value ... New session value: %s = '%s'", key, val);
                // Add a new session value
                if( eDBstore_session_value(ctx, session, SESSVAL_NEW, key, val) ) {
                        DEBUG(ctx, 32, "eDBset_session_value ... Adding value to value stack: %s = '%s'",
                              key, val);
                        // Add value to the stack
                        eAdd_value(ctx, session->sessvals, key, val);

                        DEBUG(ctx, 32, "Registered session variable to session '%s': %s = %s",
                              session->sessionkey, key, val);
                }
        } else if( svals != NULL ) {
                if( (val != NULL) && (strcmp(svals->val, val) == 0) ) {
                        DEBUG(ctx, 32, "Session value not changed('%s','%s','%s)",
                              session->sessionkey, key, val);
                        return 1;
                }
                // Update the value in the stack if database is updated without errors
                if( eDBstore_session_value(ctx, session,(val != NULL ? SESSVAL_UPDATE : SESSVAL_DELETE), key,val)){
                        free_nullsafe(ctx, svals->val);
                        svals->val = strdup_nullsafe(val);
                        DEBUG(ctx, 32, "Session variable updated in session '%s': %s = %s",
                              session->sessionkey, key, val);
                }
        } else if( (svals == NULL) && (val == NULL ) ) {
                DEBUG(ctx, 32, "Ignoring saving new session value '%s' == NULL", key);
        }
        return 1;
}


/**
 * Free up the memory used by a session structure.  This is normally not called directly, but called via the
 * eDBfree_session(...) macro.
 *
 * @param ctx      eurephiaCTX
 * @param session  Pointer to the eurephiaSESSION structure to be freed.
 */
void eDBfree_session_func(eurephiaCTX *ctx, eurephiaSESSION *session) {
        if( session == NULL ) {
                return;
        }
        DEBUG(ctx, 12, "Function call: eDBfree_session(ctx, '%s')", session->sessionkey);
        eFree_values(ctx, session->sessvals);
        free_nullsafe(ctx, session->sessionkey);
        free(session);
}