ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilFileSystemAbstractionStorage.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
26 
36 {
37  const STORAGE_WEB = 1;
38  const STORAGE_DATA = 2;
39  const STORAGE_SECURED = 3;
40  const FACTOR = 100;
41  const MAX_EXPONENT = 3;
42  const SECURED_DIRECTORY = "sec";
43  private $container_id;
44  private $storage_type;
45  private $path_conversion = false;
46  protected $path;
47 
48 
65  public function __construct($a_storage_type, $a_path_conversion, $a_container_id)
66  {
67  $this->storage_type = $a_storage_type;
68  $this->path_conversion = $a_path_conversion;
69  $this->container_id = $a_container_id;
70 
71  // Get path info
72  $this->init();
73  }
74 
75 
81  public function fileExists($a_absolute_path)
82  {
83  $relative_path = $this->createRelativePathForFileSystem($a_absolute_path);
84 
85  return $this->getFileSystemService()->has($relative_path);
86  }
87 
88 
94  protected function getLegacyFullAbsolutePath($relative_path)
95  {
96  $stream = $this->getFileSystemService()->readStream($relative_path);
97 
98  return $stream->getMetadata('uri');
99  }
100 
101 
105  protected function getFileSystemService()
106  {
107  global $DIC;
108  switch ($this->getStorageType()) {
109  case self::STORAGE_DATA:
110  return $DIC->filesystem()->storage();
111  break;
112  case self::STORAGE_WEB:
113  case self::SECURED_DIRECTORY:
114  return $DIC->filesystem()->web();
115  break;
116  }
117  }
118 
119 
120  public function getContainerId()
121  {
122  return $this->container_id;
123  }
124 
125 
134  public static function _createPathFromId($a_container_id, $a_name)
135  {
136  $path = array();
137  $found = false;
138  $num = $a_container_id;
139  $path_string = '';
140  for ($i = self::MAX_EXPONENT; $i > 0; $i--) {
141  $factor = pow(self::FACTOR, $i);
142  if (($tmp = (int) ($num / $factor)) or $found) {
143  $path[] = $tmp;
144  $num = $num % $factor;
145  $found = true;
146  }
147  }
148 
149  if (count($path)) {
150  $path_string = (implode('/', $path) . '/');
151  }
152 
153  return $path_string . $a_name . '_' . $a_container_id;
154  }
155 
156 
166  abstract protected function getPathPrefix();
167 
168 
179  abstract protected function getPathPostfix();
180 
181 
188  public function create()
189  {
190  if (!$this->getFileSystemService()->has($this->path)) {
191  $this->getFileSystemService()->createDir($this->path);
192  }
193 
194  return true;
195  }
196 
197 
207  public function getAbsolutePath()
208  {
209  return $this->getLegacyAbsolutePath();
210  }
211 
212 
220  protected function getLegacyAbsolutePath()
221  {
222  if (!$this->getFileSystemService()->has($this->path)) {
223  $this->getFileSystemService()->createDir($this->path);
224  }
225 
226  if ($this->getStorageType() === self::STORAGE_DATA) {
227  return CLIENT_DATA_DIR . '/' . $this->path;
228  }
229  return CLIENT_WEB_DIR . '/' . $this->path;
230  }
231 
232 
238  protected function init()
239  {
240  switch ($this->storage_type) {
241  case self::STORAGE_DATA:
242  case self::STORAGE_WEB:
243  break;
244  case self::STORAGE_SECURED:
245  $this->path .= '/' . self::SECURED_DIRECTORY;
246  break;
247  }
248 
249  // Append path prefix
250  $this->path .= ($this->getPathPrefix() . '/');
251 
252  if ($this->path_conversion) {
253  $this->path .= self::_createPathFromId($this->container_id, $this->getPathPostfix());
254  } else {
255  $this->path .= ($this->getPathPostfix() . '_' . $this->container_id);
256  }
257 
258  return true;
259  }
260 
261 
268  public function writeToFile($a_data, $a_absolute_path)
269  {
270  $relative_path = $this->createRelativePathForFileSystem($a_absolute_path);
271  try {
272  $this->getFileSystemService()->write($relative_path, $a_data);
273  } catch (Exception $e) {
274  return false;
275  }
276 
277  return true;
278  }
279 
280 
286  public function deleteFile($a_absolute_path)
287  {
288  $relative_path = $this->createRelativePathForFileSystem($a_absolute_path);
289  if ($this->getFileSystemService()->has($relative_path)) {
290  try {
291  $this->getFileSystemService()->delete($relative_path);
292  } catch (Exception $e) {
293  return false;
294  }
295  }
296 
297  return true;
298  }
299 
300 
306  public function deleteDirectory($a_absolute_path)
307  {
308  $relative_path = $this->createRelativePathForFileSystem($a_absolute_path);
309  if ($this->getFileSystemService()->has($relative_path)) {
310  try {
311  $this->getFileSystemService()->deleteDir($relative_path);
312  } catch (Exception $e) {
313  return false;
314  }
315  }
316 
317  return true;
318  }
319 
320 
324  public function delete()
325  {
326  try {
327  $this->getFileSystemService()->deleteDir($this->getAbsolutePath());
328  } catch (Exception $e) {
329  return false;
330  }
331 
332  return true;
333  }
334 
335 
342  public function copyFile($a_from, $a_to)
343  {
344  $relative_path_from = $this->createRelativePathForFileSystem($a_from);
345  $relative_path_to = $this->createRelativePathForFileSystem($a_to);
346  if ($this->getFileSystemService()->has($relative_path_from)) {
347  try {
348  $this->getFileSystemService()->copy($relative_path_from, $relative_path_to);
349  } catch (Exception $e) {
350  return false;
351  }
352  }
353 
354  return true;
355  }
356 
357 
364  public static function _copyDirectory($a_sdir, $a_tdir)
365  {
366  try {
367  $sourceFS = LegacyPathHelper::deriveFilesystemFrom($a_sdir);
368  $targetFS = LegacyPathHelper::deriveFilesystemFrom($a_tdir);
369 
370  $sourceDir = LegacyPathHelper::createRelativePath($a_sdir);
371  $targetDir = LegacyPathHelper::createRelativePath($a_tdir);
372 
373  // check if arguments are directories
374  if (!$sourceFS->hasDir($sourceDir)) {
375  return false;
376  }
377 
378  $sourceList = $sourceFS->listContents($sourceDir, true);
379 
380  foreach ($sourceList as $item) {
381  if ($item->isDir()) {
382  continue;
383  }
384 
385  $itemPath = $targetDir . '/' . substr($item->getPath(), strlen($sourceDir));
386  $stream = $sourceFS->readStream($sourceDir);
387  $targetFS->writeStream($itemPath, $stream);
388  }
389 
390  return true;
391  } catch (\Exception $exception) {
392  return false;
393  }
394  }
395 
396 
400  public function appendToPath($a_appendix)
401  {
402  $this->path .= $a_appendix;
403  }
404 
405 
409  public function getStorageType()
410  {
411  return $this->storage_type;
412  }
413 
414 
418  public function getPath()
419  {
420  return $this->path;
421  }
422 
423 
429  private function createRelativePathForFileSystem($a_absolute_path)
430  {
431  $relative_path = ILIAS\Filesystem\Util\LegacyPathHelper::createRelativePath($a_absolute_path);
432 
433  return $relative_path;
434  }
435 }
getPathPostfix()
Get directory name.
global $DIC
Definition: saml.php:7
$stream
PHP stream implementation.
static createRelativePath($absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
getPathPrefix()
Get path prefix.
__construct($a_storage_type, $a_path_conversion, $a_container_id)
Constructor.
getLegacyAbsolutePath()
Calculates the absolute filesystem storage location.
Create styles array
The data for the language used.
getAbsolutePath()
Calculates the full path on the filesystem.
$i
Definition: disco.tpl.php:19
static _createPathFromId($a_container_id, $a_name)
Create a path from an id: e.g 12345 will be converted to 12/34/<name>_5.