php-src/ext/standard/url_scanner_ex.re

365 lines
8.3 KiB
ReasonML
Raw Normal View History

/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Sascha Schumann <sascha@schumann.cx> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#ifdef TRANS_SID
2000-09-19 17:53:09 +00:00
#ifdef HAVE_UNISTD_H
#include <unistd.h>
2000-09-19 17:53:09 +00:00
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "php_ini.h"
#include "php_globals.h"
#define STATE_TAG SOME_OTHER_STATE_TAG
#include "basic_functions.h"
#undef STATE_TAG
#define url_adapt_ext url_adapt_ext_ex
#define url_scanner url_scanner_ex
#include "php_smart_str.h"
static PHP_INI_MH(OnUpdateTags)
{
2000-10-17 03:02:47 +00:00
url_adapt_state_ex_t *ctx;
char *key;
char *lasts;
char *tmp;
BLS_FETCH();
ctx = &BG(url_adapt_state_ex);
tmp = estrndup(new_value, new_value_length);
if (ctx->tags)
zend_hash_destroy(ctx->tags);
else
ctx->tags = malloc(sizeof(HashTable));
zend_hash_init(ctx->tags, 0, NULL, NULL, 1);
for (key = php_strtok_r(tmp, ",", &lasts);
key;
key = php_strtok_r(NULL, ",", &lasts)) {
char *val;
val = strchr(key, '=');
if (val) {
char *q;
int keylen;
*val++ = '\0';
for (q = key; *q; q++)
*q = tolower(*q);
keylen = q - key;
/* key is stored withOUT NUL
val is stored WITH NUL */
zend_hash_add(ctx->tags, key, keylen, val, strlen(val)+1, NULL);
}
}
efree(tmp);
return SUCCESS;
}
PHP_INI_BEGIN()
2000-10-17 03:02:47 +00:00
STD_PHP_INI_ENTRY("url_rewriter.tags", "a=href,area=href,frame=src,form=fakeentry", PHP_INI_ALL, OnUpdateTags, url_adapt_state_ex, php_basic_globals, basic_globals)
PHP_INI_END()
static inline void append_modified_url(smart_str *url, smart_str *dest, smart_str *name, smart_str *val, const char *separator)
{
2000-10-17 03:02:47 +00:00
register const char *p, *q;
const char *bash = NULL;
char sep = '?';
q = url->c + url->len;
for (p = url->c; p < q; p++) {
switch(*p) {
case ':':
smart_str_append(dest, url);
return;
case '?':
sep = *separator;
break;
case '#':
bash = p;
break;
}
}
/* Don't modify URLs of the format "#mark" */
if (bash - url->c == 0) {
smart_str_append(dest, url);
return;
}
if (bash)
smart_str_appendl(dest, url->c, bash - url->c);
else
smart_str_append(dest, url);
smart_str_appendc(dest, sep);
smart_str_append(dest, name);
smart_str_appendc(dest, '=');
smart_str_append(dest, val);
if (bash)
smart_str_appendl(dest, bash, q - bash);
}
static inline void tag_arg(url_adapt_state_ex_t *ctx PLS_DC)
{
2000-10-17 03:02:47 +00:00
char f = 0;
if (strncasecmp(ctx->arg.c, ctx->lookup_data, ctx->arg.len) == 0)
f = 1;
smart_str_appendc(&ctx->result, '"');
if (f) {
append_modified_url(&ctx->val, &ctx->result, &ctx->q_name, &ctx->q_value, PG(arg_separator));
} else {
smart_str_append(&ctx->result, &ctx->val);
}
smart_str_appendc(&ctx->result, '"');
}
enum {
2000-10-17 03:02:47 +00:00
STATE_PLAIN,
STATE_TAG,
STATE_NEXT_ARG,
STATE_ARG,
STATE_BEFORE_VAL,
STATE_VAL
};
#define YYFILL(n) goto stop
#define YYCTYPE unsigned char
#define YYCURSOR xp
#define YYLIMIT end
#define YYMARKER q
#define STATE ctx->state
#define PASSTHRU() {\
2000-10-17 03:02:47 +00:00
smart_str_appendl(&ctx->result, start, YYCURSOR - start); \
}
#define HANDLE_FORM() {\
2000-10-17 03:02:47 +00:00
if (ctx->tag.len == 4 && strncasecmp(ctx->tag.c, "form", 4) == 0) {\
smart_str_appends(&ctx->result, "<INPUT TYPE=HIDDEN NAME=\""); \
smart_str_append(&ctx->result, &ctx->q_name); \
smart_str_appends(&ctx->result, "\" VALUE=\""); \
smart_str_append(&ctx->result, &ctx->q_value); \
smart_str_appends(&ctx->result, "\">"); \
} \
}
/*
2000-10-17 03:02:47 +00:00
* HANDLE_TAG copies the HTML Tag and checks whether we
* have that tag in our table. If we might modify it,
* we continue to scan the tag, otherwise we simply copy the complete
* HTML stuff to the result buffer.
*/
#define HANDLE_TAG() {\
2000-10-17 03:02:47 +00:00
int ok = 0; \
int i; \
smart_str_copyl(&ctx->tag, start, YYCURSOR - start); \
for (i = 0; i < ctx->tag.len; i++) \
ctx->tag.c[i] = tolower(ctx->tag.c[i]); \
if (zend_hash_find(ctx->tags, ctx->tag.c, ctx->tag.len, &ctx->lookup_data) == SUCCESS) \
ok = 1; \
STATE = ok ? STATE_NEXT_ARG : STATE_PLAIN; \
}
#define HANDLE_ARG() {\
2000-10-17 03:02:47 +00:00
smart_str_copyl(&ctx->arg, start, YYCURSOR - start); \
}
#define HANDLE_VAL(quotes) {\
2000-10-17 03:02:47 +00:00
smart_str_setl(&ctx->val, start + quotes, YYCURSOR - start - quotes * 2); \
tag_arg(ctx PLS_CC); \
}
#ifdef SCANNER_DEBUG
#define scdebug(x) printf x
#else
#define scdebug(x)
#endif
static inline void mainloop(url_adapt_state_ex_t *ctx, const char *newdata, size_t newlen)
{
2000-10-17 03:02:47 +00:00
char *end, *q;
char *xp;
char *start;
int rest;
PLS_FETCH();
2000-10-17 03:02:47 +00:00
smart_str_appendl(&ctx->buf, newdata, newlen);
YYCURSOR = ctx->buf.c;
YYLIMIT = ctx->buf.c + ctx->buf.len;
/*!re2c
any = [\000-\377];
alpha = [a-zA-Z];
*/
2000-10-17 03:02:47 +00:00
while(1) {
start = YYCURSOR;
scdebug(("state %d at %s\n", STATE, YYCURSOR));
switch(STATE) {
case STATE_PLAIN:
/*!re2c
2000-10-17 03:02:47 +00:00
[<] { PASSTHRU(); STATE = STATE_TAG; continue; }
(any\[<]) { PASSTHRU(); continue; }
*/
2000-10-17 03:02:47 +00:00
break;
case STATE_TAG:
/*!re2c
2000-10-17 03:02:47 +00:00
alpha+ { HANDLE_TAG() /* Sets STATE */; PASSTHRU(); continue; }
any { PASSTHRU(); STATE = STATE_PLAIN; continue; }
*/
2000-10-17 03:02:47 +00:00
break;
case STATE_NEXT_ARG:
/*!re2c
2000-10-17 03:02:47 +00:00
">" { PASSTHRU(); HANDLE_FORM(); STATE = STATE_PLAIN; continue; }
[ \n] { PASSTHRU(); continue; }
alpha { YYCURSOR--; STATE = STATE_ARG; continue; }
any { PASSTHRU(); continue; }
*/
2000-10-17 03:02:47 +00:00
break;
2000-10-17 03:02:47 +00:00
case STATE_ARG:
/*!re2c
2000-10-17 03:02:47 +00:00
alpha+ { PASSTHRU(); HANDLE_ARG(); STATE = STATE_BEFORE_VAL; continue; }
any { PASSTHRU(); STATE = STATE_NEXT_ARG; continue; }
*/
2000-10-17 03:02:47 +00:00
case STATE_BEFORE_VAL:
/*!re2c
2000-10-17 03:02:47 +00:00
[ ]* "=" [ ]* { PASSTHRU(); STATE = STATE_VAL; continue; }
any { YYCURSOR--; STATE = STATE_NEXT_ARG; continue; }
*/
2000-10-17 03:02:47 +00:00
break;
2000-10-17 03:02:47 +00:00
case STATE_VAL:
/*!re2c
2000-10-17 03:02:47 +00:00
["] (any\[">])* ["] { HANDLE_VAL(1); STATE = STATE_NEXT_ARG; continue; }
['] (any\['>])* ['] { HANDLE_VAL(1); STATE = STATE_NEXT_ARG; continue; }
(any\[ \n>"])+ { HANDLE_VAL(0); STATE = STATE_NEXT_ARG; continue; }
any { PASSTHRU(); STATE = STATE_NEXT_ARG; continue; }
*/
2000-10-17 03:02:47 +00:00
break;
}
}
stop:
2000-10-17 03:02:47 +00:00
scdebug(("stopped in state %d at pos %d (%d:%c)\n", STATE, YYCURSOR - ctx->buf.c, *YYCURSOR, *YYCURSOR));
2000-10-17 03:02:47 +00:00
rest = YYLIMIT - start;
if (rest) memmove(ctx->buf.c, start, rest);
ctx->buf.len = rest;
}
char *url_adapt_ext(const char *src, size_t srclen, const char *name, const char *value, size_t *newlen)
{
2000-10-17 03:02:47 +00:00
char *ret;
url_adapt_state_ex_t *ctx;
BLS_FETCH();
ctx = &BG(url_adapt_state_ex);
smart_str_sets(&ctx->q_name, name);
smart_str_sets(&ctx->q_value, value);
mainloop(ctx, src, srclen);
*newlen = ctx->result.len;
if (ctx->result.len == 0) {
return strdup("");
}
smart_str_0(&ctx->result);
ret = malloc(ctx->result.len + 1);
memcpy(ret, ctx->result.c, ctx->result.len + 1);
ctx->result.len = 0;
return ret;
}
PHP_RINIT_FUNCTION(url_scanner)
{
2000-10-17 03:02:47 +00:00
url_adapt_state_ex_t *ctx;
BLS_FETCH();
ctx = &BG(url_adapt_state_ex);
2000-10-16 20:08:27 +00:00
2000-10-17 03:02:47 +00:00
memset(ctx, 0, ((size_t) &((url_adapt_state_ex_t *)0)->tags));
2000-10-17 03:02:47 +00:00
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(url_scanner)
{
2000-10-17 03:02:47 +00:00
url_adapt_state_ex_t *ctx;
BLS_FETCH();
ctx = &BG(url_adapt_state_ex);
2000-10-17 03:02:47 +00:00
smart_str_free(&ctx->result);
smart_str_free(&ctx->buf);
smart_str_free(&ctx->tag);
smart_str_free(&ctx->arg);
2000-10-16 20:08:27 +00:00
2000-10-17 03:02:47 +00:00
return SUCCESS;
}
PHP_MINIT_FUNCTION(url_scanner)
{
url_adapt_state_ex_t *ctx;
BLS_FETCH();
ctx = &BG(url_adapt_state_ex);
ctx->tags = NULL;
2000-10-17 03:02:47 +00:00
REGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(url_scanner)
{
2000-10-17 03:02:47 +00:00
BLS_FETCH();
2000-10-16 20:08:27 +00:00
2000-10-17 03:02:47 +00:00
UNREGISTER_INI_ENTRIES();
zend_hash_destroy(BG(url_adapt_state_ex).tags);
free(BG(url_adapt_state_ex).tags);
return SUCCESS;
}
2000-10-17 03:02:47 +00:00
#endif