cvs: pecl /tidy php_tidy.h tidy.c
| From: | Ilia Alshanetsky | Date: | Sun, 21 Sep 2003 19:22:20 +0000 |
| Subject: | cvs: pecl /tidy php_tidy.h tidy.c | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-465@lists.php.net to get a copy of this message | ||
iliaa Sun Sep 21 15:22:20 2003 EDT
Modified files:
/pecl/tidy tidy.c php_tidy.h
Log:
Added tidy_repair_string() & tidy_repair_file() that allow for quick repair
of files & strings.
Modified tidy_parse_file() to accept include_path parameter.
Fixed a few bugs that may have allowed configurations directives loaded via
a file to carry over across requests.
Index: pecl/tidy/tidy.c
diff -u pecl/tidy/tidy.c:1.9 pecl/tidy/tidy.c:1.10
--- pecl/tidy/tidy.c:1.9 Sat Sep 20 15:45:32 2003
+++ pecl/tidy/tidy.c Sun Sep 21 15:22:19 2003
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: tidy.c,v 1.9 2003/09/20 19:45:32 iliaa Exp $ */
+/* $Id: tidy.c,v 1.10 2003/09/21 19:22:19 iliaa Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -92,6 +92,8 @@
PHP_FE(tidy_get_output, NULL)
PHP_FE(tidy_get_error_buffer, NULL)
PHP_FE(tidy_clean_repair, NULL)
+ PHP_FE(tidy_repair_string, NULL)
+ PHP_FE(tidy_repair_file, NULL)
PHP_FE(tidy_diagnose, NULL)
PHP_FE(tidy_get_release, NULL)
PHP_FE(tidy_get_config, NULL)
@@ -176,7 +178,7 @@
g->used = 0;
}
-static void *_php_tidy_get_opt_val(TidyOption opt, TidyOptionType *type)
+static void *php_tidy_get_opt_val(TidyOption opt, TidyOptionType *type)
{
*type = tidyOptGetType(opt);
@@ -204,6 +206,75 @@
return NULL;
}
+static char *php_tidy_file_to_mem(char *filename, zend_bool use_include_path)
+{
+ php_stream *stream;
+ int len;
+ char *data = NULL;
+
+ if (!(stream = php_stream_open_wrapper(filename, "rb", (use_include_path ? USE_PATH : 0)
| ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL))) {
+ return NULL;
+ }
+ if ((len = php_stream_copy_to_mem(stream, &data, PHP_STREAM_COPY_ALL, 0)) > 0) {
+ /* noop */
+ } else if (len == 0) {
+ data = estrdup("");
+ }
+ php_stream_close(stream);
+
+ return data;
+}
+
+static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_file)
+{
+ char *data=NULL, *cfg_file=NULL, *arg1;
+ int cfg_file_len, arg1_len;
+ zend_bool use_include_path = 0;
+
+ if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sb", &arg1, &arg1_len,
&cfg_file, &cfg_file_len, &use_include_path) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ if (is_file) {
+ if (!(data = php_tidy_file_to_mem(arg1, use_include_path))) {
+ RETURN_FALSE;
+ }
+ } else {
+ data = arg1;
+ }
+
+ if (cfg_file && cfg_file[0]) {
+ TIDY_SAFE_MODE_CHECK(cfg_file);
+ if(tidyLoadConfig(TG(tdoc)->doc, cfg_file) < 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not load configuration file
'%s'", cfg_file);
+ RETVAL_FALSE;
+ }
+ TG(used) = 1;
+ }
+
+ if (data) {
+ if(tidyParseString(TG(tdoc)->doc, data) < 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "[Tidy error] %s",
TG(tdoc)->errbuf->bp);
+ RETVAL_FALSE;
+ } else {
+ TG(tdoc)->parsed = TRUE;
+ if (tidyCleanAndRepair(TG(tdoc)->doc) >= 0) {
+ TidyBuffer output = {0};
+
+ tidySaveBuffer (TG(tdoc)->doc, &output);
+ RETVAL_STRING(output.bp, 1);
+ tidyBufFree(&output);
+ } else {
+ RETVAL_FALSE;
+ }
+ }
+ }
+
+ if (is_file) {
+ efree(data);
+ }
+}
+
#ifdef ZEND_ENGINE_2
static inline PHPTidyObj *php_tidy_fetch_object(zval *object TSRMLS_DC)
@@ -412,7 +483,7 @@
while (itOpt) {
TidyOption opt = tidyGetNextOption(TG(tdoc)->doc, &itOpt);
- opt_value = _php_tidy_get_opt_val(opt, &optt);
+ opt_value = php_tidy_get_opt_val(opt, &optt);
switch (optt) {
case TidyString:
php_info_print_table_row(2, (char *)tidyOptGetName(opt), (char*)opt_value);
@@ -494,36 +565,32 @@
}
/* }}} */
-/* {{{ proto boolean tidy_parse_file(string file)
+/* {{{ proto boolean tidy_parse_file(string file [, bool use_include_path])
Parse markup in file or URI */
PHP_FUNCTION(tidy_parse_file)
{
char *inputfile;
int input_len;
- php_stream *stream;
+ zend_bool use_include_path = 0;
char *contents;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &inputfile, &input_len)
== FAILURE) {
RETURN_FALSE;
}
- if (!(stream = php_stream_open_wrapper(inputfile, "rb", ENFORCE_SAFE_MODE |
REPORT_ERRORS, NULL))) {
+ if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path))) {
RETURN_FALSE;
}
-
- if (php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0) > 0) {
- if(tidyParseString(TG(tdoc)->doc, contents) < 0) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "[Tidy error] %s",
TG(tdoc)->errbuf->bp);
- efree(contents);
- php_stream_close(stream);
- RETURN_FALSE;
- }
+
+ if(tidyParseString(TG(tdoc)->doc, contents) < 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "[Tidy error] %s",
TG(tdoc)->errbuf->bp);
+ RETVAL_FALSE;
+ } else {
TG(tdoc)->parsed = TRUE;
- efree(contents);
+ RETVAL_TRUE;
}
- php_stream_close(stream);
- RETURN_TRUE;
+ efree(contents);
}
/* }}} */
@@ -545,6 +612,22 @@
}
/* }}} */
+/* {{{ proto boolean tidy_repair_string(string data [, string config_file])
+ Repair a string using an optionally provided configuration file */
+PHP_FUNCTION(tidy_repair_string)
+{
+ php_tidy_quick_repair(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
+}
+/* }}} */
+
+/* {{{ proto boolean tidy_repair_file(string filename [, string config_file [, bool
use_include_path]])
+ Repair a file using an optionally provided configuration file */
+PHP_FUNCTION(tidy_repair_file)
+{
+ php_tidy_quick_repair(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+}
+/* }}} */
+
/* {{{ proto boolean tidy_diagnose()
Run configured diagnostics on parsed and repaired markup. */
PHP_FUNCTION(tidy_diagnose)
@@ -561,7 +644,6 @@
RETURN_FALSE;
}
-
/* }}} */
/* {{{ proto string tidy_get_release()
@@ -607,7 +689,7 @@
TidyOption opt = tidyGetNextOption(TG(tdoc)->doc, &itOpt);
opt_name = (char *)tidyOptGetName(opt);
- opt_value = _php_tidy_get_opt_val(opt, &optt);
+ opt_value = php_tidy_get_opt_val(opt, &optt);
switch (optt) {
case TidyString:
add_assoc_string(return_value, opt_name, (char*)opt_value, 0);
@@ -756,6 +838,7 @@
RETURN_FALSE;
}
+ TG(used) = 1;
RETURN_TRUE;
}
/* }}} */
@@ -778,6 +861,7 @@
RETURN_FALSE;
}
+ TG(used) = 1;
RETURN_TRUE;
}
/* }}} */
@@ -800,6 +884,7 @@
RETURN_FALSE;
}
+ TG(used) = 1;
RETURN_TRUE;
}
/* }}} */
@@ -898,7 +983,7 @@
RETURN_FALSE;
}
- optval = _php_tidy_get_opt_val(opt, &optt);
+ optval = php_tidy_get_opt_val(opt, &optt);
switch (optt) {
case TidyString:
RETVAL_STRING((char *)optval, 0);
Index: pecl/tidy/php_tidy.h
diff -u pecl/tidy/php_tidy.h:1.5 pecl/tidy/php_tidy.h:1.6
--- pecl/tidy/php_tidy.h:1.5 Sat Sep 20 15:45:32 2003
+++ pecl/tidy/php_tidy.h Sun Sep 21 15:22:19 2003
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_tidy.h,v 1.5 2003/09/20 19:45:32 iliaa Exp $ */
+/* $Id: php_tidy.h,v 1.6 2003/09/21 19:22:19 iliaa Exp $ */
#ifndef PHP_TIDY_H
#define PHP_TIDY_H
@@ -95,6 +95,8 @@
PHP_FUNCTION(tidy_parse_string);
PHP_FUNCTION(tidy_parse_file);
PHP_FUNCTION(tidy_clean_repair);
+PHP_FUNCTION(tidy_repair_string);
+PHP_FUNCTION(tidy_repair_file);
PHP_FUNCTION(tidy_diagnose);
PHP_FUNCTION(tidy_get_output);
PHP_FUNCTION(tidy_get_error_buffer);