Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace XdgBaseDir;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Simple implementation of the XDG standard http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
Chris@0
|
7 *
|
Chris@0
|
8 * Based on the python implementation https://github.com/takluyver/pyxdg/blob/master/xdg/BaseDirectory.py
|
Chris@0
|
9 *
|
Chris@0
|
10 * Class Xdg
|
Chris@0
|
11 * @package ShopwareCli\Application
|
Chris@0
|
12 */
|
Chris@0
|
13 class Xdg
|
Chris@0
|
14 {
|
Chris@0
|
15 const S_IFDIR = 040000; // directory
|
Chris@0
|
16 const S_IRWXO = 00007; // rwx other
|
Chris@0
|
17 const S_IRWXG = 00056; // rwx group
|
Chris@0
|
18 const RUNTIME_DIR_FALLBACK = 'php-xdg-runtime-dir-fallback-';
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * @return string
|
Chris@0
|
22 */
|
Chris@0
|
23 public function getHomeDir()
|
Chris@0
|
24 {
|
Chris@0
|
25 return getenv('HOME') ?: (getenv('HOMEDRIVE') . DIRECTORY_SEPARATOR . getenv('HOMEPATH'));
|
Chris@0
|
26 }
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @return string
|
Chris@0
|
30 */
|
Chris@0
|
31 public function getHomeConfigDir()
|
Chris@0
|
32 {
|
Chris@0
|
33 $path = getenv('XDG_CONFIG_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.config';
|
Chris@0
|
34
|
Chris@0
|
35 return $path;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * @return string
|
Chris@0
|
40 */
|
Chris@0
|
41 public function getHomeDataDir()
|
Chris@0
|
42 {
|
Chris@0
|
43 $path = getenv('XDG_DATA_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.local' . DIRECTORY_SEPARATOR . 'share';
|
Chris@0
|
44
|
Chris@0
|
45 return $path;
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * @return array
|
Chris@0
|
50 */
|
Chris@0
|
51 public function getConfigDirs()
|
Chris@0
|
52 {
|
Chris@0
|
53 $configDirs = getenv('XDG_CONFIG_DIRS') ? explode(':', getenv('XDG_CONFIG_DIRS')) : array('/etc/xdg');
|
Chris@0
|
54
|
Chris@0
|
55 $paths = array_merge(array($this->getHomeConfigDir()), $configDirs);
|
Chris@0
|
56
|
Chris@0
|
57 return $paths;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * @return array
|
Chris@0
|
62 */
|
Chris@0
|
63 public function getDataDirs()
|
Chris@0
|
64 {
|
Chris@0
|
65 $dataDirs = getenv('XDG_DATA_DIRS') ? explode(':', getenv('XDG_DATA_DIRS')) : array('/usr/local/share', '/usr/share');
|
Chris@0
|
66
|
Chris@0
|
67 $paths = array_merge(array($this->getHomeDataDir()), $dataDirs);
|
Chris@0
|
68
|
Chris@0
|
69 return $paths;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * @return string
|
Chris@0
|
74 */
|
Chris@0
|
75 public function getHomeCacheDir()
|
Chris@0
|
76 {
|
Chris@0
|
77 $path = getenv('XDG_CACHE_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.cache';
|
Chris@0
|
78
|
Chris@0
|
79 return $path;
|
Chris@0
|
80
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 public function getRuntimeDir($strict=true)
|
Chris@0
|
84 {
|
Chris@0
|
85 if ($runtimeDir = getenv('XDG_RUNTIME_DIR')) {
|
Chris@0
|
86 return $runtimeDir;
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 if ($strict) {
|
Chris@0
|
90 throw new \RuntimeException('XDG_RUNTIME_DIR was not set');
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 $fallback = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::RUNTIME_DIR_FALLBACK . getenv('USER');
|
Chris@0
|
94
|
Chris@0
|
95 $create = false;
|
Chris@0
|
96
|
Chris@0
|
97 if (!is_dir($fallback)) {
|
Chris@0
|
98 mkdir($fallback, 0700, true);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 $st = lstat($fallback);
|
Chris@0
|
102
|
Chris@0
|
103 # The fallback must be a directory
|
Chris@0
|
104 if (!$st['mode'] & self::S_IFDIR) {
|
Chris@0
|
105 rmdir($fallback);
|
Chris@0
|
106 $create = true;
|
Chris@0
|
107 } elseif ($st['uid'] != getmyuid() ||
|
Chris@0
|
108 $st['mode'] & (self::S_IRWXG | self::S_IRWXO)
|
Chris@0
|
109 ) {
|
Chris@0
|
110 rmdir($fallback);
|
Chris@0
|
111 $create = true;
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 if ($create) {
|
Chris@0
|
115 mkdir($fallback, 0700, true);
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 return $fallback;
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 }
|