-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup
More file actions
291 lines (236 loc) · 9.15 KB
/
backup
File metadata and controls
291 lines (236 loc) · 9.15 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* flashpoint extension for PHP */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_flashpoint.h"
#include <SAPI.h>
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
ZEND_DECLARE_MODULE_GLOBALS(flashpoint)
static const php_stream_wrapper_ops *original_plain_ops = NULL;
static php_stream_wrapper_ops intercept_plain_ops;
zend_bool starts_with(zend_string *haystack, const char *needle, size_t needle_len) {
if (ZSTR_LEN(haystack) < needle_len) {
return 0;
}
return memcmp(ZSTR_VAL(haystack), needle, needle_len) == 0;
}
/* Fetch file from remote server */
static int fetch_from_remote(const char *local_path, const char *remote_server)
{
php_stream *stream;
php_stream *out_stream;
zend_string *remote_url;
size_t remote_url_len;
int result = FAILURE;
const char *path_to_use = local_path;
/* Handle absolute paths - strip document root or script directory */
if (IS_ABSOLUTE_PATH(local_path, strlen(local_path))) {
const char *root_path = NULL;
/* Check if running in CLI mode */
root_path = sapi_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT") - 1);
if (!root_path) {
// CLI mode, use script dir
zend_string *script_filename = zend_get_executed_filename_ex();
if (script_filename && ZSTR_LEN(script_filename) > 0) {
char *script_dir = estrndup(ZSTR_VAL(script_filename), ZSTR_LEN(script_filename));
char *last_slash = strrchr(script_dir, DEFAULT_SLASH);
if (last_slash) {
*last_slash = '\0';
root_path = script_dir;
php_printf("Using CLI script directory as root: %s\n", root_path);
} else {
efree(script_dir);
}
}
} else {
/* Use DOCUMENT_ROOT for CGI/FPM/Apache */
php_printf("Using DOCUMENT_ROOT as root: %s\n", root_path);
}
if (root_path && strlen(root_path) > 0) {
size_t root_len = strlen(root_path);
/* Check if local_path starts with root path */
if (strncmp(local_path, root_path, root_len) == 0) {
/* Skip root path and any leading slash */
path_to_use = local_path + root_len;
while (*path_to_use == '/' || *path_to_use == '\\') {
path_to_use++;
}
}
}
} else {
if (strlen(local_path) > 2 && local_path[0] == '.' && local_path[1] == '/') {
path_to_use = local_path + 2;
}
}
// Get relative path to check on remote server
zend_string *relative_path = zend_string_init(path_to_use, strlen(path_to_use), 0);
/* Build remote URL with full relative path */
remote_url_len = strlen(remote_server) + ZSTR_LEN(relative_path) + 2;
if (remote_server[strlen(remote_server) - 1] != '/') {
// Add / to end of remote server if missing
remote_url = zend_string_alloc(remote_url_len + 1 + ZSTR_LEN(relative_path), 0);
snprintf(ZSTR_VAL(remote_url), ZSTR_LEN(remote_url) + 1, "%s/%s", remote_server, ZSTR_VAL(relative_path));
} else {
remote_url = zend_string_alloc(remote_url_len + ZSTR_LEN(relative_path), 0);
snprintf(ZSTR_VAL(remote_url), ZSTR_LEN(remote_url) + 1, "%s%s", remote_server, ZSTR_VAL(relative_path));
}
php_printf("Attempting to fetch from path: %s\n", ZSTR_VAL(relative_path));
php_printf("Attempting to fetch from remote: %s\n", ZSTR_VAL(remote_url));
// Get stream from remote server
stream = php_stream_open_wrapper(ZSTR_VAL(remote_url), "rb", REPORT_ERRORS, NULL);
if (!stream) {
return FAILURE;
}
// Resolve real path
char *absolute_path;
absolute_path = expand_filepath(local_path, NULL);
if (!absolute_path) {
php_error_docref(NULL, E_WARNING, "Failed to resolve path: %s", local_path);
php_stream_close(stream);
return FAILURE;
}
php_printf("Attempting to save local file: %s\n", absolute_path);
// Get local file's directory
char *dir_path = estrndup(absolute_path, strlen(absolute_path));
char *last_slash = strrchr(dir_path, DEFAULT_SLASH);
if (last_slash) {
*last_slash = '\0';
// Make sure directory exists
php_stream_statbuf ssb;
if (php_stream_stat_path(dir_path, &ssb) != 0) {
// Windows is weird about false errors, just let it fail when opening further down
php_stream_mkdir(dir_path, 0777, PHP_STREAM_MKDIR_RECURSIVE | REPORT_ERRORS, NULL);
}
}
efree(dir_path);
// Create local file
out_stream = php_stream_open_wrapper(local_path, "wb", REPORT_ERRORS, NULL);
if (!out_stream) {
php_stream_close(stream);
return FAILURE;
}
// Copy response to file
if (php_stream_copy_to_stream(stream, out_stream, PHP_STREAM_COPY_ALL) != -1) {
result = SUCCESS;
php_printf("Successfully fetched and saved: %s\n", local_path);
}
php_stream_close(stream);
php_stream_close(out_stream);
return result;
}
/* Custom stream opener that logs and delegates to original */
static php_stream *intercept_stream_opener(php_stream_wrapper *wrapper, const char *path,
const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
{
php_stream *stream = NULL;
/* Print the filename being requested */
php_printf("File access intercepted: %s\n", path);
if (original_plain_ops && original_plain_ops->stream_opener) {
stream = original_plain_ops->stream_opener(wrapper, path, mode, options, opened_path, context STREAMS_CC);
}
if (!stream && FLASHPOINT_G(remote_server)) {
if (strchr(mode, 'r')) { /* Only for read mode */
if (fetch_from_remote(path, FLASHPOINT_G(remote_server)) == SUCCESS) {
/* Try opening again after fetching */
if (original_plain_ops && original_plain_ops->stream_opener) {
stream = original_plain_ops->stream_opener(wrapper, path, mode, options, opened_path, context STREAMS_CC);
}
}
}
}
return stream;
}
/* INI entries */
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("flashpoint.remote_server", "", PHP_INI_ALL, OnUpdateString, remote_server, zend_flashpoint_globals, flashpoint_globals)
PHP_INI_END()
/* Module globals initialization */
static void php_flashpoint_init_globals(zend_flashpoint_globals *flashpoint_globals)
{
flashpoint_globals->remote_server = NULL;
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(flashpoint)
{
php_stream_wrapper *wrapper;
ZEND_INIT_MODULE_GLOBALS(flashpoint, php_flashpoint_init_globals, NULL);
REGISTER_INI_ENTRIES();
// Get original plain files stream operations
wrapper = php_stream_locate_url_wrapper("file.txt", NULL, 0);
if (!wrapper || !wrapper->wops || !wrapper->wops->stream_opener) {
php_error_docref(NULL, E_WARNING, "Could not access plain files wrapper");
return FAILURE;
}
original_plain_ops = php_plain_files_wrapper.wops;
if (!original_plain_ops || !original_plain_ops->stream_opener) {
php_error_docref(NULL, E_WARNING, "Could not access plain files wrapper");
return FAILURE;
}
// Make a copy of existing stream operations
memcpy(&intercept_plain_ops, original_plain_ops, sizeof(php_stream_wrapper_ops));
/* Replace only the stream_opener */
intercept_plain_ops.stream_opener = intercept_stream_opener;
*((const php_stream_wrapper_ops **)&php_plain_files_wrapper.wops) = &intercept_plain_ops;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(flashpoint)
{
php_stream_wrapper *wrapper;
wrapper = php_stream_locate_url_wrapper("file.txt", NULL, 0);
if (original_plain_ops) {
*((const php_stream_wrapper_ops **)&wrapper->wops) = original_plain_ops;
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(flashpoint)
{
#if defined(ZTS) && defined(COMPILE_DL_FLASHPOINT)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(flashpoint)
{
php_info_print_table_start();
php_info_print_table_header(2, "flashpoint support", "enabled");
php_info_print_table_row(2, "file interception", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ flashpoint_module_entry
*/
zend_module_entry flashpoint_module_entry = {
STANDARD_MODULE_HEADER,
"flashpoint", /* Extension name */
NULL,
PHP_MINIT(flashpoint), /* PHP_MINIT - Module initialization */
PHP_MSHUTDOWN(flashpoint), /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(flashpoint), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(flashpoint), /* PHP_MINFO - Module info */
PHP_FLASHPOINT_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES};
/* }}} */
#ifdef COMPILE_DL_FLASHPOINT
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(flashpoint)
#endif