summaryrefslogtreecommitdiff
path: root/ext/dav/dav.c
blob: 0f8cd56a7a07d475463ac4664df68699a7475156 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999 The PHP Group                         |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.0 of the PHP license,       |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_0.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: Stig Sæther Bakken <ssb@guardian.no>                        |
   +----------------------------------------------------------------------+
 */

/* $Id$ */

#define IS_EXT_MODULE
#if COMPILE_DL
# if PHP_31
#  include "../phpdl.h"
# else
#  include "dl/phpdl.h"
# endif
#endif
#include "php.h"
#include "php_dav.h"

#if defined(THREAD_SAFE) && !PHP_31
# undef THREAD_SAFE
#endif

#if HAVE_MOD_DAV

# include "mod_dav.h"
# include "phpdav.h"
# include "variables.h"

/* {{{ thread safety stuff */

# ifdef THREAD_SAFE
#  define DAV_GLOBAL(a) phpdav_globals->a
#  define DAV_TLS_VARS phpdav_global_struct *phpdav_globals = TlsGetValue(PHPDAVTls); 

void *phpdav_mutex;
DWORD PHPDAVTls;
static int numthreads=0;

typedef struct phpdav_global_struct {
	phpdav_module php3_dav_module;
} phpdav_global_struct;

# else /* !defined(THREAD_SAFE) */
#  define DAV_GLOBAL(a) a
#  define DAV_TLS_VARS

phpdav_module php3_dav_module;

# endif /* defined(THREAD_SAFE) */

