/* This compares two variants for equality */ static int com_objects_compare(zval *object1, zval *object2) { php_com_dotnet_object *obja, *objb; int ret; /* strange header bug problem here... the headers define the proto without the * flags parameter. However, the MSDN docs state that there is a flags parameter, * and my VC6 won't link unless the code uses the version with 4 parameters. * So, we have this declaration here to fix it */ STDAPI VarCmp(LPVARIANT pvarLeft, LPVARIANT pvarRight, LCID lcid, DWORD flags); obja = CDNO_FETCH(object1); objb = CDNO_FETCH(object2); switch (VarCmp(&obja->v, &objb->v, LOCALE_SYSTEM_DEFAULT, 0)) { case VARCMP_LT: ret = -1; break; case VARCMP_GT: ret = 1; break; case VARCMP_EQ: ret = 0; break; default: /* either or both operands are NULL... * not 100% sure how to handle this */ ret = -2; } return ret; }
static zval *com_property_read(zval *object, zval *member, int type TSRMLS_DC) { zval *return_value; php_com_dotnet_object *obj; VARIANT v; HRESULT res; MAKE_STD_ZVAL(return_value); ZVAL_NULL(return_value); Z_SET_REFCOUNT_P(return_value, 0); Z_UNSET_ISREF_P(return_value); obj = CDNO_FETCH(object); if (V_VT(&obj->v) == VT_DISPATCH) { VariantInit(&v); convert_to_string_ex(&member); res = php_com_do_invoke(obj, Z_STRVAL_P(member), Z_STRLEN_P(member), DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, 0, NULL, 1 TSRMLS_CC); if (res == SUCCESS) { php_com_zval_from_variant(return_value, &v, obj->code_page TSRMLS_CC); VariantClear(&v); } else if (res == DISP_E_BADPARAMCOUNT) { php_com_saproxy_create(object, return_value, member TSRMLS_CC); } } else { php_com_throw_exception(E_INVALIDARG, "this variant has no properties" TSRMLS_CC); } return return_value; }
static zval *com_read_dimension(zval *object, zval *offset, int type, zval *rv) { php_com_dotnet_object *obj; VARIANT v; ZVAL_NULL(rv); obj = CDNO_FETCH(object); if (V_VT(&obj->v) == VT_DISPATCH) { VariantInit(&v); if (SUCCESS == php_com_do_invoke_by_id(obj, DISPID_VALUE, DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, 1, offset, 0, 0)) { php_com_zval_from_variant(rv, &v, obj->code_page); VariantClear(&v); } } else if (V_ISARRAY(&obj->v)) { convert_to_long(offset); if (SafeArrayGetDim(V_ARRAY(&obj->v)) == 1) { if (php_com_safearray_get_elem(&obj->v, &v, (LONG)Z_LVAL_P(offset))) { php_com_wrap_variant(rv, &v, obj->code_page); VariantClear(&v); } } else { php_com_saproxy_create(object, rv, offset); } } else { php_com_throw_exception(E_INVALIDARG, "this variant is not an array type"); } return rv; }
static zval *com_property_read(zval *object, zval *member, int type, void **cahce_slot, zval *rv) { php_com_dotnet_object *obj; VARIANT v; HRESULT res; ZVAL_NULL(rv); obj = CDNO_FETCH(object); if (V_VT(&obj->v) == VT_DISPATCH) { VariantInit(&v); convert_to_string_ex(member); res = php_com_do_invoke(obj, Z_STRVAL_P(member), Z_STRLEN_P(member), DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, 0, NULL, 1); if (res == SUCCESS) { php_com_zval_from_variant(rv, &v, obj->code_page); VariantClear(&v); } else if (res == DISP_E_BADPARAMCOUNT) { php_com_saproxy_create(object, rv, member); } } else { php_com_throw_exception(E_INVALIDARG, "this variant has no properties"); } return rv; }
static void com_write_dimension(zval *object, zval *offset, zval *value) { php_com_dotnet_object *obj; zval args[2]; VARIANT v; HRESULT res; obj = CDNO_FETCH(object); if (V_VT(&obj->v) == VT_DISPATCH) { ZVAL_COPY_VALUE(&args[0], offset); ZVAL_COPY_VALUE(&args[1], value); VariantInit(&v); if (SUCCESS == php_com_do_invoke_by_id(obj, DISPID_VALUE, DISPATCH_METHOD|DISPATCH_PROPERTYPUT, &v, 2, args, 0, 0)) { VariantClear(&v); } } else if (V_ISARRAY(&obj->v)) { LONG indices = 0; VARTYPE vt; if (SafeArrayGetDim(V_ARRAY(&obj->v)) == 1) { if (FAILED(SafeArrayGetVartype(V_ARRAY(&obj->v), &vt)) || vt == VT_EMPTY) { vt = V_VT(&obj->v) & ~VT_ARRAY; } convert_to_long(offset); indices = (LONG)Z_LVAL_P(offset); VariantInit(&v); php_com_variant_from_zval(&v, value, obj->code_page); if (V_VT(&v) != vt) { VariantChangeType(&v, &v, 0, vt); } if (vt == VT_VARIANT) { res = SafeArrayPutElement(V_ARRAY(&obj->v), &indices, &v); } else { res = SafeArrayPutElement(V_ARRAY(&obj->v), &indices, &v.lVal); } VariantClear(&v); if (FAILED(res)) { php_com_throw_exception(res, NULL); } } else { php_com_throw_exception(DISP_E_BADINDEX, "this variant has multiple dimensions; you can't set a new value without specifying *all* dimensions"); } } else { php_com_throw_exception(E_INVALIDARG, "this variant is not an array type"); } }
static int com_object_cast(zval *readobj, zval *writeobj, int type) { php_com_dotnet_object *obj; VARIANT v; VARTYPE vt = VT_EMPTY; HRESULT res = S_OK; obj = CDNO_FETCH(readobj); ZVAL_NULL(writeobj); VariantInit(&v); if (V_VT(&obj->v) == VT_DISPATCH) { if (SUCCESS != php_com_do_invoke_by_id(obj, DISPID_VALUE, DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, 0, NULL, 1, 0)) { VariantCopy(&v, &obj->v); } } else { VariantCopy(&v, &obj->v); } switch(type) { case IS_LONG: vt = VT_INT; break; case IS_DOUBLE: vt = VT_R8; break; case IS_FALSE: case IS_TRUE: case _IS_BOOL: vt = VT_BOOL; break; case IS_STRING: vt = VT_BSTR; break; default: ; } if (vt != VT_EMPTY && vt != V_VT(&v)) { res = VariantChangeType(&v, &v, 0, vt); } if (SUCCEEDED(res)) { php_com_zval_from_variant(writeobj, &v, obj->code_page); } VariantClear(&v); if (SUCCEEDED(res)) { return SUCCESS; } return zend_std_cast_object_tostring(readobj, writeobj, type); }
static void variant_unary_operation(enum variant_unary_opcode op, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ { VARIANT vres; VARIANT left_val; VARIANT *vleft = NULL; zval *zleft = NULL; php_com_dotnet_object *obj; HRESULT result; int codepage = CP_ACP; VariantInit(&left_val); VariantInit(&vres); if (SUCCESS == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "O", &zleft, php_com_variant_class_entry)) { obj = CDNO_FETCH(zleft); vleft = &obj->v; } else if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z!", &zleft)) { vleft = &left_val; php_com_variant_from_zval(vleft, zleft, codepage); } else { return; } switch (op) { case VOP_ABS: result = VarAbs(vleft, &vres); break; case VOP_FIX: result = VarFix(vleft, &vres); break; case VOP_INT: result = VarInt(vleft, &vres); break; case VOP_NEG: result = VarNeg(vleft, &vres); break; case VOP_NOT: result = VarNot(vleft, &vres); break; default: result = E_INVALIDARG; } if (SUCCEEDED(result)) { php_com_wrap_variant(return_value, &vres, codepage); } else { php_com_throw_exception(result, NULL); } VariantClear(&vres); VariantClear(&left_val); }
static int com_object_count(zval *object, zend_long *count) { php_com_dotnet_object *obj; LONG ubound = 0, lbound = 0; obj = CDNO_FETCH(object); if (!V_ISARRAY(&obj->v)) { return FAILURE; } SafeArrayGetLBound(V_ARRAY(&obj->v), 1, &lbound); SafeArrayGetUBound(V_ARRAY(&obj->v), 1, &ubound); *count = ubound - lbound + 1; return SUCCESS; }
static void com_property_write(zval *object, zval *member, zval *value, void **cache_slot) { php_com_dotnet_object *obj; VARIANT v; obj = CDNO_FETCH(object); if (V_VT(&obj->v) == VT_DISPATCH) { VariantInit(&v); convert_to_string_ex(member); if (SUCCESS == php_com_do_invoke(obj, Z_STRVAL_P(member), Z_STRLEN_P(member), DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &v, 1, value, 0)) { VariantClear(&v); } } else { php_com_throw_exception(E_INVALIDARG, "this variant has no properties"); } }
static int com_property_exists(zval *object, zval *member, int check_empty, void **cache_slot) { DISPID dispid; php_com_dotnet_object *obj; obj = CDNO_FETCH(object); if (V_VT(&obj->v) == VT_DISPATCH) { convert_to_string_ex(member); if (SUCCEEDED(php_com_get_id_of_name(obj, Z_STRVAL_P(member), Z_STRLEN_P(member), &dispid))) { /* TODO: distinguish between property and method! */ return 1; } } else { /* TODO: check for safearray */ } return 0; }
} else if (res == DISP_E_BADPARAMCOUNT) { php_com_saproxy_create(object, return_value, member TSRMLS_CC); } } else { php_com_throw_exception(E_INVALIDARG, "this variant has no properties" TSRMLS_CC); } return return_value; } static void com_property_write(zval *object, zval *member, zval *value TSRMLS_DC) { php_com_dotnet_object *obj; VARIANT v; obj = CDNO_FETCH(object); if (V_VT(&obj->v) == VT_DISPATCH) { VariantInit(&v); convert_to_string_ex(&member); if (SUCCESS == php_com_do_invoke(obj, Z_STRVAL_P(member), Z_STRLEN_P(member), DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &v, 1, &value, 0 TSRMLS_CC)) { VariantClear(&v); } } else { php_com_throw_exception(E_INVALIDARG, "this variant has no properties" TSRMLS_CC); } } static zval *com_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
zend_object_iterator *php_com_iter_get(zend_class_entry *ce, zval *object, int by_ref) { php_com_dotnet_object *obj; struct php_com_iterator *I; IEnumVARIANT *iev = NULL; DISPPARAMS dp; VARIANT v; unsigned long n_fetched; zval ptr; if (by_ref) { zend_throw_error(NULL, "An iterator cannot be used with foreach by reference"); return NULL; } obj = CDNO_FETCH(object); if (V_VT(&obj->v) != VT_DISPATCH && !V_ISARRAY(&obj->v)) { php_error_docref(NULL, E_WARNING, "variant is not an object or array VT=%d", V_VT(&obj->v)); return NULL; } memset(&dp, 0, sizeof(dp)); VariantInit(&v); I = (struct php_com_iterator*)ecalloc(1, sizeof(*I)); zend_iterator_init(&I->iter); I->iter.funcs = &com_iter_funcs; Z_PTR(I->iter.data) = I; I->code_page = obj->code_page; ZVAL_UNDEF(&I->zdata); VariantInit(&I->safe_array); VariantInit(&I->v); if (V_ISARRAY(&obj->v)) { LONG bound; UINT dims; dims = SafeArrayGetDim(V_ARRAY(&obj->v)); if (dims != 1) { php_error_docref(NULL, E_WARNING, "Can only handle single dimension variant arrays (this array has %d)", dims); goto fail; } /* same semantics as foreach on a PHP array; * make a copy and enumerate that copy */ VariantCopy(&I->safe_array, &obj->v); /* determine the key value for the array */ SafeArrayGetLBound(V_ARRAY(&I->safe_array), 1, &bound); SafeArrayGetUBound(V_ARRAY(&I->safe_array), 1, &I->sa_max); /* pre-fetch the element */ if (php_com_safearray_get_elem(&I->safe_array, &I->v, bound)) { I->key = bound; ZVAL_NULL(&ptr); php_com_zval_from_variant(&ptr, &I->v, I->code_page); ZVAL_COPY_VALUE(&I->zdata, &ptr); } else { I->key = (ulong)-1; } } else { /* can we enumerate it? */ if (FAILED(IDispatch_Invoke(V_DISPATCH(&obj->v), DISPID_NEWENUM, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD|DISPATCH_PROPERTYGET, &dp, &v, NULL, NULL))) { goto fail; } /* get something useful out of it */ if (V_VT(&v) == VT_UNKNOWN) { IUnknown_QueryInterface(V_UNKNOWN(&v), &IID_IEnumVARIANT, (void**)&iev); } else if (V_VT(&v) == VT_DISPATCH) { IDispatch_QueryInterface(V_DISPATCH(&v), &IID_IEnumVARIANT, (void**)&iev); } VariantClear(&v); if (iev == NULL) { goto fail; } I->ev = iev; /* Get the first element now */ if (SUCCEEDED(IEnumVARIANT_Next(I->ev, 1, &I->v, &n_fetched)) && n_fetched > 0) { /* indicate that we have element 0 */ I->key = 0; ZVAL_NULL(&ptr); php_com_zval_from_variant(&ptr, &I->v, I->code_page); ZVAL_COPY_VALUE(&I->zdata, &ptr); } else { /* indicate that there are no more items */ I->key = (ulong)-1; } } return &I->iter; fail: if (I) { VariantClear(&I->safe_array); VariantClear(&I->v); efree(I); } return NULL; }
PHP_COM_DOTNET_API void php_com_variant_from_zval(VARIANT *v, zval *z, int codepage TSRMLS_DC) { OLECHAR *olestring; php_com_dotnet_object *obj; switch (Z_TYPE_P(z)) { case IS_NULL: V_VT(v) = VT_NULL; break; case IS_BOOL: V_VT(v) = VT_BOOL; V_BOOL(v) = Z_BVAL_P(z) ? VARIANT_TRUE : VARIANT_FALSE; break; case IS_OBJECT: if (php_com_is_valid_object(z TSRMLS_CC)) { obj = CDNO_FETCH(z); if (V_VT(&obj->v) == VT_DISPATCH) { /* pass the underlying object */ V_VT(v) = VT_DISPATCH; if (V_DISPATCH(&obj->v)) { IDispatch_AddRef(V_DISPATCH(&obj->v)); } V_DISPATCH(v) = V_DISPATCH(&obj->v); } else { /* pass the variant by reference */ V_VT(v) = VT_VARIANT | VT_BYREF; V_VARIANTREF(v) = &obj->v; } } else { /* export the PHP object using our COM wrapper */ V_VT(v) = VT_DISPATCH; V_DISPATCH(v) = php_com_wrapper_export(z TSRMLS_CC); } break; case IS_ARRAY: /* map as safe array */ safe_array_from_zval(v, z, codepage TSRMLS_CC); break; case IS_LONG: V_VT(v) = VT_I4; V_I4(v) = Z_LVAL_P(z); break; case IS_DOUBLE: V_VT(v) = VT_R8; V_R8(v) = Z_DVAL_P(z); break; case IS_STRING: V_VT(v) = VT_BSTR; olestring = php_com_string_to_olestring(Z_STRVAL_P(z), Z_STRLEN_P(z), codepage TSRMLS_CC); V_BSTR(v) = SysAllocStringByteLen((char*)olestring, Z_STRLEN_P(z) * sizeof(OLECHAR)); efree(olestring); break; case IS_RESOURCE: case IS_CONSTANT: case IS_CONSTANT_ARRAY: default: V_VT(v) = VT_NULL; break; } }
PHP_COM_DOTNET_API void php_com_variant_from_zval(VARIANT *v, zval *z, int codepage) { OLECHAR *olestring; php_com_dotnet_object *obj; zend_uchar ztype = IS_NULL; if (z) { ZVAL_DEREF(z); ztype = Z_TYPE_P(z); } switch (ztype) { case IS_NULL: V_VT(v) = VT_NULL; break; case IS_FALSE: V_VT(v) = VT_BOOL; V_BOOL(v) = VARIANT_FALSE; break; case IS_TRUE: V_VT(v) = VT_BOOL; V_BOOL(v) = VARIANT_TRUE; break; case IS_OBJECT: if (php_com_is_valid_object(z)) { obj = CDNO_FETCH(z); if (V_VT(&obj->v) == VT_DISPATCH) { /* pass the underlying object */ V_VT(v) = VT_DISPATCH; if (V_DISPATCH(&obj->v)) { IDispatch_AddRef(V_DISPATCH(&obj->v)); } V_DISPATCH(v) = V_DISPATCH(&obj->v); } else { /* pass the variant by reference */ V_VT(v) = VT_VARIANT | VT_BYREF; V_VARIANTREF(v) = &obj->v; } } else { /* export the PHP object using our COM wrapper */ V_VT(v) = VT_DISPATCH; V_DISPATCH(v) = php_com_wrapper_export(z); } break; case IS_ARRAY: /* map as safe array */ safe_array_from_zval(v, z, codepage); break; case IS_LONG: #if SIZEOF_ZEND_LONG == 4 V_VT(v) = VT_I4; V_I4(v) = Z_LVAL_P(z); #else V_VT(v) = VT_I8; V_I8(v) = Z_LVAL_P(z); #endif break; case IS_DOUBLE: V_VT(v) = VT_R8; V_R8(v) = Z_DVAL_P(z); break; case IS_STRING: V_VT(v) = VT_BSTR; olestring = php_com_string_to_olestring(Z_STRVAL_P(z), Z_STRLEN_P(z), codepage); if (CP_UTF8 == codepage) { V_BSTR(v) = SysAllocStringByteLen((char*)olestring, (UINT)(wcslen(olestring) * sizeof(OLECHAR))); } else { V_BSTR(v) = SysAllocStringByteLen((char*)olestring, (UINT)(Z_STRLEN_P(z) * sizeof(OLECHAR))); } efree(olestring); break; case IS_RESOURCE: case IS_CONSTANT_AST: default: V_VT(v) = VT_NULL; break; } }
static void variant_binary_operation(enum variant_binary_opcode op, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ { VARIANT vres; VARIANT left_val, right_val; VARIANT *vleft = NULL, *vright = NULL; zval *zleft = NULL, *zright = NULL; php_com_dotnet_object *obj; HRESULT result; int codepage = CP_ACP; VariantInit(&left_val); VariantInit(&right_val); VariantInit(&vres); if (SUCCESS == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OO", &zleft, php_com_variant_class_entry, &zright, php_com_variant_class_entry)) { obj = CDNO_FETCH(zleft); vleft = &obj->v; obj = CDNO_FETCH(zright); vright = &obj->v; } else if (SUCCESS == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "Oz!", &zleft, php_com_variant_class_entry, &zright)) { obj = CDNO_FETCH(zleft); vleft = &obj->v; vright = &right_val; php_com_variant_from_zval(vright, zright, codepage); } else if (SUCCESS == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z!O", &zleft, &zright, php_com_variant_class_entry)) { obj = CDNO_FETCH(zright); vright = &obj->v; vleft = &left_val; php_com_variant_from_zval(vleft, zleft, codepage); } else if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z!z!", &zleft, &zright)) { vleft = &left_val; php_com_variant_from_zval(vleft, zleft, codepage); vright = &right_val; php_com_variant_from_zval(vright, zright, codepage); } else { return; } switch (op) { case VOP_ADD: result = VarAdd(vleft, vright, &vres); break; case VOP_CAT: result = VarCat(vleft, vright, &vres); break; case VOP_SUB: result = VarSub(vleft, vright, &vres); break; case VOP_MUL: result = VarMul(vleft, vright, &vres); break; case VOP_AND: result = VarAnd(vleft, vright, &vres); break; case VOP_DIV: result = VarDiv(vleft, vright, &vres); break; case VOP_EQV: result = VarEqv(vleft, vright, &vres); break; case VOP_IDIV: result = VarIdiv(vleft, vright, &vres); break; case VOP_IMP: result = VarImp(vleft, vright, &vres); break; case VOP_MOD: result = VarMod(vleft, vright, &vres); break; case VOP_OR: result = VarOr(vleft, vright, &vres); break; case VOP_POW: result = VarPow(vleft, vright, &vres); break; case VOP_XOR: result = VarXor(vleft, vright, &vres); break; /*Let say it fails as no valid op has been given */ default: result = E_INVALIDARG; } if (SUCCEEDED(result)) { php_com_wrap_variant(return_value, &vres, codepage); } else { php_com_throw_exception(result, NULL); } VariantClear(&vres); VariantClear(&left_val); VariantClear(&right_val); }