Пример #1
0
int php_imagick_read_image_using_php_streams(php_imagick_object *intern, int type, char *filename, int filename_len TSRMLS_DC)
{
	php_stream *stream;
	MagickBooleanType status;
	FILE *fp;
#if ZEND_MODULE_API_NO > 20060613 
	zend_error_handling error_handling;
#endif
	
#if ZEND_MODULE_API_NO > 20060613 
	zend_replace_error_handling(EH_THROW, php_imagick_exception_class_entry, &error_handling TSRMLS_CC);
#else
	php_set_error_handling(EH_THROW, php_imagick_exception_class_entry TSRMLS_CC);
#endif
	
	stream = php_stream_open_wrapper(filename, "rb", (ENFORCE_SAFE_MODE|IGNORE_PATH) & ~REPORT_ERRORS, NULL);

	if (!stream) {
		goto return_error;
	}
	
	if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_INTERNAL) == FAILURE) {
		goto return_error;
	}

	if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_INTERNAL, (void*)&fp, 0) == FAILURE) {
		goto return_error;
	}
	
#if ZEND_MODULE_API_NO > 20060613 
	zend_restore_error_handling(&error_handling TSRMLS_CC);
#else
	php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
#endif

	if (type == 1) {
		status = MagickReadImageFile(intern->magick_wand, fp);
	} else {
		status = MagickPingImageFile(intern->magick_wand, fp);
	}
	
	if (status == MagickFalse) {
		php_stream_close(stream);
		return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
	}
	
	if (php_stream_is(stream, PHP_STREAM_IS_STDIO)) {
		char *absolute = expand_filepath(filename, NULL TSRMLS_CC);
		MagickSetImageFilename(intern->magick_wand, absolute);
		efree(absolute);
	} else {
		/* Set to empty filename, otherwise it will point to MAGICK_TEMP/magick-XXXXX */
		MagickSetImageFilename(intern->magick_wand, "");
	}
	php_stream_close(stream);

	if (status == MagickFalse) {
		return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
	}
	
	IMAGICK_CORRECT_ITERATOR_POSITION(intern);
	return IMAGICK_READ_WRITE_NO_ERROR;
	
return_error:
#if ZEND_MODULE_API_NO > 20060613 
	zend_restore_error_handling(&error_handling TSRMLS_CC);
#else
	php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
#endif
	if (stream) php_stream_close(stream);
	return IMAGICK_READ_WRITE_UNDERLYING_LIBRARY;
}
/* 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;
}
Пример #3
0
/* {{{ proto void DOMElement::__construct(string name, [string value], [string uri]); */
PHP_METHOD(domelement, __construct)
{

	zval *id;
	xmlNodePtr nodep = NULL, oldnode = NULL;
	dom_object *intern;
	char *name, *value = NULL, *uri = NULL;
	char *localname = NULL, *prefix = NULL;
	int errorcode = 0, uri_len = 0;
	int name_len, value_len = 0, name_valid;
	xmlNsPtr nsptr = NULL;
	zend_error_handling error_handling;

	zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|s!s", &id, dom_element_class_entry, &name, &name_len, &value, &value_len, &uri, &uri_len) == FAILURE) {
		zend_restore_error_handling(&error_handling TSRMLS_CC);
		return;
	}
	zend_restore_error_handling(&error_handling TSRMLS_CC);

	name_valid = xmlValidateName((xmlChar *) name, 0);
	if (name_valid != 0) {
		php_dom_throw_error(INVALID_CHARACTER_ERR, 1 TSRMLS_CC);
		RETURN_FALSE;
	}

	/* Namespace logic is seperate and only when uri passed in to insure no BC breakage */
	if (uri_len > 0) {
		errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len);
		if (errorcode == 0) {
			nodep = xmlNewNode (NULL, (xmlChar *)localname);
			if (nodep != NULL && uri != NULL) {
				nsptr = dom_get_ns(nodep, uri, &errorcode, prefix);
				xmlSetNs(nodep, nsptr);
			}
		}
		xmlFree(localname);
		if (prefix != NULL) {
			xmlFree(prefix);
		}
		if (errorcode != 0) {
			if (nodep != NULL) {
				xmlFreeNode(nodep);
			}
			php_dom_throw_error(errorcode, 1 TSRMLS_CC);
			RETURN_FALSE;
		}
	} else {
	    /* If you don't pass a namespace uri, then you can't set a prefix */
	    localname = xmlSplitQName2((xmlChar *)name, (xmlChar **) &prefix);
	    if (prefix != NULL) {
			xmlFree(localname);
			xmlFree(prefix);
	        php_dom_throw_error(NAMESPACE_ERR, 1 TSRMLS_CC);
	        RETURN_FALSE;
	    }
		nodep = xmlNewNode(NULL, (xmlChar *) name);
	}

	if (!nodep) {
		php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
		RETURN_FALSE;
	}

	if (value_len > 0) {
		xmlNodeSetContentLen(nodep, (xmlChar *) value, value_len);
	}

	intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
	if (intern != NULL) {
		oldnode = dom_object_get_node(intern);
		if (oldnode != NULL) {
			php_libxml_node_free_resource(oldnode  TSRMLS_CC);
		}
		php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC);
	}
}
/* 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, &current_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);
}