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

Last change on this file since 623 was 607, checked in by djay, 10 years ago

Introduce the Process Profiles Registry with its documentation.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 11.4 KB
RevLine 
[580]1/*
[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
[556]25#ifdef WIN32
26  #define NO_FCGI_DEFINES
27#endif
[587]28
29#ifndef ZEND_DEBUG
30  #define ZEND_DEBUG 0
31#endif 
[556]32 
[1]33#include "service_internal_php.h"
34
[509]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
[586]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
[580]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 */
[556]182int zoo_php_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){   
[1]183  maps* m=*main_conf;
184  maps* inputs=*real_inputs;
185  maps* outputs=*real_outputs;
186  char ntmp[1024];
187  getcwd(ntmp,1024);
[605]188   
189  map* libp = getMapFromMaps(m, "main", "libPath"); // KLa 
[1]190  int res=SERVICE_FAILED;
191
[605]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 
[586]197  map* cwd=getMapFromMaps(m,"lenv","cwd");
[605]198#ifdef IGNORE_METAPATH
199  map* mp = createMap("metapath", "");
200#else 
201  map* mp = getMap(request, "metapath");
202#endif
[586]203  char *scriptName;
[605]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);     
[586]208  }
[605]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  } 
[1]218  zend_file_handle iscript;
[586]219  iscript.type=ZEND_HANDLE_FD;
220  iscript.opened_path=NULL;
[1]221  iscript.filename=tmp->value;
[586]222  iscript.free_filename=0;
223  FILE* temp=fopen(scriptName,"rb");
224  if(temp==NULL){
[1]225    char tmp1[1024];
[586]226    sprintf(tmp1,_("Unable to load the PHP file %s"),tmp->value);
227    free(scriptName);
228    return errorException(m,tmp1,"NoApplicableCode",NULL);
[1]229  }
[586]230  iscript.handle.fd=fileno(temp);
[1]231
[586]232  php_embed_init(0,NULL PTSRMLS_CC);
[556]233   
[1]234  zend_try {
[586]235    zend_startup_module(&zoo_module_entry);
[1]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);
[586]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]);
[607]249      freeMaps(real_outputs);
250      free(*real_outputs);
251      freeMaps(main_conf);
252      free(*main_conf);
[586]253      *real_outputs=php_maps_from_Array(t);
254      *main_conf=php_maps_from_Array(t1);
[1]255
[586]256      res=Z_LVAL(iret);
257    }else{
258      free(scriptName);
259      fclose(temp);
260      return errorException(m,"Unable to process.","NoApplicableCode",NULL);;
261    }
[1]262  } zend_catch { 
[586]263    free(scriptName);
264    fclose(temp);
265    return errorException(m,"Unable to process.","NoApplicableCode",NULL);;
[1]266  } zend_end_try();
[586]267  free(scriptName);
268  fclose(temp);
[1]269  php_embed_shutdown(TSRMLS_C);
270
271  return res;
272}
273
[580]274/**
275 * Convert a maps to a php Array
276 *
277 * @param t the maps to convert
278 * @return the php Array
279 */
[1]280zval *php_Array_from_maps(maps* t){
281  zval *mapArray;
282  zval *mapArrayTmp;
283  maps* tmp=t;
284  int tres=0;
285  MAKE_STD_ZVAL(mapArray);
286  tres=array_init(mapArray);
287  while(tmp!=NULL){
288    add_assoc_zval(mapArray,tmp->name,php_Array_from_map(tmp->content));
289    tmp=tmp->next;
290  }
291  return mapArray;
292}
293
[580]294/**
295 * Convert a map to a php Array
296 *
297 * @param t the map to convert
298 * @return the php Array
299 */
[1]300zval *php_Array_from_map(map* t){
301  zval *mapArray;
302  zval *mapArrayTmp;
303  map* tmp=t;
304  int tres=0;
305  MAKE_STD_ZVAL(mapArray);
306  tres=array_init(mapArray);
307  while(tmp!=NULL){
[556]308    map* sMap=getMapArray(tmp,"size",0);   
309        if(strncmp(tmp->name,"value",5)==0 && sMap!=NULL && tmp->value != NULL){
[508]310      tres=add_assoc_stringl(mapArray,tmp->name,tmp->value,atoi(sMap->value),1);
[556]311        } 
312        else if (tmp->value != NULL) {
[508]313      tres=add_assoc_string(mapArray,tmp->name,tmp->value,1);
[556]314        }
[1]315    tmp=tmp->next;
316  }
317  return mapArray;
318}
319
[580]320/**
321 * Convert a php Array to a maps
322 *
323 * @param t the php Array to convert
324 * @return the created maps
325 */
[1]326maps* php_maps_from_Array(HashTable *t){
327  maps* final_res=NULL;
328  maps* cursor=final_res;
329  char key[1024];
330  for(zend_hash_internal_pointer_reset(t); 
331      zend_hash_has_more_elements(t) == SUCCESS; 
332      zend_hash_move_forward(t)) { 
333    char *key; 
334    uint keylen; 
335    ulong idx; 
336    int type; 
337    zval **ppzval, tmpcopy; 
338    type = zend_hash_get_current_key_ex(t, &key, &keylen, &idx, 0, NULL); 
339    if (zend_hash_get_current_data(t, (void**)&ppzval) == FAILURE) { 
340      /**
341       * Should never actually fail since the key is known to exist.
342       */
343      continue; 
344    }
345    /**
346     * Duplicate the zval so that * the orignal’s contents are not destroyed
347     */
348    tmpcopy = **ppzval;
[508]349#ifdef DEBUG
[1]350    fprintf(stderr,"key : %s\n",key);
[508]351#endif
[1]352    zval_copy_ctor(&tmpcopy); 
[508]353#ifdef DEBUG
[1]354    fprintf(stderr,"key : %s\n",key);
[508]355#endif
[1]356    /**
357     * Reset refcount & Convert
358     */
359    INIT_PZVAL(&tmpcopy); 
360    //convert_to_string(&tmpcopy);
361    if (type == HASH_KEY_IS_STRING) { 
362      /**
363       * String Key / Associative
364       */
365      cursor=(maps*)malloc(MAPS_SIZE);
366      cursor->name=strdup(key);
367    }
[556]368#ifdef DEBUG   
[1]369    fprintf(stderr,"key : %s\n",key);
[556]370#endif 
[1]371    HashTable* t=HASH_OF(*ppzval);
[508]372#ifdef DEBUG
[1]373    fprintf(stderr,"key : %s\n",key);
[508]374#endif
[1]375    cursor->content=php_map_from_HasTable(t);
376    cursor->next=NULL;
377    if(final_res==NULL)
378      final_res=cursor;
[586]379    else{
[1]380      addMapsToMaps(&final_res,cursor);
[586]381      freeMaps(&cursor);
382      free(cursor);
383    }
[508]384#ifdef DEBUG
[1]385    fprintf(stderr,"key : %s\n",key);
[508]386#endif
[1]387    /**
388     * Toss out old copy
389     */
390    zval_dtor(&tmpcopy);
391  }
392  return final_res;
393}
394
[580]395/**
396 * Convert a php Array to a map
397 *
398 * @param t the php Array to convert
399 * @return the created map
400 */
[1]401map* php_map_from_HasTable(HashTable* t){
402#ifdef DEBUG
403  fprintf(stderr,"mapsFromPHPArray start\n");
404#endif
405  map* final_res=(map*)malloc(MAP_SIZE);
406  final_res=NULL;
407  char key[1024];
[508]408  for(zend_hash_internal_pointer_reset(t);
409      zend_hash_has_more_elements(t) == SUCCESS;
410      zend_hash_move_forward(t)) {
411    char *key;
412    uint keylen;
413    ulong idx;
414    int type;
415    int len;
416    zval **ppzval, tmpcopy;
[1]417    type = zend_hash_get_current_key_ex(t, &key, &keylen, &idx, 0, NULL); 
418    if (zend_hash_get_current_data(t, (void**)&ppzval) == FAILURE) { 
419      /* Should never actually fail * since the key is known to exist. */ 
420      continue; 
421    }
422    /**
423     * Duplicate the zval so that * the orignal’s contents are not destroyed
424     */ 
425    tmpcopy = **ppzval; 
426    zval_copy_ctor(&tmpcopy); 
427    /**
428     * Reset refcount & Convert
429     */ 
430    INIT_PZVAL(&tmpcopy); 
[508]431    convert_to_string(&tmpcopy);
[509]432    if(strncmp(key,"value",5)==0){
433      len=Z_STRLEN_P(&tmpcopy);
434      addToMapWithSize(final_res,key,Z_STRVAL_P(&tmpcopy),len);
[1]435    }
436    else{
[508]437      if(final_res==NULL){
438#ifdef DEBUG
439        fprintf(stderr,"%s => %s\n",key,Z_STRVAL(tmpcopy));
440#endif
441        final_res=createMap(key,Z_STRVAL(tmpcopy));
442      }
443      else{
444#ifdef DEBUG
445        fprintf(stderr,"%s => %s\n",key,Z_STRVAL(tmpcopy));
446#endif
[509]447        addToMap(final_res,key,Z_STRVAL(tmpcopy));
[508]448      }
[1]449    }
450    /* Toss out old copy */ 
451    zval_dtor(&tmpcopy); 
452  }
453  return final_res;
454}
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