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