1use parking_lot::{ArcRwLockReadGuard, ArcRwLockWriteGuard, RawRwLock, RwLock};
4use std::collections::HashMap;
5use std::ffi::CStr;
6use std::ops::{Deref, DerefMut};
7use std::slice;
8use std::str;
9use std::sync::{Arc, LazyLock};
10
11use crate::boxed::ZBox;
12use crate::exception::PhpResult;
13#[cfg(php82)]
14use crate::ffi::zend_atomic_bool_store;
15use crate::ffi::{
16 _sapi_module_struct, _zend_compiler_globals, _zend_executor_globals,
17 ext_php_rs_compiler_globals, ext_php_rs_executor_globals, ext_php_rs_file_globals,
18 ext_php_rs_process_globals, ext_php_rs_sapi_globals, ext_php_rs_sapi_module, php_core_globals,
19 php_file_globals, sapi_globals_struct, sapi_header_struct, sapi_headers_struct,
20 sapi_request_info, zend_ini_entry, zend_is_auto_global, TRACK_VARS_COOKIE, TRACK_VARS_ENV,
21 TRACK_VARS_FILES, TRACK_VARS_GET, TRACK_VARS_POST, TRACK_VARS_SERVER,
22};
23#[cfg(not(php81))]
24use crate::ffi::{_zend_hash_find_known_hash, _zend_string};
25#[cfg(php81)]
26use crate::ffi::{
27 _zend_known_string_id_ZEND_STR_AUTOGLOBAL_REQUEST, zend_hash_find_known_hash,
28 zend_known_strings,
29};
30
31use crate::types::{ZendHashTable, ZendObject, ZendStr};
32
33use super::linked_list::ZendLinkedListIterator;
34
35pub type ExecutorGlobals = _zend_executor_globals;
37
38impl ExecutorGlobals {
39 pub fn get() -> GlobalReadGuard<Self> {
51 let globals = unsafe { ext_php_rs_executor_globals().as_ref() }
54 .expect("Static executor globals were invalid");
55
56 cfg_if::cfg_if! {
57 if #[cfg(php_zts)] {
58 let guard = lock::GLOBALS_LOCK.with(RwLock::read_arc);
59 } else {
60 let guard = lock::GLOBALS_LOCK.read_arc();
61 }
62 }
63
64 GlobalReadGuard { globals, guard }
65 }
66
67 pub fn get_mut() -> GlobalWriteGuard<Self> {
79 let globals = unsafe { ext_php_rs_executor_globals().as_mut() }
82 .expect("Static executor globals were invalid");
83
84 cfg_if::cfg_if! {
85 if #[cfg(php_zts)] {
86 let guard = lock::GLOBALS_LOCK.with(RwLock::write_arc);
87 } else {
88 let guard = lock::GLOBALS_LOCK.write_arc();
89 }
90 }
91
92 GlobalWriteGuard { globals, guard }
93 }
94
95 #[must_use]
97 pub fn class_table(&self) -> Option<&ZendHashTable> {
98 unsafe { self.class_table.as_ref() }
99 }
100
101 #[must_use]
103 pub fn function_table(&self) -> Option<&ZendHashTable> {
104 unsafe { self.function_table.as_ref() }
105 }
106
107 #[allow(clippy::mut_from_ref)]
111 #[must_use]
112 pub fn function_table_mut(&self) -> Option<&mut ZendHashTable> {
113 unsafe { self.function_table.as_mut() }
114 }
115
116 #[must_use]
124 pub fn ini_values(&self) -> HashMap<String, Option<String>> {
125 let hash_table = unsafe { &*self.ini_directives };
126 let mut ini_hash_map: HashMap<String, Option<String>> = HashMap::new();
127 for (key, value) in hash_table {
128 ini_hash_map.insert(key.to_string(), unsafe {
129 let ini_entry = &*value.ptr::<zend_ini_entry>().expect("Invalid ini entry");
130 if ini_entry.value.is_null() {
131 None
132 } else {
133 Some(
134 (*ini_entry.value)
135 .as_str()
136 .expect("Ini value is not a string")
137 .to_owned(),
138 )
139 }
140 });
141 }
142 ini_hash_map
143 }
144
145 #[must_use]
147 pub fn constants(&self) -> Option<&ZendHashTable> {
148 unsafe { self.zend_constants.as_ref() }
149 }
150
151 #[must_use]
158 pub fn take_exception() -> Option<ZBox<ZendObject>> {
159 {
160 if Self::get().exception.is_null() {
162 return None;
163 }
164 }
165
166 let mut globals = Self::get_mut();
167
168 let mut exception_ptr = std::ptr::null_mut();
169 std::mem::swap(&mut exception_ptr, &mut globals.exception);
170
171 Some(unsafe { ZBox::from_raw(exception_ptr.as_mut()?) })
173 }
174
175 #[must_use]
177 pub fn has_exception() -> bool {
178 !Self::get().exception.is_null()
179 }
180
181 pub fn throw_if_exception() -> PhpResult<()> {
193 if let Some(e) = Self::take_exception() {
194 Err(crate::error::Error::Exception(e).into())
195 } else {
196 Ok(())
197 }
198 }
199
200 pub fn request_interrupt(&mut self) {
204 cfg_if::cfg_if! {
205 if #[cfg(php82)] {
206 unsafe {
207 zend_atomic_bool_store(&raw mut self.vm_interrupt, true);
208 }
209 } else {
210 self.vm_interrupt = true;
211 }
212 }
213 }
214
215 pub fn cancel_interrupt(&mut self) {
217 cfg_if::cfg_if! {
218 if #[cfg(php82)] {
219 unsafe {
220 zend_atomic_bool_store(&raw mut self.vm_interrupt, false);
221 }
222 } else {
223 self.vm_interrupt = true;
224 }
225 }
226 }
227}
228
229pub type CompilerGlobals = _zend_compiler_globals;
230
231impl CompilerGlobals {
232 pub fn get() -> GlobalReadGuard<Self> {
244 let globals = unsafe { ext_php_rs_compiler_globals().as_ref() }
247 .expect("Static compiler globals were invalid");
248
249 cfg_if::cfg_if! {
250 if #[cfg(php_zts)] {
251 let guard = lock::GLOBALS_LOCK.with(RwLock::read_arc);
252 } else {
253 let guard = lock::GLOBALS_LOCK.read_arc();
254 }
255 }
256
257 GlobalReadGuard { globals, guard }
258 }
259
260 pub fn get_mut() -> GlobalWriteGuard<Self> {
272 let globals = unsafe { ext_php_rs_compiler_globals().as_mut() }
275 .expect("Static compiler globals were invalid");
276
277 cfg_if::cfg_if! {
278 if #[cfg(php_zts)] {
279 let guard = lock::GLOBALS_LOCK.with(RwLock::write_arc);
280 } else {
281 let guard = lock::GLOBALS_LOCK.write_arc();
282 }
283 }
284
285 GlobalWriteGuard { globals, guard }
286 }
287}
288
289pub type SapiModule = _sapi_module_struct;
291
292impl SapiModule {
293 pub fn get() -> GlobalReadGuard<Self> {
305 let globals = unsafe { ext_php_rs_sapi_module().as_ref() }
308 .expect("Static executor globals were invalid");
309 let guard = SAPI_MODULE_LOCK.read_arc();
310 GlobalReadGuard { globals, guard }
311 }
312
313 pub fn get_mut() -> GlobalWriteGuard<Self> {
325 let globals = unsafe { ext_php_rs_sapi_module().as_mut() }
328 .expect("Static executor globals were invalid");
329 let guard = SAPI_MODULE_LOCK.write_arc();
330 GlobalWriteGuard { globals, guard }
331 }
332}
333
334pub type ProcessGlobals = php_core_globals;
336
337impl ProcessGlobals {
338 pub fn get() -> GlobalReadGuard<Self> {
346 let globals = unsafe { &*ext_php_rs_process_globals() };
349
350 cfg_if::cfg_if! {
351 if #[cfg(php_zts)] {
352 let guard = lock::PROCESS_GLOBALS_LOCK.with(RwLock::read_arc);
353 } else {
354 let guard = lock::PROCESS_GLOBALS_LOCK.read_arc();
355 }
356 }
357
358 GlobalReadGuard { globals, guard }
359 }
360
361 pub fn get_mut() -> GlobalWriteGuard<Self> {
369 let globals = unsafe { &mut *ext_php_rs_process_globals() };
372
373 cfg_if::cfg_if! {
374 if #[cfg(php_zts)] {
375 let guard = lock::PROCESS_GLOBALS_LOCK.with(RwLock::write_arc);
376 } else {
377 let guard = lock::PROCESS_GLOBALS_LOCK.write_arc();
378 }
379 }
380
381 GlobalWriteGuard { globals, guard }
382 }
383
384 #[must_use]
386 pub fn http_server_vars(&self) -> Option<&ZendHashTable> {
387 if !self.http_globals[TRACK_VARS_SERVER as usize].is_array() {
390 let name = ZendStr::new("_SERVER", false).as_mut_ptr();
391 unsafe { zend_is_auto_global(name) };
392 }
393 if self.http_globals[TRACK_VARS_SERVER as usize].is_array() {
394 self.http_globals[TRACK_VARS_SERVER as usize].array()
395 } else {
396 None
397 }
398 }
399
400 #[must_use]
406 pub fn http_post_vars(&self) -> &ZendHashTable {
407 self.http_globals[TRACK_VARS_POST as usize]
408 .array()
409 .expect("Type is not a ZendArray")
410 }
411
412 #[must_use]
418 pub fn http_get_vars(&self) -> &ZendHashTable {
419 self.http_globals[TRACK_VARS_GET as usize]
420 .array()
421 .expect("Type is not a ZendArray")
422 }
423
424 #[must_use]
430 pub fn http_cookie_vars(&self) -> &ZendHashTable {
431 self.http_globals[TRACK_VARS_COOKIE as usize]
432 .array()
433 .expect("Type is not a ZendArray")
434 }
435
436 pub fn http_request_vars(&self) -> Option<&ZendHashTable> {
443 cfg_if::cfg_if! {
444 if #[cfg(php81)] {
445 let key = unsafe {
446 *zend_known_strings.add(_zend_known_string_id_ZEND_STR_AUTOGLOBAL_REQUEST as usize)
447 };
448 } else {
449 let key = _zend_string::new("_REQUEST", false).as_mut_ptr();
450 }
451 };
452
453 assert!(
456 unsafe { zend_is_auto_global(key) },
457 "Failed to get request global"
458 );
459
460 let symbol_table = &ExecutorGlobals::get().symbol_table;
461 cfg_if::cfg_if! {
462 if #[cfg(php81)] {
463 let request = unsafe { zend_hash_find_known_hash(symbol_table, key) };
464 } else {
465 let request = unsafe { _zend_hash_find_known_hash(symbol_table, key) };
466 }
467 };
468
469 if request.is_null() {
470 return None;
471 }
472
473 Some(unsafe { (*request).array() }.expect("Type is not a ZendArray"))
474 }
475
476 #[must_use]
482 pub fn http_env_vars(&self) -> &ZendHashTable {
483 self.http_globals[TRACK_VARS_ENV as usize]
484 .array()
485 .expect("Type is not a ZendArray")
486 }
487
488 #[must_use]
494 pub fn http_files_vars(&self) -> &ZendHashTable {
495 self.http_globals[TRACK_VARS_FILES as usize]
496 .array()
497 .expect("Type is not a ZendArray")
498 }
499}
500
501pub type SapiGlobals = sapi_globals_struct;
503
504impl SapiGlobals {
505 #[must_use]
513 pub fn get() -> GlobalReadGuard<Self> {
514 let globals = unsafe { &*ext_php_rs_sapi_globals() };
517
518 cfg_if::cfg_if! {
519 if #[cfg(php_zts)] {
520 let guard = lock::SAPI_GLOBALS_LOCK.with(RwLock::read_arc);
521 } else {
522 let guard = lock::SAPI_GLOBALS_LOCK.read_arc();
523 }
524 }
525
526 GlobalReadGuard { globals, guard }
527 }
528
529 pub fn get_mut() -> GlobalWriteGuard<Self> {
537 let globals = unsafe { &mut *ext_php_rs_sapi_globals() };
540
541 cfg_if::cfg_if! {
542 if #[cfg(php_zts)] {
543 let guard = lock::SAPI_GLOBALS_LOCK.with(RwLock::write_arc);
544 } else {
545 let guard = lock::SAPI_GLOBALS_LOCK.write_arc();
546 }
547 }
548
549 GlobalWriteGuard { globals, guard }
550 }
551
552 #[must_use]
554 pub fn request_info(&self) -> &SapiRequestInfo {
555 &self.request_info
556 }
557
558 #[must_use]
560 pub fn sapi_headers(&self) -> &SapiHeaders {
561 &self.sapi_headers
562 }
563}
564
565pub type SapiHeaders = sapi_headers_struct;
566
567impl<'a> SapiHeaders {
568 pub fn headers(&'a mut self) -> ZendLinkedListIterator<'a, SapiHeader> {
570 self.headers.iter()
571 }
572}
573
574pub type SapiHeader = sapi_header_struct;
575
576impl<'a> SapiHeader {
577 #[must_use]
583 pub fn as_str(&'a self) -> &'a str {
584 unsafe {
585 let slice = slice::from_raw_parts(self.header as *const u8, self.header_len);
586 str::from_utf8(slice).expect("Invalid header string")
587 }
588 }
589
590 #[must_use]
592 pub fn name(&'a self) -> &'a str {
593 self.as_str().split(':').next().unwrap_or("").trim()
594 }
595
596 #[must_use]
598 pub fn value(&'a self) -> Option<&'a str> {
599 self.as_str().split_once(':').map(|(_, value)| value.trim())
600 }
601}
602
603pub type SapiRequestInfo = sapi_request_info;
604
605impl SapiRequestInfo {
606 #[must_use]
608 pub fn request_method(&self) -> Option<&str> {
609 if self.request_method.is_null() {
610 return None;
611 }
612 unsafe { CStr::from_ptr(self.request_method).to_str().ok() }
613 }
614
615 #[must_use]
617 pub fn query_string(&self) -> Option<&str> {
618 if self.query_string.is_null() {
619 return None;
620 }
621 unsafe { CStr::from_ptr(self.query_string).to_str().ok() }
622 }
623
624 #[must_use]
626 pub fn cookie_data(&self) -> Option<&str> {
627 if self.cookie_data.is_null() {
628 return None;
629 }
630 unsafe { CStr::from_ptr(self.cookie_data).to_str().ok() }
631 }
632
633 #[must_use]
635 pub fn content_length(&self) -> i64 {
636 self.content_length
637 }
638
639 #[must_use]
641 pub fn path_translated(&self) -> Option<&str> {
642 if self.path_translated.is_null() {
643 return None;
644 }
645 unsafe { CStr::from_ptr(self.path_translated).to_str().ok() }
646 }
647
648 #[must_use]
650 pub fn request_uri(&self) -> Option<&str> {
651 if self.request_uri.is_null() {
652 return None;
653 }
654 unsafe { CStr::from_ptr(self.request_uri).to_str().ok() }
655 }
656
657 #[must_use]
661 pub fn content_type(&self) -> Option<&str> {
662 if self.content_type.is_null() {
663 return None;
664 }
665 unsafe { CStr::from_ptr(self.content_type).to_str().ok() }
666 }
667
668 #[must_use]
670 pub fn headers_only(&self) -> bool {
671 self.headers_only
672 }
673
674 #[must_use]
676 pub fn no_headers(&self) -> bool {
677 self.no_headers
678 }
679
680 #[must_use]
682 pub fn headers_read(&self) -> bool {
683 self.headers_read
684 }
685
686 #[must_use]
690 pub fn auth_user(&self) -> Option<&str> {
691 if self.auth_user.is_null() {
692 return None;
693 }
694 unsafe { CStr::from_ptr(self.auth_user).to_str().ok() }
695 }
696
697 #[must_use]
699 pub fn auth_password(&self) -> Option<&str> {
700 if self.auth_password.is_null() {
701 return None;
702 }
703 unsafe { CStr::from_ptr(self.auth_password).to_str().ok() }
704 }
705
706 #[must_use]
708 pub fn auth_digest(&self) -> Option<&str> {
709 if self.auth_digest.is_null() {
710 return None;
711 }
712 unsafe { CStr::from_ptr(self.auth_digest).to_str().ok() }
713 }
714
715 #[must_use]
717 pub fn argv0(&self) -> Option<&str> {
718 if self.argv0.is_null() {
719 return None;
720 }
721 unsafe { CStr::from_ptr(self.argv0).to_str().ok() }
722 }
723
724 #[must_use]
726 pub fn current_user(&self) -> Option<&str> {
727 if self.current_user.is_null() {
728 return None;
729 }
730 unsafe { CStr::from_ptr(self.current_user).to_str().ok() }
731 }
732
733 #[must_use]
735 pub fn current_user_length(&self) -> i32 {
736 self.current_user_length
737 }
738
739 #[must_use]
741 pub fn argvc(&self) -> i32 {
742 self.argc
743 }
744
745 #[must_use]
747 pub fn argv(&self) -> Option<&str> {
748 if self.argv.is_null() {
749 return None;
750 }
751 unsafe { CStr::from_ptr(*self.argv).to_str().ok() }
752 }
753
754 #[must_use]
756 pub fn proto_num(&self) -> i32 {
757 self.proto_num
758 }
759}
760
761pub type FileGlobals = php_file_globals;
763
764impl FileGlobals {
765 pub fn get() -> GlobalReadGuard<Self> {
777 let globals = unsafe { ext_php_rs_file_globals().as_ref() }
780 .expect("Static file globals were invalid");
781
782 cfg_if::cfg_if! {
783 if #[cfg(php_zts)] {
784 let guard = lock::FILE_GLOBALS_LOCK.with(RwLock::read_arc);
785 } else {
786 let guard = lock::FILE_GLOBALS_LOCK.read_arc();
787 }
788 }
789
790 GlobalReadGuard { globals, guard }
791 }
792
793 #[must_use]
801 pub fn get_mut() -> GlobalWriteGuard<Self> {
802 let globals = unsafe { &mut *ext_php_rs_file_globals() };
805
806 cfg_if::cfg_if! {
807 if #[cfg(php_zts)] {
808 let guard = lock::FILE_GLOBALS_LOCK.with(RwLock::write_arc);
809 } else {
810 let guard = lock::FILE_GLOBALS_LOCK.write_arc();
811 }
812 }
813
814 GlobalWriteGuard { globals, guard }
815 }
816
817 #[must_use]
819 pub fn stream_wrappers(&self) -> Option<&'static ZendHashTable> {
820 unsafe { self.stream_wrappers.as_ref() }
821 }
822}
823
824#[cfg(not(php_zts))]
829pub(crate) mod lock {
830 use parking_lot::RwLock;
831 use std::sync::{Arc, LazyLock};
832
833 pub(crate) static GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
834 LazyLock::new(|| Arc::new(RwLock::new(())));
835 pub(crate) static PROCESS_GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
836 LazyLock::new(|| Arc::new(RwLock::new(())));
837 pub(crate) static SAPI_GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
838 LazyLock::new(|| Arc::new(RwLock::new(())));
839 pub(crate) static FILE_GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
840 LazyLock::new(|| Arc::new(RwLock::new(())));
841}
842
843#[cfg(php_zts)]
848pub(crate) mod lock {
849 use parking_lot::{const_rwlock, RwLock};
850 use std::sync::Arc;
851
852 thread_local! {
853 pub(crate) static GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new(const_rwlock(()));
854 pub(crate) static PROCESS_GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new( const_rwlock(()) );
855 pub(crate) static SAPI_GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new( const_rwlock(()) );
856 pub(crate) static FILE_GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new( const_rwlock(()) );
857 }
858}
859
860static SAPI_MODULE_LOCK: LazyLock<Arc<RwLock<()>>> = LazyLock::new(|| Arc::new(RwLock::new(())));
865
866pub struct GlobalReadGuard<T: 'static> {
869 globals: &'static T,
870 #[allow(dead_code)]
871 guard: ArcRwLockReadGuard<RawRwLock, ()>,
872}
873
874impl<T> Deref for GlobalReadGuard<T> {
875 type Target = T;
876
877 fn deref(&self) -> &Self::Target {
878 self.globals
879 }
880}
881
882pub struct GlobalWriteGuard<T: 'static> {
885 globals: &'static mut T,
886 #[allow(dead_code)]
887 guard: ArcRwLockWriteGuard<RawRwLock, ()>,
888}
889
890impl<T> Deref for GlobalWriteGuard<T> {
891 type Target = T;
892
893 fn deref(&self) -> &Self::Target {
894 self.globals
895 }
896}
897
898impl<T> DerefMut for GlobalWriteGuard<T> {
899 fn deref_mut(&mut self) -> &mut Self::Target {
900 self.globals
901 }
902}
903
904#[cfg(feature = "embed")]
905#[cfg(test)]
906mod embed_tests {
907 use super::*;
908 use crate::embed::Embed;
909 use std::os::raw::c_char;
910
911 #[test]
912 fn test_sapi_header() {
913 Embed::run(|| {
914 let headers = [
915 ("Content-Type: text/html", "Content-Type", "text/html"),
916 ("X: Custom:Header", "X", "Custom:Header"),
917 ];
918
919 for (header_text, name, value) in headers {
920 let header = SapiHeader {
921 header: header_text.as_bytes().as_ptr() as *mut c_char,
922 header_len: header_text.len(),
923 };
924 assert_eq!(header.name(), name, "Header name mismatch");
925 assert_eq!(header.value(), Some(value), "Header value mismatch");
926 assert_eq!(
927 header.as_str(),
928 format!("{name}: {value}"),
929 "Header string mismatch"
930 );
931 }
932 });
933 }
934
935 #[test]
936 fn test_executor_globals() {
937 Embed::run(|| {
938 let state = ExecutorGlobals::get().active;
939 ExecutorGlobals::get_mut().active = !state;
940 let changed = ExecutorGlobals::get().active;
941 ExecutorGlobals::get_mut().active = state;
942 assert_eq!(changed, !state);
943 });
944 }
945
946 #[test]
947 fn test_compiler_globals() {
948 Embed::run(|| {
949 let state = CompilerGlobals::get().in_compilation;
950 CompilerGlobals::get_mut().in_compilation = !state;
951 let changed = CompilerGlobals::get().in_compilation;
952 CompilerGlobals::get_mut().in_compilation = state;
953 assert_eq!(changed, !state);
954 });
955 }
956}