2000-09-20 01:32:27 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2012-09-05 23:43:59 +08:00
|
|
|
| PHP Version 5 |
|
2000-09-20 01:32:27 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2013-01-01 16:28:54 +08:00
|
|
|
| Copyright (c) 1997-2013 The PHP Group |
|
2000-09-20 01:32:27 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:26:08 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2000-09-20 01:32:27 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:26:08 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2000-09-20 01:32:27 +08:00
|
|
|
| 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. |
|
|
|
|
+----------------------------------------------------------------------+
|
2002-02-28 16:29:35 +08:00
|
|
|
| Author: Sascha Schumann <sascha@schumann.cx> |
|
2000-09-20 01:32:27 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2005-01-10 23:20:53 +08:00
|
|
|
/* $Id$ */
|
|
|
|
|
2000-09-20 01:32:27 +08:00
|
|
|
#include "php.h"
|
|
|
|
|
2000-09-20 01:53:09 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2000-09-20 01:32:27 +08:00
|
|
|
#include <unistd.h>
|
2000-09-20 01:53:09 +08:00
|
|
|
#endif
|
2003-03-11 10:28:46 +08:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
2000-09-20 01:53:09 +08:00
|
|
|
|
2000-09-20 01:32:27 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-10-17 01:25:36 +08:00
|
|
|
#include "php_ini.h"
|
2000-09-20 01:32:27 +08:00
|
|
|
#include "php_globals.h"
|
|
|
|
#define STATE_TAG SOME_OTHER_STATE_TAG
|
|
|
|
#include "basic_functions.h"
|
2002-05-05 01:14:56 +08:00
|
|
|
#include "url.h"
|
2000-09-20 01:32:27 +08:00
|
|
|
#undef STATE_TAG
|
|
|
|
|
|
|
|
#define url_scanner url_scanner_ex
|
|
|
|
|
2000-09-27 23:26:39 +08:00
|
|
|
#include "php_smart_str.h"
|
2000-09-20 02:17:28 +08:00
|
|
|
|
2014-02-17 21:59:18 +08:00
|
|
|
static void tag_dtor(zval *zv)
|
|
|
|
{
|
|
|
|
free(Z_PTR_P(zv));
|
|
|
|
}
|
|
|
|
|
2000-10-17 01:25:36 +08:00
|
|
|
static PHP_INI_MH(OnUpdateTags)
|
|
|
|
{
|
2000-10-17 11:02:47 +08:00
|
|
|
url_adapt_state_ex_t *ctx;
|
|
|
|
char *key;
|
|
|
|
char *lasts;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
ctx = &BG(url_adapt_state_ex);
|
|
|
|
|
|
|
|
tmp = estrndup(new_value, new_value_length);
|
|
|
|
|
|
|
|
if (ctx->tags)
|
|
|
|
zend_hash_destroy(ctx->tags);
|
2011-07-28 18:52:45 +08:00
|
|
|
else {
|
2000-10-17 11:02:47 +08:00
|
|
|
ctx->tags = malloc(sizeof(HashTable));
|
2011-07-28 18:52:45 +08:00
|
|
|
if (!ctx->tags) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 21:59:18 +08:00
|
|
|
zend_hash_init(ctx->tags, 0, NULL, tag_dtor, 1);
|
2000-10-17 11:02:47 +08:00
|
|
|
|
|
|
|
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 */
|
2014-02-13 21:54:23 +08:00
|
|
|
zend_hash_str_add_mem(ctx->tags, key, keylen, val, strlen(val)+1);
|
2000-10-17 11:02:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
efree(tmp);
|
|
|
|
|
|
|
|
return SUCCESS;
|
2000-10-17 01:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PHP_INI_BEGIN()
|
2002-09-25 19:06:24 +08:00
|
|
|
STD_PHP_INI_ENTRY("url_rewriter.tags", "a=href,area=href,frame=src,form=,fieldset=", PHP_INI_ALL, OnUpdateTags, url_adapt_state_ex, php_basic_globals, basic_globals)
|
2000-10-17 01:25:36 +08:00
|
|
|
PHP_INI_END()
|
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
/*!re2c
|
|
|
|
any = [\000-\377];
|
|
|
|
N = (any\[<]);
|
|
|
|
alpha = [a-zA-Z];
|
2007-06-06 08:00:28 +08:00
|
|
|
alphanamespace = [a-zA-Z:];
|
2002-09-25 20:15:22 +08:00
|
|
|
alphadash = ([a-zA-Z] | "-");
|
2001-07-04 23:30:21 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define YYFILL(n) goto done
|
|
|
|
#define YYCTYPE unsigned char
|
|
|
|
#define YYCURSOR p
|
|
|
|
#define YYLIMIT q
|
|
|
|
#define YYMARKER r
|
|
|
|
|
2002-05-05 02:33:13 +08:00
|
|
|
static inline void append_modified_url(smart_str *url, smart_str *dest, smart_str *url_app, const char *separator)
|
2000-09-20 01:32:27 +08:00
|
|
|
{
|
2001-09-11 15:52:55 +08:00
|
|
|
register const char *p, *q;
|
2000-10-17 11:02:47 +08:00
|
|
|
const char *bash = NULL;
|
2000-12-23 06:52:11 +08:00
|
|
|
const char *sep = "?";
|
2000-10-17 11:02:47 +08:00
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
q = (p = url->c) + url->len;
|
2000-10-17 11:02:47 +08:00
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
scan:
|
|
|
|
/*!re2c
|
|
|
|
":" { smart_str_append(dest, url); return; }
|
2001-09-10 19:22:43 +08:00
|
|
|
"?" { sep = separator; goto scan; }
|
2001-09-09 22:52:39 +08:00
|
|
|
"#" { bash = p - 1; goto done; }
|
2001-07-04 23:30:21 +08:00
|
|
|
(any\[:?#])+ { goto scan; }
|
|
|
|
*/
|
|
|
|
done:
|
2001-09-09 22:38:12 +08:00
|
|
|
|
2000-10-17 11:02:47 +08:00
|
|
|
/* Don't modify URLs of the format "#mark" */
|
2001-07-04 23:30:21 +08:00
|
|
|
if (bash && bash - url->c == 0) {
|
2000-10-17 11:02:47 +08:00
|
|
|
smart_str_append(dest, url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bash)
|
|
|
|
smart_str_appendl(dest, url->c, bash - url->c);
|
|
|
|
else
|
|
|
|
smart_str_append(dest, url);
|
|
|
|
|
2002-05-05 02:33:13 +08:00
|
|
|
smart_str_appends(dest, sep);
|
|
|
|
smart_str_append(dest, url_app);
|
2000-10-17 11:02:47 +08:00
|
|
|
|
|
|
|
if (bash)
|
|
|
|
smart_str_appendl(dest, bash, q - bash);
|
2000-09-20 01:32:27 +08:00
|
|
|
}
|
|
|
|
|
2001-09-09 22:38:12 +08:00
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
#undef YYFILL
|
|
|
|
#undef YYCTYPE
|
|
|
|
#undef YYCURSOR
|
|
|
|
#undef YYLIMIT
|
|
|
|
#undef YYMARKER
|
|
|
|
|
2002-03-26 18:23:33 +08:00
|
|
|
static inline void tag_arg(url_adapt_state_ex_t *ctx, char quotes, char type TSRMLS_DC)
|
2000-09-20 01:32:27 +08:00
|
|
|
{
|
2000-10-17 11:02:47 +08:00
|
|
|
char f = 0;
|
|
|
|
|
|
|
|
if (strncasecmp(ctx->arg.c, ctx->lookup_data, ctx->arg.len) == 0)
|
|
|
|
f = 1;
|
|
|
|
|
2002-03-26 18:23:33 +08:00
|
|
|
if (quotes)
|
|
|
|
smart_str_appendc(&ctx->result, type);
|
2000-10-17 11:02:47 +08:00
|
|
|
if (f) {
|
2002-05-05 02:33:13 +08:00
|
|
|
append_modified_url(&ctx->val, &ctx->result, &ctx->url_app, PG(arg_separator).output);
|
2000-10-17 11:02:47 +08:00
|
|
|
} else {
|
|
|
|
smart_str_append(&ctx->result, &ctx->val);
|
|
|
|
}
|
2002-03-26 18:23:33 +08:00
|
|
|
if (quotes)
|
|
|
|
smart_str_appendc(&ctx->result, type);
|
2000-09-20 01:32:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
2001-07-04 23:30:21 +08:00
|
|
|
STATE_PLAIN = 0,
|
2000-10-17 11:02:47 +08:00
|
|
|
STATE_TAG,
|
|
|
|
STATE_NEXT_ARG,
|
|
|
|
STATE_ARG,
|
|
|
|
STATE_BEFORE_VAL,
|
|
|
|
STATE_VAL
|
2000-09-20 01:32:27 +08:00
|
|
|
};
|
|
|
|
|
2000-09-20 09:15:10 +08:00
|
|
|
#define YYFILL(n) goto stop
|
2000-10-05 02:06:20 +08:00
|
|
|
#define YYCTYPE unsigned char
|
2000-09-20 09:15:10 +08:00
|
|
|
#define YYCURSOR xp
|
|
|
|
#define YYLIMIT end
|
|
|
|
#define YYMARKER q
|
|
|
|
#define STATE ctx->state
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
#define STD_PARA url_adapt_state_ex_t *ctx, char *start, char *YYCURSOR TSRMLS_DC
|
|
|
|
#define STD_ARGS ctx, start, xp TSRMLS_CC
|
2000-12-04 20:26:53 +08:00
|
|
|
|
2002-09-25 20:15:22 +08:00
|
|
|
#if SCANNER_DEBUG
|
|
|
|
#define scdebug(x) printf x
|
|
|
|
#else
|
|
|
|
#define scdebug(x)
|
|
|
|
#endif
|
|
|
|
|
2000-12-04 20:26:53 +08:00
|
|
|
static inline void passthru(STD_PARA)
|
|
|
|
{
|
2002-09-25 20:15:22 +08:00
|
|
|
scdebug(("appending %d chars, starting with %c\n", YYCURSOR-start, *start));
|
2000-12-04 20:26:53 +08:00
|
|
|
smart_str_appendl(&ctx->result, start, YYCURSOR - start);
|
2000-09-20 09:15:10 +08:00
|
|
|
}
|
|
|
|
|
2002-09-25 19:06:24 +08:00
|
|
|
/*
|
|
|
|
* This function appends a hidden input field after a <form> or
|
|
|
|
* <fieldset>. The latter is important for XHTML.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void handle_form(STD_PARA)
|
2000-12-04 20:26:53 +08:00
|
|
|
{
|
2002-09-25 19:06:24 +08:00
|
|
|
int doit = 0;
|
|
|
|
|
|
|
|
if (ctx->form_app.len > 0) {
|
|
|
|
switch (ctx->tag.len) {
|
2007-10-10 08:34:40 +08:00
|
|
|
case sizeof("form") - 1:
|
|
|
|
if (!strncasecmp(ctx->tag.c, "form", sizeof("form") - 1)) {
|
|
|
|
doit = 1;
|
|
|
|
}
|
|
|
|
if (doit && ctx->val.c && ctx->lookup_data && *ctx->lookup_data) {
|
|
|
|
char *e, *p = zend_memnstr(ctx->val.c, "://", sizeof("://") - 1, ctx->val.c + ctx->val.len);
|
|
|
|
if (p) {
|
|
|
|
e = memchr(p, '/', (ctx->val.c + ctx->val.len) - p);
|
|
|
|
if (!e) {
|
|
|
|
e = ctx->val.c + ctx->val.len;
|
|
|
|
}
|
|
|
|
if ((e - p) && strncasecmp(p, ctx->lookup_data, (e - p))) {
|
|
|
|
doit = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sizeof("fieldset") - 1:
|
|
|
|
if (!strncasecmp(ctx->tag.c, "fieldset", sizeof("fieldset") - 1)) {
|
|
|
|
doit = 1;
|
|
|
|
}
|
|
|
|
break;
|
2002-09-25 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (doit)
|
|
|
|
smart_str_append(&ctx->result, &ctx->form_app);
|
2000-12-04 20:26:53 +08:00
|
|
|
}
|
2000-09-20 09:15:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-10-17 11:02:47 +08:00
|
|
|
* HANDLE_TAG copies the HTML Tag and checks whether we
|
2000-09-20 09:15:10 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2000-12-04 20:26:53 +08:00
|
|
|
static inline void handle_tag(STD_PARA)
|
|
|
|
{
|
|
|
|
int ok = 0;
|
2010-09-23 11:45:36 +08:00
|
|
|
unsigned int i;
|
2000-12-04 20:26:53 +08:00
|
|
|
|
|
|
|
ctx->tag.len = 0;
|
|
|
|
smart_str_appendl(&ctx->tag, start, YYCURSOR - start);
|
|
|
|
for (i = 0; i < ctx->tag.len; i++)
|
2003-04-17 05:40:48 +08:00
|
|
|
ctx->tag.c[i] = tolower((int)(unsigned char)ctx->tag.c[i]);
|
2014-02-13 21:54:23 +08:00
|
|
|
if ((ctx->lookup_data = zend_hash_str_find_ptr(ctx->tags, ctx->tag.c, ctx->tag.len)) != NULL)
|
2000-12-04 20:26:53 +08:00
|
|
|
ok = 1;
|
|
|
|
STATE = ok ? STATE_NEXT_ARG : STATE_PLAIN;
|
2000-09-20 09:15:10 +08:00
|
|
|
}
|
|
|
|
|
2000-12-04 20:26:53 +08:00
|
|
|
static inline void handle_arg(STD_PARA)
|
|
|
|
{
|
|
|
|
ctx->arg.len = 0;
|
|
|
|
smart_str_appendl(&ctx->arg, start, YYCURSOR - start);
|
2000-09-20 09:15:10 +08:00
|
|
|
}
|
2000-12-04 20:26:53 +08:00
|
|
|
|
|
|
|
static inline void handle_val(STD_PARA, char quotes, char type)
|
|
|
|
{
|
|
|
|
smart_str_setl(&ctx->val, start + quotes, YYCURSOR - start - quotes * 2);
|
2002-03-26 18:23:33 +08:00
|
|
|
tag_arg(ctx, quotes, type TSRMLS_CC);
|
2000-09-20 09:15:10 +08:00
|
|
|
}
|
|
|
|
|
2002-03-04 16:37:44 +08:00
|
|
|
static inline void xx_mainloop(url_adapt_state_ex_t *ctx, const char *newdata, size_t newlen TSRMLS_DC)
|
2000-09-20 01:32:27 +08:00
|
|
|
{
|
2000-10-17 11:02:47 +08:00
|
|
|
char *end, *q;
|
|
|
|
char *xp;
|
|
|
|
char *start;
|
|
|
|
int rest;
|
2000-09-20 01:32:27 +08:00
|
|
|
|
2000-10-17 11:02:47 +08:00
|
|
|
smart_str_appendl(&ctx->buf, newdata, newlen);
|
|
|
|
|
|
|
|
YYCURSOR = ctx->buf.c;
|
|
|
|
YYLIMIT = ctx->buf.c + ctx->buf.len;
|
2000-09-20 04:19:03 +08:00
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
switch (STATE) {
|
|
|
|
case STATE_PLAIN: goto state_plain;
|
|
|
|
case STATE_TAG: goto state_tag;
|
|
|
|
case STATE_NEXT_ARG: goto state_next_arg;
|
|
|
|
case STATE_ARG: goto state_arg;
|
|
|
|
case STATE_BEFORE_VAL: goto state_before_val;
|
|
|
|
case STATE_VAL: goto state_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
state_plain_begin:
|
|
|
|
STATE = STATE_PLAIN;
|
2000-10-17 11:02:47 +08:00
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
state_plain:
|
|
|
|
start = YYCURSOR;
|
2000-09-20 01:32:27 +08:00
|
|
|
/*!re2c
|
2001-07-04 23:30:21 +08:00
|
|
|
"<" { passthru(STD_ARGS); STATE = STATE_TAG; goto state_tag; }
|
|
|
|
N+ { passthru(STD_ARGS); goto state_plain; }
|
2000-09-20 01:32:27 +08:00
|
|
|
*/
|
2001-07-04 23:30:21 +08:00
|
|
|
|
|
|
|
state_tag:
|
|
|
|
start = YYCURSOR;
|
2000-09-20 01:32:27 +08:00
|
|
|
/*!re2c
|
2007-06-06 08:00:28 +08:00
|
|
|
alphanamespace+ { handle_tag(STD_ARGS); /* Sets STATE */; passthru(STD_ARGS); if (STATE == STATE_PLAIN) goto state_plain; else goto state_next_arg; }
|
2001-07-04 23:30:21 +08:00
|
|
|
any { passthru(STD_ARGS); goto state_plain_begin; }
|
2000-09-20 01:32:27 +08:00
|
|
|
*/
|
2001-07-04 23:30:21 +08:00
|
|
|
|
|
|
|
state_next_arg_begin:
|
|
|
|
STATE = STATE_NEXT_ARG;
|
|
|
|
|
|
|
|
state_next_arg:
|
|
|
|
start = YYCURSOR;
|
2000-09-20 01:32:27 +08:00
|
|
|
/*!re2c
|
2013-07-17 23:07:26 +08:00
|
|
|
[/]? [>] { passthru(STD_ARGS); handle_form(STD_ARGS); goto state_plain_begin; }
|
2006-02-28 22:45:18 +08:00
|
|
|
[ \v\r\t\n]+ { passthru(STD_ARGS); goto state_next_arg; }
|
2001-07-04 23:30:21 +08:00
|
|
|
alpha { --YYCURSOR; STATE = STATE_ARG; goto state_arg; }
|
|
|
|
any { passthru(STD_ARGS); goto state_plain_begin; }
|
2000-09-20 01:32:27 +08:00
|
|
|
*/
|
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
state_arg:
|
|
|
|
start = YYCURSOR;
|
2000-09-20 01:32:27 +08:00
|
|
|
/*!re2c
|
2002-09-25 20:15:22 +08:00
|
|
|
alpha alphadash* { passthru(STD_ARGS); handle_arg(STD_ARGS); STATE = STATE_BEFORE_VAL; goto state_before_val; }
|
2001-07-04 23:30:21 +08:00
|
|
|
any { passthru(STD_ARGS); STATE = STATE_NEXT_ARG; goto state_next_arg; }
|
2000-09-20 09:15:10 +08:00
|
|
|
*/
|
2000-09-20 02:00:58 +08:00
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
state_before_val:
|
|
|
|
start = YYCURSOR;
|
2000-09-20 09:15:10 +08:00
|
|
|
/*!re2c
|
2001-07-04 23:30:21 +08:00
|
|
|
[ ]* "=" [ ]* { passthru(STD_ARGS); STATE = STATE_VAL; goto state_val; }
|
|
|
|
any { --YYCURSOR; goto state_next_arg_begin; }
|
2000-09-20 01:32:27 +08:00
|
|
|
*/
|
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
|
|
|
|
state_val:
|
|
|
|
start = YYCURSOR;
|
2000-09-20 01:32:27 +08:00
|
|
|
/*!re2c
|
2001-07-04 23:30:21 +08:00
|
|
|
["] (any\[">])* ["] { handle_val(STD_ARGS, 1, '"'); goto state_next_arg_begin; }
|
|
|
|
['] (any\['>])* ['] { handle_val(STD_ARGS, 1, '\''); goto state_next_arg_begin; }
|
2013-07-17 23:07:26 +08:00
|
|
|
(any\[ \r\t\n>'"])+ { handle_val(STD_ARGS, 0, ' '); goto state_next_arg_begin; }
|
2001-07-04 23:30:21 +08:00
|
|
|
any { passthru(STD_ARGS); goto state_next_arg_begin; }
|
2000-09-20 01:32:27 +08:00
|
|
|
*/
|
|
|
|
|
2000-09-20 09:15:10 +08:00
|
|
|
stop:
|
2000-10-17 11:02:47 +08:00
|
|
|
rest = YYLIMIT - start;
|
2001-07-04 23:30:21 +08:00
|
|
|
scdebug(("stopped in state %d at pos %d (%d:%c) %d\n", STATE, YYCURSOR - ctx->buf.c, *YYCURSOR, *YYCURSOR, rest));
|
2000-10-20 01:40:56 +08:00
|
|
|
/* XXX: Crash avoidance. Need to work with reporter to figure out what goes wrong */
|
|
|
|
if (rest < 0) rest = 0;
|
|
|
|
|
2000-10-17 11:02:47 +08:00
|
|
|
if (rest) memmove(ctx->buf.c, start, rest);
|
|
|
|
ctx->buf.len = rest;
|
2000-09-20 01:32:27 +08:00
|
|
|
}
|
|
|
|
|
2002-05-06 00:39:49 +08:00
|
|
|
char *php_url_scanner_adapt_single_url(const char *url, size_t urllen, const char *name, const char *value, size_t *newlen TSRMLS_DC)
|
|
|
|
{
|
|
|
|
smart_str surl = {0};
|
|
|
|
smart_str buf = {0};
|
|
|
|
smart_str url_app = {0};
|
|
|
|
|
|
|
|
smart_str_setl(&surl, url, urllen);
|
|
|
|
|
|
|
|
smart_str_appends(&url_app, name);
|
|
|
|
smart_str_appendc(&url_app, '=');
|
|
|
|
smart_str_appends(&url_app, value);
|
|
|
|
|
|
|
|
append_modified_url(&surl, &buf, &url_app, PG(arg_separator).output);
|
|
|
|
|
|
|
|
smart_str_0(&buf);
|
|
|
|
if (newlen) *newlen = buf.len;
|
|
|
|
|
|
|
|
smart_str_free(&url_app);
|
|
|
|
|
|
|
|
return buf.c;
|
|
|
|
}
|
|
|
|
|
2001-07-04 23:30:21 +08:00
|
|
|
|
2003-03-11 09:47:06 +08:00
|
|
|
static char *url_adapt_ext(const char *src, size_t srclen, size_t *newlen, zend_bool do_flush TSRMLS_DC)
|
2000-09-20 01:32:27 +08:00
|
|
|
{
|
2000-10-17 11:02:47 +08:00
|
|
|
url_adapt_state_ex_t *ctx;
|
2001-08-27 08:23:55 +08:00
|
|
|
char *retval;
|
2000-10-17 11:02:47 +08:00
|
|
|
|
|
|
|
ctx = &BG(url_adapt_state_ex);
|
|
|
|
|
2002-03-04 16:37:44 +08:00
|
|
|
xx_mainloop(ctx, src, srclen TSRMLS_CC);
|
2000-10-17 11:02:47 +08:00
|
|
|
|
|
|
|
*newlen = ctx->result.len;
|
2001-08-25 23:57:42 +08:00
|
|
|
if (!ctx->result.c) {
|
2001-07-04 23:30:21 +08:00
|
|
|
smart_str_appendl(&ctx->result, "", 0);
|
2001-08-25 23:57:42 +08:00
|
|
|
}
|
2000-10-17 11:02:47 +08:00
|
|
|
smart_str_0(&ctx->result);
|
2001-08-25 23:57:42 +08:00
|
|
|
if (do_flush) {
|
|
|
|
smart_str_appendl(&ctx->result, ctx->buf.c, ctx->buf.len);
|
|
|
|
*newlen += ctx->buf.len;
|
2001-08-27 08:23:55 +08:00
|
|
|
smart_str_free(&ctx->buf);
|
2001-08-25 23:57:42 +08:00
|
|
|
}
|
2001-08-27 08:23:55 +08:00
|
|
|
retval = ctx->result.c;
|
|
|
|
ctx->result.c = NULL;
|
2000-10-17 11:02:47 +08:00
|
|
|
ctx->result.len = 0;
|
2001-08-27 08:23:55 +08:00
|
|
|
return retval;
|
2000-09-20 01:32:27 +08:00
|
|
|
}
|
|
|
|
|
2009-07-27 07:20:34 +08:00
|
|
|
static int php_url_scanner_ex_activate(TSRMLS_D)
|
2000-09-20 01:32:27 +08:00
|
|
|
{
|
2000-10-17 11:02:47 +08:00
|
|
|
url_adapt_state_ex_t *ctx;
|
|
|
|
|
|
|
|
ctx = &BG(url_adapt_state_ex);
|
2000-10-17 04:08:27 +08:00
|
|
|
|
2000-10-17 11:02:47 +08:00
|
|
|
memset(ctx, 0, ((size_t) &((url_adapt_state_ex_t *)0)->tags));
|
2000-09-20 01:32:27 +08:00
|
|
|
|
2000-10-17 11:02:47 +08:00
|
|
|
return SUCCESS;
|
2000-09-20 01:32:27 +08:00
|
|
|
}
|
|
|
|
|
2009-07-27 07:20:34 +08:00
|
|
|
static int php_url_scanner_ex_deactivate(TSRMLS_D)
|
2000-09-20 01:32:27 +08:00
|
|
|
{
|
2000-10-17 11:02:47 +08:00
|
|
|
url_adapt_state_ex_t *ctx;
|
|
|
|
|
|
|
|
ctx = &BG(url_adapt_state_ex);
|
2000-09-20 01:32:27 +08:00
|
|
|
|
2000-10-17 11:02:47 +08:00
|
|
|
smart_str_free(&ctx->result);
|
|
|
|
smart_str_free(&ctx->buf);
|
|
|
|
smart_str_free(&ctx->tag);
|
|
|
|
smart_str_free(&ctx->arg);
|
2000-10-17 04:08:27 +08:00
|
|
|
|
2000-10-17 11:02:47 +08:00
|
|
|
return SUCCESS;
|
2000-09-20 01:32:27 +08:00
|
|
|
}
|
|
|
|
|
2002-05-03 16:00:41 +08:00
|
|
|
static void php_url_scanner_output_handler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
|
|
|
|
{
|
2003-03-11 10:28:46 +08:00
|
|
|
size_t len;
|
|
|
|
|
2004-07-23 10:05:49 +08:00
|
|
|
if (BG(url_adapt_state_ex).url_app.len != 0) {
|
2010-05-31 18:29:43 +08:00
|
|
|
*handled_output = url_adapt_ext(output, output_len, &len, (zend_bool) (mode & (PHP_OUTPUT_HANDLER_END | PHP_OUTPUT_HANDLER_CONT | PHP_OUTPUT_HANDLER_FLUSH | PHP_OUTPUT_HANDLER_FINAL) ? 1 : 0) TSRMLS_CC);
|
2003-03-11 10:28:46 +08:00
|
|
|
if (sizeof(uint) < sizeof(size_t)) {
|
|
|
|
if (len > UINT_MAX)
|
|
|
|
len = UINT_MAX;
|
|
|
|
}
|
|
|
|
*handled_output_len = len;
|
2004-07-23 10:05:49 +08:00
|
|
|
} else if (BG(url_adapt_state_ex).url_app.len == 0) {
|
|
|
|
url_adapt_state_ex_t *ctx = &BG(url_adapt_state_ex);
|
|
|
|
if (ctx->buf.len) {
|
|
|
|
smart_str_appendl(&ctx->result, ctx->buf.c, ctx->buf.len);
|
|
|
|
smart_str_appendl(&ctx->result, output, output_len);
|
|
|
|
|
|
|
|
*handled_output = ctx->result.c;
|
|
|
|
*handled_output_len = ctx->buf.len + output_len;
|
|
|
|
|
|
|
|
ctx->result.c = NULL;
|
|
|
|
ctx->result.len = 0;
|
|
|
|
smart_str_free(&ctx->buf);
|
|
|
|
} else {
|
2011-05-24 13:06:53 +08:00
|
|
|
*handled_output = estrndup(output, *handled_output_len = output_len);
|
2004-07-23 10:05:49 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*handled_output = NULL;
|
|
|
|
}
|
2002-05-03 16:00:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-27 07:20:34 +08:00
|
|
|
PHPAPI int php_url_scanner_add_var(char *name, int name_len, char *value, int value_len, int urlencode TSRMLS_DC)
|
2002-05-03 16:00:41 +08:00
|
|
|
{
|
2013-08-15 11:36:50 +08:00
|
|
|
char *encoded = NULL;
|
2002-05-03 16:00:41 +08:00
|
|
|
int encoded_len;
|
2002-05-05 02:33:13 +08:00
|
|
|
smart_str val;
|
|
|
|
|
2002-05-03 16:00:41 +08:00
|
|
|
if (! BG(url_adapt_state_ex).active) {
|
|
|
|
php_url_scanner_ex_activate(TSRMLS_C);
|
2010-05-31 18:29:43 +08:00
|
|
|
php_output_start_internal(ZEND_STRL("URL-Rewriter"), php_url_scanner_output_handler, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
|
2002-05-03 16:00:41 +08:00
|
|
|
BG(url_adapt_state_ex).active = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-05 02:33:13 +08:00
|
|
|
if (BG(url_adapt_state_ex).url_app.len != 0) {
|
|
|
|
smart_str_appends(&BG(url_adapt_state_ex).url_app, PG(arg_separator).output);
|
|
|
|
}
|
2002-05-03 16:00:41 +08:00
|
|
|
|
|
|
|
if (urlencode) {
|
|
|
|
encoded = php_url_encode(value, value_len, &encoded_len);
|
2002-05-05 02:33:13 +08:00
|
|
|
smart_str_setl(&val, encoded, encoded_len);
|
2002-05-03 16:00:41 +08:00
|
|
|
} else {
|
2002-05-05 02:33:13 +08:00
|
|
|
smart_str_setl(&val, value, value_len);
|
2002-05-03 16:00:41 +08:00
|
|
|
}
|
2002-05-05 02:33:13 +08:00
|
|
|
|
|
|
|
smart_str_appendl(&BG(url_adapt_state_ex).url_app, name, name_len);
|
|
|
|
smart_str_appendc(&BG(url_adapt_state_ex).url_app, '=');
|
|
|
|
smart_str_append(&BG(url_adapt_state_ex).url_app, &val);
|
|
|
|
|
|
|
|
smart_str_appends(&BG(url_adapt_state_ex).form_app, "<input type=\"hidden\" name=\"");
|
|
|
|
smart_str_appendl(&BG(url_adapt_state_ex).form_app, name, name_len);
|
|
|
|
smart_str_appends(&BG(url_adapt_state_ex).form_app, "\" value=\"");
|
|
|
|
smart_str_append(&BG(url_adapt_state_ex).form_app, &val);
|
|
|
|
smart_str_appends(&BG(url_adapt_state_ex).form_app, "\" />");
|
|
|
|
|
|
|
|
if (urlencode)
|
|
|
|
efree(encoded);
|
2002-05-03 16:00:41 +08:00
|
|
|
|
2002-05-05 02:33:13 +08:00
|
|
|
return SUCCESS;
|
2002-05-03 16:00:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-27 07:20:34 +08:00
|
|
|
PHPAPI int php_url_scanner_reset_vars(TSRMLS_D)
|
2002-05-03 16:00:41 +08:00
|
|
|
{
|
2002-05-05 02:33:13 +08:00
|
|
|
BG(url_adapt_state_ex).form_app.len = 0;
|
|
|
|
BG(url_adapt_state_ex).url_app.len = 0;
|
2002-05-03 16:00:41 +08:00
|
|
|
|
2005-04-03 20:19:23 +08:00
|
|
|
return SUCCESS;
|
2002-05-03 16:00:41 +08:00
|
|
|
}
|
|
|
|
|
2000-10-17 01:25:36 +08:00
|
|
|
PHP_MINIT_FUNCTION(url_scanner)
|
|
|
|
{
|
2002-05-03 16:00:41 +08:00
|
|
|
BG(url_adapt_state_ex).tags = NULL;
|
2000-10-17 22:17:21 +08:00
|
|
|
|
2002-05-05 02:33:13 +08:00
|
|
|
BG(url_adapt_state_ex).form_app.c = BG(url_adapt_state_ex).url_app.c = 0;
|
|
|
|
BG(url_adapt_state_ex).form_app.len = BG(url_adapt_state_ex).url_app.len = 0;
|
|
|
|
|
2000-10-17 11:02:47 +08:00
|
|
|
REGISTER_INI_ENTRIES();
|
|
|
|
return SUCCESS;
|
2000-10-17 01:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PHP_MSHUTDOWN_FUNCTION(url_scanner)
|
|
|
|
{
|
2000-10-17 11:02:47 +08:00
|
|
|
UNREGISTER_INI_ENTRIES();
|
2002-05-03 16:00:41 +08:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_RINIT_FUNCTION(url_scanner)
|
|
|
|
{
|
|
|
|
BG(url_adapt_state_ex).active = 0;
|
2002-05-05 02:33:13 +08:00
|
|
|
|
2002-05-03 16:00:41 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_RSHUTDOWN_FUNCTION(url_scanner)
|
|
|
|
{
|
|
|
|
if (BG(url_adapt_state_ex).active) {
|
|
|
|
php_url_scanner_ex_deactivate(TSRMLS_C);
|
|
|
|
BG(url_adapt_state_ex).active = 0;
|
|
|
|
}
|
|
|
|
|
2002-05-05 10:07:16 +08:00
|
|
|
smart_str_free(&BG(url_adapt_state_ex).form_app);
|
|
|
|
smart_str_free(&BG(url_adapt_state_ex).url_app);
|
|
|
|
|
2000-10-17 11:02:47 +08:00
|
|
|
return SUCCESS;
|
2000-10-17 01:25:36 +08:00
|
|
|
}
|