1 | <?php |
||
13 | class Filesystem |
||
14 | { |
||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | const FILE = 1; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | const DIRECTORY = 2; |
||
24 | |||
25 | /** |
||
26 | * Gets the name of the owner of the current PHP script. |
||
27 | * |
||
28 | * @return string |
||
29 | */ |
||
30 | public static function getUserName() |
||
31 | { |
||
32 | return get_current_user() ?: getenv('USERNAME'); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Get user home directory. |
||
37 | * |
||
38 | * @return string |
||
39 | */ |
||
40 | public static function getUserHomeDir() |
||
41 | { |
||
42 | return self::getRealPath(self::doGetUserHomeDir()); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @return string |
||
47 | */ |
||
48 | private static function doGetUserHomeDir() |
||
49 | { |
||
50 | // have home env var |
||
51 | if ($home = getenv('HOME')) { |
||
52 | return $home; |
||
53 | } |
||
54 | |||
55 | // *nix os |
||
56 | if (!defined('PHP_WINDOWS_VERSION_BUILD')) { |
||
57 | return '/home/'.self::getUserName(); |
||
58 | } |
||
59 | |||
60 | // have drive and path env vars |
||
61 | if (getenv('HOMEDRIVE') && getenv('HOMEPATH')) { |
||
62 | return iconv('cp1251', 'utf-8', getenv('HOMEDRIVE').getenv('HOMEPATH')); |
||
63 | } |
||
64 | |||
65 | // Windows |
||
66 | if ($username = self::getUserName()) { |
||
67 | $username = iconv('cp1251', 'utf-8', $username); |
||
68 | // is Vista or older |
||
69 | if (is_dir($win7path = 'C:\Users\\'.$username.'\\')) { |
||
70 | return $win7path; |
||
71 | } |
||
72 | |||
73 | return 'C:\Documents and Settings\\'.$username.'\\'; |
||
74 | } |
||
75 | |||
76 | // is Vista or older |
||
77 | if (is_dir('C:\Users\\')) { |
||
78 | return 'C:\Users\\'; |
||
79 | } |
||
80 | |||
81 | return 'C:\Documents and Settings\\'; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * List files and directories inside the specified path. |
||
86 | * |
||
87 | * @param string $path |
||
88 | * @param int $filter |
||
89 | * @param int $order |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | public static function scandir($path, $filter = 0, $order = SCANDIR_SORT_ASCENDING) |
||
150 | |||
151 | /** |
||
152 | * @param string $path |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | public static function getRealPath($path) |
||
162 | } |
||
163 |