[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 | |
---|
| 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 | fprintf(stderr,"PHP EMBEDED\n"); |
---|
| 60 | |
---|
| 61 | zend_try { |
---|
| 62 | fprintf(stderr,"PHP EMBEDED include script %s\n",tmp->value); |
---|
| 63 | php_execute_script(&iscript TSRMLS_CC); |
---|
| 64 | fprintf(stderr,"PHP EMBEDED include script done\n"); |
---|
| 65 | |
---|
| 66 | zval *iargs[3]; |
---|
| 67 | zval iret, ifunc,ifile; |
---|
| 68 | |
---|
| 69 | ZVAL_STRING(&ifunc, s->name, 0); |
---|
| 70 | fprintf(stderr,"PHP EMBEDED include script done\n"); |
---|
| 71 | iargs[0] = php_Array_from_maps(*main_conf); |
---|
| 72 | fprintf(stderr,"PHP EMBEDED include script done\n"); |
---|
| 73 | iargs[1] = php_Array_from_maps(*real_inputs); |
---|
| 74 | fprintf(stderr,"PHP EMBEDED include script done\n"); |
---|
| 75 | iargs[2] = php_Array_from_maps(*real_outputs); |
---|
| 76 | fprintf(stderr,"PHP EMBEDED include script done\n"); |
---|
| 77 | |
---|
| 78 | call_user_function(EG(function_table), NULL, &ifunc, &iret, 3, iargs TSRMLS_CC); |
---|
| 79 | |
---|
| 80 | HashTable* t=HASH_OF(iargs[2]); |
---|
| 81 | *real_outputs=php_maps_from_Array(t); |
---|
| 82 | |
---|
| 83 | dumpMaps(*real_outputs); |
---|
| 84 | |
---|
| 85 | char tmp1[1024]; |
---|
| 86 | |
---|
| 87 | sprintf(tmp1,"PHP EMBEDED ran function successfully and return %d !?",Z_STRVAL(iret)); |
---|
| 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 | fprintf(stderr,"arra_init\n"); |
---|
| 109 | MAKE_STD_ZVAL(mapArray); |
---|
| 110 | tres=array_init(mapArray); |
---|
| 111 | fprintf(stderr,"arra_init %d\n",tres); |
---|
| 112 | while(tmp!=NULL){ |
---|
| 113 | add_assoc_zval(mapArray,tmp->name,php_Array_from_map(tmp->content)); |
---|
| 114 | tmp=tmp->next; |
---|
| 115 | } |
---|
| 116 | return mapArray; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | zval *php_Array_from_map(map* t){ |
---|
| 120 | zval *mapArray; |
---|
| 121 | zval *mapArrayTmp; |
---|
| 122 | map* tmp=t; |
---|
| 123 | int tres=0; |
---|
| 124 | fprintf(stderr,"arra_init\n"); |
---|
| 125 | MAKE_STD_ZVAL(mapArray); |
---|
| 126 | tres=array_init(mapArray); |
---|
| 127 | fprintf(stderr,"arra_init\n"); |
---|
| 128 | while(tmp!=NULL){ |
---|
| 129 | fprintf(stderr,"=> %s %d %s %d \n",tmp->name,strlen(tmp->name),tmp->value,strlen(tmp->value)); |
---|
| 130 | tres=add_assoc_string(mapArray,tmp->name,tmp->value,1); |
---|
| 131 | tmp=tmp->next; |
---|
| 132 | } |
---|
| 133 | return mapArray; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | maps* php_maps_from_Array(HashTable *t){ |
---|
| 137 | //#ifdef DEBUG |
---|
| 138 | fprintf(stderr,"mapsFromPHPArray start\n"); |
---|
| 139 | //#endif |
---|
| 140 | maps* final_res=NULL; |
---|
| 141 | maps* cursor=final_res; |
---|
| 142 | char key[1024]; |
---|
| 143 | for(zend_hash_internal_pointer_reset(t); |
---|
| 144 | zend_hash_has_more_elements(t) == SUCCESS; |
---|
| 145 | zend_hash_move_forward(t)) { |
---|
| 146 | char *key; |
---|
| 147 | uint keylen; |
---|
| 148 | ulong idx; |
---|
| 149 | int type; |
---|
| 150 | zval **ppzval, tmpcopy; |
---|
| 151 | type = zend_hash_get_current_key_ex(t, &key, &keylen, &idx, 0, NULL); |
---|
| 152 | fprintf(stderr,"key : %s\n",key); |
---|
| 153 | if (zend_hash_get_current_data(t, (void**)&ppzval) == FAILURE) { |
---|
| 154 | /** |
---|
| 155 | * Should never actually fail since the key is known to exist. |
---|
| 156 | */ |
---|
| 157 | continue; |
---|
| 158 | } |
---|
| 159 | /** |
---|
| 160 | * Duplicate the zval so that * the orignal’s contents are not destroyed |
---|
| 161 | */ |
---|
| 162 | tmpcopy = **ppzval; |
---|
| 163 | fprintf(stderr,"key : %s\n",key); |
---|
| 164 | zval_copy_ctor(&tmpcopy); |
---|
| 165 | fprintf(stderr,"key : %s\n",key); |
---|
| 166 | /** |
---|
| 167 | * Reset refcount & Convert |
---|
| 168 | */ |
---|
| 169 | INIT_PZVAL(&tmpcopy); |
---|
| 170 | //convert_to_string(&tmpcopy); |
---|
| 171 | if (type == HASH_KEY_IS_STRING) { |
---|
| 172 | /** |
---|
| 173 | * String Key / Associative |
---|
| 174 | */ |
---|
| 175 | cursor=(maps*)malloc(MAPS_SIZE); |
---|
| 176 | cursor->name=strdup(key); |
---|
| 177 | } |
---|
| 178 | fprintf(stderr,"key : %s\n",key); |
---|
| 179 | HashTable* t=HASH_OF(*ppzval); |
---|
| 180 | fprintf(stderr,"key : %s\n",key); |
---|
| 181 | cursor->content=php_map_from_HasTable(t); |
---|
| 182 | cursor->next=NULL; |
---|
| 183 | if(final_res==NULL) |
---|
| 184 | final_res=cursor; |
---|
| 185 | else |
---|
| 186 | addMapsToMaps(&final_res,cursor); |
---|
| 187 | fprintf(stderr,"key : %s\n",key); |
---|
| 188 | /** |
---|
| 189 | * Toss out old copy |
---|
| 190 | */ |
---|
| 191 | zval_dtor(&tmpcopy); |
---|
| 192 | } |
---|
| 193 | return final_res; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | map* php_map_from_HasTable(HashTable* t){ |
---|
| 197 | #ifdef DEBUG |
---|
| 198 | fprintf(stderr,"mapsFromPHPArray start\n"); |
---|
| 199 | #endif |
---|
| 200 | map* final_res=(map*)malloc(MAP_SIZE); |
---|
| 201 | final_res=NULL; |
---|
| 202 | char key[1024]; |
---|
| 203 | for(zend_hash_internal_pointer_reset(t); |
---|
| 204 | zend_hash_has_more_elements(t) == SUCCESS; |
---|
| 205 | zend_hash_move_forward(t)) { |
---|
| 206 | char *key; |
---|
| 207 | uint keylen; |
---|
| 208 | ulong idx; |
---|
| 209 | int type; |
---|
| 210 | zval **ppzval, tmpcopy; |
---|
| 211 | type = zend_hash_get_current_key_ex(t, &key, &keylen, &idx, 0, NULL); |
---|
| 212 | if (zend_hash_get_current_data(t, (void**)&ppzval) == FAILURE) { |
---|
| 213 | /* Should never actually fail * since the key is known to exist. */ |
---|
| 214 | continue; |
---|
| 215 | } |
---|
| 216 | /** |
---|
| 217 | * Duplicate the zval so that * the orignal’s contents are not destroyed |
---|
| 218 | */ |
---|
| 219 | tmpcopy = **ppzval; |
---|
| 220 | zval_copy_ctor(&tmpcopy); |
---|
| 221 | /** |
---|
| 222 | * Reset refcount & Convert |
---|
| 223 | */ |
---|
| 224 | INIT_PZVAL(&tmpcopy); |
---|
| 225 | convert_to_string(&tmpcopy); |
---|
| 226 | if(final_res==NULL){ |
---|
| 227 | fprintf(stderr,"%s => %s\n",key,Z_STRVAL(tmpcopy)); |
---|
| 228 | final_res=createMap(key,Z_STRVAL(tmpcopy)); |
---|
| 229 | } |
---|
| 230 | else{ |
---|
| 231 | fprintf(stderr,"%s => %s\n",key,Z_STRVAL(tmpcopy)); |
---|
| 232 | addMapToMap(&final_res,createMap(key,Z_STRVAL(tmpcopy))); |
---|
| 233 | } |
---|
| 234 | /* Toss out old copy */ |
---|
| 235 | zval_dtor(&tmpcopy); |
---|
| 236 | } |
---|
| 237 | return final_res; |
---|
| 238 | } |
---|