ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
System.php
Go to the documentation of this file.
1 <?php
2 namespace SimpleSAML\Utils;
3 
9 class 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  // remove trailing slashes
126  $base = rtrim($base, '/');
127 
128  // check for absolute path
129  if (substr($path, 0, 1) === '/') {
130  // absolute path. */
131  $ret = '/';
132  } else {
133  // path relative to base
134  $ret = $base;
135  }
136 
137  $path = explode('/', $path);
138  foreach ($path as $d) {
139  if ($d === '.') {
140  continue;
141  } elseif ($d === '..') {
142  $ret = dirname($ret);
143  } else {
144  if (substr($ret, -1) !== '/') {
145  $ret .= '/';
146  }
147  $ret .= $d;
148  }
149  }
150 
151  return $ret;
152  }
153 
154 
176  public static function writeFile($filename, $data, $mode = 0600)
177  {
178  if (!is_string($filename) || !is_string($data) || !is_numeric($mode)) {
179  throw new \InvalidArgumentException('Invalid input parameters');
180  }
181 
182  $tmpFile = self::getTempDir().DIRECTORY_SEPARATOR.rand();
183 
184  $res = @file_put_contents($tmpFile, $data);
185  if ($res === false) {
186  $error = error_get_last();
187  throw new \SimpleSAML_Error_Exception(
188  'Error saving file "'.$tmpFile.'": '.
189  (is_array($error) ? $error['message'] : 'no error available')
190  );
191  }
192 
193  if (self::getOS() !== self::WINDOWS) {
194  if (!chmod($tmpFile, $mode)) {
195  unlink($tmpFile);
196  $error = error_get_last();
197  //$error = (is_array($error) ? $error['message'] : 'no error available');
198  throw new \SimpleSAML_Error_Exception(
199  'Error changing file mode of "'.$tmpFile.'": '.
200  (is_array($error) ? $error['message'] : 'no error available')
201  );
202  }
203  }
204 
205  if (!rename($tmpFile, $filename)) {
206  unlink($tmpFile);
207  $error = error_get_last();
208  throw new \SimpleSAML_Error_Exception(
209  'Error moving "'.$tmpFile.'" to "'.$filename.'": '.
210  (is_array($error) ? $error['message'] : 'no error available')
211  );
212  }
213 
214  if (function_exists('opcache_invalidate')) {
215  opcache_invalidate($filename);
216  }
217  }
218 }
static writeFile($filename, $data, $mode=0600)
Atomically write a file.
Definition: System.php:176
static getTempDir()
This function retrieves the path to a directory where temporary files can be saved.
Definition: System.php:70
static getOS()
This function returns the Operating System we are running on.
Definition: System.php:29
$base
Definition: index.php:4
$error
Definition: Error.php:17
foreach($_POST as $key=> $value) $res
$globalConfig
static resolvePath($path, $base=null)
Resolve a (possibly) relative path from the given base path.
Definition: System.php:118
$ret
Definition: parser.php:6
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