ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilExportImportDirectory.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
29 {
30  private const PATH_PREFIX = 'export';
31 
32  protected function getPathPrefix(): string
33  {
34  return self::PATH_PREFIX;
35  }
36 
37  public function hasFilesFor(int $user_id, string $type): bool
38  {
39  return (bool) count($this->getFilesFor($user_id, $type));
40  }
41 
42  public function getFilesFor(int $user_id, string $type): array
43  {
44  if (!$this->exists()) {
45  return [];
46  }
47  $finder = $this->storage->finder()
48  ->in([$this->getRelativePath()])
49  ->files()
50  ->depth('< 1')
51  ->sortByName();
52  $files = [];
53  foreach ($finder as $file) {
54  $basename = basename($file->getPath());
55  if ($this->matchesType($type, $basename)) {
56  $files[base64_encode($file->getPath())] = $basename;
57  }
58  }
59  if ($this->storage->hasDir($this->getRelativePath() . '/' . $user_id)) {
60  $finder = $this->storage->finder()->in([$this->getRelativePath() . '/' . $user_id])
61  ->depth('< 1')
62  ->files()
63  ->sortByName();
64  foreach ($finder as $file) {
65  $basename = basename($file->getPath());
66  if ($this->matchesType($type, $basename)) {
67  $files[base64_encode($file->getPath())] = $basename;
68  }
69  }
70  }
71  asort($files);
72  return $files;
73  }
74 
78  protected function matchesType(string $type, string $filename): bool
79  {
80  $matches = [];
81  $result = preg_match('/[0-9]{10}__[0-9]{1,6}__([a-z]{1,4})_[0-9]{2,9}.zip/', $filename, $matches);
82  if (!$result) {
83  return false;
84  }
85  if (isset($matches[1]) && $matches[1] == $type) {
86  return true;
87  }
88  return false;
89  }
90 
91  public function getAbsolutePathForHash(int $user_id, string $type, string $post_hash): string
92  {
93  foreach ($this->getFilesFor($user_id, $type) as $hash => $file) {
94  if (strcmp($hash, $post_hash) === 0) {
95  $file_path = base64_decode($hash);
96  return ilFileUtils::getDataDir() . '/' . base64_decode($hash);
97  }
98  }
99  return '';
100  }
101 }
getAbsolutePathForHash(int $user_id, string $type, string $post_hash)
$type
getFilesFor(int $user_id, string $type)
static getDataDir()
get data directory (outside webspace)
$filename
Definition: buildRTE.php:78
hasFilesFor(int $user_id, string $type)
matchesType(string $type, string $filename)
Check if filename matches a given type.