~ubuntu-branches/ubuntu/saucy/mapserver/saucy-security

« back to all changes in this revision

Viewing changes to mapscript/php/owsrequest.c

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2011-12-23 14:02:06 UTC
  • mfrom: (26.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111223140206-n3h9t2hsa8hyslmu
Tags: 6.0.1-2
Added missed stuff for libmapscript-perl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
 * $Id: php_mapscript.c 9765 2010-01-28 15:32:10Z aboudreault $
 
3
 *
 
4
 * Project:  MapServer
 
5
 * Purpose:  PHP/MapScript extension for MapServer.  External interface 
 
6
 *           functions
 
7
 * Author:   Daniel Morissette, DM Solutions Group (dmorissette@dmsolutions.ca)
 
8
 *           Alan Boudreault, Mapgears
 
9
 *
 
10
 **********************************************************************
 
11
 * Copyright (c) 2000-2010, Daniel Morissette, DM Solutions Group Inc.
 
12
 *
 
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:
 
19
 * 
 
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.
 
22
 * 
 
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
 **********************************************************************/
 
31
 
 
32
#include "php_mapscript.h"
 
33
#include "SAPI.h"
 
34
#include "php_variables.h"
 
35
 
 
36
char *owsrequest_getenv(const char *name, void *thread_context);
 
37
 
 
38
zend_class_entry *mapscript_ce_owsrequest;
 
39
 
 
40
ZEND_BEGIN_ARG_INFO_EX(owsrequest___get_args, 0, 0, 1)
 
41
  ZEND_ARG_INFO(0, property)
 
42
ZEND_END_ARG_INFO()
 
43
 
 
44
ZEND_BEGIN_ARG_INFO_EX(owsrequest___set_args, 0, 0, 2)
 
45
  ZEND_ARG_INFO(0, property)
 
46
  ZEND_ARG_INFO(0, value)
 
47
ZEND_END_ARG_INFO()
 
48
 
 
49
ZEND_BEGIN_ARG_INFO_EX(owsrequest_setParameter_args, 0, 0, 2)
 
50
  ZEND_ARG_INFO(0, name)
 
51
  ZEND_ARG_INFO(0, value)
 
52
ZEND_END_ARG_INFO()
 
53
 
 
54
ZEND_BEGIN_ARG_INFO_EX(owsrequest_getName_args, 0, 0, 1)
 
55
  ZEND_ARG_INFO(0, index)
 
56
ZEND_END_ARG_INFO()
 
57
 
 
58
ZEND_BEGIN_ARG_INFO_EX(owsrequest_getValue_args, 0, 0, 1)
 
59
  ZEND_ARG_INFO(0, index)
 
60
ZEND_END_ARG_INFO()
 
61
 
 
62
ZEND_BEGIN_ARG_INFO_EX(owsrequest_getValueByName_args, 0, 0, 1)
 
63
  ZEND_ARG_INFO(0, name)
 
64
ZEND_END_ARG_INFO()
 
65
 
 
66
/* {{{ proto owsrequest __construct()
 
67
   Create a new OWSRequestObj instance. */
 
68
PHP_METHOD(OWSRequestObj, __construct)
 
69
{
 
70
    zval *zobj = getThis();
 
71
    php_owsrequest_object *php_owsrequest;
 
72
    cgiRequestObj *request;
 
73
 
 
74
    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
 
75
    if (zend_parse_parameters_none() == FAILURE) {
 
76
        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
77
        return;
 
78
    }
 
79
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
80
    
 
81
    php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
82
 
 
83
    if ((request = cgirequestObj_new()) == NULL)
 
84
    {
 
85
        mapscript_throw_mapserver_exception("" TSRMLS_CC);
 
86
        return;
 
87
    }
 
88
    
 
89
    php_owsrequest->cgirequest = request;
 
90
}
 
91
/* }}} */
 
92
 
 
93
PHP_METHOD(OWSRequestObj, __get)
 
94
{
 
95
    char *property;
 
96
    long property_len;
 
97
    zval *zobj = getThis();
 
98
    php_owsrequest_object *php_owsrequest;
 
99
 
 
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);
 
104
        return;
 
105
    }
 
106
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
107
    
 
108
    php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
109
 
 
110
 
 
111
    IF_GET_LONG("numparams", php_owsrequest->cgirequest->NumParams)
 
