blob: 62dc66660ebf911bdeb34194361e01a4d51dd392 (
plain)
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
|
use crate::PhpMixed;
use indexmap::IndexMap;
pub fn getenv(_name: &str) -> Option<String> {
std::env::var(_name).ok()
}
// TODO(phase-c): only the simple `^(\w+)(=(.+))?$` form is supported.
pub fn putenv(setting: &str) -> bool {
let is_word =
|s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_');
// A setting without `=` deletes the variable, mirroring PHP's putenv('NAME').
match setting.split_once('=') {
Some((name, value)) => {
if !is_word(name) {
panic!("putenv: unsupported setting format: {setting:?}");
}
unsafe {
std::env::set_var(name, value);
}
}
None => {
if !is_word(setting) {
panic!("putenv: unsupported setting format: {setting:?}");
}
unsafe {
std::env::remove_var(setting);
}
}
}
true
}
/// PHP superglobal $_SERVER access. In the CLI SAPI $_SERVER is populated from
/// the environment, which is the only source modeled here.
pub fn server_get(name: &str) -> Option<String> {
// TODO: is var_os() better?
std::env::var(name).ok()
}
// TODO(php-runtime): modify the real PHP's $_SERVER.
pub fn server_set(_name: &str, _value: String) {}
// TODO(php-runtime): modify the real PHP's $_SERVER.
pub fn server_unset(_name: &str) {}
pub fn server_contains_key(name: &str) -> bool {
std::env::var_os(name).is_some()
}
/// PHP superglobal $_ENV access.
pub fn env_get(name: &str) -> Option<String> {
// TODO: is var_os() better?
std::env::var(name).ok()
}
// TODO(php-runtime): modify the real PHP's $_ENV.
pub fn env_set(_name: &str, _value: String) {}
// TODO(php-runtime): modify the real PHP's $_ENV.
pub fn env_unset(_name: &str) {}
pub fn env_contains_key(name: &str) -> bool {
std::env::var_os(name).is_some()
}
/// PHP `getenv()` with no argument: all environment variables.
pub fn getenv_all() -> IndexMap<String, String> {
todo!()
}
/// PHP superglobal `$_ENV`.
pub fn php_env() -> IndexMap<String, PhpMixed> {
todo!()
}
/// PHP superglobal `$_SERVER`.
pub fn php_server() -> IndexMap<String, PhpMixed> {
todo!()
}
pub fn server(key: &str) -> String {
if key == "PHP_SELF" {
return server_php_self();
}
server_get(key).unwrap_or_default()
}
pub fn server_argv() -> Vec<String> {
std::env::args().collect()
}
pub fn server_php_self() -> String {
// CLI SAPI: $_SERVER['PHP_SELF'] is the path of the executed script, i.e. argv[0].
std::env::args().next().unwrap_or_default()
}
pub fn server_shell() -> Option<String> {
todo!()
}
|