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

Last change on this file since 580 was 580, checked in by djay, 9 years ago

Continue adding initial doxygen documentation.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 7.6 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#include "service_internal_php.h"
30
31#include <sapi/embed/php_embed.h>
32#include <zend_stream.h>
33
34#ifdef ZTS
35void ***tsrm_ls;
36#endif
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/**
44 * Load a PHP script then run the function corresponding to the service by
45 * passing the conf, inputs and outputs parameters by reference.
46 *
47 * @param main_conf the conf maps containing the main.cfg settings
48 * @param request the map containing the HTTP request
49 * @param s the service structure
50 * @param real_inputs the maps containing the inputs
51 * @param real_outputs the maps containing the outputs
52 */
53int zoo_php_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){   
54  maps* m=*main_conf;
55  maps* inputs=*real_inputs;
56  maps* outputs=*real_outputs;
57  char ntmp[1024];
58  getcwd(ntmp,1024);
59  map* tmp=getMap(request,"metapath");
60
61  int res=SERVICE_FAILED;
62
63  tmp=getMap(s->content,"serviceProvider");
64  zend_file_handle iscript;
65  iscript.type=ZEND_HANDLE_FP;
66  iscript.filename=tmp->value;
67  iscript.opened_path=NULL;
68  //iscript.free_filname=0;
69  if(!(iscript.handle.fp=fopen(iscript.filename,"rb"))){
70    char tmp1[1024];
71    sprintf(tmp1,"Unable to load PHP file %s",tmp->value);
72    map* err=createMap("text",tmp1);
73    addToMap(err,"code","NoApplicableCode");
74    printExceptionReportResponse(m,err);
75    exit(-1);
76  }
77
78  php_embed_init(0,NULL,&tsrm_ls);
79   
80  zend_try {
81
82    php_execute_script(&iscript TSRMLS_CC);
83
84    zval *iargs[3];
85    zval iret, ifunc,ifile;
86     
87    ZVAL_STRING(&ifunc, s->name, 0);
88    iargs[0] = php_Array_from_maps(*main_conf);
89    iargs[1] = php_Array_from_maps(*real_inputs);
90    iargs[2] = php_Array_from_maps(*real_outputs);
91   
92    call_user_function(EG(function_table), NULL, &ifunc, &iret, 3, iargs TSRMLS_CC);
93
94    HashTable* t=HASH_OF(iargs[2]);
95    *real_outputs=php_maps_from_Array(t);
96   
97    char tmp1[1024];
98
99    res=SERVICE_SUCCEEDED;
100
101  } zend_catch { 
102    map* err=createMap("text","Unable to process.");
103    addToMap(err,"code","NoApplicableCode");
104    printExceptionReportResponse(m,err);
105    exit(-1);
106  } zend_end_try();
107
108  php_embed_shutdown(TSRMLS_C);
109
110  return res;
111}
112
113/**
114 * Convert a maps to a php Array
115 *
116 * @param t the maps to convert
117 * @return the php Array
118 */
119zval *php_Array_from_maps(maps* t){
120  zval *mapArray;
121  zval *mapArrayTmp;
122  maps* tmp=t;
123  int tres=0;
124  MAKE_STD_ZVAL(mapArray);
125  tres=array_init(mapArray);
126  while(tmp!=NULL){
127    add_assoc_zval(mapArray,tmp->name,php_Array_from_map(tmp->content));
128    tmp=tmp->next;
129  }
130  return mapArray;
131}
132
133/**
134 * Convert a map to a php Array
135 *
136 * @param t the map to convert
137 * @return the php Array
138 */
139zval *php_Array_from_map(map* t){
140  zval *mapArray;
141  zval *mapArrayTmp;
142  map* tmp=t;
143  int tres=0;
144  MAKE_STD_ZVAL(mapArray);
145  tres=array_init(mapArray);
146  while(tmp!=NULL){
147    map* sMap=getMapArray(tmp,"size",0);   
148        if(strncmp(tmp->name,"value",5)==0 && sMap!=NULL && tmp->value != NULL){
149      tres=add_assoc_stringl(mapArray,tmp->name,tmp->value,atoi(sMap->value),1);
150        } 
151        else if (tmp->value != NULL) {
152      tres=add_assoc_string(mapArray,tmp->name,tmp->value,1);
153        }
154    tmp=tmp->next;
155  }
156  return mapArray;
157}
158
159/**
160 * Convert a php Array to a maps
161 *
162 * @param t the php Array to convert
163 * @return the created maps
164 */
165maps* php_maps_from_Array(HashTable *t){
166  maps* final_res=NULL;
167  maps* cursor=final_res;
168  char key[1024];
169  for(zend_hash_internal_pointer_reset(t); 
170      zend_hash_has_more_elements(t) == SUCCESS; 
171      zend_hash_move_forward(t)) { 
172    char *key; 
173    uint keylen; 
174    ulong idx; 
175    int type; 
176    zval **ppzval, tmpcopy; 
177    type = zend_hash_get_current_key_ex(t, &key, &keylen, &idx, 0, NULL); 
178    if (zend_hash_get_current_data(t, (void**)&ppzval) == FAILURE) { 
179      /**
180       * Should never actually fail since the key is known to exist.
181       */
182      continue; 
183    }
184    /**
185     * Duplicate the zval so that * the orignal’s contents are not destroyed
186     */
187    tmpcopy = **ppzval;
188#ifdef DEBUG
189    fprintf(stderr,"key : %s\n",key);
190#endif
191    zval_copy_ctor(&tmpcopy); 
192#ifdef DEBUG
193    fprintf(stderr,"key : %s\n",key);
194#endif
195    /**
196     * Reset refcount & Convert
197     */
198    INIT_PZVAL(&tmpcopy); 
199    //convert_to_string(&tmpcopy);
200    if (type == HASH_KEY_IS_STRING) { 
201      /**
202       * String Key / Associative
203       */
204      cursor=(maps*)malloc(MAPS_SIZE);
205      cursor->name=strdup(key);
206    }
207#ifdef DEBUG   
208    fprintf(stderr,"key : %s\n",key);
209#endif 
210    HashTable* t=HASH_OF(*ppzval);
211#ifdef DEBUG
212    fprintf(stderr,"key : %s\n",key);
213#endif
214    cursor->content=php_map_from_HasTable(t);
215    cursor->next=NULL;
216    if(final_res==NULL)
217      final_res=cursor;
218    else
219      addMapsToMaps(&final_res,cursor);
220#ifdef DEBUG
221    fprintf(stderr,"key : %s\n",key);
222#endif
223    /**
224     * Toss out old copy
225     */
226    zval_dtor(&tmpcopy);
227  }
228  return final_res;
229}
230
231/**
232 * Convert a php Array to a map
233 *
234 * @param t the php Array to convert
235 * @return the created map
236 */
237map* php_map_from_HasTable(HashTable* t){
238#ifdef DEBUG
239  fprintf(stderr,"mapsFromPHPArray start\n");
240#endif
241  map* final_res=(map*)malloc(MAP_SIZE);
242  final_res=NULL;
243  char key[1024];
244  for(zend_hash_internal_pointer_reset(t);
245      zend_hash_has_more_elements(t) == SUCCESS;
246      zend_hash_move_forward(t)) {
247    char *key;
248    uint keylen;
249    ulong idx;
250    int type;
251    int len;
252    zval **ppzval, tmpcopy;
253    type = zend_hash_get_current_key_ex(t, &key, &keylen, &idx, 0, NULL); 
254    if (zend_hash_get_current_data(t, (void**)&ppzval) == FAILURE) { 
255      /* Should never actually fail * since the key is known to exist. */ 
256      continue; 
257    }
258    /**
259     * Duplicate the zval so that * the orignal’s contents are not destroyed
260     */ 
261    tmpcopy = **ppzval; 
262    zval_copy_ctor(&tmpcopy); 
263    /**
264     * Reset refcount & Convert
265     */ 
266    INIT_PZVAL(&tmpcopy); 
267    convert_to_string(&tmpcopy);
268    if(strncmp(key,"value",5)==0){
269      len=Z_STRLEN_P(&tmpcopy);
270      addToMapWithSize(final_res,key,Z_STRVAL_P(&tmpcopy),len);
271    }
272    else{
273      if(final_res==NULL){
274#ifdef DEBUG
275        fprintf(stderr,"%s => %s\n",key,Z_STRVAL(tmpcopy));
276#endif
277        final_res=createMap(key,Z_STRVAL(tmpcopy));
278      }
279      else{
280#ifdef DEBUG
281        fprintf(stderr,"%s => %s\n",key,Z_STRVAL(tmpcopy));
282#endif
283        addToMap(final_res,key,Z_STRVAL(tmpcopy));
284      }
285    }
286    /* Toss out old copy */ 
287    zval_dtor(&tmpcopy); 
288  }
289  return final_res;
290}
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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