Skip to content

Commit 5de7c7f

Browse files
authored
Merge pull request #249 from davidcole1340/process-globals
Add streams API, ProcessGlobals, FileGlobals and SapiGlobals
2 parents bb8f608 + 961b948 commit 5de7c7f

File tree

12 files changed

+718
-32
lines changed

12 files changed

+718
-32
lines changed

allowed_bindings.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ bind! {
245245
zend_class_serialize_deny,
246246
zend_class_unserialize_deny,
247247
zend_executor_globals,
248-
sapi_globals_struct,
249248
sapi_module_struct,
250249
zend_objects_store_del,
251250
zend_hash_move_forward_ex,
@@ -257,13 +256,39 @@ bind! {
257256
gc_possible_root,
258257
ZEND_ACC_NOT_SERIALIZABLE,
259258
executor_globals,
259+
php_core_globals,
260+
core_globals,
261+
sapi_globals_struct,
260262
sapi_globals,
261263
sapi_module,
262264
php_printf,
263265
__zend_malloc,
264266
tsrm_get_ls_cache,
265267
executor_globals_offset,
268+
core_globals_offset,
266269
sapi_globals_offset,
270+
php_file_globals,
271+
file_globals,
272+
file_globals_id,
273+
TRACK_VARS_POST,
274+
TRACK_VARS_GET,
275+
TRACK_VARS_COOKIE,
276+
TRACK_VARS_SERVER,
277+
TRACK_VARS_ENV,
278+
TRACK_VARS_FILES,
279+
TRACK_VARS_REQUEST,
280+
sapi_request_info,
281+
sapi_header_struct,
282+
zend_is_auto_global,
283+
zend_llist_get_next_ex,
284+
zend_llist_get_prev_ex,
285+
php_register_url_stream_wrapper,
286+
php_stream_locate_url_wrapper,
287+
php_unregister_url_stream_wrapper,
288+
php_unregister_url_stream_wrapper_volatile,
289+
php_register_url_stream_wrapper_volatile,
290+
php_stream_wrapper,
291+
php_stream_stdio_ops,
267292
zend_atomic_bool_store,
268293
zend_interrupt_function,
269294
zend_eval_string,

docsrs_bindings.rs

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ pub const ZEND_MODULE_API_NO: u32 = 20230831;
182182
pub const USING_ZTS: u32 = 0;
183183
pub const MAY_BE_BOOL: u32 = 12;
184184
pub const MAY_BE_ANY: u32 = 1022;
185+
pub const TRACK_VARS_POST: u32 = 0;
186+
pub const TRACK_VARS_GET: u32 = 1;
187+
pub const TRACK_VARS_COOKIE: u32 = 2;
188+
pub const TRACK_VARS_SERVER: u32 = 3;
189+
pub const TRACK_VARS_ENV: u32 = 4;
190+
pub const TRACK_VARS_FILES: u32 = 5;
191+
pub const TRACK_VARS_REQUEST: u32 = 6;
185192
pub const PHP_INI_USER: u32 = 1;
186193
pub const PHP_INI_PERDIR: u32 = 2;
187194
pub const PHP_INI_SYSTEM: u32 = 4;
@@ -506,6 +513,19 @@ pub struct _zend_llist {
506513
pub traverse_ptr: *mut zend_llist_element,
507514
}
508515
pub type zend_llist = _zend_llist;
516+
pub type zend_llist_position = *mut zend_llist_element;
517+
extern "C" {
518+
pub fn zend_llist_get_next_ex(
519+
l: *mut zend_llist,
520+
pos: *mut zend_llist_position,
521+
) -> *mut ::std::os::raw::c_void;
522+
}
523+
extern "C" {
524+
pub fn zend_llist_get_prev_ex(
525+
l: *mut zend_llist,
526+
pos: *mut zend_llist_position,
527+
) -> *mut ::std::os::raw::c_void;
528+
}
509529
pub type zend_string_init_interned_func_t = ::std::option::Option<
510530
unsafe extern "C" fn(
511531
str_: *const ::std::os::raw::c_char,
@@ -1469,6 +1489,9 @@ pub struct _zend_executor_globals {
14691489
pub reserved_stack_size: zend_ulong,
14701490
pub reserved: [*mut ::std::os::raw::c_void; 6usize],
14711491
}
1492+
extern "C" {
1493+
pub fn zend_is_auto_global(name: *mut zend_string) -> bool;
1494+
}
14721495
pub type zend_module_entry = _zend_module_entry;
14731496
#[repr(C)]
14741497
#[derive(Debug, Copy, Clone)]
@@ -2094,6 +2117,127 @@ impl _php_stream {
20942117
__bindgen_bitfield_unit
20952118
}
20962119
}
2120+
extern "C" {
2121+
pub static mut php_stream_stdio_ops: php_stream_ops;
2122+
}
2123+
extern "C" {
2124+
pub fn php_register_url_stream_wrapper(
2125+
protocol: *const ::std::os::raw::c_char,
2126+
wrapper: *const php_stream_wrapper,
2127+
) -> zend_result;
2128+
}
2129+
extern "C" {
2130+
pub fn php_unregister_url_stream_wrapper(
2131+
protocol: *const ::std::os::raw::c_char,
2132+
) -> zend_result;
2133+
}
2134+
extern "C" {
2135+
pub fn php_register_url_stream_wrapper_volatile(
2136+
protocol: *mut zend_string,
2137+
wrapper: *mut php_stream_wrapper,
2138+
) -> zend_result;
2139+
}
2140+
extern "C" {
2141+
pub fn php_unregister_url_stream_wrapper_volatile(protocol: *mut zend_string) -> zend_result;
2142+
}
2143+
extern "C" {
2144+
pub fn php_stream_locate_url_wrapper(
2145+
path: *const ::std::os::raw::c_char,
2146+
path_for_open: *mut *const ::std::os::raw::c_char,
2147+
options: ::std::os::raw::c_int,
2148+
) -> *mut php_stream_wrapper;
2149+
}
2150+
pub type php_core_globals = _php_core_globals;
2151+
#[repr(C)]
2152+
pub struct _php_core_globals {
2153+
pub output_buffering: zend_long,
2154+
pub implicit_flush: bool,
2155+
pub enable_dl: bool,
2156+
pub display_errors: u8,
2157+
pub display_startup_errors: bool,
2158+
pub log_errors: bool,
2159+
pub ignore_repeated_errors: bool,
2160+
pub ignore_repeated_source: bool,
2161+
pub report_memleaks: bool,
2162+
pub output_handler: *mut ::std::os::raw::c_char,
2163+
pub unserialize_callback_func: *mut ::std::os::raw::c_char,
2164+
pub serialize_precision: zend_long,
2165+
pub memory_limit: zend_long,
2166+
pub max_input_time: zend_long,
2167+
pub error_log: *mut ::std::os::raw::c_char,
2168+
pub doc_root: *mut ::std::os::raw::c_char,
2169+
pub user_dir: *mut ::std::os::raw::c_char,
2170+
pub include_path: *mut ::std::os::raw::c_char,
2171+
pub open_basedir: *mut ::std::os::raw::c_char,
2172+
pub open_basedir_modified: bool,
2173+
pub extension_dir: *mut ::std::os::raw::c_char,
2174+
pub php_binary: *mut ::std::os::raw::c_char,
2175+
pub sys_temp_dir: *mut ::std::os::raw::c_char,
2176+
pub upload_tmp_dir: *mut ::std::os::raw::c_char,
2177+
pub upload_max_filesize: zend_long,
2178+
pub error_append_string: *mut ::std::os::raw::c_char,
2179+
pub error_prepend_string: *mut ::std::os::raw::c_char,
2180+
pub auto_prepend_file: *mut ::std::os::raw::c_char,
2181+
pub auto_append_file: *mut ::std::os::raw::c_char,
2182+
pub input_encoding: *mut ::std::os::raw::c_char,
2183+
pub internal_encoding: *mut ::std::os::raw::c_char,
2184+
pub output_encoding: *mut ::std::os::raw::c_char,
2185+
pub arg_separator: arg_separators,
2186+
pub variables_order: *mut ::std::os::raw::c_char,
2187+
pub rfc1867_protected_variables: HashTable,
2188+
pub connection_status: ::std::os::raw::c_short,
2189+
pub ignore_user_abort: bool,
2190+
pub header_is_being_sent: ::std::os::raw::c_uchar,
2191+
pub tick_functions: zend_llist,
2192+
pub http_globals: [zval; 6usize],
2193+
pub expose_php: bool,
2194+
pub register_argc_argv: bool,
2195+
pub auto_globals_jit: bool,
2196+
pub html_errors: bool,
2197+
pub xmlrpc_errors: bool,
2198+
pub docref_root: *mut ::std::os::raw::c_char,
2199+
pub docref_ext: *mut ::std::os::raw::c_char,
2200+
pub xmlrpc_error_number: zend_long,
2201+
pub activated_auto_globals: [bool; 8usize],
2202+
pub modules_activated: bool,
2203+
pub file_uploads: bool,
2204+
pub during_request_startup: bool,
2205+
pub allow_url_fopen: bool,
2206+
pub enable_post_data_reading: bool,
2207+
pub report_zend_debug: bool,
2208+
pub last_error_type: ::std::os::raw::c_int,
2209+
pub last_error_lineno: ::std::os::raw::c_int,
2210+
pub last_error_message: *mut zend_string,
2211+
pub last_error_file: *mut zend_string,
2212+
pub php_sys_temp_dir: *mut ::std::os::raw::c_char,
2213+
pub disable_classes: *mut ::std::os::raw::c_char,
2214+
pub max_input_nesting_level: zend_long,
2215+
pub max_input_vars: zend_long,
2216+
pub user_ini_filename: *mut ::std::os::raw::c_char,
2217+
pub user_ini_cache_ttl: zend_long,
2218+
pub request_order: *mut ::std::os::raw::c_char,
2219+
pub mail_log: *mut ::std::os::raw::c_char,
2220+
pub mail_x_header: bool,
2221+
pub mail_mixed_lf_and_crlf: bool,
2222+
pub in_error_log: bool,
2223+
pub allow_url_include: bool,
2224+
pub in_user_include: bool,
2225+
pub have_called_openlog: bool,
2226+
pub syslog_facility: zend_long,
2227+
pub syslog_ident: *mut ::std::os::raw::c_char,
2228+
pub syslog_filter: zend_long,
2229+
pub error_log_mode: zend_long,
2230+
}
2231+
extern "C" {
2232+
pub static mut core_globals: _php_core_globals;
2233+
}
2234+
#[repr(C)]
2235+
#[derive(Debug, Copy, Clone)]
2236+
pub struct _arg_separators {
2237+
pub output: *mut ::std::os::raw::c_char,
2238+
pub input: *mut ::std::os::raw::c_char,
2239+
}
2240+
pub type arg_separators = _arg_separators;
20972241
#[repr(C)]
20982242
#[derive(Debug, Copy, Clone)]
20992243
pub struct _zend_ini_entry_def {
@@ -2201,6 +2345,37 @@ extern "C" {
22012345
extern "C" {
22022346
pub fn php_info_print_table_end();
22032347
}
2348+
#[repr(C)]
2349+
#[derive(Debug, Copy, Clone)]
2350+
pub struct hostent {
2351+
pub h_name: *mut ::std::os::raw::c_char,
2352+
pub h_aliases: *mut *mut ::std::os::raw::c_char,
2353+
pub h_addrtype: ::std::os::raw::c_int,
2354+
pub h_length: ::std::os::raw::c_int,
2355+
pub h_addr_list: *mut *mut ::std::os::raw::c_char,
2356+
}
2357+
#[repr(C)]
2358+
#[derive(Debug, Copy, Clone)]
2359+
pub struct php_file_globals {
2360+
pub pclose_ret: ::std::os::raw::c_int,
2361+
pub def_chunk_size: usize,
2362+
pub auto_detect_line_endings: bool,
2363+
pub default_socket_timeout: zend_long,
2364+
pub user_agent: *mut ::std::os::raw::c_char,
2365+
pub from_address: *mut ::std::os::raw::c_char,
2366+
pub user_stream_current_filename: *const ::std::os::raw::c_char,
2367+
pub default_context: *mut php_stream_context,
2368+
pub stream_wrappers: *mut HashTable,
2369+
pub stream_filters: *mut HashTable,
2370+
pub wrapper_errors: *mut HashTable,
2371+
pub pclose_wait: ::std::os::raw::c_int,
2372+
pub tmp_host_info: hostent,
2373+
pub tmp_host_buf: *mut ::std::os::raw::c_char,
2374+
pub tmp_host_buf_len: usize,
2375+
}
2376+
extern "C" {
2377+
pub static mut file_globals: php_file_globals;
2378+
}
22042379
extern "C" {
22052380
pub static mut zend_ce_throwable: *mut zend_class_entry;
22062381
}

src/error.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ pub enum Error {
6565
IntegerOverflow,
6666
/// An exception was thrown in a function.
6767
Exception(ZBox<ZendObject>),
68+
/// A failure occurred while registering the stream wrapper
69+
StreamWrapperRegistrationFailure,
70+
/// A failure occurred while unregistering the stream wrapper
71+
StreamWrapperUnregistrationFailure,
6872
}
6973

7074
impl Display for Error {
@@ -99,6 +103,15 @@ impl Display for Error {
99103
write!(f, "Converting integer arguments resulted in an overflow.")
100104
}
101105
Error::Exception(e) => write!(f, "Exception was thrown: {e:?}"),
106+
Error::StreamWrapperRegistrationFailure => {
107+
write!(f, "A failure occurred while registering the stream wrapper")
108+
}
109+
Error::StreamWrapperUnregistrationFailure => {
110+
write!(
111+
f,
112+
"A failure occurred while unregistering the stream wrapper"
113+
)
114+
}
102115
}
103116
}
104117
}

src/exception.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl PhpException {
6969

7070
/// Set the Zval object for the exception.
7171
///
72-
/// Exceptions can be based of instantiated Zval objects when you are throwing a custom exception with
73-
/// stateful properties.
72+
/// Exceptions can be based of instantiated Zval objects when you are
73+
/// throwing a custom exception with stateful properties.
7474
///
7575
/// # Parameters
7676
///

src/ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ extern "C" {
2626
pub fn ext_php_rs_zend_object_alloc(obj_size: usize, ce: *mut zend_class_entry) -> *mut c_void;
2727
pub fn ext_php_rs_zend_object_release(obj: *mut zend_object);
2828
pub fn ext_php_rs_executor_globals() -> *mut zend_executor_globals;
29+
pub fn ext_php_rs_process_globals() -> *mut php_core_globals;
2930
pub fn ext_php_rs_sapi_globals() -> *mut sapi_globals_struct;
31+
pub fn ext_php_rs_file_globals() -> *mut php_file_globals;
3032
pub fn ext_php_rs_sapi_module() -> *mut sapi_module_struct;
3133
pub fn ext_php_rs_zend_try_catch(
3234
func: unsafe extern "C" fn(*const c_void) -> *const c_void,

src/types/zval.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ impl Zval {
222222
}
223223
}
224224

225-
/// Returns a mutable reference to the zval if it is an internal indirect reference.
225+
/// Returns a mutable reference to the zval if it is an internal indirect
226+
/// reference.
226227
pub fn indirect_mut(&self) -> Option<&mut Zval> {
227228
if self.is_indirect() {
228229
Some(unsafe { &mut *(self.value.zv as *mut Zval) })

src/wrapper.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ zend_executor_globals *ext_php_rs_executor_globals() {
4040
#endif
4141
}
4242

43+
php_core_globals *ext_php_rs_process_globals() {
44+
#ifdef ZTS
45+
#ifdef ZEND_ENABLE_STATIC_TSRMLS_CACHE
46+
return TSRMG_FAST_BULK_STATIC(core_globals_offset, php_core_globals);
47+
#else
48+
return TSRMG_FAST_BULK(core_globals_offset, php_core_globals *);
49+
#endif
50+
#else
51+
return &core_globals;
52+
#endif
53+
}
54+
4355
sapi_globals_struct *ext_php_rs_sapi_globals() {
4456
#ifdef ZTS
4557
#ifdef ZEND_ENABLE_STATIC_TSRMLS_CACHE
@@ -52,6 +64,14 @@ sapi_globals_struct *ext_php_rs_sapi_globals() {
5264
#endif
5365
}
5466

67+
php_file_globals *ext_php_rs_file_globals() {
68+
#ifdef ZTS
69+
return TSRMG_FAST_BULK(file_globals_id, php_file_globals *);
70+
#else
71+
return &file_globals;
72+
#endif
73+
}
74+
5575
sapi_module_struct *ext_php_rs_sapi_module() {
5676
return &sapi_module;
5777
}

src/wrapper.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
#include "php.h"
1818

1919
#include "ext/standard/info.h"
20+
#include "ext/standard/php_var.h"
21+
#include "ext/standard/file.h"
2022
#include "zend_exceptions.h"
2123
#include "zend_inheritance.h"
2224
#include "zend_interfaces.h"
25+
#include "php_variables.h"
2326
#include "zend_ini.h"
2427
#include "main/SAPI.h"
2528

@@ -31,8 +34,10 @@ void ext_php_rs_set_known_valid_utf8(zend_string *zs);
3134
const char *ext_php_rs_php_build_id();
3235
void *ext_php_rs_zend_object_alloc(size_t obj_size, zend_class_entry *ce);
3336
void ext_php_rs_zend_object_release(zend_object *obj);
34-
zend_executor_globals *ext_php_rs_executor_globals();;
37+
zend_executor_globals *ext_php_rs_executor_globals();
38+
php_core_globals *ext_php_rs_process_globals();
3539
sapi_globals_struct *ext_php_rs_sapi_globals();
40+
php_file_globals *ext_php_rs_file_globals();
3641
sapi_module_struct *ext_php_rs_sapi_module();
3742
bool ext_php_rs_zend_try_catch(void* (*callback)(void *), void *ctx, void **result);
3843
void ext_php_rs_zend_bailout();

0 commit comments

Comments
 (0)