This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathytrace_file.c
153 lines (134 loc) · 4.72 KB
/
ytrace_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2016 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.0 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://xdebug.derickrethans.nl/license.php |
| If you did not receive a copy of the Xdebug license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_ytrace.h"
#include "ext/standard/php_lcg.h"
#include "ytrace_str.h"
ZEND_EXTERN_MODULE_GLOBALS(ytrace)
#define MICRO_IN_SEC 1000000.00
double ytrace_get_utime(void)
{
#ifdef HAVE_GETTIMEOFDAY
struct timeval tp;
long sec = 0L;
double msec = 0.0;
if (gettimeofday((struct timeval *) &tp, NULL) == 0) {
sec = tp.tv_sec;
msec = (double) (tp.tv_usec / MICRO_IN_SEC);
if (msec >= 1.0) {
msec -= (long) msec;
}
return msec + sec;
}
#endif
return 0;
}
void ytrace_format_output_filename(ytrace_str *filename, char *format)
{
char cwd[128];
TSRMLS_FETCH();
ytrace_str_append(filename, "/", 0);
while (*format)
{
if (*format != '%') {
ytrace_str_appendl(filename, (char *) format, 1, 0);
} else {
format++;
switch (*format)
{
case 'p': /* pid */
ytrace_str_append(filename, ytrace_sprintf("%ld", getpid()), 1);
break;
case 'r': /* random number */
ytrace_str_append(filename, ytrace_sprintf("%06x", (long) (1000000 * php_combined_lcg(TSRMLS_C))), 1);
break;
case 't': { /* timestamp (in seconds) */
time_t the_time = time(NULL);
ytrace_str_append(filename, ytrace_sprintf("%ld", the_time), 1);
} break;
case 'u': { /* timestamp (in microseconds) */
char *char_ptr, *utime_str = ytrace_sprintf("%F", ytrace_get_utime());
/* Replace . with _ (or should it be nuked?) */
char_ptr = strrchr(utime_str, '.');
if (char_ptr) {
char_ptr[0] = '_';
}
ytrace_str_append(filename, utime_str, 1);
} break;
case 'H': /* $_SERVER['HTTP_HOST'] */
case 'U': /* $_SERVER['UNIQUE_ID'] */
case 'R': { /* $_SERVER['REQUEST_URI'] */
char *char_ptr, *strval;
#if PHP_VERSION_ID >= 70000
zval *data = NULL;
if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY) {
switch (*format) {
case 'H':
data = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_HOST", sizeof("HTTP_HOST") - 1);
break;
case 'R':
data = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "REQUEST_URI", sizeof("REQUEST_URI") - 1);
break;
case 'U':
data = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "UNIQUE_ID", sizeof("UNIQUE_ID") - 1);
break;
}
if (data) {
strval = estrdup(Z_STRVAL_P(data));
#else
int retval = FAILURE;
zval **data;
if (PG(http_globals)[TRACK_VARS_SERVER]) {
switch (*format) {
case 'H':
retval = zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_HOST", sizeof("HTTP_HOST"), (void **) &data);
break;
case 'R':
retval = zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "REQUEST_URI", sizeof("REQUEST_URI"), (void **) &data);
break;
case 'U':
retval = zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "UNIQUE_ID", sizeof("UNIQUE_ID"), (void **) &data);
break;
}
if (retval == SUCCESS) {
strval = estrdup(Z_STRVAL_PP(data));
#endif
char *pos = strstr(strval, "?");
if (pos) {
*pos = '\0';
}
/* replace slashes, dots, question marks, plus
* signs, ampersands, spaces and other evil chars
* with underscores */
while ((char_ptr = strpbrk(strval, "/\\.?&+:*\"<>| ")) != NULL) {
char_ptr[0] = '_';
}
ytrace_str_append(filename, strval, 0);
efree(strval);
}
}
} break;
case '%': /* literal % */
ytrace_str_appendl(filename, "%", 1, 0);
break;
}
}
format++;
}
ytrace_str_append(filename, ".yt", 0);
}