[PHP-DEV] CVS update: php3/functions From: rasmus (php-dev <email protected>)
Date: 03/30/99

Date: Tuesday March 30, 1999 @ 7:43
Author: rasmus

Update of /repository/php3/functions
In directory asf:/home/rasmus/php3/functions

Modified Files:
        basic_functions.c file.c php3_string.h string.c
Log Message:
Yank the tag stripping state machine out of fgetss and move it into
string.c as a standalone API function so it can now be called by the
strip_tags() function to allow it to work directly on a string as opposed
to only from data read from a file.

Index: php3/functions/basic_functions.c
diff -c php3/functions/basic_functions.c:1.258 php3/functions/basic_functions.c:1.259
*** php3/functions/basic_functions.c:1.258 Thu Feb 11 01:23:40 1999
--- php3/functions/basic_functions.c Tue Mar 30 07:43:29 1999
***************
*** 28,34 ****
     +----------------------------------------------------------------------+
   */
  
! /* $Id: basic_functions.c,v 1.258 1999/02/11 06:23:40 andrey Exp $ */
  #ifdef THREAD_SAFE
  #include "tls.h"
  #endif
--- 28,34 ----
     +----------------------------------------------------------------------+
   */
  
! /* $Id: basic_functions.c,v 1.259 1999/03/30 12:43:29 rasmus Exp $ */
  #ifdef THREAD_SAFE
  #include "tls.h"
  #endif
***************
*** 149,154 ****
--- 149,155 ----
          {"chop", php3_chop, NULL},
          {"str_replace", php3_str_replace, NULL},
          {"chunk_split", php3_chunk_split, NULL},
+ {"strip_tags", php3_strip_tags, NULL},
          {"trim", php3_trim, NULL},
          {"ltrim", php3_ltrim, NULL},
          {"rtrim", php3_chop, NULL},
Index: php3/functions/file.c
diff -c php3/functions/file.c:1.202 php3/functions/file.c:1.203
*** php3/functions/file.c:1.202 Mon Mar 15 11:02:50 1999
--- php3/functions/file.c Tue Mar 30 07:43:29 1999
***************
*** 26,32 ****
     | Authors: Rasmus Lerdorf <rasmus <email protected>> |
     +----------------------------------------------------------------------+
   */
! /* $Id: file.c,v 1.202 1999/03/15 16:02:50 sas Exp $ */
  #ifdef THREAD_SAFE
  #include "tls.h"
  #endif
--- 26,32 ----
     | Authors: Rasmus Lerdorf <rasmus <email protected>> |
     +----------------------------------------------------------------------+
   */
! /* $Id: file.c,v 1.203 1999/03/30 12:43:29 rasmus Exp $ */
  #ifdef THREAD_SAFE
  #include "tls.h"
  #endif
***************
*** 864,871 ****
  {
          pval *fd, *bytes;
          FILE *fp;
! int id, len, br, type;
! char *buf, *p, *rbuf, *rp, c, lc;
          int issock=0;
          int *sock,socketd=0;
          TLS_VARS;
--- 864,871 ----
  {
          pval *fd, *bytes;
          FILE *fp;
! int id, len, type;
! char *buf;
          int issock=0;
          int *sock,socketd=0;
          TLS_VARS;
***************
*** 898,984 ****
                  efree(buf);
                  RETURN_FALSE;
          }
-
- rbuf = estrdup(buf);
- c = *buf;
- lc = '\0';
- p = buf;
- rp = rbuf;
- br = 0;
-
- while (c) {
- switch (c) {
- case '<':
- if (GLOBAL(fgetss_state) == 0) {
- lc = '<';
- GLOBAL(fgetss_state) = 1;
- }
- break;
-
- case '(':
- if (GLOBAL(fgetss_state) == 2) {
- if (lc != '\"') {
- lc = '(';
- br++;
- }
- } else if (GLOBAL(fgetss_state) == 0) {
- *(rp++) = c;
- }
- break;
-
- case ')':
- if (GLOBAL(fgetss_state) == 2) {
- if (lc != '\"') {
- lc = ')';
- br--;
- }
- } else if (GLOBAL(fgetss_state) == 0) {
- *(rp++) = c;
- }
- break;
  
! case '>':
! if (GLOBAL(fgetss_state) == 1) {
! lc = '>';
! GLOBAL(fgetss_state) = 0;
! } else if (GLOBAL(fgetss_state) == 2) {
! if (!br && lc != '\"') {
! GLOBAL(fgetss_state) = 0;
! }
! }
! break;
!
! case '\"':
! if (GLOBAL(fgetss_state) == 2) {
! if (lc == '\"') {
! lc = '\0';
! } else if (lc != '\\') {
! lc = '\"';
! }
! } else if (GLOBAL(fgetss_state) == 0) {
! *(rp++) = c;
! }
! break;
!
! case '?':
! if (GLOBAL(fgetss_state)==1) {
! br=0;
! GLOBAL(fgetss_state)=2;
! break;
! }
! /* fall-through */
!
! default:
! if (GLOBAL(fgetss_state) == 0) {
! *(rp++) = c;
! }
! }
! c = *(++p);
! }
! *rp = '\0';
! efree(buf);
! RETVAL_STRING(rbuf,1);
! efree(rbuf);
  }
  /* }}} */
  
--- 898,906 ----
                  efree(buf);
                  RETURN_FALSE;
          }
  
! _php3_strip_tags(buf,GLOBAL(fgetss_state));
! RETURN_STRING(buf,0);
  }
  /* }}} */
  
