cvs: php4 /ext/standard html.c html.h

From: Date: Tue, 12 Sep 2000 17:22:37 +0000
Subject: cvs: php4 /ext/standard html.c html.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-1731@lists.php.net to get a copy of this message
rasmus Tue Sep 12 10:22:37 2000 EDT Modified files: /php4/ext/standard html.c html.h Log: Clean up htmlspecialchars/htmlentities inconsistencies. @Clean up htmlspecialchars/htmlentities inconsistencies. (Rasmus) Index: php4/ext/standard/html.c diff -u php4/ext/standard/html.c:1.19 php4/ext/standard/html.c:1.20 --- php4/ext/standard/html.c:1.19 Wed Aug 30 08:27:23 2000 +++ php4/ext/standard/html.c Tue Sep 12 10:22:37 2000 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: html.c,v 1.19 2000/08/30 15:27:23 cmv Exp $ */ +/* $Id: html.c,v 1.20 2000/09/12 17:22:37 rasmus Exp $ */ #include "php.h" #include "reg.h" @@ -45,7 +45,11 @@ "uuml","yacute","thorn","yuml" }; -PHPAPI char *php_escape_html_entities(unsigned char *old, int oldlen, int *newlen, int all) +#define ENT_COMPAT 1 +#define ENT_QUOTES 2 +#define ENT_NOQUOTES 4 + +PHPAPI char *php_escape_html_entities(unsigned char *old, int oldlen, int *newlen, int all, int quote_style) { int i, maxlen, len; char *new; @@ -63,9 +67,12 @@ if (38 == *old) { memcpy (new + len, "&amp;", 5); len += 5; - } else if (34 == *old) { + } else if (34 == *old && !(quote_style&ENT_NOQUOTES)) { memcpy (new + len, "&quot;", 6); len += 6; + } else if (39 == *old && (quote_style&ENT_QUOTES)) { + memcpy (new + len, "&#039;", 6); + len += 6; } else if (60 == *old) { memcpy (new + len, "&lt;", 4); len += 4; @@ -90,17 +97,22 @@ static void php_html_entities(INTERNAL_FUNCTION_PARAMETERS, int all) { - zval **arg; - int len; + zval **arg, **quotes; + int len, quote_style = ENT_COMPAT; + int ac = ZEND_NUM_ARGS(); char *new; - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) { - WRONG_PARAM_COUNT; + if (ac < 1 || ac > 2 || zend_get_parameters_ex(ac, &arg, &quotes) == FAILURE) { + WRONG_PARAM_COUNT; } convert_to_string_ex(arg); + if(ac==2) { + convert_to_long_ex(quotes); + quote_style = (*quotes)->value.lval; + } - new = php_escape_html_entities((*arg)->value.str.val, (*arg)->value.str.len, &len, all); + new = php_escape_html_entities((*arg)->value.str.val, (*arg)->value.str.len, &len, all, quote_style); RETVAL_STRINGL(new,len,0); } @@ -111,6 +123,9 @@ { REGISTER_LONG_CONSTANT("HTML_SPECIALCHARS", HTML_SPECIALCHARS, CONST_PERSISTENT|CONST_CS); REGISTER_LONG_CONSTANT("HTML_ENTITIES", HTML_ENTITIES, CONST_PERSISTENT|CONST_CS); + REGISTER_LONG_CONSTANT("ENT_COMPAT", ENT_COMPAT, CONST_PERSISTENT|CONST_CS); + REGISTER_LONG_CONSTANT("ENT_QUOTES", ENT_QUOTES, CONST_PERSISTENT|CONST_CS); + REGISTER_LONG_CONSTANT("ENT_NOQUOTES", ENT_NOQUOTES, CONST_PERSISTENT|CONST_CS); } /* {{{ proto string htmlspecialchars(string string) @@ -129,23 +144,27 @@ } /* }}} */ -/* {{{ proto array get_html_translation_table([int whichone]) +/* {{{ proto array get_html_translation_table([int table [, int quote_style]]) Returns the internal translation table used by htmlspecialchars and htmlentities */ PHP_FUNCTION(get_html_translation_table) { - zval **whichone; - int which = 0; + zval **whichone, **quotes; + int which = 0, quote_style = ENT_COMPAT; int ac = ZEND_NUM_ARGS(); int inx; char ind[ 2 ]; - if (ac < 0 || ac > 1 || zend_get_parameters_ex(ac, &whichone) == FAILURE) { + if (ac < 0 || ac > 2 || zend_get_parameters_ex(ac, &whichone, &quotes) == FAILURE) { WRONG_PARAM_COUNT; } - if (ac == 1) { + if (ac > 0) { convert_to_long_ex(whichone); which = (*whichone)->value.lval; + } + if (ac == 2) { + convert_to_long_ex(quotes); + quote_style = (*quotes)->value.lval; } array_init(return_value); @@ -164,7 +183,8 @@ case HTML_SPECIALCHARS: ind[0]=38; add_assoc_string(return_value,ind,"&amp;",1); - ind[0]=34; add_assoc_string(return_value,ind,"&quot;",1); + if(quote_style&ENT_QUOTES) ind[0]=39; add_assoc_string(return_value,ind,"&#039;",1); + if(!(quote_style&ENT_NOQUOTES)) ind[0]=34; add_assoc_string(return_value,ind,"&quot;",1); ind[0]=60; add_assoc_string(return_value,ind,"&lt;",1); ind[0]=62; add_assoc_string(return_value,ind,"&gt;",1); break; Index: php4/ext/standard/html.h diff -u php4/ext/standard/html.h:1.6 php4/ext/standard/html.h:1.7 --- php4/ext/standard/html.h:1.6 Sun Jul 23 18:39:49 2000 +++ php4/ext/standard/html.h Tue Sep 12 10:22:37 2000 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: html.h,v 1.6 2000/07/24 01:39:49 david Exp $ */ +/* $Id: html.h,v 1.7 2000/09/12 17:22:37 rasmus Exp $ */ #ifndef HTML_H #define HTML_H @@ -27,6 +27,6 @@ PHP_FUNCTION(htmlentities); PHP_FUNCTION(get_html_translation_table); -PHPAPI char *php_escape_html_entities(unsigned char *old, int oldlen, int *newlen, int all); +PHPAPI char *php_escape_html_entities(unsigned char *old, int oldlen, int *newlen, int all, int quote_style); #endif /* HTML_H */

« previous php.cvs (#1731) next »