8 * Delete an entire directory structure
10 * @param string $path Path to delete
14 public static function recursiveDelete($path)
16 if (!file_exists($path)) {
19 if (!is_dir($path) || is_link($path)) {
22 foreach (scandir($path) as $file) {
23 if ($file == '.' || $file == '..') {
26 $filepath = $path . DIRECTORY_SEPARATOR . $file;
27 if (!static::recursiveDelete($filepath)) {
35 * Create a full URL with protocol and host name
37 * @param string $path Path to the file, with leading /
39 * @return string Full URL
41 public static function fullUrl($path = '')
43 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) {
48 return $prot . '://' . $_SERVER['HTTP_HOST'] . $GLOBALS['phorkie']['cfg']['baseurl'] . $path;
52 * Get the full URL to a path, but remove the .phar file from
53 * the base URL if necessary
55 * @param string $path Path to the file
57 * @return string Full URL without .phar/
59 public static function fullUrlNoPhar($path = '')
61 $base = static::fullUrl();
62 if (substr($base, -6) == '.phar/') {
63 $base = dirname($base) . '/';
69 * Removes malicious parts from a file name
71 * @param string $file File name from the user
73 * @return string Fixed and probably secure filename
75 public static function sanitizeFilename($file)
78 $file = str_replace(array('\\', '//'), '/', $file);
79 $file = str_replace('/../', '/', $file);
80 if (substr($file, 0, 3) == '../') {
81 $file = substr($file, 3);
83 if (substr($file, 0, 1) == '../') {
84 $file = substr($file, 1);
91 public static function detectBaseUrl()
93 if (!isset($_SERVER['REQUEST_URI'])
94 || !isset($_SERVER['SCRIPT_NAME'])
99 $scriptName = $_SERVER['SCRIPT_NAME'];
100 $requestUri = $_SERVER['REQUEST_URI'];
101 if (substr($scriptName, -4) != '.php') {
103 return $scriptName . '/';
106 if (isset($_GET['id'])) {
107 $idp = strpos($requestUri, '/' . $_GET['id'] . '/');
108 if ($idp !== false) {
109 return substr($requestUri, 0, $idp) . '/';
113 if (substr($requestUri, -4) != '.php') {
114 $requestUri .= '.php';
116 $snl = strlen($scriptName);
117 if (substr($requestUri, -$snl) == $scriptName) {
118 return substr($requestUri, 0, -$snl) . '/';
125 * Resolves "/../" and "/./" in file paths without validating them.
127 public static function foldPath($path)
129 $path = str_replace('/./', '/', $path);
130 $path = str_replace('/./', '/', $path);
131 $path = preg_replace('#/[^/]+/\.\./#', '/', $path);
132 $path = preg_replace('#/[^/]+/\.\./#', '/', $path);