# define DAV_HANDLER(a) DAV_GLOBAL(php3_dav_module).a##_handler
# define DAV_SET_HANDLER(a,b) \
	dav_set_handler(&DAV_GLOBAL(php3_dav_module).a##_handler,(b))


/* }}} */
/* {{{ dynamically loadable module stuff */

# if COMPILE_DL
DLEXPORT php3_module_entry *get_module() { return &phpdav_module_entry; };
# endif /* COMPILE_DL */

/* }}} */
/* {{{ function prototypes */

int php3_minit_phpdav(INIT_FUNC_ARGS);
int php3_rinit_phpdav(INIT_FUNC_ARGS);
int php3_mshutdown_phpdav(SHUTDOWN_FUNC_ARGS);
int php3_rshutdown_phpdav(SHUTDOWN_FUNC_ARGS);
void php3_info_phpdav(ZEND_MODULE_INFO_FUNC_ARGS);

/* }}} */
/* {{{ extension definition structures */

function_entry phpdav_functions[] = {
    PHP_FE(dav_set_mkcol_handlers, NULL)
    {NULL, NULL, NULL}
};

php3_module_entry phpdav_module_entry = {
    "DAV",                   /* extension name */
    phpdav_functions,        /* extension function list */
    php3_minit_phpdav,       /* extension-wide startup function */
    php3_mshutdown_phpdav,   /* extension-wide shutdown function */
    php3_rinit_phpdav,       /* per-request startup function */
    php3_rshutdown_phpdav,   /* per-request shutdown function */
    php3_info_phpdav,        /* information function */
    STANDARD_MODULE_PROPERTIES
};

/* }}} */
/* {{{ startup, shutdown and info functions */

    /* {{{ php3_minit_phpdav */

int php3_minit_phpdav(INIT_FUNC_ARGS)
{
#if defined(THREAD_SAFE)
    phpdav_global_struct *phpdav_globals;
    PHP3_MUTEX_ALLOC(phpdav_mutex);
    PHP3_MUTEX_LOCK(phpdav_mutex);
    numthreads++;
    if (numthreads==1){
		if (!PHP3_TLS_PROC_STARTUP(PHPDAVTls)){
			PHP3_MUTEX_UNLOCK(phpdav_mutex);
			PHP3_MUTEX_FREE(phpdav_mutex);
			return FAILURE;
		}
    }
    PHP3_MUTEX_UNLOCK(phdpav_mutex);
    if(!PHP3_TLS_THREAD_INIT(PHPDAVTls,phpdav_globals,phpdav_global_struct)){
		PHP3_MUTEX_FREE(phpdav_mutex);
		return FAILURE;
    }
#endif
    return SUCCESS;
}

/* }}} */
    /* {{{ php3_rinit_phpdav */

int php3_rinit_phpdav(INIT_FUNC_ARGS)
{
    return SUCCESS;
}

/* }}} */
    /* {{{ php3_mshutdown_phpdav() */

int php3_mshutdown_phpdav(SHUTDOWN_FUNC_ARGS)
{
    DAV_TLS_VARS;
#ifdef THREAD_SAFE
    PHP3_TLS_THREAD_FREE(phpdav_globals);
    PHP3_MUTEX_LOCK(phpdav_mutex);
    numthreads--;
    if (numthreads < 1) {
		PHP3_TLS_PROC_SHUTDOWN(PHPDAVTls);
		PHP3_MUTEX_UNLOCK(phpdav_mutex);
		PHP3_MUTEX_FREE(phpdav_mutex);
		return SUCCESS;
    }
    PHP3_MUTEX_UNLOCK(phpdav_mutex);
#endif
    return SUCCESS;
}

/* }}} */
    /* {{{ php3_rshutdown_phpdav() */

int php3_rshutdown_phpdav(SHUTDOWN_FUNC_ARGS)
{
    if (DAV_HANDLER(mkcol_test)) {
		efree(DAV_HANDLER(mkcol_test));
    }
    if (DAV_HANDLER(mkcol_create)) {
		efree(DAV_HANDLER(mkcol_create));
    }
    return SUCCESS;
}

/* }}} */
    /* {{{ php3_info_phpdav() */

void php3_info_phpdav(ZEND_MODULE_INFO_FUNC_ARGS)
{
}

/* }}} */

/* }}} */
/* {{{ extension-internal functions */

    /* {{{ dav_set_handler() */

static void
dav_set_handler(char **nameBufp, pval *data)
{
    if (data->value.str.len > 0) {
		if (*nameBufp != NULL) {
			efree(*nameBufp);
		}
		*nameBufp = php3i_pval_strdup(data);
    } else {
		if (*nameBufp != NULL) {
			efree(*nameBufp);
		}
		*nameBufp = NULL;
    }
}

/* }}} */
    /* {{{ dav_call_handler() */

static int
dav_call_handler(char *funcName, int argc, pval **argv)
{
    if (funcName) {
		pval *retval, *func;
		int i, ret;
		HashTable *function_table;

		func = php3i_string_pval(funcName);
		retval = emalloc(sizeof(pval));
		function_table = php3i_get_function_table();
		if (call_user_function(function_table, NULL, func, retval, argc, argv) == FAILURE) {
			php3tls_pval_destructor(retval);
			efree(retval);
			return HTTP_INTERNAL_SERVER_ERROR;
		}
		php3tls_pval_destructor(func);
		efree(func);
		for (i = 0; i < argc; i++) {
			php3tls_pval_destructor(argv[i]);
			efree(argv[i]);
		}
		convert_to_long(retval);
		ret = retval->value.lval;
		efree(retval);
		return ret;
    }
    return DECLINED;
}

/* }}} */

int phpdav_mkcol_test_handler(request_rec *r)
{
    pval *arg;

    if (DAV_HANDLER(mkcol_test) == NULL) {
		return DECLINED;
    }
    arg = php3i_string_pval(r->filename);
	return dav_call_handler(DAV_HANDLER(mkcol_test), 1, &arg);
}

int phpdav_mkcol_create_handler(request_rec *r)
{
    pval *arg;

    if (DAV_HANDLER(mkcol_create) == NULL) {
		return DECLINED;
    }
    arg = php3i_string_pval(r->filename);
	return dav_call_handler(DAV_HANDLER(mkcol_create), 1, &arg);
}

/* }}} */

/************************* EXTENSION FUNCTIONS *************************/

/* {{{ proto void   dav_set_mkcol_handlers(string test, string create)
   Sets the function to test whether a DAV collection exists for MKCOL */
PHP_FUNCTION(dav_set_mkcol_handlers)
{
    pval *test, *create;
    DAV_TLS_VARS;

    if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &test, &create) == FAILURE) {
		WRONG_PARAM_COUNT;
    }
    DAV_SET_HANDLER(mkcol_test, test);
    DAV_SET_HANDLER(mkcol_create, create);
    RETVAL_TRUE;
}
/* }}} */

#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */