1
/**********************************************************************
2
* $Id: php_mapscript.c 9765 2010-01-28 15:32:10Z aboudreault $
5
* Purpose: PHP/MapScript extension for MapServer. External interface
7
* Author: Daniel Morissette, DM Solutions Group (dmorissette@dmsolutions.ca)
8
* Alan Boudreault, Mapgears
10
**********************************************************************
11
* Copyright (c) 2000-2010, Daniel Morissette, DM Solutions Group Inc.
13
* Permission is hereby granted, free of charge, to any person obtaining a
14
* copy of this software and associated documentation files (the "Software"),
15
* to deal in the Software without restriction, including without limitation
16
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
17
* and/or sell copies of the Software, and to permit persons to whom the
18
* Software is furnished to do so, subject to the following conditions:
20
* The above copyright notice and this permission notice shall be included in
21
* all copies of this Software or works derived from this Software.
23
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
* DEALINGS IN THE SOFTWARE.
30
**********************************************************************/
32
#include "php_mapscript.h"
34
#include "php_variables.h"
36
char *owsrequest_getenv(const char *name, void *thread_context);
38
zend_class_entry *mapscript_ce_owsrequest;
40
ZEND_BEGIN_ARG_INFO_EX(owsrequest___get_args, 0, 0, 1)
41
ZEND_ARG_INFO(0, property)
44
ZEND_BEGIN_ARG_INFO_EX(owsrequest___set_args, 0, 0, 2)
45
ZEND_ARG_INFO(0, property)
46
ZEND_ARG_INFO(0, value)
49
ZEND_BEGIN_ARG_INFO_EX(owsrequest_setParameter_args, 0, 0, 2)
50
ZEND_ARG_INFO(0, name)
51
ZEND_ARG_INFO(0, value)
54
ZEND_BEGIN_ARG_INFO_EX(owsrequest_getName_args, 0, 0, 1)
55
ZEND_ARG_INFO(0, index)
58
ZEND_BEGIN_ARG_INFO_EX(owsrequest_getValue_args, 0, 0, 1)
59
ZEND_ARG_INFO(0, index)
62
ZEND_BEGIN_ARG_INFO_EX(owsrequest_getValueByName_args, 0, 0, 1)
63
ZEND_ARG_INFO(0, name)
66
/* {{{ proto owsrequest __construct()
67
Create a new OWSRequestObj instance. */
68
PHP_METHOD(OWSRequestObj, __construct)
70
zval *zobj = getThis();
71
php_owsrequest_object *php_owsrequest;
72
cgiRequestObj *request;
74
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
75
if (zend_parse_parameters_none() == FAILURE) {
76
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
79
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
81
php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
83
if ((request = cgirequestObj_new()) == NULL)
85
mapscript_throw_mapserver_exception("" TSRMLS_CC);
89
php_owsrequest->cgirequest = request;
93
PHP_METHOD(OWSRequestObj, __get)
97
zval *zobj = getThis();
98
php_owsrequest_object *php_owsrequest;
100
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
101
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
102
&property, &property_len) == FAILURE) {
103
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
106
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
108
php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
111
IF_GET_LONG("numparams", php_owsrequest->cgirequest->NumParams)
112
else IF_GET_LONG("type", php_owsrequest->cgirequest->type)
115
mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
119
PHP_METHOD(OWSRequestObj, __set)
124
zval *zobj = getThis();
125
php_owsrequest_object *php_owsrequest;
127
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
128
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz",
129
&property, &property_len, &value) == FAILURE) {
130
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
133
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
135
php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
137
if ( (STRING_EQUAL("numparams", property)) ||
138
(STRING_EQUAL("type", property)))
140
mapscript_throw_exception("Property '%s' is read-only and cannot be set." TSRMLS_CC, property);
144
mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
148
/* {{{ proto int owsrequest.loadParams()
149
Initializes the OWSRequest object from the cgi environment variables
150
REQUEST_METHOD, QUERY_STRING and HTTP_COOKIE. Returns the number of
151
name/value pairs collected. */
152
PHP_METHOD(OWSRequestObj, loadParams)
154
zval *zobj = getThis();
156
php_owsrequest_object *php_owsrequest;
157
void *thread_context;
160
thread_context = (void*)TSRMLS_C;
162
thread_context = NULL;
165
//PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
166
if (zend_parse_parameters_none() == FAILURE) {
167
//PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
170
//PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
172
php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
174
if ( (STRING_EQUAL(sapi_module.name,"cli")) ||
175
(STRING_EQUAL(sapi_module.name,"cgi")) ||
176
(STRING_EQUAL(sapi_module.name,"cgi-fcgi")) )
178
cgirequestObj_loadParams(php_owsrequest->cgirequest, NULL, NULL, 0, thread_context);
182
// check if we have input data for GET method
183
if (SG(request_info).request_method &&
184
STRING_EQUAL(SG(request_info).request_method, "GET"))
186
zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
187
if ( PG(http_globals)[TRACK_VARS_SERVER] &&
188
(zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "QUERY_STRING", sizeof("QUERY_STRING"), (void **) &val) == SUCCESS) &&
189
(Z_TYPE_PP(val) == IS_STRING) &&
190
(Z_STRLEN_PP(val) > 0) )
192
cgirequestObj_loadParams(php_owsrequest->cgirequest, owsrequest_getenv, NULL, 0, thread_context);
197
cgirequestObj_loadParams(php_owsrequest->cgirequest, owsrequest_getenv,
198
SG(request_info).raw_post_data,
199
SG(request_info).raw_post_data_length, thread_context);
203
RETURN_LONG(php_owsrequest->cgirequest->NumParams);
207
/* {{{ proto int owsrequest.setParameter(string name, string value)
208
Set a request parameter. */
209
PHP_METHOD(OWSRequestObj, setParameter)
215
zval *zobj = getThis();
216
php_owsrequest_object *php_owsrequest;
218
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
219
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
220
&name, &name_len, &value, &value_len) == FAILURE) {
221
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
224
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
226
php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
228
cgirequestObj_setParameter(php_owsrequest->cgirequest, name, value);
230
RETURN_LONG(MS_SUCCESS);
234
/* {{{ proto string owsrequest.getName(int index)
235
Return the name of the parameter at index in the request’s array of parameter names. */
236
PHP_METHOD(OWSRequestObj, getName)
239
zval *zobj = getThis();
241
php_owsrequest_object *php_owsrequest;
243
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
244
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l",
245
&index) == FAILURE) {
246
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
249
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
251
php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
253
value = cgirequestObj_getName(php_owsrequest->cgirequest, index);
255
RETURN_STRING("", 1);
257
RETURN_STRING(value,1);
261
/* {{{ proto string owsrequest.getValue(int index)
262
Return the value of the parameter at index in the request’s array of parameter values.*/
263
PHP_METHOD(OWSRequestObj, getValue)
266
zval *zobj = getThis();
268
php_owsrequest_object *php_owsrequest;
270
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
271
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l",
272
&index) == FAILURE) {
273
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
276
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
278
php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
280
value = cgirequestObj_getValue(php_owsrequest->cgirequest, index);
282
RETURN_STRING("", 1);
284
RETURN_STRING(value,1);
288
/* {{{ proto string owsrequest.getValueByName(string name)
289
Return the value associated with the parameter name.*/
290
PHP_METHOD(OWSRequestObj, getValueByName)
294
zval *zobj = getThis();
296
php_owsrequest_object *php_owsrequest;
298
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
299
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
300
&name, &name_len) == FAILURE) {
301
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
304
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
306
php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
308
value = cgirequestObj_getValueByName(php_owsrequest->cgirequest, name);
310
RETURN_STRING("", 1);
312
RETURN_STRING(value,1);
316
zend_function_entry owsrequest_functions[] = {
317
PHP_ME(OWSRequestObj, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
318
PHP_ME(OWSRequestObj, __get, owsrequest___get_args, ZEND_ACC_PUBLIC)
319
PHP_ME(OWSRequestObj, __set, owsrequest___set_args, ZEND_ACC_PUBLIC)
320
PHP_ME(OWSRequestObj, loadParams, NULL, ZEND_ACC_PUBLIC)
321
PHP_ME(OWSRequestObj, setParameter, owsrequest_setParameter_args, ZEND_ACC_PUBLIC)
322
PHP_ME(OWSRequestObj, getName, owsrequest_getName_args, ZEND_ACC_PUBLIC)
323
PHP_ME(OWSRequestObj, getValue, owsrequest_getValue_args, ZEND_ACC_PUBLIC)
324
PHP_ME(OWSRequestObj, getValueByName, owsrequest_getValueByName_args, ZEND_ACC_PUBLIC)
328
char *owsrequest_getenv(const char *name, void *thread_context)
330
zval **val, **ppzval;
331
zval *cookie_result, *key;
333
char *string_key = NULL, *cookie_tmp;
335
int numElements, i = 0;
336
TSRMLS_FETCH_FROM_CTX(thread_context);
338
if (STRING_EQUAL(name, "HTTP_COOKIE"))
340
cookies = PG(http_globals)[TRACK_VARS_COOKIE]->value.ht;
341
numElements = zend_hash_num_elements(cookies);
342
MAKE_STD_ZVAL(cookie_result);
343
ZVAL_STRING(cookie_result, "",1);
344
for(zend_hash_internal_pointer_reset(cookies);
345
zend_hash_has_more_elements(cookies) == SUCCESS;
346
zend_hash_move_forward(cookies), ++i)
348
zend_hash_get_current_data(cookies, (void **)&ppzval);
349
zend_hash_get_current_key(cookies, &string_key, &num_key, 1);
350
cookie_tmp = malloc((strlen(string_key)+Z_STRLEN_PP(ppzval)+3) * sizeof(char));
351
sprintf(cookie_tmp, "%s=%s;",string_key,Z_STRVAL_PP(ppzval));
353
ZVAL_STRING(key, cookie_tmp,1);
354
add_string_to_string(cookie_result,cookie_result, key);
358
return Z_STRVAL_P(cookie_result);
362
zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
363
if ( PG(http_globals)[TRACK_VARS_SERVER] &&
364
(zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, name, strlen(name)+1, (void **) &val) == SUCCESS) &&
365
(Z_TYPE_PP(val) == IS_STRING))
367
return Z_STRVAL_PP(val);
374
void mapscript_create_owsrequest(cgiRequestObj *cgirequest, zval *return_value TSRMLS_DC)
376
php_owsrequest_object * php_owsrequest;
377
object_init_ex(return_value, mapscript_ce_owsrequest);
378
php_owsrequest = (php_owsrequest_object *)zend_object_store_get_object(return_value TSRMLS_CC);
379
php_owsrequest->cgirequest = cgirequest;
382
static void mapscript_owsrequest_object_destroy(void *object TSRMLS_DC)
384
php_owsrequest_object *php_owsrequest = (php_owsrequest_object *)object;
386
MAPSCRIPT_FREE_OBJECT(php_owsrequest);
388
cgirequestObj_destroy(php_owsrequest->cgirequest);
393
static zend_object_value mapscript_owsrequest_object_new(zend_class_entry *ce TSRMLS_DC)
395
zend_object_value retval;
396
php_owsrequest_object *php_owsrequest;
398
MAPSCRIPT_ALLOC_OBJECT(php_owsrequest, php_owsrequest_object);
400
retval = mapscript_object_new(&php_owsrequest->std, ce,
401
&mapscript_owsrequest_object_destroy TSRMLS_CC);
405
PHP_MINIT_FUNCTION(owsrequest)
409
MAPSCRIPT_REGISTER_CLASS("OWSRequestObj",
410
owsrequest_functions,
411
mapscript_ce_owsrequest,
412
mapscript_owsrequest_object_new);
414
mapscript_ce_owsrequest->ce_flags |= ZEND_ACC_FINAL_CLASS;