112
    else IF_GET_LONG("type", php_owsrequest->cgirequest->type)
 
113
    else 
 
114
    {
 
115
        mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
 
116
    }
 
117
}
 
118
 
 
119
PHP_METHOD(OWSRequestObj, __set)
 
120
{
 
121
    char *property;
 
122
    long property_len;
 
123
    zval *value;
 
124
    zval *zobj = getThis();
 
125
    php_owsrequest_object *php_owsrequest;
 
126
 
 
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);
 
131
        return;
 
132
    }
 
133
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
134
    
 
135
    php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
136
 
 
137
    if ( (STRING_EQUAL("numparams", property)) ||
 
138
         (STRING_EQUAL("type", property)))
 
139
    {
 
140
        mapscript_throw_exception("Property '%s' is read-only and cannot be set." TSRMLS_CC, property);
 
141
    }
 
142
    else 
 
143
    {
 
144
        mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
 
145
    }
 
146
}
 
147
 
 
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)
 
153
{
 
154
    zval *zobj = getThis();
 
155
    zval **val;
 
156
    php_owsrequest_object *php_owsrequest;
 
157
    void *thread_context;
 
158
 
 
159
#ifdef ZTS
 
160
    thread_context = (void*)TSRMLS_C;
 
161
#else
 
162
    thread_context = NULL;
 
163
#endif
 
164
 
 
165
    //PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
 
166
    if (zend_parse_parameters_none() == FAILURE) {
 
167
      //PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
168
        return;
 
169
    }
 
170
    //PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
171
    
 
172
    php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
173
 
 
174
    if ( (STRING_EQUAL(sapi_module.name,"cli")) || 
 
175
         (STRING_EQUAL(sapi_module.name,"cgi")) ||
 
176
         (STRING_EQUAL(sapi_module.name,"cgi-fcgi")) )
 
177
    {
 
178
        cgirequestObj_loadParams(php_owsrequest->cgirequest, NULL, NULL, 0, thread_context);
 
179
    }
 
180
    else
 
181
    {
 
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"))
 
185
        {
 
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) ) 
 
191
            {
 
192
                cgirequestObj_loadParams(php_owsrequest->cgirequest, owsrequest_getenv, NULL, 0, thread_context);
 
193
            }
 
194
        }
 
195
        else
 
196
        {
 
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);
 
200
        }
 
201
    }
 
202
    
 
203
    RETURN_LONG(php_owsrequest->cgirequest->NumParams);
 
204
}
 
205
/* }}} */
 
206
 
 
207
/* {{{ proto int owsrequest.setParameter(string name, string value)
 
208
   Set a request parameter. */
 
209
PHP_METHOD(OWSRequestObj, setParameter)
 
210
{
 
211
    char *name;
 
212
    long name_len;
 
213
    char *value;
 
214
    long value_len;
 
215
    zval *zobj = getThis();
 
216
    php_owsrequest_object *php_owsrequest;
 
217
 
 
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);
 
222
        return;
 
223
    }
 
224
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
225
    
 
226
    php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
227
 
 
228
    cgirequestObj_setParameter(php_owsrequest->cgirequest, name, value);
 
229
 
 
230
    RETURN_LONG(MS_SUCCESS);
 
231
}
 
232
/* }}} */
 
233
 
 
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)
 
237
{
 
238
    long index;
 
239
    zval *zobj = getThis();
 
240
    char *value = NULL;
 
241
    php_owsrequest_object *php_owsrequest;
 
242
 
 
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);
 
247
        return;
 
248
    }
 
249
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
250
    
 
251
    php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
252
 
 
253
    value = cgirequestObj_getName(php_owsrequest->cgirequest, index);
 
254
    if (!value)
 
255
        RETURN_STRING("", 1);
 
256
    
 
257
    RETURN_STRING(value,1);
 
258
}
 
259
/* }}} */
 
260
 
 
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)
 
264
{
 
265
    long index;
 
266
    zval *zobj = getThis();
 
267
    char *value = NULL;
 
268
    php_owsrequest_object *php_owsrequest;
 
269
 
 
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);
 
274
        return;
 
275
    }
 
276
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
277
    
 
278
    php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
279
 
 
280
    value = cgirequestObj_getValue(php_owsrequest->cgirequest, index);
 
