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