/* {{{ internal functions */ static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject) { char *dirname; size_t dir_len; zval *zcontext = NULL; php_stream_context *context = NULL; php_stream *dirp; ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_PATH(dirname, dir_len) Z_PARAM_OPTIONAL Z_PARAM_RESOURCE(zcontext) ZEND_PARSE_PARAMETERS_END(); context = php_stream_context_from_zval(zcontext, 0); dirp = php_stream_opendir(dirname, REPORT_ERRORS, context); if (dirp == NULL) { RETURN_FALSE; } dirp->flags |= PHP_STREAM_FLAG_NO_FCLOSE; php_set_default_dir(dirp->res); if (createobject) { object_init_ex(return_value, dir_class_entry_ptr); add_property_stringl(return_value, "path", dirname, dir_len); add_property_resource(return_value, "handle", dirp->res); php_stream_auto_cleanup(dirp); /* so we don't get warnings under debug */ } else { php_stream_to_zval(dirp, return_value); } }
/* {{{ internal functions */ static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject) { char *dirname; size_t dir_len; zval *zcontext = NULL; php_stream_context *context = NULL; php_stream *dirp; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|r", &dirname, &dir_len, &zcontext) == FAILURE) { RETURN_NULL(); } context = php_stream_context_from_zval(zcontext, 0); dirp = php_stream_opendir(dirname, REPORT_ERRORS, context); if (dirp == NULL) { RETURN_FALSE; } dirp->flags |= PHP_STREAM_FLAG_NO_FCLOSE; php_set_default_dir(dirp->res); if (createobject) { object_init_ex(return_value, dir_class_entry_ptr); add_property_stringl(return_value, "path", dirname, dir_len); add_property_resource(return_value, "handle", dirp->res); php_stream_auto_cleanup(dirp); /* so we don't get warnings under debug */ } else { php_stream_to_zval(dirp, return_value); } }
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; }
RedisArray* ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval TSRMLS_DC) { int i, host_len, id; int count = zend_hash_num_elements(hosts); 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", 0); /* init connections */ for(i = 0; i < count; ++i) { if(FAILURE == zend_hash_quick_find(hosts, NULL, 0, i, (void**)&zpData)) { efree(ra); return NULL; } ra->hosts[i] = estrdup(Z_STRVAL_PP(zpData)); /* default values */ host = Z_STRVAL_PP(zpData); host_len = Z_STRLEN_PP(zpData); port = 6379; if((p = strchr(host, ':'))) { /* found port */ host_len = p - host; port = (short)atoi(p+1); } /* create Redis object */ MAKE_STD_ZVAL(ra->redis[i]); object_init_ex(ra->redis[i], redis_ce); INIT_PZVAL(ra->redis[i]); 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, 0, 0, NULL, retry_interval); /* TODO: persistence? */ /* connect */ redis_sock_server_open(redis_sock, 1 TSRMLS_CC); /* attach */ #if PHP_VERSION_ID >= 50400 id = zend_list_insert(redis_sock, le_redis_sock TSRMLS_CC); #else id = zend_list_insert(redis_sock, le_redis_sock); #endif add_property_resource(ra->redis[i], "socket", id); } return ra; }
/* {{{ proto object _php_create_ffmpeg_frame() creates an ffmpeg_frame object, adds a ffmpeg_frame resource to the object, registers the resource and returns a direct pointer to the resource. */ ff_frame_context* _php_create_ffmpeg_frame(INTERNAL_FUNCTION_PARAMETERS) { int ret; ff_frame_context *ff_frame; ff_frame = _php_alloc_ff_frame(); ret = ZEND_REGISTER_RESOURCE(NULL, ff_frame, le_ffmpeg_frame); object_init_ex(return_value, ffmpeg_frame_class_entry_ptr); add_property_resource(return_value, "ffmpeg_frame", ret); return ff_frame; }
/* ArchiveReader::close {{{ * */ ZEND_METHOD(ArchiveReader, close) { zval *this = getThis(); int resourse_id; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, ce_ArchiveException, &error_handling TSRMLS_CC); if ((resourse_id = _archive_get_rsrc_id(this TSRMLS_CC))) { add_property_resource(this, "fd", 0); zend_list_delete(resourse_id); zend_restore_error_handling(&error_handling TSRMLS_CC); RETURN_TRUE; } php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to close archive file descriptor"); zend_restore_error_handling(&error_handling TSRMLS_CC); }
/* ArchiveWriter::__construct {{{ * */ ZEND_METHOD(ArchiveWriter, __construct) { archive_file_t *arch = NULL; int resource_id; zval *this = getThis(); const char *error_string = NULL; char *filename; long error_num, filename_len, result, format=0, compression=0; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, ce_ArchiveException, &error_handling TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &filename, &filename_len, &format, &compression) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } #if PHP_API_VERSION < 20100412 if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } #endif if (php_check_open_basedir(filename TSRMLS_CC)) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } arch = (archive_file_t *) emalloc(sizeof(archive_file_t)); arch->stream = NULL; ALLOC_HASHTABLE(arch->entries); zend_hash_init(arch->entries, 10, NULL, _archive_entries_hash_dtor, 0); arch->mode = PHP_ARCHIVE_WRITE_MODE; arch->buf = emalloc(PHP_ARCHIVE_BUF_LEN + 1); arch->filename = estrndup(filename, filename_len); arch->arch = archive_write_new(); switch (compression) { case PHP_ARCHIVE_COMPRESSION_GZIP: if (archive_write_add_filter_gzip(arch->arch) != ARCHIVE_OK) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Gzip compression is not supported in this build"); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } break; case PHP_ARCHIVE_COMPRESSION_BZIP2: if (archive_write_add_filter_bzip2(arch->arch) != ARCHIVE_OK) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bzip2 compression is not supported in this build"); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } break; case 0: /* default value */ case PHP_ARCHIVE_COMPRESSION_NONE: /* always supported */ break; default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported compression type %ld", compression); zend_restore_error_handling(&error_handling TSRMLS_CC); return; break; } switch (format) { case 0: /* default value */ case PHP_ARCHIVE_FORMAT_TAR: case PHP_ARCHIVE_FORMAT_PAX_RESTRICTED: archive_write_set_format_pax_restricted(arch->arch); break; case PHP_ARCHIVE_FORMAT_PAX: archive_write_set_format_pax(arch->arch); break; case PHP_ARCHIVE_FORMAT_CPIO: archive_write_set_format_cpio(arch->arch); break; case PHP_ARCHIVE_FORMAT_SHAR: archive_write_set_format_shar(arch->arch); break; case PHP_ARCHIVE_FORMAT_USTAR: archive_write_set_format_ustar(arch->arch); break; default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported archive format: %ld", format); zend_restore_error_handling(&error_handling TSRMLS_CC); return; break; } archive_write_set_bytes_per_block(arch->arch, DEFAULT_BYTES_PER_BLOCK); result = archive_write_open(arch->arch, arch, _archive_open_clbk, _archive_write_clbk, _archive_close_clbk); /* do not pad the last block */ archive_write_set_bytes_in_last_block(arch->arch, 1); if (result) { error_num = archive_errno(arch->arch); error_string = archive_error_string(arch->arch); efree(arch->filename); efree(arch->buf); efree(arch); if (error_num && error_string) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for writing: error #%ld, %s", filename, error_num, error_string); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for writing: unknown error %ld", filename, result); } zend_restore_error_handling(&error_handling TSRMLS_CC); return; } resource_id = zend_list_insert(arch,le_archive); add_property_resource(this, "fd", resource_id); zend_restore_error_handling(&error_handling TSRMLS_CC); return; }
/* ArchiveWriter::finish {{{ * */ ZEND_METHOD(ArchiveWriter, finish) { zval *this = getThis(); int resource_id; HashPosition pos; archive_file_t *arch; archive_entry_t **entry; int result, error_num; const char *error_string; mode_t mode; php_stream *stream; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, ce_ArchiveException, &error_handling TSRMLS_CC); if (!_archive_get_fd(this, &arch TSRMLS_CC)) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } if (zend_hash_sort(arch->entries, zend_qsort, _archive_pathname_compare, 0 TSRMLS_CC) == FAILURE) { RETURN_FALSE; } zend_hash_internal_pointer_reset_ex(arch->entries, &pos); while (zend_hash_get_current_data_ex(arch->entries, (void **)&entry, &pos) == SUCCESS) { mode = archive_entry_mode((*entry)->entry); if (S_ISREG(mode) && archive_entry_size((*entry)->entry) > 0) { if ((stream = php_stream_open_wrapper_ex((*entry)->filename, "r", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, NULL))) { char buf[PHP_ARCHIVE_BUF_LEN]; int header_written=0; int read_bytes; while ((read_bytes = php_stream_read(stream, buf, PHP_ARCHIVE_BUF_LEN))) { if (!header_written) { /* write header only after the first successful read */ result = archive_write_header(arch->arch, (*entry)->entry); if (result == ARCHIVE_FATAL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write entry header for file %s: fatal error", (*entry)->filename); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } header_written = 1; } result = archive_write_data(arch->arch, buf, read_bytes); if (result <= 0) { error_num = archive_errno(arch->arch); error_string = archive_error_string(arch->arch); if (error_num && error_string) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write file %s to archive: error #%d, %s", (*entry)->filename, error_num, error_string); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write file %s: unknown error %d", (*entry)->filename, result); } php_stream_close(stream); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } } php_stream_close(stream); } } else { result = archive_write_header(arch->arch, (*entry)->entry); if (result == ARCHIVE_FATAL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write entry header for file %s: fatal error", (*entry)->filename); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } } zend_hash_move_forward_ex(arch->entries, &pos); } if ((resource_id = _archive_get_rsrc_id(this TSRMLS_CC))) { add_property_resource(this, "fd", 0); zend_list_delete(resource_id); zend_restore_error_handling(&error_handling TSRMLS_CC); RETURN_TRUE; } php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to finish writing of archive file"); zend_restore_error_handling(&error_handling TSRMLS_CC); }
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; }
/* ArchiveReader::__construct {{{ * */ ZEND_METHOD(ArchiveReader, __construct) { archive_file_t *arch = NULL; int resource_id; zval *this = getThis(); const char *error_string = NULL; char *filename; long error_num, filename_len, result, format = 0, compression = 0, block_size = 0; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, ce_ArchiveException, &error_handling TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lll", &filename, &filename_len, &format, &compression, &block_size) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } #if PHP_API_VERSION < 20100412 if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } #endif if (php_check_open_basedir(filename TSRMLS_CC)) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } if(block_size <= 0){ block_size = PHP_ARCHIVE_BUF_LEN; } arch = (archive_file_t *) emalloc(sizeof(archive_file_t)); arch->stream = NULL; arch->current_entry = NULL; arch->entries = NULL; arch->struct_state = ARCHIVE_OK; arch->block_size = block_size; arch->mode = PHP_ARCHIVE_READ_MODE; arch->buf = emalloc(arch->block_size + 1); arch->filename = estrndup(filename, filename_len); arch->arch = archive_read_new(); archive_read_support_filter_all(arch->arch); switch(format){ case PHP_ARCHIVE_FORMAT_TAR: archive_read_support_format_tar(arch->arch); break; case PHP_ARCHIVE_FORMAT_CPIO: archive_read_support_format_cpio(arch->arch); break; default: archive_read_support_format_all(arch->arch); break; } switch(compression){ case PHP_ARCHIVE_COMPRESSION_NONE: break; case PHP_ARCHIVE_COMPRESSION_GZIP: if(archive_read_support_filter_gzip(arch->arch) != ARCHIVE_OK){ efree(arch->filename); efree(arch->buf); efree(arch); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Gzip compression support is not available in this build "); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } break; case PHP_ARCHIVE_COMPRESSION_BZIP2: if(archive_read_support_filter_gzip(arch->arch) != ARCHIVE_OK){ efree(arch->filename); efree(arch->buf); efree(arch); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bzip2 compression support is not available in this build "); zend_restore_error_handling(&error_handling TSRMLS_CC); return; } default: archive_read_support_filter_all(arch->arch); break; } result = archive_read_open(arch->arch, arch, _archive_open_clbk, _archive_read_clbk, _archive_close_clbk); if (result) { error_num = archive_errno(arch->arch); error_string = archive_error_string(arch->arch); if (arch->stream) { php_stream_close(arch->stream); } if (error_num && error_string) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for reading: error #%d, %s", filename, error_num, error_string); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for reading: unknown error %d", filename, result); } zend_restore_error_handling(&error_handling TSRMLS_CC); archive_read_close(arch->arch); archive_read_free(arch->arch); efree(arch->filename); efree(arch->buf); efree(arch); return; } resource_id = zend_list_insert(arch,le_archive); add_property_resource(this, "fd", resource_id); zend_restore_error_handling(&error_handling TSRMLS_CC); return; }
/* ArchiveReader::getNextEntry {{{ * */ ZEND_METHOD(ArchiveReader, getNextEntry) { zval *this = getThis(); archive_file_t *arch; int result, error_num, resource_id; const char *error_string; zend_bool fetch_entry_data = 0; archive_entry_t *entry; struct archive_entry *current_entry; size_t len; off_t offset; char *buf; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, ce_ArchiveException, &error_handling TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &fetch_entry_data) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } if (!_archive_get_fd(this, &arch TSRMLS_CC)) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } if (arch->struct_state == ARCHIVE_OK) { result = archive_read_next_header(arch->arch, ¤t_entry); arch->struct_state = result; entry = (archive_entry_t *) emalloc(sizeof(archive_entry_t)); entry->entry = current_entry; entry->data = NULL; entry->filename = NULL; entry->resolved_filename = NULL; entry->data_len = 0; arch->current_entry = entry; } else { zend_restore_error_handling(&error_handling TSRMLS_CC); RETURN_FALSE; } if (result && result != ARCHIVE_EOF) { arch->current_entry = NULL; error_num = archive_errno(arch->arch); error_string = archive_error_string(arch->arch); efree(entry); if (error_num && error_string) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read file %s: error #%d, %s", arch->filename, error_num, error_string); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read file %s: unknown error %d", arch->filename, result); } zend_restore_error_handling(&error_handling TSRMLS_CC); return; } if (result == ARCHIVE_EOF) { arch->current_entry = NULL; efree(entry); zend_restore_error_handling(&error_handling TSRMLS_CC); RETURN_FALSE; } object_init_ex(return_value, ce_ArchiveEntry); if (fetch_entry_data) { while ((result = archive_read_data_block(arch->arch, (const void **)&buf, &len, &offset)) == ARCHIVE_OK) { entry->data = erealloc(entry->data, entry->data_len + len + 1); memcpy(entry->data + entry->data_len, buf, len); entry->data_len += len; } if (result && result != ARCHIVE_EOF) { error_num = archive_errno(arch->arch); error_string = archive_error_string(arch->arch); efree(entry); if (error_num && error_string) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read file %s: error #%d, %s", arch->filename, error_num, error_string); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read file %s: unknown error %d", arch->filename, result); } zend_restore_error_handling(&error_handling TSRMLS_CC); return; } } if (entry->entry) { resource_id = zend_list_insert(entry,le_archive_entry); add_property_resource(return_value, "entry", resource_id); } zend_restore_error_handling(&error_handling TSRMLS_CC); }
static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject) { pval **arg; php_dir *dirp; DIRLS_FETCH(); if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(arg); if (php_check_open_basedir((*arg)->value.str.val)) { RETURN_FALSE; } dirp = emalloc(sizeof(php_dir)); dirp->dir = VCWD_OPENDIR((*arg)->value.str.val); #ifdef PHP_WIN32 if (!dirp->dir || dirp->dir->finished) { if (dirp->dir) { closedir(dirp->dir); } #else if (!dirp->dir) { #endif efree(dirp); php_error(E_WARNING, "OpenDir: %s (errno %d)", strerror(errno), errno); RETURN_FALSE; } dirp->id = zend_list_insert(dirp,le_dirp); php_set_default_dir(dirp->id DIRLS_CC); if (createobject) { object_init_ex(return_value, dir_class_entry_ptr); add_property_stringl(return_value, "path", (*arg)->value.str.val, (*arg)->value.str.len, 1); add_property_resource(return_value, "handle", dirp->id); zend_list_addref(dirp->id); } else { RETURN_RESOURCE(dirp->id); } } /* }}} */ /* {{{ proto int opendir(string path) Open a directory and return a dir_handle */ PHP_FUNCTION(opendir) { _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU,0); } /* }}} */ /* {{{ proto class dir(string directory) Directory class with properties, handle and class and methods read, rewind and close */ PHP_FUNCTION(getdir) { _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU,1); } /* }}} */ /* {{{ proto void closedir([int dir_handle]) Close directory connection identified by the dir_handle */ PHP_FUNCTION(closedir) { pval **id, **tmp, *myself; php_dir *dirp; DIRLS_FETCH(); FETCH_DIRP(); zend_list_delete(dirp->id); if (dirp->id == DIRG(default_dir)) { php_set_default_dir(-1 DIRLS_CC); } } /* }}} */ #if defined(HAVE_CHROOT) && !defined(ZTS) /* {{{ proto int chroot(string directory) Change root directory */ PHP_FUNCTION(chroot) { pval **arg; int ret; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(arg); ret = chroot((*arg)->value.str.val); if (ret != 0) { php_error(E_WARNING, "chroot: %s (errno %d)", strerror(errno), errno); RETURN_FALSE; } ret = chdir("/"); if (ret != 0) { php_error(E_WARNING, "chdir: %s (errno %d)", strerror(errno), errno); RETURN_FALSE; } RETURN_TRUE; } /* }}} */ #endif /* {{{ proto int chdir(string directory) Change the current directory */ PHP_FUNCTION(chdir) { pval **arg; int ret; PLS_FETCH(); if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(arg); if (PG(safe_mode) && !php_checkuid((*arg)->value.str.val, NULL, CHECKUID_ALLOW_ONLY_DIR)) { RETURN_FALSE; } ret = VCWD_CHDIR((*arg)->value.str.val); if (ret != 0) { php_error(E_WARNING, "ChDir: %s (errno %d)", strerror(errno), errno); RETURN_FALSE; } RETURN_TRUE; } /* }}} */ /* {{{ proto string getcwd(void) Gets the current directory */ PHP_FUNCTION(getcwd) { char path[MAXPATHLEN]; char *ret=NULL; if (ZEND_NUM_ARGS() != 0) { WRONG_PARAM_COUNT; } #if HAVE_GETCWD ret = VCWD_GETCWD(path, MAXPATHLEN); #elif HAVE_GETWD ret = VCWD_GETWD(path); /* * #warning is not ANSI C * #else * #warning no proper getcwd support for your site */ #endif if (ret) { RETURN_STRING(path,1); } else { RETURN_FALSE; } } /* }}} */ /* {{{ proto void rewinddir([int dir_handle]) Rewind dir_handle back to the start */ PHP_FUNCTION(rewinddir) { pval **id, **tmp, *myself; php_dir *dirp; DIRLS_FETCH(); FETCH_DIRP(); rewinddir(dirp->dir); } /* }}} */ /* {{{ proto string readdir([int dir_handle]) Read directory entry from dir_handle */ PHP_NAMED_FUNCTION(php_if_readdir) { pval **id, **tmp, *myself; php_dir *dirp; char entry[sizeof(struct dirent)+MAXPATHLEN]; struct dirent *result = (struct dirent *)&entry; /* patch for libc5 readdir problems */ DIRLS_FETCH(); FETCH_DIRP(); if (php_readdir_r(dirp->dir, (struct dirent *) entry, &result) == 0 && result) { RETURN_STRINGL(result->d_name, strlen(result->d_name), 1); } RETURN_FALSE; }
/* {{{ proto object ffmpeg_movie(string filename) Constructor for ffmpeg_movie objects */ FFMPEG_PHP_CONSTRUCTOR(ffmpeg_movie, __construct) { int hashkey_length = 0, filename_len; char *filename = NULL, *fullpath = NULL, *hashkey = NULL; zend_bool persistent = 0; ff_movie_context *ffmovie_ctx = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &filename_len, &persistent) != SUCCESS) { return; } if (persistent && !INI_BOOL("ffmpeg.allow_persistent")) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Persistent movies have been disabled in php.ini"); RETURN_FALSE; } if (persistent) { zend_rsrc_list_entry *le; /* resolve the fully-qualified path name to use as the hash key */ fullpath = expand_filepath(filename, NULL TSRMLS_CC); hashkey_length = sizeof("ffmpeg-php_")-1 + filename_len; hashkey = (char *) emalloc(hashkey_length+1); snprintf(hashkey, hashkey_length, "ffmpeg-php_%s", filename); /* do we have an existing persistent movie? */ if (SUCCESS == zend_hash_find(&EG(persistent_list), hashkey, hashkey_length+1, (void**)&le)) { int type; if (Z_TYPE_P(le) != le_ffmpeg_pmovie) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to retrieve persistent resource"); efree(hashkey); RETURN_FALSE; } ffmovie_ctx = (ff_movie_context*)le->ptr; /* sanity check to ensure that the resource is still a valid * regular resource number */ if (zend_list_find(ffmovie_ctx->rsrc_id, &type) == ffmovie_ctx) { /* add a reference to the persistent movie */ zend_list_addref(ffmovie_ctx->rsrc_id); } else { //php_error_docref(NULL TSRMLS_CC, E_ERROR, //"Not a valid persistent movie resource"); ffmovie_ctx->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ffmovie_ctx, le_ffmpeg_pmovie); } } else { /* no existing persistant movie, create one */ zend_rsrc_list_entry new_le; ffmovie_ctx = _php_alloc_ffmovie_ctx(1); if (_php_open_movie_file(ffmovie_ctx, filename)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't open movie file %s", filename); ZVAL_BOOL(getThis(), 0); RETURN_FALSE; } Z_TYPE(new_le) = le_ffmpeg_pmovie; new_le.ptr = ffmovie_ctx; if (FAILURE == zend_hash_update(&EG(persistent_list), hashkey, hashkey_length+1, (void *)&new_le, sizeof(zend_rsrc_list_entry), NULL)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to register persistent resource"); RETURN_FALSE; } ffmovie_ctx->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ffmovie_ctx, le_ffmpeg_pmovie); } } else { ffmovie_ctx = _php_alloc_ffmovie_ctx(0); if (_php_open_movie_file(ffmovie_ctx, filename)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't open movie file %s", filename); ZVAL_BOOL(getThis(), 0); RETURN_FALSE; } /* pass NULL for resource result since we're not returning the resource directly, but adding it to the returned object. */ ffmovie_ctx->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ffmovie_ctx, le_ffmpeg_movie); } object_init_ex(getThis(), ffmpeg_movie_class_entry_ptr); add_property_resource(getThis(), "ffmpeg_movie", ffmovie_ctx->rsrc_id); if (fullpath) { efree(fullpath); } if (hashkey) { efree(hashkey); } }
ZEND_METHOD(YConsistent,consistent_init){ zval *object = getThis(); conhash *con = conhash_init(); int res_id = ZEND_REGISTER_RESOURCE(NULL,con,le_conhash); add_property_resource(object,"conhash",res_id); }
/* {{{ proto object ffmpeg_movie(string filename) Constructor for ffmpeg_movie objects */ FFMPEG_PHP_CONSTRUCTOR(ffmpeg_movie, __construct) { int persistent = 0, hashkey_length = 0; char *filename = NULL, *fullpath = NULL, *hashkey = NULL; zval ***argv; ff_movie_context *ffmovie_ctx = NULL; /* retrieve arguments */ argv = (zval ***) safe_emalloc(sizeof(zval **), ZEND_NUM_ARGS(), 0); if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), argv) != SUCCESS) { efree(argv); php_error_docref(NULL TSRMLS_CC, E_ERROR, "Error parsing arguments"); } switch (ZEND_NUM_ARGS()) { case 2: convert_to_boolean_ex(argv[1]); if (! INI_BOOL("ffmpeg.allow_persistent") && Z_LVAL_PP(argv[1])) { zend_error(E_WARNING, "Persistent movies have been disabled in php.ini"); break; } persistent = Z_LVAL_PP(argv[1]); /* fallthru */ case 1: convert_to_string_ex(argv[0]); filename = Z_STRVAL_PP(argv[0]); break; default: WRONG_PARAM_COUNT; } if (persistent) { list_entry *le; /* resolve the fully-qualified path name to use as the hash key */ fullpath = expand_filepath(filename, NULL TSRMLS_CC); hashkey_length = sizeof("ffmpeg-php_")-1 + strlen(SAFE_STRING(filename)); hashkey = (char *) emalloc(hashkey_length+1); snprintf(hashkey, hashkey_length, "ffmpeg-php_%s", SAFE_STRING(filename)); /* do we have an existing persistent movie? */ if (SUCCESS == zend_hash_find(&EG(persistent_list), hashkey, hashkey_length+1, (void**)&le)) { int type; if (Z_TYPE_P(le) != le_ffmpeg_pmovie) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to retrieve persistent resource"); } ffmovie_ctx = (ff_movie_context*)le->ptr; /* sanity check to ensure that the resource is still a valid * regular resource number */ if (zend_list_find(ffmovie_ctx->rsrc_id, &type) == ffmovie_ctx) { /* add a reference to the persistent movie */ zend_list_addref(ffmovie_ctx->rsrc_id); } else { //php_error_docref(NULL TSRMLS_CC, E_ERROR, //"Not a valid persistent movie resource"); ffmovie_ctx->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ffmovie_ctx, le_ffmpeg_pmovie); } } else { /* no existing persistant movie, create one */ list_entry new_le; ffmovie_ctx = _php_alloc_ffmovie_ctx(1); if (_php_open_movie_file(ffmovie_ctx, filename)) { zend_error(E_WARNING, "Can't open movie file %s", filename); efree(argv); ZVAL_BOOL(getThis(), 0); RETURN_FALSE; } Z_TYPE(new_le) = le_ffmpeg_pmovie; new_le.ptr = ffmovie_ctx; if (FAILURE == zend_hash_update(&EG(persistent_list), hashkey, hashkey_length+1, (void *)&new_le, sizeof(list_entry), NULL)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to register persistent resource"); } ffmovie_ctx->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ffmovie_ctx, le_ffmpeg_pmovie); } } else { ffmovie_ctx = _php_alloc_ffmovie_ctx(0); if (_php_open_movie_file(ffmovie_ctx, Z_STRVAL_PP(argv[0]))) { zend_error(E_WARNING, "Can't open movie file %s", Z_STRVAL_PP(argv[0])); efree(argv); ZVAL_BOOL(getThis(), 0); RETURN_FALSE; } /* pass NULL for resource result since we're not returning the resource directly, but adding it to the returned object. */ ffmovie_ctx->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ffmovie_ctx, le_ffmpeg_movie); } object_init_ex(getThis(), ffmpeg_movie_class_entry_ptr); add_property_resource(getThis(), "ffmpeg_movie", ffmovie_ctx->rsrc_id); efree(argv); if (fullpath) { efree(fullpath); } if (hashkey) { efree(hashkey); } }