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