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