281
    if (!value)
 
282
        RETURN_STRING("", 1);
 
283
    
 
284
    RETURN_STRING(value,1);
 
285
}
 
286
/* }}} */
 
287
 
 
288
/* {{{ proto string owsrequest.getValueByName(string name)
 
289
   Return the value associated with the parameter name.*/
 
290
PHP_METHOD(OWSRequestObj, getValueByName)
 
291
{
 
292
    char *name;
 
293
    long name_len;
 
294
    zval *zobj = getThis();
 
295
    char *value = NULL;
 
296
    php_owsrequest_object *php_owsrequest;
 
297
 
 
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);
 
302
        return;
 
303
    }
 
304
    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
 
305
    
 
306
    php_owsrequest = (php_owsrequest_object *) zend_object_store_get_object(zobj TSRMLS_CC);
 
307
 
 
308
    value = cgirequestObj_getValueByName(php_owsrequest->cgirequest, name);
 
309
    if (!value)
 
310
        RETURN_STRING("", 1);
 
311
    
 
312
    RETURN_STRING(value,1);
 
313
}
 
314
/* }}} */
 
315
 
 
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)
 
325
    {NULL, NULL, NULL}
 
326
};
 
327
 
 
328
char *owsrequest_getenv(const char *name, void *thread_context)
 
329
{
 
330
    zval **val, **ppzval;
 
331
    zval *cookie_result, *key;
 
332
    HashTable *cookies;
 
333
    char *string_key = NULL, *cookie_tmp;
 
334
    ulong num_key;
 
335
    int numElements, i = 0;
 
336
    TSRMLS_FETCH_FROM_CTX(thread_context);
 
337
 
 
338
    if  (STRING_EQUAL(name, "HTTP_COOKIE"))
 
339
    {
 
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)
 
347
        { 
 
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));
 
352
            MAKE_STD_ZVAL(key);
 
353
            ZVAL_STRING(key, cookie_tmp,1);
 
354
            add_string_to_string(cookie_result,cookie_result, key);
 
355
            zval_dtor(key);
 
356
            free(cookie_tmp);
 
357
        }
 
358
        return Z_STRVAL_P(cookie_result);
 
359
    }
 
360
    else 
 
361
    {
 
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))
 
366
        {
 
367
            return Z_STRVAL_PP(val);
 
368
        }
 
369
    }
 
370
    
 
371
    return NULL;
 
372
}
 
373
 
 
374
void mapscript_create_owsrequest(cgiRequestObj *cgirequest, zval *return_value TSRMLS_DC)
 
375
{
 
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;
 
380
}
 
381
 
 
382
static void mapscript_owsrequest_object_destroy(void *object TSRMLS_DC)
 
383
{
 
384
    php_owsrequest_object *php_owsrequest = (php_owsrequest_object *)object;
 
385
 
 
386
    MAPSCRIPT_FREE_OBJECT(php_owsrequest);
 
387
 
 
388
    cgirequestObj_destroy(php_owsrequest->cgirequest); 
 
389
    
 
390
    efree(object);
 
391
}
 
392
 
 
393
static zend_object_value mapscript_owsrequest_object_new(zend_class_entry *ce TSRMLS_DC)
 
394
{
 
395
    zend_object_value retval;
 
396
    php_owsrequest_object *php_owsrequest;
 
397
 
 
398
    MAPSCRIPT_ALLOC_OBJECT(php_owsrequest, php_owsrequest_object);
 
399
 
 
400
    retval = mapscript_object_new(&php_owsrequest->std, ce,
 
401
                                  &mapscript_owsrequest_object_destroy TSRMLS_CC);
 
402
    return retval;
 
403
}
 
404
 
 
405
PHP_MINIT_FUNCTION(owsrequest)
 
406
{
 
407
    zend_class_entry ce;
 
408
 
 
409
    MAPSCRIPT_REGISTER_CLASS("OWSRequestObj", 
 
410
                             owsrequest_functions,
 
411
                             mapscript_ce_owsrequest,
 
412
                             mapscript_owsrequest_object_new);
 
413
 
 
414
    mapscript_ce_owsrequest->ce_flags |= ZEND_ACC_FINAL_CLASS; 
 
415
 
 
416
    return SUCCESS;
 
417
}
 
418