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