1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
|
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Wez Furlong <wez@thebrainroom.com> |
+----------------------------------------------------------------------+
*/
/* Infrastructure for working with persistent COM objects.
* Implements: IStream* wrapper for PHP streams.
* TODO:
* - Magic __wakeup and __sleep handlers for serialization.
* - Track the stream and dispatch instances in a global list to make sure
* they are destroyed when a fatal error occurs.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_com_dotnet.h"
#include "php_com_dotnet_internal.h"
#include "Zend/zend_exceptions.h"
#include "com_persist_arginfo.h"
/* {{{ expose php_stream as a COM IStream */
typedef struct {
CONST_VTBL struct IStreamVtbl *lpVtbl;
DWORD engine_thread;
LONG refcount;
php_stream *stream;
} php_istream;
static void istream_destructor(php_istream *stm);
#define FETCH_STM() \
php_istream *stm = (php_istream*)This; \
if (GetCurrentThreadId() != stm->engine_thread) \
return RPC_E_WRONG_THREAD;
#define FETCH_STM_EX() \
php_istream *stm = (php_istream*)This; \
if (GetCurrentThreadId() != stm->engine_thread) \
return RPC_E_WRONG_THREAD;
static HRESULT STDMETHODCALLTYPE stm_queryinterface(
IStream *This,
/* [in] */ REFIID riid,
/* [iid_is][out] */ void **ppvObject)
{
FETCH_STM_EX();
if (IsEqualGUID(&IID_IUnknown, riid) ||
IsEqualGUID(&IID_IStream, riid)) {
*ppvObject = This;
InterlockedIncrement(&stm->refcount);
return S_OK;
}
*ppvObject = NULL;
return E_NOINTERFACE;
}
static ULONG STDMETHODCALLTYPE stm_addref(IStream *This)
{
FETCH_STM_EX();
return InterlockedIncrement(&stm->refcount);
}
static ULONG STDMETHODCALLTYPE stm_release(IStream *This)
{
ULONG ret;
FETCH_STM();
ret = InterlockedDecrement(&stm->refcount);
if (ret == 0) {
/* destroy it */
istream_destructor(stm);
}
return ret;
}
static HRESULT STDMETHODCALLTYPE stm_read(IStream *This, void *pv, ULONG cb, ULONG *pcbRead)
{
ULONG nread;
FETCH_STM();
nread = (ULONG)php_stream_read(stm->stream, pv, cb);
if (pcbRead) {
*pcbRead = nread > 0 ? nread : 0;
}
if (nread > 0) {
return S_OK;
}
return S_FALSE;
}
static HRESULT STDMETHODCALLTYPE stm_write(IStream *This, void const *pv, ULONG cb, ULONG *pcbWritten)
{
ssize_t nwrote;
FETCH_STM();
nwrote = php_stream_write(stm->stream, pv, cb);
if (pcbWritten) {
*pcbWritten = nwrote > 0 ? (ULONG)nwrote : 0;
}
if (nwrote > 0) {
return S_OK;
}
return S_FALSE;
}
static HRESULT STDMETHODCALLTYPE stm_seek(IStream *This, LARGE_INTEGER dlibMove,
DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
{
off_t offset;
int whence;
int ret;
FETCH_STM();
switch (dwOrigin) {
case STREAM_SEEK_SET: whence = SEEK_SET; break;
case STREAM_SEEK_CUR: whence = SEEK_CUR; break;
case STREAM_SEEK_END: whence = SEEK_END; break;
default:
return STG_E_INVALIDFUNCTION;
}
if (dlibMove.HighPart) {
/* we don't support 64-bit offsets */
return STG_E_INVALIDFUNCTION;
}
offset = (off_t) dlibMove.QuadPart;
ret = php_stream_seek(stm->stream, offset, whence);
if (plibNewPosition) {
plibNewPosition->QuadPart = (ULONGLONG)(ret >= 0 ? ret : 0);
}
return ret >= 0 ? S_OK : STG_E_INVALIDFUNCTION;
}
static HRESULT STDMETHODCALLTYPE stm_set_size(IStream *This, ULARGE_INTEGER libNewSize)
{
FETCH_STM();
if (libNewSize.HighPart) {
return STG_E_INVALIDFUNCTION;
}
if (php_stream_truncate_supported(stm->stream)) {
int ret = php_stream_truncate_set_size(stm->stream, (size_t)libNewSize.QuadPart);
if (ret == 0) {
return S_OK;
}
}
return STG_E_INVALIDFUNCTION;
}
static HRESULT STDMETHODCALLTYPE stm_copy_to(IStream *This, IStream *pstm, ULARGE_INTEGER cb,
ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
{
FETCH_STM_EX();
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE stm_commit(IStream *This, DWORD grfCommitFlags)
{
FETCH_STM();
php_stream_flush(stm->stream);
return S_OK;
}
static HRESULT STDMETHODCALLTYPE stm_revert(IStream *This)
{
/* NOP */
return S_OK;
}
static HRESULT STDMETHODCALLTYPE stm_lock_region(IStream *This,
ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD lockType)
{
return STG_E_INVALIDFUNCTION;
}
static HRESULT STDMETHODCALLTYPE stm_unlock_region(IStream *This,
ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD lockType)
{
return STG_E_INVALIDFUNCTION;
}
static HRESULT STDMETHODCALLTYPE stm_stat(IStream *This,
STATSTG *pstatstg, DWORD grfStatFlag)
{
return STG_E_INVALIDFUNCTION;
}
static HRESULT STDMETHODCALLTYPE stm_clone(IStream *This, IStream **ppstm)
{
return STG_E_INVALIDFUNCTION;
}
static struct IStreamVtbl php_istream_vtbl = {
stm_queryinterface,
stm_addref,
stm_release,
stm_read,
stm_write,
stm_seek,
stm_set_size,
stm_copy_to,
stm_commit,
stm_revert,
stm_lock_region,
stm_unlock_region,
stm_stat,
stm_clone
};
static void istream_destructor(php_istream *stm)
{
if (stm->refcount > 0) {
CoDisconnectObject((IUnknown*)stm, 0);
}
zend_list_delete(stm->stream->res);
CoTaskMemFree(stm);
}
/* }}} */
PHP_COM_DOTNET_API IStream *php_com_wrapper_export_stream(php_stream *stream)
{
php_istream *stm = (php_istream*)CoTaskMemAlloc(sizeof(*stm));
if (stm == NULL)
return NULL;
memset(stm, 0, sizeof(*stm));
stm->engine_thread = GetCurrentThreadId();
stm->lpVtbl = &php_istream_vtbl;
stm->refcount = 1;
stm->stream = stream;
GC_ADDREF(stream->res);
return (IStream*)stm;
}
#define CPH_METHOD(fname) PHP_METHOD(COMPersistHelper, fname)
#define CPH_FETCH() php_com_persist_helper *helper = (php_com_persist_helper*)Z_OBJ_P(ZEND_THIS);
#define CPH_NO_OBJ() if (helper->unk == NULL) { php_com_throw_exception(E_INVALIDARG, "No COM object is associated with this helper instance"); RETURN_THROWS(); }
typedef struct {
zend_object std;
long codepage;
IUnknown *unk;
IPersistStream *ips;
IPersistStreamInit *ipsi;
IPersistFile *ipf;
} php_com_persist_helper;
static zend_object_handlers helper_handlers;
static zend_class_entry *helper_ce;
static inline HRESULT get_persist_stream(php_com_persist_helper *helper)
{
if (!helper->ips && helper->unk) {
return IUnknown_QueryInterface(helper->unk, &IID_IPersistStream, &helper->ips);
}
return helper->ips ? S_OK : E_NOTIMPL;
}
static inline HRESULT get_persist_stream_init(php_com_persist_helper *helper)
{
if (!helper->ipsi && helper->unk) {
return IUnknown_QueryInterface(helper->unk, &IID_IPersistStreamInit, &helper->ipsi);
}
return helper->ipsi ? S_OK : E_NOTIMPL;
}
static inline HRESULT get_persist_file(php_com_persist_helper *helper)
{
if (!helper->ipf && helper->unk) {
return IUnknown_QueryInterface(helper->unk, &IID_IPersistFile, &helper->ipf);
}
return helper->ipf ? S_OK : E_NOTIMPL;
}
/* {{{ Determines the filename into which an object will be saved, or false if none is set, via IPersistFile::GetCurFile */
CPH_METHOD(GetCurFileName)
{
HRESULT res;
OLECHAR *olename = NULL;
CPH_FETCH();
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CPH_NO_OBJ();
res = get_persist_file(helper);
if (helper->ipf) {
res = IPersistFile_GetCurFile(helper->ipf, &olename);
if (res == S_OK) {
zend_string *str = php_com_olestring_to_string(olename, helper->codepage);
CoTaskMemFree(olename);
RETURN_STR(str);
} else if (res == S_FALSE) {
CoTaskMemFree(olename);
RETURN_FALSE;
}
php_com_throw_exception(res, NULL);
} else {
php_com_throw_exception(res, NULL);
}
}
/* }}} */
/* {{{ Persist object data to file, via IPersistFile::Save */
CPH_METHOD(SaveToFile)
{
HRESULT res;
char *filename, *fullpath = NULL;
size_t filename_len;
bool remember = TRUE;
OLECHAR *olefilename = NULL;
CPH_FETCH();
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "p!|b",
&filename, &filename_len, &remember)) {
RETURN_THROWS();
}
CPH_NO_OBJ();
res = get_persist_file(helper);
if (helper->ipf) {
if (filename) {
fullpath = expand_filepath(filename, NULL);
if (!fullpath) {
RETURN_FALSE;
}
if (php_check_open_basedir(fullpath)) {
efree(fullpath);
RETURN_FALSE;
}
olefilename = php_com_string_to_olestring(fullpath, strlen(fullpath), helper->codepage);
efree(fullpath);
}
res = IPersistFile_Save(helper->ipf, olefilename, remember);
if (SUCCEEDED(res)) {
if (!olefilename) {
res = IPersistFile_GetCurFile(helper->ipf, &olefilename);
if (S_OK == res) {
IPersistFile_SaveCompleted(helper->ipf, olefilename);
CoTaskMemFree(olefilename);
olefilename = NULL;
}
} else if (remember) {
IPersistFile_SaveCompleted(helper->ipf, olefilename);
}
}
if (olefilename) {
efree(olefilename);
}
if (FAILED(res)) {
php_com_throw_exception(res, NULL);
}
} else {
php_com_throw_exception(res, NULL);
}
}
/* }}} */
/* {{{ Load object data from file, via IPersistFile::Load */
CPH_METHOD(LoadFromFile)
{
HRESULT res;
char *filename, *fullpath;
size_t filename_len;
zend_long flags = 0;
OLECHAR *olefilename;
CPH_FETCH();
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "p|l",
&filename, &filename_len, &flags)) {
RETURN_THROWS();
}
CPH_NO_OBJ();
res = get_persist_file(helper);
if (helper->ipf) {
if (!(fullpath = expand_filepath(filename, NULL))) {
RETURN_FALSE;
}
if (php_check_open_basedir(fullpath)) {
efree(fullpath);
RETURN_FALSE;
}
olefilename = php_com_string_to_olestring(fullpath, strlen(fullpath), helper->codepage);
efree(fullpath);
res = IPersistFile_Load(helper->ipf, olefilename, (DWORD)flags);
efree(olefilename);
if (FAILED(res)) {
php_com_throw_exception(res, NULL);
}
} else {
php_com_throw_exception(res, NULL);
}
}
/* }}} */
/* {{{ Gets maximum stream size required to store the object data, via IPersistStream::GetSizeMax (or IPersistStreamInit::GetSizeMax) */
CPH_METHOD(GetMaxStreamSize)
{
HRESULT res;
ULARGE_INTEGER size;
CPH_FETCH();
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CPH_NO_OBJ();
res = get_persist_stream_init(helper);
if (helper->ipsi) {
res = IPersistStreamInit_GetSizeMax(helper->ipsi, &size);
} else {
res = get_persist_stream(helper);
if (helper->ips) {
res = IPersistStream_GetSizeMax(helper->ips, &size);
} else {
php_com_throw_exception(res, NULL);
RETURN_THROWS();
}
}
if (res != S_OK) {
php_com_throw_exception(res, NULL);
} else {
/* TODO: handle 64 bit properly */
RETURN_LONG((zend_long)size.QuadPart);
}
}
/* }}} */
/* {{{ Initializes the object to a default state, via IPersistStreamInit::InitNew */
CPH_METHOD(InitNew)
{
HRESULT res;
CPH_FETCH();
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CPH_NO_OBJ();
res = get_persist_stream_init(helper);
if (helper->ipsi) {
res = IPersistStreamInit_InitNew(helper->ipsi);
if (res != S_OK) {
php_com_throw_exception(res, NULL);
} else {
RETURN_TRUE;
}
} else {
php_com_throw_exception(res, NULL);
}
}
/* }}} */
/* {{{ Initializes an object from the stream where it was previously saved, via IPersistStream::Load or OleLoadFromStream */
CPH_METHOD(LoadFromStream)
{
zval *zstm;
php_stream *stream;
IStream *stm = NULL;
HRESULT res;
CPH_FETCH();
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstm)) {
RETURN_THROWS();
}
php_stream_from_zval_no_verify(stream, zstm);
if (stream == NULL) {
php_com_throw_exception(E_INVALIDARG, "expected a stream");
RETURN_THROWS();
}
stm = php_com_wrapper_export_stream(stream);
if (stm == NULL) {
php_com_throw_exception(E_UNEXPECTED, "failed to wrap stream");
RETURN_THROWS();
}
res = S_OK;
RETVAL_TRUE;
if (helper->unk == NULL) {
IDispatch *disp = NULL;
/* we need to create an object and load using OleLoadFromStream */
res = OleLoadFromStream(stm, &IID_IDispatch, &disp);
if (SUCCEEDED(res)) {
php_com_wrap_dispatch(return_value, disp, COMG(code_page));
}
} else {
res = get_persist_stream_init(helper);
if (helper->ipsi) {
res = IPersistStreamInit_Load(helper->ipsi, stm);
} else {
res = get_persist_stream(helper);
if (helper->ips) {
res = IPersistStreamInit_Load(helper->ipsi, stm);
}
}
}
IStream_Release(stm);
if (FAILED(res)) {
php_com_throw_exception(res, NULL);
RETURN_THROWS();
}
}
/* }}} */
/* {{{ Saves the object to a stream, via IPersistStream::Save */
CPH_METHOD(SaveToStream)
{
zval *zstm;
php_stream *stream;
IStream *stm = NULL;
HRESULT res;
CPH_FETCH();
CPH_NO_OBJ();
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstm)) {
RETURN_THROWS();
}
php_stream_from_zval_no_verify(stream, zstm);
if (stream == NULL) {
php_com_throw_exception(E_INVALIDARG, "expected a stream");
RETURN_THROWS();
}
stm = php_com_wrapper_export_stream(stream);
if (stm == NULL) {
php_com_throw_exception(E_UNEXPECTED, "failed to wrap stream");
RETURN_THROWS();
}
res = get_persist_stream_init(helper);
if (helper->ipsi) {
res = IPersistStreamInit_Save(helper->ipsi, stm, TRUE);
} else {
res = get_persist_stream(helper);
if (helper->ips) {
res = IPersistStream_Save(helper->ips, stm, TRUE);
}
}
IStream_Release(stm);
if (FAILED(res)) {
php_com_throw_exception(res, NULL);
RETURN_THROWS();
}
RETURN_TRUE;
}
/* }}} */
/* {{{ Creates a persistence helper object, usually associated with a com_object */
CPH_METHOD(__construct)
{
php_com_dotnet_object *obj = NULL;
zval *zobj = NULL;
CPH_FETCH();
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|O!",
&zobj, php_com_variant_class_entry)) {
RETURN_THROWS();
}
if (!zobj) {
return;
}
obj = CDNO_FETCH(zobj);
if (V_VT(&obj->v) != VT_DISPATCH || V_DISPATCH(&obj->v) == NULL) {
php_com_throw_exception(E_INVALIDARG, "parameter must represent an IDispatch COM object");
RETURN_THROWS();
}
/* it is always safe to cast an interface to IUnknown */
helper->unk = (IUnknown*)V_DISPATCH(&obj->v);
IUnknown_AddRef(helper->unk);
helper->codepage = obj->code_page;
}
/* }}} */
static void helper_free_storage(zend_object *obj)
{
php_com_persist_helper *object = (php_com_persist_helper*)obj;
if (object->ipf) {
IPersistFile_Release(object->ipf);
}
if (object->ips) {
IPersistStream_Release(object->ips);
}
if (object->ipsi) {
IPersistStreamInit_Release(object->ipsi);
}
if (object->unk) {
IUnknown_Release(object->unk);
}
zend_object_std_dtor(&object->std);
}
static zend_object* helper_clone(zend_object *obj)
{
php_com_persist_helper *clone, *object = (php_com_persist_helper*) obj;
clone = emalloc(sizeof(*object));
memcpy(clone, object, sizeof(*object));
zend_object_std_init(&clone->std, object->std.ce);
if (clone->ipf) {
IPersistFile_AddRef(clone->ipf);
}
if (clone->ips) {
IPersistStream_AddRef(clone->ips);
}
if (clone->ipsi) {
IPersistStreamInit_AddRef(clone->ipsi);
}
if (clone->unk) {
IUnknown_AddRef(clone->unk);
}
return (zend_object*)clone;
}
static zend_object* helper_new(zend_class_entry *ce)
{
php_com_persist_helper *helper;
helper = emalloc(sizeof(*helper));
memset(helper, 0, sizeof(*helper));
zend_object_std_init(&helper->std, helper_ce);
return &helper->std;
}
void php_com_persist_minit(INIT_FUNC_ARGS)
{
memcpy(&helper_handlers, &std_object_handlers, sizeof(helper_handlers));
helper_handlers.free_obj = helper_free_storage;
helper_handlers.clone_obj = helper_clone;
helper_ce = register_class_COMPersistHelper();
helper_ce->create_object = helper_new;
helper_ce->default_object_handlers = &helper_handlers;
}
|