static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mimetype_emu) /* {{{ */ { zend_long options = 0; char *ret_val = NULL, *buffer = NULL; size_t buffer_len; php_fileinfo *finfo = NULL; zval *zfinfo, *zcontext = NULL; zval *what; char mime_directory[] = "directory"; struct magic_set *magic = NULL; FILEINFO_DECLARE_INIT_OBJECT(object) if (mimetype_emu) { /* mime_content_type(..) emulation */ if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &what) == FAILURE) { return; } switch (Z_TYPE_P(what)) { case IS_STRING: buffer = Z_STRVAL_P(what); buffer_len = Z_STRLEN_P(what); mode = FILEINFO_MODE_FILE; break; case IS_RESOURCE: mode = FILEINFO_MODE_STREAM; break; default: php_error_docref(NULL, E_WARNING, "Can only process string or stream arguments"); RETURN_FALSE; } magic = magic_open(MAGIC_MIME_TYPE); if (magic_load(magic, NULL) == -1) { php_error_docref(NULL, E_WARNING, "Failed to load magic database."); goto common; } } else if (object) { if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lr", &buffer, &buffer_len, &options, &zcontext) == FAILURE) { RETURN_FALSE; } FILEINFO_FROM_OBJECT(finfo, object); magic = finfo->magic; } else { if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|lr", &zfinfo, &buffer, &buffer_len, &options, &zcontext) == FAILURE) { RETURN_FALSE; } if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) { RETURN_FALSE; } magic = finfo->magic; } /* Set options for the current file/buffer. */ if (options) { FINFO_SET_OPTION(magic, options) } switch (mode) { case FILEINFO_MODE_BUFFER: { ret_val = (char *) magic_buffer(magic, buffer, buffer_len); break; } case FILEINFO_MODE_STREAM: { php_stream *stream; zend_off_t streampos; php_stream_from_zval_no_verify(stream, what); if (!stream) { goto common; } streampos = php_stream_tell(stream); /* remember stream position for restoration */ php_stream_seek(stream, 0, SEEK_SET); ret_val = (char *) magic_stream(magic, stream); php_stream_seek(stream, streampos, SEEK_SET); break; } case FILEINFO_MODE_FILE: { /* determine if the file is a local file or remote URL */ const char *tmp2; php_stream_wrapper *wrap; php_stream_statbuf ssb; if (buffer == NULL || !*buffer) { php_error_docref(NULL, E_WARNING, "Empty filename or path"); RETVAL_FALSE; goto clean; } wrap = php_stream_locate_url_wrapper(buffer, &tmp2, 0); if (wrap) { php_stream *stream; php_stream_context *context = php_stream_context_from_zval(zcontext, 0); #ifdef PHP_WIN32 if (php_stream_stat_path_ex(buffer, 0, &ssb, context) == SUCCESS) { if (ssb.sb.st_mode & S_IFDIR) { ret_val = mime_directory; goto common; } } #endif #if PHP_API_VERSION < 20100412 stream = php_stream_open_wrapper_ex(buffer, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); #else stream = php_stream_open_wrapper_ex(buffer, "rb", REPORT_ERRORS, NULL, context); #endif if (!stream) { RETVAL_FALSE; goto clean; } if (php_stream_stat(stream, &ssb) == SUCCESS) { if (ssb.sb.st_mode & S_IFDIR) { ret_val = mime_directory; } else { ret_val = (char *)magic_stream(magic, stream); } } php_stream_close(stream); } break; } default: php_error_docref(NULL, E_WARNING, "Can only process string or stream arguments"); } common: if (ret_val) { RETVAL_STRING(ret_val); } else { php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); RETVAL_FALSE; } clean: if (mimetype_emu) { magic_close(magic); } /* Restore options */ if (options) { FINFO_SET_OPTION(magic, finfo->options) } return; }
void _php_curl_multi_close(zend_resource *rsrc) /* {{{ */ { php_curlm *mh = (php_curlm *)rsrc->ptr; if (mh) { zend_llist_position pos; php_curl *ch; zval *pz_ch; for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch; pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) { /* ptr is NULL means it already be freed */ if (Z_RES_P(pz_ch)->ptr) { if ((ch = (php_curl *) zend_fetch_resource(Z_RES_P(pz_ch), le_curl_name, le_curl))) { _php_curl_verify_handlers(ch, 0); } } } curl_multi_cleanup(mh->multi); zend_llist_clean(&mh->easyh); if (mh->handlers->server_push) { efree(mh->handlers->server_push); } if (mh->handlers) { efree(mh->handlers); } efree(mh); rsrc->ptr = NULL; } }
static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type) { zval *arg; #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) { RETURN_FALSE; } ZVAL_DEREF(arg); #else ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ZVAL_DEREF(arg) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); #endif if (Z_TYPE_P(arg) == type) { if (type == IS_OBJECT) { zend_class_entry *ce = Z_OBJCE_P(arg); if (ce->name->len == sizeof(INCOMPLETE_CLASS) - 1 && !memcmp(ce->name->val, INCOMPLETE_CLASS, sizeof(INCOMPLETE_CLASS) - 1)) { RETURN_FALSE; } } else if (type == IS_RESOURCE) { const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg)); if (!type_name) { RETURN_FALSE; } } RETURN_TRUE; } else { RETURN_FALSE; } }
static isc_callback _php_ibase_callback(ibase_event *event, /* {{{ */ unsigned short buffer_size, char *result_buf) { zval *res; /* this function is called asynchronously by the Interbase client library. */ TSRMLS_FETCH_FROM_CTX(event->thread_ctx); /** * The callback function is called when the event is first registered and when the event * is cancelled. I consider this is a bug. By clearing event->callback first and setting * it to -1 later, we make sure nothing happens if no event was actually posted. */ switch (event->state) { unsigned short i; unsigned long occurred_event[15]; zval return_value, args[2]; default: /* == DEAD */ break; case ACTIVE: /* copy the updated results into the result buffer */ memcpy(event->result_buffer, result_buf, buffer_size); res = zend_hash_index_find(&EG(regular_list), event->link_res_id); ZVAL_RES(&args[1], Z_RES_P(res)); /* find out which event occurred */ isc_event_counts(occurred_event, buffer_size, event->event_buffer, event->result_buffer); for (i = 0; i < event->event_count; ++i) { if (occurred_event[i]) { ZVAL_STRING(&args[0], event->events[i]); efree(event->events[i]); break; } } /* call the callback provided by the user */ if (SUCCESS != call_user_function(EG(function_table), NULL, &event->callback, &return_value, 2, args)) { _php_ibase_module_error("Error calling callback %s", Z_STRVAL(event->callback)); break; } if (Z_TYPE(return_value) == IS_FALSE) { event->state = DEAD; break; } case NEW: /* re-register the event */ if (isc_que_events(IB_STATUS, &event->link->handle, &event->event_id, buffer_size, event->event_buffer,(isc_callback)_php_ibase_callback, (void *)event)) { _php_ibase_error(); } event->state = ACTIVE; } return 0; }
void _php_curl_multi_cleanup_list(void *data) /* {{{ */ { zval *z_ch = (zval *)data; php_curl *ch; if (!z_ch) { return; } if (!Z_RES_P(z_ch)->ptr) { return; } if ((ch = (php_curl *)zend_fetch_resource(Z_RES_P(z_ch), le_curl_name, le_curl)) == NULL) { return; } zend_list_delete(Z_RES_P(z_ch)); }
RedisArray* ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b_lazy_connect) { int i = 0, host_len; zval *id; char *host, *p; short port; zval *zpData, z_cons, z_ret; RedisSock *redis_sock = NULL; /* function calls on the Redis object */ ZVAL_STRING(&z_cons, "__construct"); /* init connections */ ZEND_HASH_FOREACH_VAL(hosts, zpData) { if (Z_TYPE_P(zpData) != IS_STRING) { efree(ra); return NULL; } ra->hosts[i] = estrdup(Z_STRVAL_P(zpData)); /* default values */ host = Z_STRVAL_P(zpData); host_len = Z_STRLEN_P(zpData); port = 6379; if ((p = strchr(host, ':'))) { /* found port */ host_len = p - host; port = (short)atoi(p+1); } else if (strchr(host,'/') != NULL) { /* unix socket */ port = -1; } /* create Redis object */ object_init_ex(&ra->redis[i], redis_ce); call_user_function(&redis_ce->function_table, &ra->redis[i], &z_cons, &z_ret, 0, NULL); /* create socket */ redis_sock = redis_sock_create(host, host_len, port, ra->connect_timeout, ra->pconnect, NULL, retry_interval, b_lazy_connect); if (!b_lazy_connect) { /* connect */ redis_sock_server_open(redis_sock, 1); } /* attach */ id = zend_list_insert(redis_sock, le_redis_sock); add_property_resource(&ra->redis[i], "socket", Z_RES_P(id)); i++; } ZEND_HASH_FOREACH_END(); return ra; }
/* {{{ dba_close_pe_rsrc_deleter */ int dba_close_pe_rsrc_deleter(zval *el, void *pDba) { if (Z_RES_P(el)->ptr == pDba) { if (Z_DELREF_P(el) == 0) { return ZEND_HASH_APPLY_REMOVE; } else { return ZEND_HASH_APPLY_KEEP | ZEND_HASH_APPLY_STOP; } } else { return ZEND_HASH_APPLY_KEEP; } }
static EC_KEY* php_ec_key_from_zval(zval* value) { EC_KEY* key = NULL; if (Z_TYPE_P(value) == IS_RESOURCE) { zend_resource* resource = Z_RES_P(value); // zend_fetch_resource will return NULL if the cast is unsuccessful. key = zend_fetch_resource(resource, "P-256 key pair", le_ec_key); } else { php_error_docref(NULL, E_WARNING, kInvalidKeyParameter); } return key; }
/* {{{ php_stream_bucket_attach */ static void php_stream_bucket_attach(int append, INTERNAL_FUNCTION_PARAMETERS) { zval *zbrigade, *zobject; zval *pzbucket, *pzdata; php_stream_bucket_brigade *brigade; php_stream_bucket *bucket; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_RESOURCE(zbrigade) Z_PARAM_OBJECT(zobject) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); if (NULL == (pzbucket = zend_hash_str_find(Z_OBJPROP_P(zobject), "bucket", sizeof("bucket")-1))) { php_error_docref(NULL, E_WARNING, "Object has no bucket property"); RETURN_FALSE; } if ((brigade = (php_stream_bucket_brigade*)zend_fetch_resource( Z_RES_P(zbrigade), PHP_STREAM_BRIGADE_RES_NAME, le_bucket_brigade)) == NULL) { RETURN_FALSE; } if ((bucket = (php_stream_bucket *)zend_fetch_resource_ex(pzbucket, PHP_STREAM_BUCKET_RES_NAME, le_bucket)) == NULL) { RETURN_FALSE; } if (NULL != (pzdata = zend_hash_str_find(Z_OBJPROP_P(zobject), "data", sizeof("data")-1)) && Z_TYPE_P(pzdata) == IS_STRING) { if (!bucket->own_buf) { bucket = php_stream_bucket_make_writeable(bucket); } if (bucket->buflen != Z_STRLEN_P(pzdata)) { bucket->buf = perealloc(bucket->buf, Z_STRLEN_P(pzdata), bucket->is_persistent); bucket->buflen = Z_STRLEN_P(pzdata); } memcpy(bucket->buf, Z_STRVAL_P(pzdata), bucket->buflen); } if (append) { php_stream_bucket_append(brigade, bucket); } else { php_stream_bucket_prepend(brigade, bucket); } /* This is a hack necessary to accommodate situations where bucket is appended to the stream * multiple times. See bug35916.phpt for reference. */ if (bucket->refcount == 1) { bucket->refcount++; } }
/** * Returns the type of a variable as a string */ void zephir_gettype(zval *return_value, zval *arg) { switch (Z_TYPE_P(arg)) { case IS_NULL: RETVAL_STRING("NULL"); break; case IS_TRUE: case IS_FALSE: RETVAL_STRING("boolean"); break; case IS_LONG: RETVAL_STRING("integer"); break; case IS_DOUBLE: RETVAL_STRING("double"); break; case IS_STRING: RETVAL_STRING("string"); break; case IS_ARRAY: RETVAL_STRING("array"); break; case IS_OBJECT: RETVAL_STRING("object"); break; case IS_RESOURCE: { const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg)); if (type_name) { RETVAL_STRING("resource"); break; } } default: RETVAL_STRING("unknown type"); } }
/* Fill ib_link and trans with the correct database link and transaction. */ void _php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAMETERS, /* {{{ */ zval *link_id, ibase_db_link **ib_link, ibase_trans **trans) { IBDEBUG("Transaction or database link?"); if (Z_RES_P(link_id)->type == le_trans) { /* Transaction resource: make sure it refers to one link only, then fetch it; database link is stored in ib_trans->db_link[]. */ IBDEBUG("Type is le_trans"); ZEND_FETCH_RESOURCE(*trans, ibase_trans *, link_id, -1, LE_TRANS, le_trans); if ((*trans)->link_cnt > 1) { _php_ibase_module_error("Link id is ambiguous: transaction spans multiple connections." ); return; } *ib_link = (*trans)->db_link[0]; return; }
void _php_curl_multi_cleanup_list(void *data) /* {{{ */ { zval *z_ch = (zval *)data; php_curl *ch; TSRMLS_FETCH(); if (!z_ch) { return; } ch = zend_fetch_resource(z_ch TSRMLS_CC, -1, le_curl_name, NULL, 1, le_curl); if (!ch) { return; } zend_list_close(Z_RES_P(z_ch)); }
/* {{{ php_sysvsem_semop */ static void php_sysvsem_semop(INTERNAL_FUNCTION_PARAMETERS, int acquire) { zval *arg_id; zend_bool nowait = 0; sysvsem_sem *sem_ptr; struct sembuf sop; if (acquire) { if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|b", &arg_id, &nowait) == FAILURE) { return; } } else { if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg_id) == FAILURE) { return; } } if ((sem_ptr = (sysvsem_sem *)zend_fetch_resource(Z_RES_P(arg_id), "SysV semaphore", php_sysvsem_module.le_sem)) == NULL) { RETURN_FALSE; } if (!acquire && sem_ptr->count == 0) { php_error_docref(NULL, E_WARNING, "SysV semaphore %ld (key 0x%x) is not currently acquired", Z_LVAL_P(arg_id), sem_ptr->key); RETURN_FALSE; } sop.sem_num = SYSVSEM_SEM; sop.sem_op = acquire ? -1 : 1; sop.sem_flg = SEM_UNDO | (nowait ? IPC_NOWAIT : 0); while (semop(sem_ptr->semid, &sop, 1) == -1) { if (errno != EINTR) { if (errno != EAGAIN) { php_error_docref(NULL, E_WARNING, "failed to %s key 0x%x: %s", acquire ? "acquire" : "release", sem_ptr->key, strerror(errno)); } RETURN_FALSE; } } sem_ptr->count -= acquire ? -1 : 1; RETURN_TRUE; }
/* Used to find the php_curl resource for a given curl easy handle */ static zval *_php_curl_multi_find_easy_handle(php_curlm *mh, CURL *easy) /* {{{ */ { php_curl *tmp_ch; zend_llist_position pos; zval *pz_ch_temp; for(pz_ch_temp = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch_temp; pz_ch_temp = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) { if ((tmp_ch = (php_curl *)zend_fetch_resource(Z_RES_P(pz_ch_temp), le_curl_name, le_curl)) == NULL) { return NULL; } if (tmp_ch->cp == easy) { return pz_ch_temp; } } return NULL; }
static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type) { zval *arg; ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ZVAL(arg) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); if (Z_TYPE_P(arg) == type) { if (type == IS_RESOURCE) { const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg)); if (!type_name) { RETURN_FALSE; } } RETURN_TRUE; } else { RETURN_FALSE; } }
PHP_COM_DOTNET_API IStream *php_com_wrapper_export_stream(php_stream *stream) { php_istream *stm = (php_istream*)CoTaskMemAlloc(sizeof(*stm)); zval *tmp; 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_REFCOUNT(stream->res)++; tmp = zend_list_insert(stm, le_istream); stm->res = Z_RES_P(tmp); return (IStream*)stm; }
int zephir_fclose(zval *stream_zval) { php_stream *stream; if (Z_TYPE_P(stream_zval) != IS_RESOURCE) { php_error_docref(NULL, E_WARNING, "Invalid arguments supplied for zephir_fclose()"); return 0; } if ((stream = (php_stream*)zend_fetch_resource2(Z_RES_P(stream_zval), "stream", php_file_le_stream(), php_file_le_pstream())) == NULL) { return 0; } if ((stream->flags & PHP_STREAM_FLAG_NO_FCLOSE) != 0) { php_error_docref(NULL, E_WARNING, "%d is not a valid stream resource", stream->res->handle); return 0; } php_stream_free(stream, PHP_STREAM_FREE_KEEP_RSRC | (stream->is_persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE)); return 1; }
/* Fill ib_link and trans with the correct database link and transaction. */ void _php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAMETERS, /* {{{ */ zval *link_id, ibase_db_link **ib_link, ibase_trans **trans) { IBDEBUG("Transaction or database link?"); if (Z_RES_P(link_id)->type == le_trans) { /* Transaction resource: make sure it refers to one link only, then fetch it; database link is stored in ib_trans->db_link[]. */ IBDEBUG("Type is le_trans"); *trans = (ibase_trans *)zend_fetch_resource_ex(link_id, LE_TRANS, le_trans); if ((*trans)->link_cnt > 1) { _php_ibase_module_error("Link id is ambiguous: transaction spans multiple connections." ); return; } *ib_link = (*trans)->db_link[0]; return; } IBDEBUG("Type is le_[p]link or id not found"); /* Database link resource, use default transaction. */ *trans = NULL; *ib_link = (ibase_db_link *)zend_fetch_resource2_ex(link_id, LE_LINK, le_link, le_plink); }
static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type) { zval *arg; #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg) == FAILURE) { RETURN_FALSE; } ZVAL_DEREF(arg); #else ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_ZVAL_DEREF(arg) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); #endif if (Z_TYPE_P(arg) == type) { if (type == IS_OBJECT) { zend_class_entry *ce; if (Z_OBJ_HT_P(arg)->get_class_entry == NULL) { /* if there's no get_class_entry it's not a PHP object, so it can't be INCOMPLETE_CLASS */ RETURN_TRUE; } ce = Z_OBJCE_P(arg); if (ce->name->len == sizeof(INCOMPLETE_CLASS) - 1 && !strncmp(ce->name->val, INCOMPLETE_CLASS, ce->name->len)) { RETURN_FALSE; } } else if (type == IS_RESOURCE) { const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg) TSRMLS_CC); if (!type_name) { RETURN_FALSE; } } RETURN_TRUE; } else { RETURN_FALSE; } }
/* {{{ _php_image_output_ctx */ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)()) { zval *imgind; char *file = NULL; size_t file_len = 0; zend_long quality, basefilter; gdImagePtr im; int argc = ZEND_NUM_ARGS(); int q = -1, i; int f = -1; gdIOCtx *ctx = NULL; zval *to_zval = NULL; php_stream *stream; /* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp(). * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called * from imagey<type>(). */ if (image_type == PHP_GDIMG_TYPE_XBM) { if (zend_parse_parameters(ZEND_NUM_ARGS(), "rp!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) { return; } } else { /* PHP_GDIMG_TYPE_GIF * PHP_GDIMG_TYPE_PNG * PHP_GDIMG_TYPE_JPG * PHP_GDIMG_TYPE_WBM * PHP_GDIMG_TYPE_WEBP * */ if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z/!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) { return; } } if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(imgind), "Image", phpi_get_le_gd())) == NULL) { RETURN_FALSE; } if (argc >= 3) { q = quality; /* or colorindex for foreground of BW images (defaults to black) */ if (argc == 4) { f = basefilter; } } if (argc > 1 && to_zval != NULL) { if (Z_TYPE_P(to_zval) == IS_RESOURCE) { php_stream_from_zval_no_verify(stream, to_zval); if (stream == NULL) { RETURN_FALSE; } } else if (Z_TYPE_P(to_zval) == IS_STRING) { if (CHECK_ZVAL_NULL_PATH(to_zval)) { php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, filename must not contain null bytes"); RETURN_FALSE; } stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL); if (stream == NULL) { RETURN_FALSE; } } else { php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream"); RETURN_FALSE; } } else { ctx = emalloc(sizeof(gdIOCtx)); ctx->putC = _php_image_output_putc; ctx->putBuf = _php_image_output_putbuf; ctx->gd_free = _php_image_output_ctxfree; #if APACHE && defined(CHARSET_EBCDIC) /* XXX this is unlikely to work any more [email protected] */ /* This is a binary file already: avoid EBCDIC->ASCII conversion */ ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0); #endif } if (!ctx) { ctx = emalloc(sizeof(gdIOCtx)); ctx->putC = _php_image_stream_putc; ctx->putBuf = _php_image_stream_putbuf; ctx->gd_free = _php_image_stream_ctxfree; ctx->data = (void *)stream; } switch(image_type) { case PHP_GDIMG_CONVERT_WBM: if(q<0||q>255) { php_error_docref(NULL, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q); } case PHP_GDIMG_TYPE_JPG: (*func_p)(im, ctx, q); break; case PHP_GDIMG_TYPE_WEBP: if (q == -1) { q = 80; } (*func_p)(im, ctx, q); break; case PHP_GDIMG_TYPE_PNG: (*func_p)(im, ctx, q, f); break; case PHP_GDIMG_TYPE_XBM: case PHP_GDIMG_TYPE_WBM: if (argc < 3) { for(i=0; i < gdImageColorsTotal(im); i++) { if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break; } q = i; } if (image_type == PHP_GDIMG_TYPE_XBM) { (*func_p)(im, file, q, ctx); } else { (*func_p)(im, q, ctx); } break; default: (*func_p)(im, ctx); break; } ctx->gd_free(ctx); RETURN_TRUE; }
bool ScriptEngine::checkSessionId(const std::string& sessionId) { if(_disposing) return false; std::unique_ptr<BaseLib::HTTP> request(new BaseLib::HTTP()); ts_resource_ex(0, NULL); //Replaces TSRMLS_FETCH() try { zend_homegear_globals* globals = php_homegear_get_globals(); if(!globals) { ts_free_thread(); return false; } globals->http = request.get(); if(!tsrm_get_ls_cache() || !((sapi_globals_struct *) (*((void ***) tsrm_get_ls_cache()))[((sapi_globals_id)-1)])) { GD::out.printCritical("Critical: Error in PHP: No thread safe resource exists."); ts_free_thread(); return false; } SG(server_context) = (void*)request.get(); //Must be defined! Otherwise POST data is not processed. SG(sapi_headers).http_response_code = 200; SG(default_mimetype) = nullptr; SG(default_charset) = nullptr; SG(request_info).content_length = 0; SG(request_info).request_method = "GET"; SG(request_info).proto_num = 1001; request->getHeader()->cookie = "PHPSESSID=" + sessionId; if (php_request_startup(TSRMLS_C) == FAILURE) { GD::bl->out.printError("Error calling php_request_startup..."); ts_free_thread(); return false; } zval returnValue; zval function; ZVAL_STRING(&function, "session_start"); call_user_function(EG(function_table), NULL, &function, &returnValue, 0, nullptr); bool result = false; zval* reference = zend_hash_str_find(&EG(symbol_table), "_SESSION", sizeof("_SESSION") - 1); if(reference != NULL) { if(Z_ISREF_P(reference) && Z_RES_P(reference)->ptr && Z_TYPE_P(Z_REFVAL_P(reference)) == IS_ARRAY) { zval* token = zend_hash_str_find(Z_ARRVAL_P(Z_REFVAL_P(reference)), "authorized", sizeof("authorized") - 1); if(token != NULL) { result = (Z_TYPE_P(token) == IS_TRUE); } } } php_request_shutdown(NULL); ts_free_thread(); return result; } catch(const std::exception& ex) { GD::bl->out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(BaseLib::Exception& ex) { GD::bl->out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(...) { GD::bl->out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__); } std::string error("Error executing script. Check Homegear log for more details."); ts_free_thread(); return false; }
/* {{{ _php_image_output_ctx */ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)()) { zval *imgind; char *file = NULL; size_t file_len = 0; zend_long quality, basefilter; zend_bool compressed = 1; gdImagePtr im; int argc = ZEND_NUM_ARGS(); int q = -1, i; int f = -1; gdIOCtx *ctx = NULL; zval *to_zval = NULL; php_stream *stream; int close_stream = 1; /* The third (quality) parameter for Wbmp stands for the foreground when called from image2wbmp(). * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called * from imagey<type>(). */ switch (image_type) { case PHP_GDIMG_TYPE_XBM: if (zend_parse_parameters(argc, "rp!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) { return; } break; case PHP_GDIMG_TYPE_BMP: if (zend_parse_parameters(argc, "r|z!b", &imgind, &to_zval, &compressed) == FAILURE) { return; } break; default: /* PHP_GDIMG_TYPE_GIF * PHP_GDIMG_TYPE_PNG * PHP_GDIMG_TYPE_JPG * PHP_GDIMG_TYPE_WBM * PHP_GDIMG_TYPE_WEBP * */ if (zend_parse_parameters(argc, "r|z!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) { return; } } if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(imgind), "Image", phpi_get_le_gd())) == NULL) { RETURN_FALSE; } if (image_type != PHP_GDIMG_TYPE_BMP && argc >= 3) { q = quality; /* or colorindex for foreground of BW images (defaults to black) */ if (argc == 4) { f = basefilter; } } if (argc > 1 && to_zval != NULL) { if (Z_TYPE_P(to_zval) == IS_RESOURCE) { php_stream_from_zval_no_verify(stream, to_zval); if (stream == NULL) { RETURN_FALSE; } close_stream = 0; } else if (Z_TYPE_P(to_zval) == IS_STRING) { if (CHECK_ZVAL_NULL_PATH(to_zval)) { php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, filename must not contain null bytes"); RETURN_FALSE; } stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL); if (stream == NULL) { RETURN_FALSE; } } else { php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream"); RETURN_FALSE; } } else if (argc > 1 && file != NULL) { stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL); if (stream == NULL) { RETURN_FALSE; } } else { ctx = ecalloc(1, sizeof(gdIOCtx)); ctx->putC = _php_image_output_putc; ctx->putBuf = _php_image_output_putbuf; ctx->gd_free = _php_image_output_ctxfree; } if (!ctx) { ctx = ecalloc(1, sizeof(gdIOCtx)); ctx->putC = _php_image_stream_putc; ctx->putBuf = _php_image_stream_putbuf; if (close_stream) { ctx->gd_free = _php_image_stream_ctxfreeandclose; } else { ctx->gd_free = _php_image_stream_ctxfree; } ctx->data = (void *)stream; } switch(image_type) { case PHP_GDIMG_CONVERT_WBM: if(q<0||q>255) { php_error_docref(NULL, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q); } case PHP_GDIMG_TYPE_JPG: (*func_p)(im, ctx, q); break; case PHP_GDIMG_TYPE_WEBP: if (q == -1) { q = 80; } (*func_p)(im, ctx, q); break; case PHP_GDIMG_TYPE_PNG: (*func_p)(im, ctx, q, f); break; case PHP_GDIMG_TYPE_XBM: case PHP_GDIMG_TYPE_WBM: if (argc < 3) { for(i=0; i < gdImageColorsTotal(im); i++) { if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break; } q = i; } if (image_type == PHP_GDIMG_TYPE_XBM) { (*func_p)(im, file ? file : "", q, ctx); } else { (*func_p)(im, q, ctx); } break; case PHP_GDIMG_TYPE_BMP: (*func_p)(im, ctx, (int) compressed); break; default: (*func_p)(im, ctx); break; } ctx->gd_free(ctx); RETURN_TRUE; }
PHP_METHOD(DefaultCluster, connect) { CassFuture *future = NULL; char *hash_key; php5to7_size hash_key_len = 0; char *keyspace = NULL; php5to7_size keyspace_len; zval *timeout = NULL; cassandra_psession *psession; cassandra_cluster *cluster = NULL; cassandra_session *session = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sz", &keyspace, &keyspace_len, &timeout) == FAILURE) { return; } cluster = PHP_CASSANDRA_GET_CLUSTER(getThis()); object_init_ex(return_value, cassandra_default_session_ce); session = PHP_CASSANDRA_GET_SESSION(return_value); session->default_consistency = cluster->default_consistency; session->default_page_size = cluster->default_page_size; session->persist = cluster->persist; if (!PHP5TO7_ZVAL_IS_UNDEF(session->default_timeout)) { PHP5TO7_ZVAL_COPY(PHP5TO7_ZVAL_MAYBE_P(session->default_timeout), PHP5TO7_ZVAL_MAYBE_P(cluster->default_timeout)); } if (session->persist) { php5to7_zend_resource_le *le; hash_key_len = spprintf(&hash_key, 0, "%s:session:%s", cluster->hash_key, SAFE_STR(keyspace)); if (PHP5TO7_ZEND_HASH_FIND(&EG(persistent_list), hash_key, hash_key_len + 1, le) && Z_RES_P(le)->type == php_le_cassandra_session()) { psession = (cassandra_psession *) Z_RES_P(le)->ptr; session->session = psession->session; future = psession->future; } } if (future == NULL) { php5to7_zend_resource_le resource; session->session = cass_session_new(); if (keyspace) { future = cass_session_connect_keyspace(session->session, cluster->cluster, keyspace); } else { future = cass_session_connect(session->session, cluster->cluster); } if (session->persist) { psession = (cassandra_psession *) pecalloc(1, sizeof(cassandra_psession), 1); psession->session = session->session; psession->future = future; #if PHP_MAJOR_VERSION >= 7 ZVAL_NEW_PERSISTENT_RES(&resource, 0, psession, php_le_cassandra_session()); PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, &resource, sizeof(php5to7_zend_resource_le)); CASSANDRA_G(persistent_sessions)++; #else resource.type = php_le_cassandra_session(); resource.ptr = psession; PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, resource, sizeof(php5to7_zend_resource_le)); CASSANDRA_G(persistent_sessions)++; #endif } } if (php_cassandra_future_wait_timed(future, timeout TSRMLS_CC) == FAILURE) { if (session->persist) { efree(hash_key); } else { cass_future_free(future); } return; } if (php_cassandra_future_is_error(future TSRMLS_CC) == FAILURE) { if (session->persist) { if (PHP5TO7_ZEND_HASH_DEL(&EG(persistent_list), hash_key, hash_key_len + 1)) { session->session = NULL; } efree(hash_key); } else { cass_future_free(future); } return; } if (session->persist) efree(hash_key); }
/* Used internally as comparison routine passed to zend_list_del_element */ static int curl_compare_resources( zval *z1, zval *z2 ) /* {{{ */ { return (Z_TYPE_P(z1) == Z_TYPE_P(z2) && Z_TYPE_P(z1) == IS_RESOURCE && Z_RES_P(z1) == Z_RES_P(z2)); }
static PHP_METHOD(swoole_coroutine_util, fgets) { coro_check(TSRMLS_C); zval *handle; php_stream *stream; #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_RESOURCE(handle) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &handle) == FAILURE) { return; } #endif int async; int fd = swoole_convert_to_fd_ex(handle, &async); if (fd < 0) { RETURN_FALSE; } if (async == 1) { swoole_php_fatal_error(E_WARNING, "only support file resources."); RETURN_FALSE; } swAio_event ev; bzero(&ev, sizeof(swAio_event)); php_stream_from_res(stream, Z_RES_P(handle)); if (stream->readbuf == NULL) { stream->readbuflen = stream->chunk_size; stream->readbuf = emalloc(stream->chunk_size); } ev.nbytes = stream->readbuflen; ev.buf = stream->readbuf; if (!ev.buf) { RETURN_FALSE; } php_context *context = emalloc(sizeof(php_context)); ev.flags = 0; ev.type = SW_AIO_STREAM_GET_LINE; ev.object = context; ev.callback = aio_onStreamGetLineCompleted; ev.handler = swAio_handler_stream_get_line; ev.fd = fd; ev.offset = stream->readpos; ev.req = (void *) (long) stream->writepos; if (!SwooleAIO.init) { php_swoole_check_reactor(); swAio_init(); } swTrace("fd=%d, offset=%jd, length=%ld", fd, (intmax_t) ev.offset, ev.nbytes); int ret = swAio_dispatch(&ev); if (ret < 0) { efree(context); RETURN_FALSE; } context->coro_params = *handle; context->state = SW_CORO_CONTEXT_RUNNING; coro_save(context); coro_yield(); }
PHP_METHOD(DefaultCluster, connectAsync) { char *hash_key; php5to7_size hash_key_len = 0; char *keyspace = NULL; php5to7_size keyspace_len; cassandra_cluster *cluster = NULL; cassandra_future_session *future = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &keyspace, &keyspace_len) == FAILURE) { return; } cluster = PHP_CASSANDRA_GET_CLUSTER(getThis()); object_init_ex(return_value, cassandra_future_session_ce); future = PHP_CASSANDRA_GET_FUTURE_SESSION(return_value); future->persist = cluster->persist; if (cluster->persist) { php5to7_zend_resource_le *le; hash_key_len = spprintf(&hash_key, 0, "%s:session:%s", cluster->hash_key, SAFE_STR(keyspace)); future->hash_key = hash_key; future->hash_key_len = hash_key_len; if (PHP5TO7_ZEND_HASH_FIND(&EG(persistent_list), hash_key, hash_key_len + 1, le)) { if (Z_TYPE_P(le) == php_le_cassandra_session()) { cassandra_psession *psession = (cassandra_psession *) Z_RES_P(le)->ptr; future->session = psession->session; future->future = psession->future; return; } } } future->session = cass_session_new(); if (keyspace) { future->future = cass_session_connect_keyspace(future->session, cluster->cluster, keyspace); } else { future->future = cass_session_connect(future->session, cluster->cluster); } if (cluster->persist) { php5to7_zend_resource_le resource; cassandra_psession *psession = (cassandra_psession *) pecalloc(1, sizeof(cassandra_psession), 1); psession->session = future->session; psession->future = future->future; #if PHP_MAJOR_VERSION >= 7 ZVAL_NEW_PERSISTENT_RES(&resource, 0, psession, php_le_cassandra_session()); PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, &resource, sizeof(php5to7_zend_resource_le)); CASSANDRA_G(persistent_sessions)++; #else resource.type = php_le_cassandra_session(); resource.ptr = psession; PHP5TO7_ZEND_HASH_UPDATE(&EG(persistent_list), hash_key, hash_key_len + 1, resource, sizeof(php5to7_zend_resource_le)); CASSANDRA_G(persistent_sessions)++; #endif } }
static int _php_server_push_callback(CURL *parent_ch, CURL *easy, size_t num_headers, struct curl_pushheaders *push_headers, void *userp) /* {{{ */ { php_curl *ch; php_curl *parent; php_curlm *mh = (php_curlm *)userp; size_t rval = CURL_PUSH_DENY; php_curlm_server_push *t = mh->handlers->server_push; zval *pz_parent_ch = NULL; zval pz_ch; zval headers; zval retval; zend_resource *res; char *header; int error; zend_fcall_info fci = empty_fcall_info; pz_parent_ch = _php_curl_multi_find_easy_handle(mh, parent_ch); if (pz_parent_ch == NULL) { return rval; } parent = (php_curl*)zend_fetch_resource(Z_RES_P(pz_parent_ch), le_curl_name, le_curl); ch = alloc_curl_handle(); ch->cp = easy; _php_setup_easy_copy_handlers(ch, parent); Z_ADDREF_P(pz_parent_ch); res = zend_register_resource(ch, le_curl); ZVAL_RES(&pz_ch, res); size_t i; array_init(&headers); for(i=0; i<num_headers; i++) { header = curl_pushheader_bynum(push_headers, i); add_next_index_string(&headers, header); } zend_fcall_info_init(&t->func_name, 0, &fci, &t->fci_cache, NULL, NULL); zend_fcall_info_argn( &fci, 3, pz_parent_ch, &pz_ch, &headers ); fci.retval = &retval; error = zend_call_function(&fci, &t->fci_cache); zend_fcall_info_args_clear(&fci, 1); zval_dtor(&headers); if (error == FAILURE) { php_error_docref(NULL, E_WARNING, "Cannot call the CURLMOPT_PUSHFUNCTION"); } else if (!Z_ISUNDEF(retval)) { if (CURL_PUSH_DENY != zval_get_long(&retval)) { rval = CURL_PUSH_OK; /* we want to create a copy of this zval that we store in the multihandle structure element "easyh" */ zval tmp_val; ZVAL_DUP(&tmp_val, &pz_ch); zend_llist_add_element(&mh->easyh, &tmp_val); } else { /* libcurl will free this easy handle, avoid double free */ ch->cp = NULL; } } return rval; }
static void php_mimepart_free_child(zval *childpart_z) { php_mimepart_free((php_mimepart *)zend_fetch_resource(Z_RES_P(childpart_z), php_mailparse_msg_name(), php_mailparse_le_mime_part())); }
RedisArray* ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b_lazy_connect TSRMLS_DC) { int i = 0, host_len; zval *id; char *host, *p; short port; zval *zpData, z_cons, z_ret; RedisSock *redis_sock = NULL; /* function calls on the Redis object */ ZVAL_STRING(&z_cons, "__construct"); /* init connections */ for (zend_hash_internal_pointer_reset(hosts); zend_hash_has_more_elements(hosts) == SUCCESS; zend_hash_move_forward(hosts)) { if ((zpData = zend_hash_get_current_data(hosts)) == NULL || (Z_TYPE_P(zpData) != IS_STRING)) { zval_dtor(&z_cons); efree(ra); return NULL; } ra->hosts[i] = estrdup(Z_STRVAL_P(zpData)); /* default values */ host = Z_STRVAL_P(zpData); host_len = Z_STRLEN_P(zpData); port = 6379; if(((p = strrchr(host, ':')))) { /* found port */ host_len = p - host; port = (short)atoi(p+1); } else if(strchr(host,'/') != NULL) { /* unix socket */ port = -1; } /* create Redis object */ object_init_ex(&ra->redis[i], redis_ce); call_user_function(&redis_ce->function_table, &ra->redis[i], &z_cons, &z_ret, 0, NULL TSRMLS_CC); /* create socket */ redis_sock = redis_sock_create(host, host_len, port, ra->connect_timeout, ra->pconnect, NULL, retry_interval, b_lazy_connect); if (!b_lazy_connect) { /* connect */ redis_sock_server_open(redis_sock, 1 TSRMLS_CC); } /* attach */ id = zend_list_insert(redis_sock, le_redis_sock TSRMLS_CC); add_property_resource(&ra->redis[i], "socket", Z_RES_P(id)); i++; } /* Cleanup constructor zval */ zval_dtor(&z_cons); return ra; }