Index: php3/functions/php3_string.h
diff -c php3/functions/php3_string.h:1.37 php3/functions/php3_string.h:1.38
*** php3/functions/php3_string.h:1.37 Tue Mar 30 06:25:04 1999
--- php3/functions/php3_string.h Tue Mar 30 07:43:30 1999
***************
*** 29,35 ****
   */
  
  
! /* $Id: php3_string.h,v 1.37 1999/03/30 11:25:04 rasmus Exp $ */
  
  #ifndef _PHPSTRING_H
  #define _PHPSTRING_H
--- 29,35 ----
   */
  
  
! /* $Id: php3_string.h,v 1.38 1999/03/30 12:43:30 rasmus Exp $ */
  
  #ifndef _PHPSTRING_H
  #define _PHPSTRING_H
***************
*** 79,84 ****
--- 79,85 ----
  extern void php3_setlocale(INTERNAL_FUNCTION_PARAMETERS);
  extern void php3_stristr(INTERNAL_FUNCTION_PARAMETERS);
  extern void php3_chunk_split(INTERNAL_FUNCTION_PARAMETERS);
+ extern void php3_strip_tags(INTERNAL_FUNCTION_PARAMETERS);
  
  #if HAVE_CRYPT
  extern php3_module_entry crypt_module_entry;
***************
*** 99,104 ****
--- 100,106 ----
  extern PHPAPI void _php3_stripslashes(char *string, int *len);
  extern PHPAPI void _php3_dirname(char *str, int len);
  extern PHPAPI char *php3i_stristr(unsigned char *s, unsigned char *t);
+ extern PHPAPI void _php3_strip_tags(char *rbuf, int state);
  
  #if 0
  extern PHPAPI char *_php3_str_to_str(char *haystack, int length,
Index: php3/functions/string.c
diff -c php3/functions/string.c:1.181 php3/functions/string.c:1.182
*** php3/functions/string.c:1.181 Mon Mar 22 09:56:41 1999
--- php3/functions/string.c Tue Mar 30 07:43:30 1999
***************
*** 30,36 ****
   */
  
  
! /* $Id: string.c,v 1.181 1999/03/22 14:56:41 sas Exp $ */
  #ifdef THREAD_SAFE
  #include "tls.h"
  #endif
--- 30,36 ----
   */
  
  
! /* $Id: string.c,v 1.182 1999/03/30 12:43:30 rasmus Exp $ */
  #ifdef THREAD_SAFE
  #include "tls.h"
  #endif
***************
*** 1679,1684 ****
--- 1679,1702 ----
  }
  /* }}} */
  
+ /* {{{ proto string strip_tags(string str)
+ Strips HTML and PHP tags from a string */
+ void php3_strip_tags(INTERNAL_FUNCTION_PARAMETERS)
+ {
+ char *buf;
+ pval *str;
+
+ if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &str)==FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_string(str);
+ buf=estrdup(str->value.str.val);
+ _php3_strip_tags(buf,0);
+ RETURN_STRING(buf,0);
+ }
+ /* }}} */
+
+
  /* {{{ proto string setlocale(string category, string locale)
     Set locale information */
  void php3_setlocale(INTERNAL_FUNCTION_PARAMETERS)
***************
*** 1721,1726 ****
--- 1739,1840 ----
          RETURN_FALSE;
  }
  /* }}} */
+
+ /* A simple little state-machine to strip out html and php tags
+
+ State 0 is the output state, State 1 means we are inside a
+ normal html tag and state 2 means we are inside a php tag.
+
+ The state variable is passed in to allow a function like fgetss
+ to maintain state across calls to the function.
+
+ lc holds the last significant character read and br is a bracket
+ counter.
+ */
+ void _php3_strip_tags(char *rbuf, int state) {
+ char *buf, *p, *rp, c, lc;
+ int br;
+
+ buf = estrdup(rbuf);
+ c = *buf;
+ lc = '\0';
+ p = buf;
+ rp = rbuf;
+ br = 0;
+
+ while (c) { /* This is not binary-safe. Don't see why it should be */
+ switch (c) {
+ case '<':
+ if (state == 0) {
+ lc = '<';
+ state = 1;
+ }
+ break;
+
+ case '(':
+ if (state == 2) {
+ if (lc != '\"') {
+ lc = '(';
+ br++;
+ }
+ } else if (state == 0) {
+ *(rp++) = c;
+ }
+ break;
+
+ case ')':
+ if (state == 2) {
+ if (lc != '\"') {
+ lc = ')';
+ br--;
+ }
+ } else if (state == 0) {
+ *(rp++) = c;
+ }
+ break;
+
+ case '>':
+ if (state == 1) {
+ lc = '>';
+ state = 0;
+ } else if (state == 2) {
+ if (!br && lc != '\"' && *(p-1)=='?') {
+ state = 0;
+ }
+ }
+ break;
+
+ case '\"':
+ if (state == 2) {
+ if (lc == '\"') {
+ lc = '\0';
+ } else if (lc != '\\') {
+ lc = '\"';
+ }
+ } else if (state == 0) {
+ *(rp++) = c;
+ }
+ break;
+
+ case '?':
+ if (state==1 && *(p-1)=='<') {
+ br=0;
+ state=2;
+ break;
+ }
+ /* fall-through */
+
+ default:
+ if (state == 0) {
+ *(rp++) = c;
+ }
+ break;
+ }
+ c = *(++p);
+ }
+ *rp = '\0';
+ efree(buf);
+ }
  
  /*
   * Local variables:

--
PHP Development Mailing List   http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe <email protected>
For help: php-dev-help <email protected>