ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
System.php
Go to the documentation of this file.
1<?php
2namespace SimpleSAML\Utils;
3
9class System
10{
11
12 const WINDOWS = 1;
13 const LINUX = 2;
14 const OSX = 3;
15 const HPUX = 4;
16 const UNIX = 5;
17 const BSD = 6;
18 const IRIX = 7;
19 const SUNOS = 8;
20
21
29 public static function getOS()
30 {
31 if (stristr(PHP_OS, 'LINUX')) {
32 return self::LINUX;
33 }
34 if (stristr(PHP_OS, 'DARWIN')) {
35 return self::OSX;
36 }
37 if (stristr(PHP_OS, 'WIN')) {
38 return self::WINDOWS;
39 }
40 if (stristr(PHP_OS, 'BSD')) {
41 return self::BSD;
42 }
43 if (stristr(PHP_OS, 'UNIX')) {
44 return self::UNIX;
45 }
46 if (stristr(PHP_OS, 'HP-UX')) {
47 return self::HPUX;
48 }
49 if (stristr(PHP_OS, 'IRIX')) {
50 return self::IRIX;
51 }
52 if (stristr(PHP_OS, 'SUNOS')) {
53 return self::SUNOS;
54 }
55 return false;
56 }
57
58
70 public static function getTempDir()
71 {
73
74 $tempDir = rtrim(
75 $globalConfig->getString(
76 'tempdir',
77 sys_get_temp_dir().DIRECTORY_SEPARATOR.'simplesaml'
78 ),
79 DIRECTORY_SEPARATOR
80 );
81
82 if (!is_dir($tempDir)) {
83 if (!mkdir($tempDir, 0700, true)) {
84 $error = error_get_last();
85 throw new \SimpleSAML_Error_Exception(
86 'Error creating temporary directory "'.$tempDir.'": '.
87 (is_array($error) ? $error['message'] : 'no error available')
88 );
89 }
90 } elseif (function_exists('posix_getuid')) {
91 // check that the owner of the temp directory is the current user
92 $stat = lstat($tempDir);
93 if ($stat['uid'] !== posix_getuid()) {
94 throw new \SimpleSAML_Error_Exception(
95 'Temporary directory "'.$tempDir.'" does not belong to the current user.'
96 );
97 }
98 }
99
100 return $tempDir;
101 }
102
103
118 public static function resolvePath($path, $base = null)
119 {
120 if ($base === null) {
122 $base = $config->getBaseDir();
123 }
124
125 // normalise directory separator
126 $base = str_replace('\\', '/', $base);
127 $path = str_replace('\\', '/', $path);
128
129 // remove trailing slashes
130 $base = rtrim($base, '/');
131 $path = rtrim($path, '/');
132
133 // check for absolute path
134 if (substr($path, 0, 1) === '/') {
135 // absolute path. */
136 $ret = '/';
137 } elseif (static::pathContainsDriveLetter($path)) {
138 $ret = '';
139 } else {
140 // path relative to base
141 $ret = $base;
142 }
143
144 $path = explode('/', $path);
145 foreach ($path as $d) {
146 if ($d === '.') {
147 continue;
148 } elseif ($d === '..') {
149 $ret = dirname($ret);
150 } else {
151 if ($ret && substr($ret, -1) !== '/') {
152 $ret .= '/';
153 }
154 $ret .= $d;
155 }
156 }
157
158 return $ret;
159 }
160
161
183 public static function writeFile($filename, $data, $mode = 0600)
184 {
185 if (!is_string($filename) || !is_string($data) || !is_numeric($mode)) {
186 throw new \InvalidArgumentException('Invalid input parameters');
187 }
188
189 $tmpFile = self::getTempDir().DIRECTORY_SEPARATOR.rand();
190
191 $res = @file_put_contents($tmpFile, $data);
192 if ($res === false) {
193 $error = error_get_last();
194 throw new \SimpleSAML_Error_Exception(
195 'Error saving file "'.$tmpFile.'": '.
196 (is_array($error) ? $error['message'] : 'no error available')
197 );
198 }
199
200 if (self::getOS() !== self::WINDOWS) {
201 if (!chmod($tmpFile, $mode)) {
202 unlink($tmpFile);
203 $error = error_get_last();
204 //$error = (is_array($error) ? $error['message'] : 'no error available');
205 throw new \SimpleSAML_Error_Exception(
206 'Error changing file mode of "'.$tmpFile.'": '.
207 (is_array($error) ? $error['message'] : 'no error available')
208 );
209 }
210 }
211
212 if (!rename($tmpFile, $filename)) {
213 unlink($tmpFile);
214 $error = error_get_last();
215 throw new \SimpleSAML_Error_Exception(
216 'Error moving "'.$tmpFile.'" to "'.$filename.'": '.
217 (is_array($error) ? $error['message'] : 'no error available')
218 );
219 }
220
221 if (function_exists('opcache_invalidate')) {
222 opcache_invalidate($filename);
223 }
224 }
225
233 private static function pathContainsDriveLetter($path)
234 {
235 $letterAsciiValue = ord(strtoupper(substr($path, 0, 1)));
236 return substr($path, 1, 1) === ':'
237 && $letterAsciiValue >= 65 && $letterAsciiValue <= 90;
238 }
239}
$path
Definition: aliased.php:25
$filename
Definition: buildRTE.php:89
An exception for terminatinating execution or to throw for unit testing.
static resolvePath($path, $base=null)
Resolve a (possibly) relative path from the given base path.
Definition: System.php:118
static pathContainsDriveLetter($path)
Check if the supplied path contains a Windows-style drive letter.
Definition: System.php:233
static writeFile($filename, $data, $mode=0600)
Atomically write a file.
Definition: System.php:183
static getOS()
This function returns the Operating System we are running on.
Definition: System.php:29
static getTempDir()
This function retrieves the path to a directory where temporary files can be saved.
Definition: System.php:70
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
$base
Definition: index.php:4
$config
Definition: bootstrap.php:15
$ret
Definition: parser.php:6
foreach($_POST as $key=> $value) $res
$data
Definition: bench.php:6
$globalConfig