-
-
Notifications
You must be signed in to change notification settings - Fork 470
/
Copy pathrequire.c
187 lines (151 loc) · 4.77 KB
/
require.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
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
/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <team@zephir-lang.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. If you did not receive
* a copy of the license it is available through the world-wide-web at the
* following url: https://docs.zephir-lang.com/en/latest/license
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ext.h"
#include "kernel/require.h"
#include "kernel/backtrace.h"
#include <main/php_main.h>
#include <Zend/zend_exceptions.h>
#ifndef ENFORCE_SAFE_MODE
#define ENFORCE_SAFE_MODE 0
#endif
/**
* Do an internal require to a plain php file taking care of the value returned by the file
*/
int zephir_require_ret(zval *return_value_ptr, const char *require_path)
{
zend_file_handle file_handle;
zend_op_array *new_op_array;
zval dummy, local_retval;
int ret;
ZVAL_UNDEF(&local_retval);
#ifndef ZEPHIR_RELEASE
if (return_value_ptr != NULL && Z_TYPE_P(return_value_ptr) > IS_NULL) {
fprintf(stderr, "%s: *return_value_ptr is expected to be NULL", __func__);
zephir_print_backtrace();
abort();
}
#endif
#if PHP_VERSION_ID >= 80100
zend_string *zend_string_path = zend_string_init(require_path, strlen(require_path), 0);
zend_stream_init_filename_ex(&file_handle, zend_string_path);
ret = php_stream_open_for_zend_ex(&file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
zval_ptr_dtor(zend_string_path);
#else
ret = php_stream_open_for_zend_ex(require_path, &file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
#endif
if (ret != SUCCESS) {
return FAILURE;
}
new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
if (new_op_array) {
if (file_handle.handle.stream.handle) {
ZVAL_NULL(&dummy);
if (!file_handle.opened_path) {
file_handle.opened_path = zend_string_init(require_path, strlen(require_path), 0);
}
zend_hash_add(&EG(included_files), file_handle.opened_path, &dummy);
zend_destroy_file_handle(&file_handle);
}
new_op_array->scope = EG(fake_scope) ? EG(fake_scope) : zend_get_executed_scope();
zend_execute(new_op_array, &local_retval);
if (return_value_ptr) {
zval_ptr_dtor(return_value_ptr);
ZVAL_COPY_VALUE(return_value_ptr, &local_retval);
} else {
zval_ptr_dtor(&local_retval);
}
destroy_op_array(new_op_array);
efree_size(new_op_array, sizeof(zend_op_array));
if (EG(exception)) {
ret = FAILURE;
} else {
ret = SUCCESS;
}
return ret;
} else {
zend_destroy_file_handle(&file_handle);
}
return FAILURE;
}
/**
* Do an internal require once to a plain php file taking care of the value returned by the file
*/
int zephir_require_once_ret(zval *return_value_ptr, const char *require_path)
{
zend_file_handle file_handle;
zend_op_array *new_op_array;
zval dummy, local_retval;
int ret;
ZVAL_UNDEF(&local_retval);
#ifndef ZEPHIR_RELEASE
if (return_value_ptr != NULL && Z_TYPE_P(return_value_ptr) > IS_NULL) {
fprintf(stderr, "%s: *return_value_ptr is expected to be NULL", __func__);
zephir_print_backtrace();
abort();
}
#endif
#if PHP_VERSION_ID >= 80100
zend_string *zend_string_path = zend_string_init(require_path, strlen(require_path), 0);
zend_stream_init_filename_ex(&file_handle, zend_string_path);
ret = php_stream_open_for_zend_ex(&file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
zval_ptr_dtor(zend_string_path);
#else
ret = php_stream_open_for_zend_ex(require_path, &file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
#endif
if (ret != SUCCESS) {
return FAILURE;
}
if (zend_hash_exists(&EG(included_files), file_handle.opened_path)) {
zend_destroy_file_handle(&file_handle);
if (return_value_ptr) {
ZVAL_TRUE(&local_retval);
zval_ptr_dtor(return_value_ptr);
ZVAL_COPY_VALUE(return_value_ptr, &local_retval);
} else {
zval_ptr_dtor(&local_retval);
}
return SUCCESS;
}
new_op_array = zend_compile_file(&file_handle, ZEND_INCLUDE);
if (new_op_array) {
if (file_handle.handle.stream.handle) {
ZVAL_NULL(&dummy);
if (!file_handle.opened_path) {
file_handle.opened_path = zend_string_init(require_path, strlen(require_path), 0);
}
zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path);
zend_destroy_file_handle(&file_handle);
}
new_op_array->scope = EG(fake_scope) ? EG(fake_scope) : zend_get_executed_scope();
zend_execute(new_op_array, &local_retval);
if (return_value_ptr) {
zval_ptr_dtor(return_value_ptr);
ZVAL_COPY_VALUE(return_value_ptr, &local_retval);
} else {
zval_ptr_dtor(&local_retval);
}
destroy_op_array(new_op_array);
efree_size(new_op_array, sizeof(zend_op_array));
if (EG(exception)) {
ret = FAILURE;
} else {
ret = SUCCESS;
}
return ret;
} else {
zend_destroy_file_handle(&file_handle);
}
return FAILURE;
}