source: trunk/zoo-project/zoo-kernel/service_internal_php.c @ 605

Last change on this file since 605 was 605, checked in by knut, 9 years ago

Altered the path of stored response files from [tmpPath]/[serviceProvider]_[cpid].xml to [tmpPath]/[serviceName]_[cpid].xml. Added support for a new parameter, libPath, in the [main] block of main.cfg. The libPath parameter is interpreted as the absolute path of the directory containing library files. If the libPath parameter is set, it will override the metapath parameter such that library files are loaded from [libPath]/[serviceProvider] instead of [CWD]/[metapath]/[serviceProvider]. Added the option to disable the metapath parameter (for security reasons) at compile time: If the Zoo kernel is compiled with the preprocessor directive IGNORE_METAPATH, the metapath will be set to the empty string. Note however, that the libPath and IGNORE_METAPATH options so far only work for C and PHP; the functions for loading libraries written in other languages should be modified correspondingly before the documentation is updated. See also ticket no. 112 (http://www.zoo-project.org/trac/ticket/112). Fixed some spelling errors and modified the language in some of the messages returned by the Zoo kernel.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 11.3 KB
Line 
1/*
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2010 GeoLabs SARL
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#ifdef WIN32
26  #define NO_FCGI_DEFINES
27#endif
28
29#ifndef ZEND_DEBUG
30  #define ZEND_DEBUG 0
31#endif 
32 
33#include "service_internal_php.h"
34
35#include <sapi/embed/php_embed.h>
36#include <zend_stream.h>
37
38zval *php_Array_from_maps(maps* t);
39zval*  php_Array_from_map(map*);
40maps* php_maps_from_Array(HashTable* t);
41map* php_map_from_HasTable(HashTable* t);
42
43#ifdef ZTS
44void ***tsrm_ls;
45#endif
46
47ZEND_BEGIN_MODULE_GLOBALS(zoo)
48long _SERVICE_SUCCEEDED;
49long _SERVICE_FAILED;
50ZEND_END_MODULE_GLOBALS(zoo)
51
52#ifdef ZTS
53#define ZOO_G(v) TSRMG(zoo_globals_id, zend_zoo_globals *, v)
54#else
55#define ZOO_G(v) (zoo_globals.v)
56#endif
57
58#define PHP_ZOO_VERSION "1.0"
59#define PHP_ZOO_EXTNAME "ZOO"
60
61PHP_MINIT_FUNCTION(zoo);
62PHP_MSHUTDOWN_FUNCTION(zoo);
63PHP_RINIT_FUNCTION(zoo);
64
65PHP_FUNCTION(zoo_Translate);
66PHP_FUNCTION(zoo_UpdateStatus);
67PHP_FUNCTION(zoo_SERVICE_SUCCEEDED);
68PHP_FUNCTION(zoo_SERVICE_FAILED);
69
70extern zend_module_entry zoo_module_entry;
71#define phpext_zoo_ptr &zoo_entry
72
73ZEND_DECLARE_MODULE_GLOBALS(zoo)
74
75static zend_function_entry zoo_functions[] = {
76  PHP_FE(zoo_Translate, NULL)
77  PHP_FE(zoo_UpdateStatus, NULL)
78  PHP_FE(zoo_SERVICE_SUCCEEDED, NULL)
79  PHP_FE(zoo_SERVICE_FAILED, NULL)
80  {NULL, NULL, NULL}
81};
82
83zend_module_entry zoo_module_entry = {
84#if ZEND_MODULE_API_NO >= 20010901
85    STANDARD_MODULE_HEADER,
86#endif
87    PHP_ZOO_EXTNAME,
88    zoo_functions,
89    PHP_MINIT(zoo),
90    PHP_MSHUTDOWN(zoo),
91    PHP_RINIT(zoo),
92    NULL,
93    NULL,
94#if ZEND_MODULE_API_NO >= 20010901
95    PHP_ZOO_VERSION,
96#endif
97    STANDARD_MODULE_PROPERTIES
98};
99
100ZEND_GET_MODULE(zoo)
101
102PHP_INI_BEGIN()
103PHP_INI_END()
104
105static void
106php_zoo_init_globals(zend_zoo_globals *zoo_globals)
107{
108  zoo_globals->_SERVICE_SUCCEEDED=3;
109  zoo_globals->_SERVICE_FAILED=4;
110}
111
112PHP_RINIT_FUNCTION(zoo)
113{
114  return SUCCESS;
115}
116
117PHP_MINIT_FUNCTION(zoo)
118{
119  ZEND_INIT_MODULE_GLOBALS(zoo, php_zoo_init_globals,NULL);
120  REGISTER_INI_ENTRIES();
121  return SUCCESS;
122}
123
124PHP_MSHUTDOWN_FUNCTION(zoo)
125{
126  UNREGISTER_INI_ENTRIES();
127  return SUCCESS;
128}
129
130PHP_FUNCTION(zoo_Translate)
131{
132  char *value;
133  int value_len;
134  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &value, &value_len) == FAILURE) {
135    RETURN_NULL();
136  }
137  RETURN_STRING(_ss(value), 1);
138}
139
140PHP_FUNCTION(zoo_UpdateStatus)
141{
142  zval *arr;
143  char *message;
144  int message_len;
145  long pourcent;
146  char *status=(char*)malloc(4*sizeof(char));
147  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "asl", &arr, &message, &message_len,&pourcent) == FAILURE) {
148    RETURN_NULL();
149  }
150  HashTable* t=HASH_OF(arr);
151  maps* conf=php_maps_from_Array(t);
152  setMapInMaps(conf,"lenv","message",message);
153  sprintf(status,"%d",pourcent);
154  setMapInMaps(conf,"lenv","status",status);
155  _updateStatus(conf);
156  freeMaps(&conf);
157  free(conf);
158  free(status);
159  RETURN_NULL();
160}
161
162PHP_FUNCTION(zoo_SERVICE_SUCCEEDED)
163{
164  RETURN_LONG(ZOO_G(_SERVICE_SUCCEEDED));
165}
166
167PHP_FUNCTION(zoo_SERVICE_FAILED)
168{
169  RETURN_LONG(ZOO_G(_SERVICE_FAILED));
170}
171
172/**
173 * Load a PHP script then run the function corresponding to the service by
174 * passing the conf, inputs and outputs parameters by reference.
175 *
176 * @param main_conf the conf maps containing the main.cfg settings
177 * @param request the map containing the HTTP request
178 * @param s the service structure
179 * @param real_inputs the maps containing the inputs
180 * @param real_outputs the maps containing the outputs
181 */
182int zoo_php_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){   
183  maps* m=*main_conf;
184  maps* inputs=*real_inputs;
185  maps* outputs=*real_outputs;
186  char ntmp[1024];
187  getcwd(ntmp,1024);
188   
189  map* libp = getMapFromMaps(m, "main", "libPath"); // KLa 
190  int res=SERVICE_FAILED;
191
192  map* tmp=getMap(s->content,"serviceProvider");
193  if (tmp == NULL || tmp->value == NULL) {
194          return errorException(m, "Missing serviceProvider (library file)", "NoApplicableCode", NULL);
195  }
196 
197  map* cwd=getMapFromMaps(m,"lenv","cwd");
198#ifdef IGNORE_METAPATH
199  map* mp = createMap("metapath", "");
200#else 
201  map* mp = getMap(request, "metapath");
202#endif
203  char *scriptName;
204 
205  if (libp != NULL && libp->value != NULL) {
206        scriptName = (char*) malloc((strlen(libp->value) + strlen(tmp->value) + 2)*sizeof(char));
207    sprintf (scriptName, "%s/%s", libp->value, tmp->value);     
208  }
209  else {       
210    if(mp!=NULL && strlen(mp->value)>0){
211      scriptName=(char*)malloc((strlen(cwd->value)+strlen(mp->value)+strlen(tmp->value)+3)*sizeof(char));
212      sprintf(scriptName,"%s/%s/%s",cwd->value,mp->value,tmp->value);
213    }else{
214      scriptName=(char*)malloc((strlen(cwd->value)+strlen(tmp->value)+2)*sizeof(char));
215      sprintf(scriptName,"%s/%s",cwd->value,tmp->value);
216    }
217  } 
218  zend_file_handle iscript;
219  iscript.type=ZEND_HANDLE_FD;
220  iscript.opened_path=NULL;
221  iscript.filename=tmp->value;
222  iscript.free_filename=0;
223  FILE* temp=fopen(scriptName,"rb");
224  if(temp==NULL){
225    char tmp1[1024];
226    sprintf(tmp1,_("Unable to load the PHP file %s"),tmp->value);
227    free(scriptName);
228    return errorException(m,tmp1,"NoApplicableCode",NULL);
229  }
230  iscript.handle.fd=fileno(temp);
231
232  php_embed_init(0,NULL PTSRMLS_CC);
233   
234  zend_try {
235    zend_startup_module(&zoo_module_entry);
236    php_execute_script(&iscript TSRMLS_CC);
237    zval *iargs[3];
238    zval iret, ifunc,ifile;
239     
240    ZVAL_STRING(&ifunc, s->name, 0);
241    iargs[0] = php_Array_from_maps(*main_conf);
242    iargs[1] = php_Array_from_maps(*real_inputs);
243    iargs[2] = php_Array_from_maps(*real_outputs);
244     
245    if((res=call_user_function(EG(function_table), NULL, &ifunc, &iret, 3, iargs TSRMLS_CC))==SUCCESS){
246     
247      HashTable* t=HASH_OF(iargs[2]);
248      HashTable* t1=HASH_OF(iargs[0]);
249      *real_outputs=php_maps_from_Array(t);
250      *main_conf=php_maps_from_Array(t1);
251
252      res=Z_LVAL(iret);
253    }else{
254      free(scriptName);
255      fclose(temp);
256      return errorException(m,"Unable to process.","NoApplicableCode",NULL);;
257    }
258  } zend_catch { 
259    free(scriptName);
260    fclose(temp);
261    return errorException(m,"Unable to process.","NoApplicableCode",NULL);;
262  } zend_end_try();
263  free(scriptName);
264  fclose(temp);
265  php_embed_shutdown(TSRMLS_C);
266
267  return res;
268}
269
270/**
271 * Convert a maps to a php Array
272 *
273 * @param t the maps to convert
274 * @return the php Array
275 */
276zval *php_Array_from_maps(maps* t){
277  zval *mapArray;
278  zval *mapArrayTmp;
279  maps* tmp=t;
280  int tres=0;
281  MAKE_STD_ZVAL(mapArray);
282  tres=array_init(mapArray);
283  while(tmp!=NULL){
284    add_assoc_zval(mapArray,tmp->name,php_Array_from_map(tmp->content));
285    tmp=tmp->next;
286  }
287  return mapArray;
288}
289
290/**
291 * Convert a map to a php Array
292 *
293 * @param t the map to convert
294 * @return the php Array
295 */
296zval *php_Array_from_map(map* t){
297  zval *mapArray;
298  zval *mapArrayTmp;
299  map* tmp=t;
300  int tres=0;
301  MAKE_STD_ZVAL(mapArray);
302  tres=array_init(mapArray);
303  while(tmp!=NULL){
304    map* sMap=getMapArray(tmp,"size",0);   
305        if(strncmp(tmp->name,"value",5)==0 && sMap!=NULL && tmp->value != NULL){
306      tres=add_assoc_stringl(mapArray,tmp->name,tmp->value,atoi(sMap->value),1);
307        } 
308        else if (tmp->value != NULL) {
309      tres=add_assoc_string(mapArray,tmp->name,tmp->value,1);
310        }
311    tmp=tmp->next;
312  }
313  return mapArray;
314}
315
316/**
317 * Convert a php Array to a maps
318 *
319 * @param t the php Array to convert
320 * @return the created maps
321 */
322maps* php_maps_from_Array(HashTable *t){
323  maps* final_res=NULL;
324  maps* cursor=final_res;
325  char key[1024];
326  for(zend_hash_internal_pointer_reset(t); 
327      zend_hash_has_more_elements(t) == SUCCESS; 
328      zend_hash_move_forward(t)) { 
329    char *key; 
330    uint keylen; 
331    ulong idx; 
332    int type; 
333    zval **ppzval, tmpcopy; 
334    type = zend_hash_get_current_key_ex(t, &key, &keylen, &idx, 0, NULL); 
335    if (zend_hash_get_current_data(t, (void**)&ppzval) == FAILURE) { 
336      /**
337       * Should never actually fail since the key is known to exist.
338       */
339      continue; 
340    }
341    /**
342     * Duplicate the zval so that * the orignal’s contents are not destroyed
343     */
344    tmpcopy = **ppzval;
345#ifdef DEBUG
346    fprintf(stderr,"key : %s\n",key);
347#endif
348    zval_copy_ctor(&tmpcopy); 
349#ifdef DEBUG
350    fprintf(stderr,"key : %s\n",key);
351#endif
352    /**
353     * Reset refcount & Convert
354     */
355    INIT_PZVAL(&tmpcopy); 
356    //convert_to_string(&tmpcopy);
357    if (type == HASH_KEY_IS_STRING) { 
358      /**
359       * String Key / Associative
360       */
361      cursor=(maps*)malloc(MAPS_SIZE);
362      cursor->name=strdup(key);
363    }
364#ifdef DEBUG   
365    fprintf(stderr,"key : %s\n",key);
366#endif 
367    HashTable* t=HASH_OF(*ppzval);
368#ifdef DEBUG
369    fprintf(stderr,"key : %s\n",key);
370#endif
371    cursor->content=php_map_from_HasTable(t);
372    cursor->next=NULL;
373    if(final_res==NULL)
374      final_res=cursor;
375    else{
376      addMapsToMaps(&final_res,cursor);
377      freeMaps(&cursor);
378      free(cursor);
379    }
380#ifdef DEBUG
381    fprintf(stderr,"key : %s\n",key);
382#endif
383    /**
384     * Toss out old copy
385     */
386    zval_dtor(&tmpcopy);
387  }
388  return final_res;
389}
390
391/**
392 * Convert a php Array to a map
393 *
394 * @param t the php Array to convert
395 * @return the created map
396 */
397map* php_map_from_HasTable(HashTable* t){
398#ifdef DEBUG
399  fprintf(stderr,"mapsFromPHPArray start\n");
400#endif
401  map* final_res=(map*)malloc(MAP_SIZE);
402  final_res=NULL;
403  char key[1024];
404  for(zend_hash_internal_pointer_reset(t);
405      zend_hash_has_more_elements(t) == SUCCESS;
406      zend_hash_move_forward(t)) {
407    char *key;
408    uint keylen;
409    ulong idx;
410    int type;
411    int len;
412    zval **ppzval, tmpcopy;
413    type = zend_hash_get_current_key_ex(t, &key, &keylen, &idx, 0, NULL); 
414    if (zend_hash_get_current_data(t, (void**)&ppzval) == FAILURE) { 
415      /* Should never actually fail * since the key is known to exist. */ 
416      continue; 
417    }
418    /**
419     * Duplicate the zval so that * the orignal’s contents are not destroyed
420     */ 
421    tmpcopy = **ppzval; 
422    zval_copy_ctor(&tmpcopy); 
423    /**
424     * Reset refcount & Convert
425     */ 
426    INIT_PZVAL(&tmpcopy); 
427    convert_to_string(&tmpcopy);
428    if(strncmp(key,"value",5)==0){
429      len=Z_STRLEN_P(&tmpcopy);
430      addToMapWithSize(final_res,key,Z_STRVAL_P(&tmpcopy),len);
431    }
432    else{
433      if(final_res==NULL){
434#ifdef DEBUG
435        fprintf(stderr,"%s => %s\n",key,Z_STRVAL(tmpcopy));
436#endif
437        final_res=createMap(key,Z_STRVAL(tmpcopy));
438      }
439      else{
440#ifdef DEBUG
441        fprintf(stderr,"%s => %s\n",key,Z_STRVAL(tmpcopy));
442#endif
443        addToMap(final_res,key,Z_STRVAL(tmpcopy));
444      }
445    }
446    /* Toss out old copy */ 
447    zval_dtor(&tmpcopy); 
448  }
449  return final_res;
450}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png