Remove releasenotes from build.xml
[phorkie.git] / src / phorkie / Tools.php
1 <?php
2 namespace phorkie;
3
4
5 class Tools
6 {
7     /**
8      * Delete an entire directory structure
9      *
10      * @param string $path Path to delete
11      *
12      * @return bool
13      */
14     public static function recursiveDelete($path)
15     {
16         if (!file_exists($path)) {
17             return true;
18         }
19         if (!is_dir($path) || is_link($path)) {
20             return unlink($path);
21         }
22         foreach (scandir($path) as $file) {
23             if ($file == '.' || $file == '..') {
24                 continue;
25             }
26             $filepath = $path . DIRECTORY_SEPARATOR . $file;
27             if (!static::recursiveDelete($filepath)) {
28                 return false;
29             };
30         }
31         return rmdir($path);
32     }
33
34     /**
35      * Create a full URL with protocol and host name
36      *
37      * @param string $path Path to the file, with leading /
38      *
39      * @return string Full URL
40      */
41     public static function fullUrl($path = '')
42     {
43         if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) {
44             $prot = 'https';
45         } else {
46             $prot = 'http';
47         }
48         return $prot . '://' . $_SERVER['HTTP_HOST'] . $GLOBALS['phorkie']['cfg']['baseurl'] . $path;
49     }
50
51     /**
52      * Get the full URL to a path, but remove the .phar file from
53      * the base URL if necessary
54      *
55      * @param string $path Path to the file
56      *
57      * @return string Full URL without .phar/
58      */
59     public static function fullUrlNoPhar($path = '')
60     {
61         $base = static::fullUrl();
62         if (substr($base, -6) == '.phar/') {
63             $base = dirname($base) . '/';
64         }
65         return $base . $path;
66     }
67
68     /**
69      * Removes malicious parts from a file name
70      *
71      * @param string $file File name from the user
72      *
73      * @return string Fixed and probably secure filename
74      */
75     public static function sanitizeFilename($file)
76     {
77         $file = trim($file);
78         $file = str_replace(array('\\', '//'), '/', $file);
79         $file = str_replace('/../', '/', $file);
80         if (substr($file, 0, 3) == '../') {
81             $file = substr($file, 3);
82         }
83         if (substr($file, 0, 1) == '../') {
84             $file = substr($file, 1);
85         }
86
87         return $file;
88     }
89
90
91     public static function detectBaseUrl()
92     {
93         if (!isset($_SERVER['REQUEST_URI'])
94             || !isset($_SERVER['SCRIPT_NAME'])
95         ) {
96             return '/';
97         }
98
99         $scriptName = $_SERVER['SCRIPT_NAME'];
100         $requestUri = $_SERVER['REQUEST_URI'];
101         if (substr($scriptName, -4) != '.php') {
102             //a phar
103             return $scriptName . '/';
104         }
105
106         if (isset($_GET['id'])) {
107             $idp = strpos($requestUri, '/' . $_GET['id'] . '/');
108             if ($idp !== false) {
109                 return substr($requestUri, 0, $idp) . '/';
110             }
111         }
112
113         if (substr($requestUri, -4) != '.php') {
114             $requestUri .= '.php';
115         }
116         $snl = strlen($scriptName);
117         if (substr($requestUri, -$snl) == $scriptName) {
118             return substr($requestUri, 0, -$snl) . '/';
119         }
120
121         return '/';
122     }
123
124     /**
125      * Resolves "/../" and "/./" in file paths without validating them.
126      */
127     public static function foldPath($path)
128     {
129         $path = str_replace('/./', '/', $path);
130         $path = str_replace('/./', '/', $path);
131         $path = preg_replace('#/[^/]+/\.\./#', '/', $path);
132         $path = preg_replace('#/[^/]+/\.\./#', '/', $path);
133         return $path;
134     }
135 }
136 ?>