phpDirectoryIncludePath (#3298): `__DIR__ . '/inc/a.php'`, `dirname(__FILE__) . DIRECTORY_SEPARATOR . 'a.php'` — a path appended to the including file's directory (php_appended_path), written relative to it (`./inc/a.php`, `../config/a.php`). None when an unqualified `dirname` reaches one the file declares or imports. `$_SERVER['DOCUMENT_ROOT'] . '/inc/c.php'` is a path appended to the web server'
(&mut self, expr: Node<'t>)
| 1057 | /// `$_SERVER['DOCUMENT_ROOT'] . '/inc/c.php'` is a path appended to the |
| 1058 | /// web server's root instead, written behind PHP_DOCUMENT_ROOT. |
| 1059 | fn php_directory_include_path(&mut self, expr: Node<'t>) -> Option<String> { |
| 1060 | let parts = php_concat_parts(expr, self.src)?; |
| 1061 | let (head, tail) = parts.split_first()?; |
| 1062 | if tail.is_empty() { |
| 1063 | return None; |
| 1064 | } |
| 1065 | if self.php_is_document_root(*head) { |
| 1066 | let rest = self.php_appended_path(tail)?; |
| 1067 | return Some(format!("{PHP_DOCUMENT_ROOT}{rest}")); |
| 1068 | } |
| 1069 | let head_text = self.text(*head); |
| 1070 | let squeezed: String = head_text.chars().filter(|c| !is_js_space(*c)).collect(); |
| 1071 | let dir = php_own_directory_re().captures(&squeezed)?; |
| 1072 | let dirname_arg = dir.get(1).map(|m| m.as_str()); |
| 1073 | let qualified = head_text.trim_start_matches(is_js_space).starts_with('\\'); |
| 1074 | if dirname_arg.is_some() && !qualified && self.php_shadows_dirname() { |
| 1075 | return None; |
| 1076 | } |
| 1077 | let rest = self.php_appended_path(tail)?; |
| 1078 | // `__DIR__` and `dirname(__FILE__)` are the file's directory; |
| 1079 | // `dirname(__DIR__)` is one up; a levels argument climbs that many |
| 1080 | // from `__FILE__` (one more from `__DIR__`). A levels number past u64 |
| 1081 | // is past the cap as well (JS: a huge Number > 16). |
| 1082 | let levels: u64 = match dirname_arg { |
| 1083 | None => 1, |
| 1084 | Some(arg) => { |
| 1085 | let n: u64 = match dir.get(2) { |
| 1086 | Some(m) => m.as_str().parse().ok()?, |
| 1087 | None => 1, |
| 1088 | }; |
| 1089 | n.saturating_add(u64::from(arg.eq_ignore_ascii_case("__DIR__"))) |
| 1090 | } |
| 1091 | }; |
| 1092 | if levels > 16 { |
| 1093 | return None; |
| 1094 | } |
| 1095 | let mut relative = if levels == 1 { "./".to_string() } else { "../".repeat(levels as usize - 1) }; |
| 1096 | relative.push_str(&rest[1..]); |
| 1097 | Some(relative) |
| 1098 | } |
| 1099 | |
| 1100 | /// phpShadowsDirname (#3298): whether an unqualified `dirname(...)` in |
| 1101 | /// this file reaches a function other than PHP's — one a `use function` |
no test coverage detected