com_handlers.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Wez Furlong <wez@thebrainroom.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #include "php_ini.h"
  21. #include "ext/standard/info.h"
  22. #include "php_com_dotnet.h"
  23. #include "php_com_dotnet_internal.h"
  24. #include "Zend/zend_exceptions.h"
  25. static zval *com_property_read(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv)
  26. {
  27. php_com_dotnet_object *obj;
  28. VARIANT v;
  29. HRESULT res;
  30. ZVAL_NULL(rv);
  31. obj = (php_com_dotnet_object*) object;
  32. if (V_VT(&obj->v) == VT_DISPATCH) {
  33. VariantInit(&v);
  34. res = php_com_do_invoke(obj, ZSTR_VAL(member), ZSTR_LEN(member),
  35. DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, 0, NULL, 1);
  36. if (res == SUCCESS) {
  37. php_com_zval_from_variant(rv, &v, obj->code_page);
  38. VariantClear(&v);
  39. } else if (res == DISP_E_BADPARAMCOUNT) {
  40. zval zv;
  41. ZVAL_STR(&zv, member);
  42. php_com_saproxy_create(object, rv, &zv);
  43. }
  44. } else {
  45. php_com_throw_exception(E_INVALIDARG, "this variant has no properties");
  46. }
  47. return rv;
  48. }
  49. static zval *com_property_write(zend_object *object, zend_string *member, zval *value, void **cache_slot)
  50. {
  51. php_com_dotnet_object *obj;
  52. VARIANT v;
  53. obj = (php_com_dotnet_object*) object;
  54. if (V_VT(&obj->v) == VT_DISPATCH) {
  55. VariantInit(&v);
  56. if (SUCCESS == php_com_do_invoke(obj, ZSTR_VAL(member), ZSTR_LEN(member),
  57. DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &v, 1, value, 0)) {
  58. VariantClear(&v);
  59. }
  60. } else {
  61. php_com_throw_exception(E_INVALIDARG, "this variant has no properties");
  62. }
  63. return value;
  64. }
  65. static zval *com_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
  66. {
  67. php_com_dotnet_object *obj;
  68. VARIANT v;
  69. ZVAL_NULL(rv);
  70. obj = (php_com_dotnet_object*) object;
  71. if (V_VT(&obj->v) == VT_DISPATCH) {
  72. VariantInit(&v);
  73. if (SUCCESS == php_com_do_invoke_by_id(obj, DISPID_VALUE,
  74. DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, 1, offset, 0, 0)) {
  75. php_com_zval_from_variant(rv, &v, obj->code_page);
  76. VariantClear(&v);
  77. }
  78. } else if (V_ISARRAY(&obj->v)) {
  79. convert_to_long(offset);
  80. if (SafeArrayGetDim(V_ARRAY(&obj->v)) == 1) {
  81. if (php_com_safearray_get_elem(&obj->v, &v, (LONG)Z_LVAL_P(offset))) {
  82. php_com_wrap_variant(rv, &v, obj->code_page);
  83. VariantClear(&v);
  84. }
  85. } else {
  86. php_com_saproxy_create(object, rv, offset);
  87. }
  88. } else {
  89. php_com_throw_exception(E_INVALIDARG, "this variant is not an array type");
  90. }
  91. return rv;
  92. }
  93. static void com_write_dimension(zend_object *object, zval *offset, zval *value)
  94. {
  95. php_com_dotnet_object *obj;
  96. zval args[2];
  97. VARIANT v;
  98. HRESULT res;
  99. obj = (php_com_dotnet_object*) object;
  100. if (offset == NULL) {
  101. php_com_throw_exception(DISP_E_BADINDEX, "appending to variants is not supported");
  102. return;
  103. }
  104. if (V_VT(&obj->v) == VT_DISPATCH) {
  105. ZVAL_COPY_VALUE(&args[0], offset);
  106. ZVAL_COPY_VALUE(&args[1], value);
  107. VariantInit(&v);
  108. if (SUCCESS == php_com_do_invoke_by_id(obj, DISPID_VALUE,
  109. DISPATCH_METHOD|DISPATCH_PROPERTYPUT, &v, 2, args, 0, 0)) {
  110. VariantClear(&v);
  111. }
  112. } else if (V_ISARRAY(&obj->v)) {
  113. LONG indices = 0;
  114. VARTYPE vt;
  115. if (SafeArrayGetDim(V_ARRAY(&obj->v)) == 1) {
  116. if (FAILED(SafeArrayGetVartype(V_ARRAY(&obj->v), &vt)) || vt == VT_EMPTY) {
  117. vt = V_VT(&obj->v) & ~VT_ARRAY;
  118. }
  119. convert_to_long(offset);
  120. indices = (LONG)Z_LVAL_P(offset);
  121. VariantInit(&v);
  122. php_com_variant_from_zval(&v, value, obj->code_page);
  123. if (V_VT(&v) != vt) {
  124. VariantChangeType(&v, &v, 0, vt);
  125. }
  126. if (vt == VT_VARIANT) {
  127. res = SafeArrayPutElement(V_ARRAY(&obj->v), &indices, &v);
  128. } else {
  129. res = SafeArrayPutElement(V_ARRAY(&obj->v), &indices, &v.lVal);
  130. }
  131. VariantClear(&v);
  132. if (FAILED(res)) {
  133. php_com_throw_exception(res, NULL);
  134. }
  135. } else {
  136. php_com_throw_exception(DISP_E_BADINDEX, "this variant has multiple dimensions; you can't set a new value without specifying *all* dimensions");
  137. }
  138. } else {
  139. php_com_throw_exception(E_INVALIDARG, "this variant is not an array type");
  140. }
  141. }
  142. static zval *com_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot)
  143. {
  144. return NULL;
  145. }
  146. static int com_property_exists(zend_object *object, zend_string *member, int check_empty, void **cache_slot)
  147. {
  148. DISPID dispid;
  149. php_com_dotnet_object *obj;
  150. obj = (php_com_dotnet_object*) object;
  151. if (V_VT(&obj->v) == VT_DISPATCH) {
  152. if (SUCCEEDED(php_com_get_id_of_name(obj, ZSTR_VAL(member), ZSTR_LEN(member), &dispid))) {
  153. /* TODO: distinguish between property and method! */
  154. return 1;
  155. }
  156. } else {
  157. /* TODO: check for safearray */
  158. }
  159. return 0;
  160. }
  161. static int com_dimension_exists(zend_object *object, zval *member, int check_empty)
  162. {
  163. /* TODO Add support */
  164. zend_throw_error(NULL, "Cannot check dimension on a COM object");
  165. return 0;
  166. }
  167. static void com_property_delete(zend_object *object, zend_string *member, void **cache_slot)
  168. {
  169. zend_throw_error(NULL, "Cannot delete properties from a COM object");
  170. }
  171. static void com_dimension_delete(zend_object *object, zval *offset)
  172. {
  173. zend_throw_error(NULL, "Cannot delete dimension from a COM object");
  174. }
  175. static HashTable *com_properties_get(zend_object *object)
  176. {
  177. /* TODO: use type-info to get all the names and values ?
  178. * DANGER: if we do that, there is a strong possibility for
  179. * infinite recursion when the hash is displayed via var_dump().
  180. * Perhaps it is best to leave it un-implemented.
  181. */
  182. return (HashTable *) &zend_empty_array;
  183. }
  184. static HashTable *com_get_gc(zend_object *object, zval **table, int *n)
  185. {
  186. *table = NULL;
  187. *n = 0;
  188. return NULL;
  189. }
  190. static void function_dtor(zval *zv)
  191. {
  192. zend_internal_function *f = (zend_internal_function*)Z_PTR_P(zv);
  193. zend_string_release_ex(f->function_name, 0);
  194. if (f->arg_info) {
  195. efree(f->arg_info);
  196. }
  197. efree(f);
  198. }
  199. static PHP_FUNCTION(com_method_handler)
  200. {
  201. zval *object = getThis();
  202. zend_string *method = EX(func)->common.function_name;
  203. zval *args = NULL;
  204. php_com_dotnet_object *obj = CDNO_FETCH(object);
  205. int nargs;
  206. VARIANT v;
  207. int ret = FAILURE;
  208. if (V_VT(&obj->v) != VT_DISPATCH) {
  209. goto exit;
  210. }
  211. nargs = ZEND_NUM_ARGS();
  212. if (nargs) {
  213. args = (zval *)safe_emalloc(sizeof(zval), nargs, 0);
  214. zend_get_parameters_array_ex(nargs, args);
  215. }
  216. VariantInit(&v);
  217. if (SUCCESS == php_com_do_invoke_byref(obj, (zend_internal_function*)EX(func), DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, nargs, args)) {
  218. php_com_zval_from_variant(return_value, &v, obj->code_page);
  219. ret = SUCCESS;
  220. VariantClear(&v);
  221. }
  222. if (args) {
  223. efree(args);
  224. }
  225. exit:
  226. /* Cleanup trampoline */
  227. ZEND_ASSERT(EX(func)->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE);
  228. zend_string_release(EX(func)->common.function_name);
  229. zend_free_trampoline(EX(func));
  230. EX(func) = NULL;
  231. }
  232. static zend_function *com_method_get(zend_object **object_ptr, zend_string *name, const zval *key)
  233. {
  234. zend_internal_function f, *fptr = NULL;
  235. zend_function *func;
  236. DISPID dummy;
  237. php_com_dotnet_object *obj = (php_com_dotnet_object*)*object_ptr;
  238. if (V_VT(&obj->v) != VT_DISPATCH) {
  239. return NULL;
  240. }
  241. if (FAILED(php_com_get_id_of_name(obj, name->val, name->len, &dummy))) {
  242. return NULL;
  243. }
  244. /* check cache */
  245. if (obj->method_cache == NULL || NULL == (fptr = zend_hash_find_ptr(obj->method_cache, name))) {
  246. memset(&f, 0, sizeof(zend_internal_function));
  247. f.type = ZEND_INTERNAL_FUNCTION;
  248. f.num_args = 0;
  249. f.arg_info = NULL;
  250. f.scope = obj->ce;
  251. f.fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
  252. f.function_name = zend_string_copy(name);
  253. f.handler = PHP_FN(com_method_handler);
  254. fptr = &f;
  255. if (obj->typeinfo) {
  256. /* look for byref params */
  257. ITypeComp *comp;
  258. ITypeInfo *TI = NULL;
  259. DESCKIND kind;
  260. BINDPTR bindptr;
  261. OLECHAR *olename;
  262. ULONG lhash;
  263. int i;
  264. if (SUCCEEDED(ITypeInfo_GetTypeComp(obj->typeinfo, &comp))) {
  265. olename = php_com_string_to_olestring(name->val, name->len, obj->code_page);
  266. lhash = LHashValOfNameSys(SYS_WIN32, LOCALE_SYSTEM_DEFAULT, olename);
  267. if (SUCCEEDED(ITypeComp_Bind(comp, olename, lhash, INVOKE_FUNC, &TI, &kind, &bindptr))) {
  268. switch (kind) {
  269. case DESCKIND_FUNCDESC:
  270. f.arg_info = ecalloc(bindptr.lpfuncdesc->cParams, sizeof(zend_arg_info));
  271. for (i = 0; i < bindptr.lpfuncdesc->cParams; i++) {
  272. bool by_ref = (bindptr.lpfuncdesc->lprgelemdescParam[i].paramdesc.wParamFlags & PARAMFLAG_FOUT) != 0;
  273. f.arg_info[i].type = (zend_type) ZEND_TYPE_INIT_NONE(_ZEND_ARG_INFO_FLAGS(by_ref, 0, 0));
  274. }
  275. f.num_args = bindptr.lpfuncdesc->cParams;
  276. ITypeInfo_ReleaseFuncDesc(TI, bindptr.lpfuncdesc);
  277. break;
  278. /* these should not happen, but *might* happen if the user
  279. * screws up; lets avoid a leak in that case */
  280. case DESCKIND_VARDESC:
  281. ITypeInfo_ReleaseVarDesc(TI, bindptr.lpvardesc);
  282. break;
  283. case DESCKIND_TYPECOMP:
  284. ITypeComp_Release(bindptr.lptcomp);
  285. break;
  286. case DESCKIND_NONE:
  287. break;
  288. }
  289. if (TI) {
  290. ITypeInfo_Release(TI);
  291. }
  292. }
  293. ITypeComp_Release(comp);
  294. efree(olename);
  295. }
  296. }
  297. zend_set_function_arg_flags((zend_function*)&f);
  298. /* save this method in the cache */
  299. if (!obj->method_cache) {
  300. ALLOC_HASHTABLE(obj->method_cache);
  301. zend_hash_init(obj->method_cache, 2, NULL, function_dtor, 0);
  302. }
  303. zend_hash_update_mem(obj->method_cache, name, &f, sizeof(f));
  304. }
  305. if (fptr) {
  306. /* duplicate this into a new chunk of emalloc'd memory,
  307. * since the engine will efree it */
  308. zend_string_addref(fptr->function_name);
  309. func = emalloc(sizeof(*fptr));
  310. memcpy(func, fptr, sizeof(*fptr));
  311. return func;
  312. }
  313. return NULL;
  314. }
  315. static zend_string* com_class_name_get(const zend_object *object)
  316. {
  317. php_com_dotnet_object *obj = (php_com_dotnet_object *)object;
  318. return zend_string_copy(obj->ce->name);
  319. }
  320. /* This compares two variants for equality */
  321. static int com_objects_compare(zval *object1, zval *object2)
  322. {
  323. php_com_dotnet_object *obja, *objb;
  324. int ret;
  325. /* strange header bug problem here... the headers define the proto without the
  326. * flags parameter. However, the MSDN docs state that there is a flags parameter,
  327. * and my VC6 won't link unless the code uses the version with 4 parameters.
  328. * So, we have this declaration here to fix it */
  329. STDAPI VarCmp(LPVARIANT pvarLeft, LPVARIANT pvarRight, LCID lcid, DWORD flags);
  330. ZEND_COMPARE_OBJECTS_FALLBACK(object1, object2);
  331. obja = CDNO_FETCH(object1);
  332. objb = CDNO_FETCH(object2);
  333. switch (VarCmp(&obja->v, &objb->v, LOCALE_SYSTEM_DEFAULT, 0)) {
  334. case VARCMP_LT:
  335. ret = -1;
  336. break;
  337. case VARCMP_GT:
  338. ret = 1;
  339. break;
  340. case VARCMP_EQ:
  341. ret = 0;
  342. break;
  343. default:
  344. /* either or both operands are NULL...
  345. * not 100% sure how to handle this */
  346. ret = -2;
  347. }
  348. return ret;
  349. }
  350. static int com_object_cast(zend_object *readobj, zval *writeobj, int type)
  351. {
  352. php_com_dotnet_object *obj;
  353. VARIANT v;
  354. VARTYPE vt = VT_EMPTY;
  355. HRESULT res = S_OK;
  356. obj = (php_com_dotnet_object*) readobj;
  357. ZVAL_NULL(writeobj);
  358. VariantInit(&v);
  359. if (V_VT(&obj->v) == VT_DISPATCH) {
  360. if (SUCCESS != php_com_do_invoke_by_id(obj, DISPID_VALUE,
  361. DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, 0, NULL, 1, 0)) {
  362. VariantCopy(&v, &obj->v);
  363. }
  364. } else {
  365. VariantCopy(&v, &obj->v);
  366. }
  367. switch(type) {
  368. case IS_LONG:
  369. case _IS_NUMBER:
  370. #if SIZEOF_ZEND_LONG == 4
  371. vt = VT_I4;
  372. #else
  373. vt = VT_I8;
  374. #endif
  375. break;
  376. case IS_DOUBLE:
  377. vt = VT_R8;
  378. break;
  379. case IS_FALSE:
  380. case IS_TRUE:
  381. case _IS_BOOL:
  382. vt = VT_BOOL;
  383. break;
  384. case IS_STRING:
  385. vt = VT_BSTR;
  386. break;
  387. default:
  388. ;
  389. }
  390. if (vt != VT_EMPTY && vt != V_VT(&v)) {
  391. res = VariantChangeType(&v, &v, 0, vt);
  392. }
  393. if (SUCCEEDED(res)) {
  394. php_com_zval_from_variant(writeobj, &v, obj->code_page);
  395. }
  396. VariantClear(&v);
  397. if (SUCCEEDED(res)) {
  398. return SUCCESS;
  399. }
  400. return zend_std_cast_object_tostring(readobj, writeobj, type);
  401. }
  402. static int com_object_count(zend_object *object, zend_long *count)
  403. {
  404. php_com_dotnet_object *obj;
  405. LONG ubound = 0, lbound = 0;
  406. obj = (php_com_dotnet_object*) object;
  407. if (!V_ISARRAY(&obj->v)) {
  408. return FAILURE;
  409. }
  410. SafeArrayGetLBound(V_ARRAY(&obj->v), 1, &lbound);
  411. SafeArrayGetUBound(V_ARRAY(&obj->v), 1, &ubound);
  412. *count = ubound - lbound + 1;
  413. return SUCCESS;
  414. }
  415. zend_object_handlers php_com_object_handlers = {
  416. 0,
  417. php_com_object_free_storage,
  418. zend_objects_destroy_object,
  419. php_com_object_clone,
  420. com_property_read,
  421. com_property_write,
  422. com_read_dimension,
  423. com_write_dimension,
  424. com_get_property_ptr_ptr,
  425. com_property_exists,
  426. com_property_delete,
  427. com_dimension_exists,
  428. com_dimension_delete,
  429. com_properties_get,
  430. com_method_get,
  431. zend_std_get_constructor,
  432. com_class_name_get,
  433. com_object_cast,
  434. com_object_count,
  435. NULL, /* get_debug_info */
  436. NULL, /* get_closure */
  437. com_get_gc, /* get_gc */
  438. NULL, /* do_operation */
  439. com_objects_compare, /* compare */
  440. NULL, /* get_properties_for */
  441. };
  442. void php_com_object_enable_event_sink(php_com_dotnet_object *obj, int enable)
  443. {
  444. if (obj->sink_dispatch) {
  445. IConnectionPointContainer *cont;
  446. IConnectionPoint *point;
  447. if (SUCCEEDED(IDispatch_QueryInterface(V_DISPATCH(&obj->v),
  448. &IID_IConnectionPointContainer, (void**)&cont))) {
  449. if (SUCCEEDED(IConnectionPointContainer_FindConnectionPoint(cont,
  450. &obj->sink_id, &point))) {
  451. if (enable) {
  452. IConnectionPoint_Advise(point, (IUnknown*)obj->sink_dispatch, &obj->sink_cookie);
  453. } else {
  454. IConnectionPoint_Unadvise(point, obj->sink_cookie);
  455. }
  456. IConnectionPoint_Release(point);
  457. }
  458. IConnectionPointContainer_Release(cont);
  459. }
  460. }
  461. }
  462. void php_com_object_free_storage(zend_object *object)
  463. {
  464. php_com_dotnet_object *obj = (php_com_dotnet_object*)object;
  465. if (obj->typeinfo) {
  466. ITypeInfo_Release(obj->typeinfo);
  467. obj->typeinfo = NULL;
  468. }
  469. if (obj->sink_dispatch) {
  470. php_com_object_enable_event_sink(obj, FALSE);
  471. IDispatch_Release(obj->sink_dispatch);
  472. obj->sink_dispatch = NULL;
  473. }
  474. VariantClear(&obj->v);
  475. if (obj->method_cache) {
  476. zend_hash_destroy(obj->method_cache);
  477. FREE_HASHTABLE(obj->method_cache);
  478. }
  479. if (obj->id_of_name_cache) {
  480. zend_hash_destroy(obj->id_of_name_cache);
  481. FREE_HASHTABLE(obj->id_of_name_cache);
  482. }
  483. zend_object_std_dtor(object);
  484. }
  485. zend_object* php_com_object_clone(zend_object *object)
  486. {
  487. php_com_dotnet_object *cloneobj, *origobject;
  488. origobject = (php_com_dotnet_object*) object;
  489. cloneobj = (php_com_dotnet_object*)emalloc(sizeof(php_com_dotnet_object));
  490. memcpy(cloneobj, origobject, sizeof(*cloneobj));
  491. /* VariantCopy will perform VariantClear; we don't want to clobber
  492. * the IDispatch that we memcpy'd, so we init a new variant in the
  493. * clone structure */
  494. VariantInit(&cloneobj->v);
  495. /* We use the Indirection-following version of the API since we
  496. * want to clone as much as possible */
  497. VariantCopyInd(&cloneobj->v, &origobject->v);
  498. if (cloneobj->typeinfo) {
  499. ITypeInfo_AddRef(cloneobj->typeinfo);
  500. }
  501. return (zend_object*)cloneobj;
  502. }
  503. zend_object* php_com_object_new(zend_class_entry *ce)
  504. {
  505. php_com_dotnet_object *obj;
  506. php_com_initialize();
  507. obj = emalloc(sizeof(*obj));
  508. memset(obj, 0, sizeof(*obj));
  509. VariantInit(&obj->v);
  510. obj->code_page = CP_ACP;
  511. obj->ce = ce;
  512. zend_object_std_init(&obj->zo, ce);
  513. obj->zo.handlers = &php_com_object_handlers;
  514. obj->typeinfo = NULL;
  515. return (zend_object*)obj;
  516. }