ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Util.php
Go to the documentation of this file.
1 <?php
2 
3 namespace League\Flysystem;
4 
7 
8 class Util
9 {
17  public static function pathinfo($path)
18  {
19  $pathinfo = pathinfo($path) + compact('path');
20  $pathinfo['dirname'] = array_key_exists('dirname', $pathinfo)
21  ? static::normalizeDirname($pathinfo['dirname']) : '';
22 
23  return $pathinfo;
24  }
25 
33  public static function normalizeDirname($dirname)
34  {
35  return $dirname === '.' ? '' : $dirname;
36  }
37 
45  public static function dirname($path)
46  {
47  return static::normalizeDirname(dirname($path));
48  }
49 
58  public static function map(array $object, array $map)
59  {
60  $result = [];
61 
62  foreach ($map as $from => $to) {
63  if ( ! isset($object[$from])) {
64  continue;
65  }
66 
67  $result[$to] = $object[$from];
68  }
69 
70  return $result;
71  }
72 
82  public static function normalizePath($path)
83  {
84  return static::normalizeRelativePath($path);
85  }
86 
96  public static function normalizeRelativePath($path)
97  {
98  $path = str_replace('\\', '/', $path);
99  $path = static::removeFunkyWhiteSpace($path);
100 
101  $parts = [];
102 
103  foreach (explode('/', $path) as $part) {
104  switch ($part) {
105  case '':
106  case '.':
107  break;
108 
109  case '..':
110  if (empty($parts)) {
111  throw new LogicException(
112  'Path is outside of the defined root, path: [' . $path . ']'
113  );
114  }
115  array_pop($parts);
116  break;
117 
118  default:
119  $parts[] = $part;
120  break;
121  }
122  }
123 
124  return implode('/', $parts);
125  }
126 
134  protected static function removeFunkyWhiteSpace($path) {
135  // We do this check in a loop, since removing invalid unicode characters
136  // can lead to new characters being created.
137  while (preg_match('#\p{C}+|^\./#u', $path)) {
138  $path = preg_replace('#\p{C}+|^\./#u', '', $path);
139  }
140 
141  return $path;
142  }
143 
152  public static function normalizePrefix($prefix, $separator)
153  {
154  return rtrim($prefix, $separator) . $separator;
155  }
156 
164  public static function contentSize($contents)
165  {
166  return defined('MB_OVERLOAD_STRING') ? mb_strlen($contents, '8bit') : strlen($contents);
167  }
168 
177  public static function guessMimeType($path, $content)
178  {
179  $mimeType = MimeType::detectByContent($content);
180 
181  if ( ! (empty($mimeType) || in_array($mimeType, ['application/x-empty', 'text/plain', 'text/x-asm']))) {
182  return $mimeType;
183  }
184 
186  }
187 
195  public static function emulateDirectories(array $listing)
196  {
197  $directories = [];
198  $listedDirectories = [];
199 
200  foreach ($listing as $object) {
201  list($directories, $listedDirectories) = static::emulateObjectDirectories(
202  $object,
203  $directories,
204  $listedDirectories
205  );
206  }
207 
208  $directories = array_diff(array_unique($directories), array_unique($listedDirectories));
209 
210  foreach ($directories as $directory) {
211  $listing[] = static::pathinfo($directory) + ['type' => 'dir'];
212  }
213 
214  return $listing;
215  }
216 
226  public static function ensureConfig($config)
227  {
228  if ($config === null) {
229  return new Config();
230  }
231 
232  if ($config instanceof Config) {
233  return $config;
234  }
235 
236  if (is_array($config)) {
237  return new Config($config);
238  }
239 
240  throw new LogicException('A config should either be an array or a Flysystem\Config object.');
241  }
242 
248  public static function rewindStream($resource)
249  {
250  if (ftell($resource) !== 0 && static::isSeekableStream($resource)) {
251  rewind($resource);
252  }
253  }
254 
255  public static function isSeekableStream($resource)
256  {
257  $metadata = stream_get_meta_data($resource);
258 
259  return $metadata['seekable'];
260  }
261 
269  public static function getStreamSize($resource)
270  {
271  $stat = fstat($resource);
272 
273  return $stat['size'];
274  }
275 
285  protected static function emulateObjectDirectories(array $object, array $directories, array $listedDirectories)
286  {
287  if ($object['type'] === 'dir') {
288  $listedDirectories[] = $object['path'];
289  }
290 
291  if (empty($object['dirname'])) {
292  return [$directories, $listedDirectories];
293  }
294 
295  $parent = $object['dirname'];
296 
297  while ( ! empty($parent) && ! in_array($parent, $directories)) {
298  $directories[] = $parent;
299  $parent = static::dirname($parent);
300  }
301 
302  if (isset($object['type']) && $object['type'] === 'dir') {
303  $listedDirectories[] = $object['path'];
304 
305  return [$directories, $listedDirectories];
306  }
307 
308  return [$directories, $listedDirectories];
309  }
310 }
static normalizePath($path)
Normalize path.
Definition: Util.php:82
static normalizeRelativePath($path)
Normalize relative directories in a path.
Definition: Util.php:96
static detectByFilename($filename)
Definition: MimeType.php:61
static rewindStream($resource)
Rewind a stream.
Definition: Util.php:248
static contentSize($contents)
Get content size.
Definition: Util.php:164
static removeFunkyWhiteSpace($path)
Removes unprintable characters and invalid unicode characters.
Definition: Util.php:134
static dirname($path)
Get a normalized dirname from a path.
Definition: Util.php:45
$result
static guessMimeType($path, $content)
Guess MIME Type based on the path of the file and it&#39;s content.
Definition: Util.php:177
$from
$metadata['__DYNAMIC:1__']
static emulateDirectories(array $listing)
Emulate directories.
Definition: Util.php:195
static pathinfo($path)
Get normalized pathinfo.
Definition: Util.php:17
static map(array $object, array $map)
Map result arrays.
Definition: Util.php:58
static emulateObjectDirectories(array $object, array $directories, array $listedDirectories)
Emulate the directories of a single object.
Definition: Util.php:285
static ensureConfig($config)
Ensure a Config instance.
Definition: Util.php:226
static normalizeDirname($dirname)
Normalize a dirname return value.
Definition: Util.php:33
Create styles array
The data for the language used.
static isSeekableStream($resource)
Definition: Util.php:255
static normalizePrefix($prefix, $separator)
Normalize prefix.
Definition: Util.php:152
static detectByContent($content)
Detects MIME Type based on given content.
Definition: MimeType.php:20
static getStreamSize($resource)
Get the size of a stream.
Definition: Util.php:269
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27