summaryrefslogtreecommitdiff
path: root/ext/tidy/tidy.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/tidy/tidy.c')
-rw-r--r--ext/tidy/tidy.c484
1 files changed, 267 insertions, 217 deletions
diff --git a/ext/tidy/tidy.c b/ext/tidy/tidy.c
index dfc15d9..1dc9688 100644
--- a/ext/tidy/tidy.c
+++ b/ext/tidy/tidy.c
@@ -43,8 +43,10 @@
/* {{{ ext/tidy macros
*/
+#define FIX_BUFFER(bptr) do { if ((bptr)->size) { (bptr)->bp[(bptr)->size-1] = '\0'; } } while(0)
+
#define TIDY_SET_CONTEXT \
- zval *object = getThis();
+ zval *object = getThis();
#define TIDY_FETCH_OBJECT \
PHPTidyObj *obj; \
@@ -58,7 +60,7 @@
RETURN_FALSE; \
} \
} \
- obj = (PHPTidyObj *) zend_object_store_get_object(object TSRMLS_CC); \
+ obj = (PHPTidyObj *) zend_object_store_get_object(object TSRMLS_CC);
#define TIDY_FETCH_ONLY_OBJECT \
PHPTidyObj *obj; \
@@ -66,25 +68,25 @@
if (zend_parse_parameters_none() == FAILURE) { \
return; \
} \
- obj = (PHPTidyObj *) zend_object_store_get_object(object TSRMLS_CC); \
+ obj = (PHPTidyObj *) zend_object_store_get_object(object TSRMLS_CC);
#define TIDY_APPLY_CONFIG_ZVAL(_doc, _val) \
- if(_val) { \
- if(Z_TYPE_PP(_val) == IS_ARRAY) { \
- _php_tidy_apply_config_array(_doc, HASH_OF(*_val) TSRMLS_CC); \
- } else { \
- convert_to_string_ex(_val); \
- TIDY_SAFE_MODE_CHECK(Z_STRVAL_PP(_val)); \
- switch (tidyLoadConfig(_doc, Z_STRVAL_PP(_val))) { \
- case -1: \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not load configuration file '%s'", Z_STRVAL_PP(_val)); \
- break; \
- case 1: \
- php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There were errors while parsing the configuration file '%s'", Z_STRVAL_PP(_val)); \
- break; \
- } \
- } \
- }
+ if (_val) { \
+ if (Z_TYPE_PP(_val) == IS_ARRAY) { \
+ _php_tidy_apply_config_array(_doc, HASH_OF(*_val) TSRMLS_CC); \
+ } else if (Z_TYPE_PP(_val) != IS_NULL) { \
+ convert_to_string_ex(_val); \
+ TIDY_SAFE_MODE_CHECK(Z_STRVAL_PP(_val)); \
+ switch (tidyLoadConfig(_doc, Z_STRVAL_PP(_val))) { \
+ case -1: \
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not load configuration file '%s'", Z_STRVAL_PP(_val)); \
+ break; \
+ case 1: \
+ php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There were errors while parsing the configuration file '%s'", Z_STRVAL_PP(_val)); \
+ break; \
+ } \
+ } \
+ }
#define REGISTER_TIDY_CLASS(classname, name, parent, __flags) \
{ \
@@ -121,16 +123,16 @@
}
#define ADD_PROPERTY_STRINGL(_table, _key, _string, _len) \
- { \
- zval *tmp; \
- MAKE_STD_ZVAL(tmp); \
- if (_string) { \
- ZVAL_STRINGL(tmp, (char *)_string, _len, 1); \
- } else { \
- ZVAL_EMPTY_STRING(tmp); \
- } \
- zend_hash_update(_table, #_key, sizeof(#_key), (void *)&tmp, sizeof(zval *), NULL); \
- }
+ { \
+ zval *tmp; \
+ MAKE_STD_ZVAL(tmp); \
+ if (_string) { \
+ ZVAL_STRINGL(tmp, (char *)_string, _len, 1); \
+ } else { \
+ ZVAL_EMPTY_STRING(tmp); \
+ } \
+ zend_hash_update(_table, #_key, sizeof(#_key), (void *)&tmp, sizeof(zval *), NULL); \
+ }
#define ADD_PROPERTY_LONG(_table, _key, _long) \
{ \
@@ -149,27 +151,27 @@
}
#define ADD_PROPERTY_BOOL(_table, _key, _bool) \
- { \
- zval *tmp; \
- MAKE_STD_ZVAL(tmp); \
- ZVAL_BOOL(tmp, _bool); \
- zend_hash_update(_table, #_key, sizeof(#_key), (void *)&tmp, sizeof(zval *), NULL); \
- }
+ { \
+ zval *tmp; \
+ MAKE_STD_ZVAL(tmp); \
+ ZVAL_BOOL(tmp, _bool); \
+ zend_hash_update(_table, #_key, sizeof(#_key), (void *)&tmp, sizeof(zval *), NULL); \
+ }
#define TIDY_SAFE_MODE_CHECK(filename) \
-if ((PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(filename TSRMLS_CC)) { \
- RETURN_FALSE; \
-} \
+ if ((PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(filename TSRMLS_CC)) { \
+ RETURN_FALSE; \
+ }
#define TIDY_SET_DEFAULT_CONFIG(_doc) \
if (TG(default_config) && TG(default_config)[0]) { \
if (tidyLoadConfig(_doc, TG(default_config)) < 0) { \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load Tidy configuration file at '%s'.", TG(default_config)); \
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load Tidy configuration file at '%s'", TG(default_config)); \
} \
}
/* }}} */
-/* {{{ ext/tidy structs
+/* {{{ ext/tidy structs
*/
typedef struct _PHPTidyDoc PHPTidyDoc;
typedef struct _PHPTidyObj PHPTidyObj;
@@ -187,16 +189,16 @@ typedef enum {
} tidy_base_nodetypes;
struct _PHPTidyDoc {
- TidyDoc doc;
- TidyBuffer *errbuf;
- unsigned int ref_count;
+ TidyDoc doc;
+ TidyBuffer *errbuf;
+ unsigned int ref_count;
};
struct _PHPTidyObj {
- zend_object std;
- TidyNode node;
- tidy_obj_type type;
- PHPTidyDoc *ptdoc;
+ zend_object std;
+ TidyNode node;
+ tidy_obj_type type;
+ PHPTidyDoc *ptdoc;
};
/* }}} */
@@ -217,6 +219,10 @@ static int _php_tidy_set_tidy_opt(TidyDoc, char *, zval * TSRMLS_DC);
static int _php_tidy_apply_config_array(TidyDoc doc, HashTable *ht_options TSRMLS_DC);
static void _php_tidy_register_nodetypes(INIT_FUNC_ARGS);
static void _php_tidy_register_tags(INIT_FUNC_ARGS);
+static PHP_INI_MH(php_tidy_set_clean_output);
+static void php_tidy_clean_output_start(zval *name TSRMLS_DC);
+static php_output_handler *php_tidy_output_handler_init(zval *handler_name, size_t chunk_size, int flags TSRMLS_DC);
+static int php_tidy_output_handler(void **nothing, php_output_context *output_context);
static PHP_MINIT_FUNCTION(tidy);
static PHP_MSHUTDOWN_FUNCTION(tidy);
@@ -246,8 +252,6 @@ static PHP_FUNCTION(tidy_warning_count);
static PHP_FUNCTION(tidy_access_count);
static PHP_FUNCTION(tidy_config_count);
-static PHP_FUNCTION(ob_tidyhandler);
-
static PHP_FUNCTION(tidy_get_root);
static PHP_FUNCTION(tidy_get_html);
static PHP_FUNCTION(tidy_get_head);
@@ -272,8 +276,8 @@ static TIDY_NODE_METHOD(__construct);
ZEND_DECLARE_MODULE_GLOBALS(tidy)
PHP_INI_BEGIN()
-STD_PHP_INI_ENTRY("tidy.default_config", "", PHP_INI_SYSTEM, OnUpdateString, default_config, zend_tidy_globals, tidy_globals)
-PHP_INI_ENTRY("tidy.clean_output", "0", PHP_INI_PERDIR, NULL)
+STD_PHP_INI_ENTRY("tidy.default_config", "", PHP_INI_SYSTEM, OnUpdateString, default_config, zend_tidy_globals, tidy_globals)
+STD_PHP_INI_ENTRY("tidy.clean_output", "0", PHP_INI_USER, php_tidy_set_clean_output, clean_output, zend_tidy_globals, tidy_globals)
PHP_INI_END()
/* {{{ arginfo */
@@ -283,8 +287,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_parse_string, 0, 0, 1)
ZEND_ARG_INFO(0, encoding)
ZEND_END_ARG_INFO()
-ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_get_error_buffer, 0, 0, 0)
- ZEND_ARG_INFO(0, detailed)
+ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_error_buffer, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_output, 0)
@@ -369,11 +372,6 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_get_body, 0, 0, 1)
ZEND_ARG_INFO(0, tidy)
ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_ob_tidyhandler, 0, 0, 1)
- ZEND_ARG_INFO(0, input)
- ZEND_ARG_INFO(0, mode)
-ZEND_END_ARG_INFO()
/* }}} */
static const zend_function_entry tidy_functions[] = {
@@ -381,21 +379,21 @@ static const zend_function_entry tidy_functions[] = {
PHP_FE(tidy_parse_string, arginfo_tidy_parse_string)
PHP_FE(tidy_parse_file, arginfo_tidy_parse_file)
PHP_FE(tidy_get_output, arginfo_tidy_get_output)
- PHP_FE(tidy_get_error_buffer, arginfo_tidy_get_error_buffer)
+ PHP_FE(tidy_get_error_buffer, arginfo_tidy_get_error_buffer)
PHP_FE(tidy_clean_repair, arginfo_tidy_clean_repair)
PHP_FE(tidy_repair_string, arginfo_tidy_repair_string)
PHP_FE(tidy_repair_file, arginfo_tidy_repair_file)
- PHP_FE(tidy_diagnose, arginfo_tidy_diagnose)
+ PHP_FE(tidy_diagnose, arginfo_tidy_diagnose)
PHP_FE(tidy_get_release, arginfo_tidy_get_release)
PHP_FE(tidy_get_config, arginfo_tidy_get_config)
PHP_FE(tidy_get_status, arginfo_tidy_get_status)
PHP_FE(tidy_get_html_ver, arginfo_tidy_get_html_ver)
PHP_FE(tidy_is_xhtml, arginfo_tidy_is_xhtml)
- PHP_FE(tidy_is_xml, arginfo_tidy_is_xml)
+ PHP_FE(tidy_is_xml, arginfo_tidy_is_xml)
PHP_FE(tidy_error_count, arginfo_tidy_error_count)
PHP_FE(tidy_warning_count, arginfo_tidy_warning_count)
PHP_FE(tidy_access_count, arginfo_tidy_access_count)
- PHP_FE(tidy_config_count, arginfo_tidy_config_count)
+ PHP_FE(tidy_config_count, arginfo_tidy_config_count)
#if HAVE_TIDYOPTGETDOC
PHP_FE(tidy_get_opt_doc, arginfo_tidy_get_opt_doc)
#endif
@@ -403,7 +401,6 @@ static const zend_function_entry tidy_functions[] = {
PHP_FE(tidy_get_head, arginfo_tidy_get_head)
PHP_FE(tidy_get_html, arginfo_tidy_get_html)
PHP_FE(tidy_get_body, arginfo_tidy_get_body)
- PHP_FE(ob_tidyhandler, arginfo_ob_tidyhandler)
{NULL, NULL, NULL}
};
@@ -502,7 +499,7 @@ static int _php_tidy_set_tidy_opt(TidyDoc doc, char *optname, zval *value TSRMLS
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown Tidy Configuration Option '%s'", optname);
return FAILURE;
}
-
+
if (tidyOptIsReadOnly(opt)) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Attempting to set read-only option '%s'", optname);
return FAILURE;
@@ -548,7 +545,7 @@ static int _php_tidy_set_tidy_opt(TidyDoc doc, char *optname, zval *value TSRMLS
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to determine type of configuration option");
break;
- }
+ }
return FAILURE;
}
@@ -565,7 +562,7 @@ static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_fil
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zsb", &arg1, &arg1_len, &config, &enc, &enc_len, &use_include_path) == FAILURE) {
RETURN_FALSE;
}
-
+
if (is_file) {
if (!(data = php_tidy_file_to_mem(arg1, use_include_path, &data_len TSRMLS_CC))) {
RETURN_FALSE;
@@ -578,35 +575,35 @@ static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_fil
doc = tidyCreate();
errbuf = emalloc(sizeof(TidyBuffer));
tidyBufInit(errbuf);
-
+
if (tidySetErrorBuffer(doc, errbuf) != 0) {
tidyBufFree(errbuf);
efree(errbuf);
tidyRelease(doc);
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not set Tidy error buffer");
}
-
+
tidyOptSetBool(doc, TidyForceOutput, yes);
tidyOptSetBool(doc, TidyMark, no);
-
+
TIDY_SET_DEFAULT_CONFIG(doc);
-
+
if (config) {
TIDY_APPLY_CONFIG_ZVAL(doc, config);
}
- if(enc_len) {
+ if (enc_len) {
if (tidySetCharEncoding(doc, enc) < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not set encoding '%s'", enc);
RETVAL_FALSE;
}
}
-
+
if (data) {
TidyBuffer buf;
tidyBufInit(&buf);
- tidyBufAppend(&buf, data, data_len);
+ tidyBufAttach(&buf, (byte *) data, data_len);
if (tidyParseBuffer(doc, &buf) < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errbuf->bp);
@@ -617,20 +614,19 @@ static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_fil
tidyBufInit(&output);
tidySaveBuffer (doc, &output);
- RETVAL_STRINGL((char*)output.bp, output.size ? output.size-1 : 0, 1);
+ FIX_BUFFER(&output);
+ RETVAL_STRINGL((char *) output.bp, output.size ? output.size-1 : 0, 1);
tidyBufFree(&output);
} else {
RETVAL_FALSE;
}
}
-
- tidyBufFree(&buf);
}
if (is_file) {
efree(data);
}
-
+
tidyBufFree(errbuf);
efree(errbuf);
tidyRelease(doc);
@@ -644,7 +640,7 @@ static char *php_tidy_file_to_mem(char *filename, zend_bool use_include_path, in
if (!(stream = php_stream_open_wrapper(filename, "rb", (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE, NULL))) {
return NULL;
}
- if ((*len = (int) php_stream_copy_to_mem(stream, &data, PHP_STREAM_COPY_ALL, 0)) == 0) {
+ if ((*len = (int) php_stream_copy_to_mem(stream, (void*) &data, PHP_STREAM_COPY_ALL, 0)) == 0) {
data = estrdup("");
*len = 0;
}
@@ -673,16 +669,15 @@ static void tidy_object_free_storage(void *object TSRMLS_DC)
efree(object);
}
-static void tidy_object_new(zend_class_entry *class_type, zend_object_handlers *handlers,
- zend_object_value *retval, tidy_obj_type objtype TSRMLS_DC)
+static void tidy_object_new(zend_class_entry *class_type, zend_object_handlers *handlers, zend_object_value *retval, tidy_obj_type objtype TSRMLS_DC)
{
PHPTidyObj *intern;
zval *tmp;
intern = emalloc(sizeof(PHPTidyObj));
memset(intern, 0, sizeof(PHPTidyObj));
+
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
-
zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
switch(objtype) {
@@ -690,11 +685,6 @@ static void tidy_object_new(zend_class_entry *class_type, zend_object_handlers *
break;
case is_doc:
- tidySetMallocCall(php_tidy_malloc);
- tidySetReallocCall(php_tidy_realloc);
- tidySetFreeCall(php_tidy_free);
- tidySetPanicCall(php_tidy_panic);
-
intern->ptdoc = emalloc(sizeof(PHPTidyDoc));
intern->ptdoc->doc = tidyCreate();
intern->ptdoc->ref_count = 1;
@@ -717,9 +707,6 @@ static void tidy_object_new(zend_class_entry *class_type, zend_object_handlers *
tidy_add_default_properties(intern, is_doc TSRMLS_CC);
break;
-
- default:
- break;
}
retval->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) tidy_object_free_storage, NULL TSRMLS_CC);
@@ -775,7 +762,7 @@ static int tidy_doc_cast_handler(zval *in, zval *out, int type TSRMLS_DC)
obj = (PHPTidyObj *)zend_object_store_get_object(in TSRMLS_CC);
tidyBufInit(&output);
tidySaveBuffer (obj->ptdoc->doc, &output);
- ZVAL_STRINGL(out, (char*)output.bp, output.size ? output.size-1 : 0, TRUE);
+ ZVAL_STRINGL(out, (char *) output.bp, output.size ? output.size-1 : 0, 1);
tidyBufFree(&output);
break;
@@ -809,8 +796,10 @@ static int tidy_node_cast_handler(zval *in, zval *out, int type TSRMLS_DC)
tidyBufInit(&buf);
if (obj->ptdoc) {
tidyNodeGetText(obj->ptdoc->doc, obj->node, &buf);
+ ZVAL_STRINGL(out, (char *) buf.bp, buf.size-1, 1);
+ } else {
+ ZVAL_EMPTY_STRING(out);
}
- ZVAL_STRINGL(out, (char*)buf.bp, buf.size ? buf.size-1 : 0, TRUE);
tidyBufFree(&buf);
break;
@@ -823,24 +812,23 @@ static int tidy_node_cast_handler(zval *in, zval *out, int type TSRMLS_DC)
static void tidy_doc_update_properties(PHPTidyObj *obj TSRMLS_DC)
{
-
TidyBuffer output;
zval *temp;
tidyBufInit(&output);
tidySaveBuffer (obj->ptdoc->doc, &output);
-
+
if (output.size) {
MAKE_STD_ZVAL(temp);
- ZVAL_STRINGL(temp, (char*)output.bp, output.size-1, TRUE);
+ ZVAL_STRINGL(temp, (char *) output.bp, output.size-1, 1);
zend_hash_update(obj->std.properties, "value", sizeof("value"), (void *)&temp, sizeof(zval *), NULL);
}
-
+
tidyBufFree(&output);
if (obj->ptdoc->errbuf->size) {
MAKE_STD_ZVAL(temp);
- ZVAL_STRINGL(temp, (char*)obj->ptdoc->errbuf->bp, obj->ptdoc->errbuf->size-1, TRUE);
+ ZVAL_STRINGL(temp, (char *) obj->ptdoc->errbuf->bp, obj->ptdoc->errbuf->size-1, TRUE);
zend_hash_update(obj->std.properties, "errorBuffer", sizeof("errorBuffer"), (void *)&temp, sizeof(zval *), NULL);
}
}
@@ -859,7 +847,7 @@ static void tidy_add_default_properties(PHPTidyObj *obj, tidy_obj_type type TSRM
case is_node:
tidyBufInit(&buf);
tidyNodeGetText(obj->ptdoc->doc, obj->node, &buf);
- ADD_PROPERTY_STRINGL(obj->std.properties, value, buf.bp, buf.size-1);
+ ADD_PROPERTY_STRINGL(obj->std.properties, value, buf.bp, buf.size ? buf.size-1 : 0);
tidyBufFree(&buf);
ADD_PROPERTY_STRING(obj->std.properties, name, tidyNodeGetName(obj->node));
@@ -874,7 +862,7 @@ static void tidy_add_default_properties(PHPTidyObj *obj, tidy_obj_type type TSRM
case TidyNode_Text:
case TidyNode_Comment:
break;
-
+
default:
ADD_PROPERTY_LONG(obj->std.properties, id, tidyNodeGetId(obj->node));
}
@@ -1000,49 +988,55 @@ static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetyp
static int _php_tidy_apply_config_array(TidyDoc doc, HashTable *ht_options TSRMLS_DC)
{
- char *opt_name = NULL;
+ char *opt_name;
zval **opt_val;
ulong opt_indx;
-
+ uint opt_name_len;
+ zend_bool clear_str;
+
for (zend_hash_internal_pointer_reset(ht_options);
- zend_hash_get_current_data(ht_options, (void **)&opt_val) == SUCCESS;
+ zend_hash_get_current_data(ht_options, (void *) &opt_val) == SUCCESS;
zend_hash_move_forward(ht_options)) {
-
- if(zend_hash_get_current_key(ht_options, &opt_name, &opt_indx, FALSE) == FAILURE) {
+
+ switch (zend_hash_get_current_key_ex(ht_options, &opt_name, &opt_name_len, &opt_indx, FALSE, NULL)) {
+ case HASH_KEY_IS_STRING:
+ clear_str = 0;
+ break;
+
+ case HASH_KEY_IS_LONG:
+ continue; /* ignore numeric keys */
+
+ default:
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not retrieve key from option array");
return FAILURE;
}
- if(opt_name) {
- _php_tidy_set_tidy_opt(doc, opt_name, *opt_val TSRMLS_CC);
- opt_name = NULL;
+ _php_tidy_set_tidy_opt(doc, opt_name, *opt_val TSRMLS_CC);
+ if (clear_str) {
+ efree(opt_name);
}
-
}
-
+
return SUCCESS;
}
static int php_tidy_parse_string(PHPTidyObj *obj, char *string, int len, char *enc TSRMLS_DC)
{
TidyBuffer buf;
-
- if(enc) {
+
+ if (enc) {
if (tidySetCharEncoding(obj->ptdoc->doc, enc) < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not set encoding '%s'", enc);
return FAILURE;
}
}
-
+
tidyBufInit(&buf);
- tidyBufAppend(&buf, string, len);
+ tidyBufAttach(&buf, (byte *) string, len);
if (tidyParseBuffer(obj->ptdoc->doc, &buf) < 0) {
- tidyBufFree(&buf);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", obj->ptdoc->errbuf->bp);
return FAILURE;
-
}
- tidyBufFree(&buf);
tidy_doc_update_properties(obj TSRMLS_CC);
return SUCCESS;
@@ -1050,6 +1044,11 @@ static int php_tidy_parse_string(PHPTidyObj *obj, char *string, int len, char *e
static PHP_MINIT_FUNCTION(tidy)
{
+ tidySetMallocCall(php_tidy_malloc);
+ tidySetReallocCall(php_tidy_realloc);
+ tidySetFreeCall(php_tidy_free);
+ tidySetPanicCall(php_tidy_panic);
+
REGISTER_INI_ENTRIES();
REGISTER_TIDY_CLASS(tidy, doc, NULL, 0);
REGISTER_TIDY_CLASS(tidyNode, node, NULL, ZEND_ACC_FINAL_CLASS);
@@ -1060,16 +1059,19 @@ static PHP_MINIT_FUNCTION(tidy)
_php_tidy_register_tags(INIT_FUNC_ARGS_PASSTHRU);
_php_tidy_register_nodetypes(INIT_FUNC_ARGS_PASSTHRU);
+ PHP_OUTPUT_ALIAS_REGISTER("ob_tidyhandler", php_tidy_output_handler_init);
+
return SUCCESS;
}
static PHP_RINIT_FUNCTION(tidy)
{
- if (INI_BOOL("tidy.clean_output") == TRUE) {
- if (php_start_ob_buffer_named("ob_tidyhandler", 0, 1 TSRMLS_CC) == FAILURE) {
- zend_error(E_NOTICE, "Failure installing Tidy output buffering.");
- }
- }
+ zval *name;
+
+ MAKE_STD_ZVAL(name);
+ ZVAL_STRING(name, "ob_tidyhandler", 1);
+ php_tidy_clean_output_start(name TSRMLS_CC);
+ zval_ptr_dtor(&name);
return SUCCESS;
}
@@ -1091,97 +1093,148 @@ static PHP_MINFO_FUNCTION(tidy)
DISPLAY_INI_ENTRIES();
}
-static PHP_FUNCTION(ob_tidyhandler)
+static PHP_INI_MH(php_tidy_set_clean_output)
{
- char *input;
- int input_len;
- long mode;
- TidyBuffer errbuf;
- TidyDoc doc;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &input, &input_len, &mode) == FAILURE) {
- return;
+ int status;
+ zend_bool value;
+
+ if (new_value_length==2 && strcasecmp("on", new_value)==0) {
+ value = (zend_bool) 1;
+ } else if (new_value_length==3 && strcasecmp("yes", new_value)==0) {
+ value = (zend_bool) 1;
+ } else if (new_value_length==4 && strcasecmp("true", new_value)==0) {
+ value = (zend_bool) 1;
+ } else {
+ value = (zend_bool) atoi(new_value);
}
- doc = tidyCreate();
- tidyBufInit(&errbuf);
+ if (stage == PHP_INI_STAGE_RUNTIME) {
+ status = php_output_get_status(TSRMLS_C);
- tidyOptSetBool(doc, TidyForceOutput, yes);
- tidyOptSetBool(doc, TidyMark, no);
+ if (value && (status & PHP_OUTPUT_WRITTEN)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot enable tidy.clean_output - there has already been output");
+ return FAILURE;
+ }
+ if (status & PHP_OUTPUT_SENT) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot change tidy.clean_output - headers already sent");
+ return FAILURE;
+ }
+ }
- if (tidySetErrorBuffer(doc, &errbuf) != 0) {
- tidyRelease(doc);
- tidyBufFree(&errbuf);
+ status = OnUpdateBool(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not set Tidy error buffer");
+ if (stage == PHP_INI_STAGE_RUNTIME && value) {
+ zval *tmp;
+
+ MAKE_STD_ZVAL(tmp);
+ ZVAL_STRING(tmp, "ob_tidyhandler", 1);
+ if (!php_output_handler_started(tmp TSRMLS_CC)) {
+ php_tidy_clean_output_start(tmp TSRMLS_CC);
+ }
+ zval_ptr_dtor(&tmp);
}
- TIDY_SET_DEFAULT_CONFIG(doc);
+ return status;
+}
- if (input_len > 1) {
- TidyBuffer buf;
-
- tidyBufInit(&buf);
- tidyBufAppend(&buf, input, input_len);
-
- if (tidyParseBuffer(doc, &buf) < 0 || tidyCleanAndRepair(doc) < 0) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errbuf.bp);
- RETVAL_NULL();
- } else {
- TidyBuffer output;
- tidyBufInit(&output);
+/*
+ * NOTE: tidy does not support iterative/cumulative parsing, so chunk-sized output handler is not possible
+ */
- tidySaveBuffer(doc, &output);
- RETVAL_STRINGL((char*)output.bp, output.size ? output.size-1 : 0, 1);
+static void php_tidy_clean_output_start(zval *name TSRMLS_DC)
+{
+ php_output_handler *h;
- tidyBufFree(&output);
+ if (TG(clean_output) && (h = php_tidy_output_handler_init(name, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC))) {
+ php_output_handler_start(h TSRMLS_CC);
+ }
+}
+
+static php_output_handler *php_tidy_output_handler_init(zval *handler_name, size_t chunk_size, int flags TSRMLS_DC)
+{
+ if (chunk_size) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot use a chunk size for ob_tidyhandler");
+ return NULL;
+ }
+ if (!TG(clean_output)) {
+ TG(clean_output) = 1;
+ }
+ return php_output_handler_create_internal(handler_name, php_tidy_output_handler, chunk_size, flags TSRMLS_CC);
+}
+
+static int php_tidy_output_handler(void **nothing, php_output_context *output_context)
+{
+ int status = FAILURE;
+ TidyDoc doc;
+ TidyBuffer inbuf, outbuf, errbuf;
+ PHP_OUTPUT_TSRMLS(output_context);
+
+ if (TG(clean_output) && (output_context->op & PHP_OUTPUT_HANDLER_START) && (output_context->op & PHP_OUTPUT_HANDLER_FINAL)) {
+ doc = tidyCreate();
+ tidyBufInit(&errbuf);
+
+ if (0 == tidySetErrorBuffer(doc, &errbuf)) {
+ tidyOptSetBool(doc, TidyForceOutput, yes);
+ tidyOptSetBool(doc, TidyMark, no);
+
+ TIDY_SET_DEFAULT_CONFIG(doc);
+
+ tidyBufInit(&inbuf);
+ tidyBufAttach(&inbuf, (byte *) output_context->in.data, output_context->in.used);
+
+ if (0 <= tidyParseBuffer(doc, &inbuf) && 0 <= tidyCleanAndRepair(doc)) {
+ tidyBufInit(&outbuf);
+ tidySaveBuffer(doc, &outbuf);
+ FIX_BUFFER(&outbuf);
+ output_context->out.data = (char *) outbuf.bp;
+ output_context->out.used = outbuf.size ? outbuf.size-1 : 0;
+ output_context->out.free = 1;
+ status = SUCCESS;
+ }
}
-
- tidyBufFree(&buf);
- } else {
- RETVAL_NULL();
+
+ tidyRelease(doc);
+ tidyBufFree(&errbuf);
}
- tidyRelease(doc);
- tidyBufFree(&errbuf);
+ return status;
}
-/* {{{ proto bool tidy_parse_string(string input [, mixed config_options [, string encoding]])
+/* {{{ proto bool tidy_parse_string(string input [, mixed config_options [, string encoding]]) U
Parse a document stored in a string */
static PHP_FUNCTION(tidy_parse_string)
{
- char *input, *enc = NULL;
- int input_len, enc_len = 0;
+ char *input;
+ char *enc = NULL;
+ int input_len, enc_len;
zval **options = NULL;
-
PHPTidyObj *obj;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zs", &input, &input_len, &options, &enc, &enc_len) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zs&", &input, &input_len, &options, &enc, &enc_len) == FAILURE) {
RETURN_FALSE;
}
tidy_instanciate(tidy_ce_doc, return_value TSRMLS_CC);
obj = (PHPTidyObj *) zend_object_store_get_object(return_value TSRMLS_CC);
-
+
TIDY_APPLY_CONFIG_ZVAL(obj->ptdoc->doc, options);
-
- if(php_tidy_parse_string(obj, input, input_len, enc TSRMLS_CC) == FAILURE) {
+
+ if (php_tidy_parse_string(obj, input, input_len, enc TSRMLS_CC) == FAILURE) {
zval_dtor(return_value);
INIT_ZVAL(*return_value);
- RETURN_FALSE;
+ RETVAL_FALSE;
}
-
}
/* }}} */
-/* {{{ proto string tidy_get_error_buffer([boolean detailed])
+/* {{{ proto string tidy_get_error_buffer()
Return warnings and errors which occured parsing the specified document*/
static PHP_FUNCTION(tidy_get_error_buffer)
{
TIDY_FETCH_OBJECT;
if (obj->ptdoc->errbuf && obj->ptdoc->errbuf->bp) {
- RETURN_STRINGL((char*)obj->ptdoc->errbuf->bp, obj->ptdoc->errbuf->size-1, 1);
+ RETURN_STRINGL((char *) obj->ptdoc->errbuf->bp, obj->ptdoc->errbuf->size-1, 1);
} else {
RETURN_FALSE;
}
@@ -1197,9 +1250,8 @@ static PHP_FUNCTION(tidy_get_output)
tidyBufInit(&output);
tidySaveBuffer(obj->ptdoc->doc, &output);
-
- RETVAL_STRINGL((char*)output.bp, output.size ? output.size-1 : 0, 1);
-
+ FIX_BUFFER(&output);
+ RETVAL_STRINGL((char *) output.bp, output.size ? output.size-1 : 0, 1);
tidyBufFree(&output);
}
/* }}} */
@@ -1213,11 +1265,10 @@ static PHP_FUNCTION(tidy_parse_file)
zend_bool use_include_path = 0;
char *contents;
zval **options = NULL;
-
+
PHPTidyObj *obj;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zsb", &inputfile, &input_len,
- &options, &enc, &enc_len, &use_include_path) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zsb", &inputfile, &input_len, &options, &enc, &enc_len, &use_include_path) == FAILURE) {
RETURN_FALSE;
}
@@ -1225,13 +1276,13 @@ static PHP_FUNCTION(tidy_parse_file)
obj = (PHPTidyObj *) zend_object_store_get_object(return_value TSRMLS_CC);
if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path, &contents_len TSRMLS_CC))) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot Load '%s' into memory %s", inputfile, (use_include_path) ? "(Using include path)" : "");
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot Load '%s' into memory%s", inputfile, (use_include_path) ? " (Using include path)" : "");
RETURN_FALSE;
}
TIDY_APPLY_CONFIG_ZVAL(obj->ptdoc->doc, options);
- if(php_tidy_parse_string(obj, contents, contents_len, enc TSRMLS_CC) == FAILURE) {
+ if (php_tidy_parse_string(obj, contents, contents_len, enc TSRMLS_CC) == FAILURE) {
zval_dtor(return_value);
INIT_ZVAL(*return_value);
RETVAL_FALSE;
@@ -1306,18 +1357,18 @@ static PHP_FUNCTION(tidy_get_release)
static PHP_FUNCTION(tidy_get_opt_doc)
{
PHPTidyObj *obj;
- char *optname, *optval;
+ char *optval, *optname;
int optname_len;
TidyOption opt;
TIDY_SET_CONTEXT;
if (object) {
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &optname, &optname_len) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&", &optname, &optname_len) == FAILURE) {
RETURN_FALSE;
}
} else {
- if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, NULL, "Os", &object, tidy_ce_doc, &optname, &optname_len) == FAILURE) {
+ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, NULL, "Os&", &object, tidy_ce_doc, &optname, &optname_len) == FAILURE) {
RETURN_FALSE;
}
}
@@ -1342,7 +1393,7 @@ static PHP_FUNCTION(tidy_get_opt_doc)
/* {{{ proto array tidy_get_config()
- Get current Tidy configuarion */
+ Get current Tidy configuration */
static PHP_FUNCTION(tidy_get_config)
{
TidyIterator itOpt;
@@ -1363,7 +1414,7 @@ static PHP_FUNCTION(tidy_get_config)
opt_value = php_tidy_get_opt_val(obj->ptdoc, opt, &optt TSRMLS_CC);
switch (optt) {
case TidyString:
- add_assoc_string(return_value, opt_name, (char*)opt_value, 0);
+ add_assoc_string(return_value, opt_name, (char*)opt_value, 1);
break;
case TidyInteger:
@@ -1400,7 +1451,7 @@ static PHP_FUNCTION(tidy_get_html_ver)
}
/* }}} */
-/* {{{ proto boolean tidy_is_xhtml()
+/* {{{ proto bool tidy_is_xhtml()
Indicates if the document is a XHTML document. */
static PHP_FUNCTION(tidy_is_xhtml)
{
@@ -1410,7 +1461,7 @@ static PHP_FUNCTION(tidy_is_xhtml)
}
/* }}} */
-/* {{{ proto boolean tidy_is_xml()
+/* {{{ proto bool tidy_is_xml()
Indicates if the document is a generic (non HTML/XHTML) XML document. */
static PHP_FUNCTION(tidy_is_xml)
{
@@ -1463,22 +1514,22 @@ static PHP_FUNCTION(tidy_config_count)
/* {{{ proto mixed tidy_getopt(string option)
Returns the value of the specified configuration option for the tidy document. */
static PHP_FUNCTION(tidy_getopt)
-{
+{
PHPTidyObj *obj;
char *optname;
void *optval;
int optname_len;
TidyOption opt;
TidyOptionType optt;
-
+
TIDY_SET_CONTEXT;
if (object) {
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &optname, &optname_len) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&", &optname, &optname_len) == FAILURE) {
RETURN_FALSE;
}
} else {
- if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, NULL, "Os", &object, tidy_ce_doc, &optname, &optname_len) == FAILURE) {
+ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, NULL, "Os&", &object, tidy_ce_doc, &optname, &optname_len) == FAILURE) {
RETURN_FALSE;
}
}
@@ -1495,7 +1546,7 @@ static PHP_FUNCTION(tidy_getopt)
optval = php_tidy_get_opt_val(obj->ptdoc, opt, &optt TSRMLS_CC);
switch (optt) {
case TidyString:
- RETURN_STRING((char *)optval, 0);
+ RETURN_STRING((char *)optval, 1);
break;
case TidyInteger:
@@ -1526,20 +1577,19 @@ static TIDY_DOC_METHOD(__construct)
zend_bool use_include_path = 0;
char *contents;
zval **options = NULL;
-
+
PHPTidyObj *obj;
- TIDY_SET_CONTEXT;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sZsb", &inputfile, &input_len,
- &options, &enc, &enc_len, &use_include_path) == FAILURE) {
+ TIDY_SET_CONTEXT;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sZsb", &inputfile, &input_len, &options, &enc, &enc_len, &use_include_path) == FAILURE) {
RETURN_FALSE;
}
-
+
obj = (PHPTidyObj *)zend_object_store_get_object(object TSRMLS_CC);
-
+
if (inputfile) {
if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path, &contents_len TSRMLS_CC))) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot Load '%s' into memory %s", inputfile, (use_include_path) ? "(Using include path)" : "");
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot Load '%s' into memory%s", inputfile, (use_include_path) ? " (Using include path)" : "");
return;
}
@@ -1564,19 +1614,18 @@ static TIDY_DOC_METHOD(parseFile)
obj = (PHPTidyObj *)zend_object_store_get_object(object TSRMLS_CC);
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zsb", &inputfile, &input_len,
- &options, &enc, &enc_len, &use_include_path) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zsb", &inputfile, &input_len, &options, &enc, &enc_len, &use_include_path) == FAILURE) {
RETURN_FALSE;
}
-
+
if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path, &contents_len TSRMLS_CC))) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot Load '%s' into memory %s", inputfile, (use_include_path) ? "(Using include path)" : "");
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot Load '%s' into memory%s", inputfile, (use_include_path) ? " (Using include path)" : "");
RETURN_FALSE;
}
TIDY_APPLY_CONFIG_ZVAL(obj->ptdoc->doc, options);
- if(php_tidy_parse_string(obj, contents, contents_len, enc TSRMLS_CC) == FAILURE) {
+ if (php_tidy_parse_string(obj, contents, contents_len, enc TSRMLS_CC) == FAILURE) {
RETVAL_FALSE;
} else {
RETVAL_TRUE;
@@ -1602,10 +1651,10 @@ static TIDY_DOC_METHOD(parseString)
TIDY_APPLY_CONFIG_ZVAL(obj->ptdoc->doc, options);
- if(php_tidy_parse_string(obj, input, input_len, enc TSRMLS_CC) == SUCCESS) {
+ if (php_tidy_parse_string(obj, input, input_len, enc TSRMLS_CC) == SUCCESS) {
RETURN_TRUE;
}
-
+
RETURN_FALSE;
}
@@ -1642,7 +1691,7 @@ static PHP_FUNCTION(tidy_get_body)
}
/* }}} */
-/* {{{ proto boolean tidyNode::hasChildren()
+/* {{{ proto bool tidyNode::hasChildren()
Returns true if this node has children */
static TIDY_NODE_METHOD(hasChildren)
{
@@ -1656,7 +1705,7 @@ static TIDY_NODE_METHOD(hasChildren)
}
/* }}} */
-/* {{{ proto boolean tidyNode::hasSiblings()
+/* {{{ proto bool tidyNode::hasSiblings()
Returns true if this node has siblings */
static TIDY_NODE_METHOD(hasSiblings)
{
@@ -1670,7 +1719,7 @@ static TIDY_NODE_METHOD(hasSiblings)
}
/* }}} */
-/* {{{ proto boolean tidyNode::isComment()
+/* {{{ proto bool tidyNode::isComment()
Returns true if this node represents a comment */
static TIDY_NODE_METHOD(isComment)
{
@@ -1684,7 +1733,7 @@ static TIDY_NODE_METHOD(isComment)
}
/* }}} */
-/* {{{ proto boolean tidyNode::isHtml()
+/* {{{ proto bool tidyNode::isHtml()
Returns true if this node is part of a HTML document */
static TIDY_NODE_METHOD(isHtml)
{
@@ -1698,7 +1747,7 @@ static TIDY_NODE_METHOD(isHtml)
}
/* }}} */
-/* {{{ proto boolean tidyNode::isText()
+/* {{{ proto bool tidyNode::isText()
Returns true if this node represents text (no markup) */
static TIDY_NODE_METHOD(isText)
{
@@ -1712,7 +1761,7 @@ static TIDY_NODE_METHOD(isText)
}
/* }}} */
-/* {{{ proto boolean tidyNode::isJste()
+/* {{{ proto bool tidyNode::isJste()
Returns true if this node is JSTE */
static TIDY_NODE_METHOD(isJste)
{
@@ -1726,7 +1775,7 @@ static TIDY_NODE_METHOD(isJste)
}
/* }}} */
-/* {{{ proto boolean tidyNode::isAsp()
+/* {{{ proto bool tidyNode::isAsp()
Returns true if this node is ASP */
static TIDY_NODE_METHOD(isAsp)
{
@@ -1740,7 +1789,7 @@ static TIDY_NODE_METHOD(isAsp)
}
/* }}} */
-/* {{{ proto boolean tidyNode::isPhp()
+/* {{{ proto bool tidyNode::isPhp()
Returns true if this node is PHP */
static TIDY_NODE_METHOD(isPhp)
{
@@ -1763,7 +1812,7 @@ static TIDY_NODE_METHOD(getParent)
TIDY_FETCH_ONLY_OBJECT;
parent_node = tidyGetParent(obj->node);
- if(parent_node) {
+ if (parent_node) {
tidy_instanciate(tidy_ce_node, return_value TSRMLS_CC);
newobj = (PHPTidyObj *) zend_object_store_get_object(return_value TSRMLS_CC);
newobj->node = parent_node;
@@ -1777,12 +1826,13 @@ static TIDY_NODE_METHOD(getParent)
}
/* }}} */
+
/* {{{ proto void tidyNode::__construct()
- __constructor for tidyNode. */
+ __constructor for tidyNode. */
static TIDY_NODE_METHOD(__construct)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "You should not create a tidyNode manually");
-}
+}
/* }}} */
static void _php_tidy_register_nodetypes(INIT_FUNC_ARGS)