ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilFileSystemStorage.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
34abstract class ilFileSystemStorage
35{
36 const STORAGE_WEB = 1;
37 const STORAGE_DATA = 2;
38 const STORAGE_SECURED = 3;
39
40 const FACTOR = 100;
41 const MAX_EXPONENT = 3;
42 const SECURED_DIRECTORY = "sec";
43
46 private $path_conversion = false;
47
48 protected $path;
49
59 public function __construct($a_storage_type, $a_path_conversion, $a_container_id)
60 {
61 $this->storage_type = $a_storage_type;
62 $this->path_conversion = $a_path_conversion;
63 $this->container_id = $a_container_id;
64
65 // Get path info
66 $this->init();
67 }
68
69 public function getContainerId()
70 {
72 }
73
83 public static function _createPathFromId($a_container_id, $a_name)
84 {
85 $path = array();
86 $found = false;
87 $num = $a_container_id;
88 for ($i = self::MAX_EXPONENT; $i > 0;$i--) {
89 $factor = pow(self::FACTOR, $i);
90 if (($tmp = (int) ($num / $factor)) or $found) {
91 $path[] = $tmp;
92 $num = $num % $factor;
93 $found = true;
94 }
95 }
96
97 if (count($path)) {
98 $path_string = (implode('/', $path) . '/');
99 }
100 return $path_string . $a_name . '_' . $a_container_id;
101 }
102
112 abstract protected function getPathPrefix();
113
124 abstract protected function getPathPostfix();
125
132 public function create()
133 {
134 if (!file_exists($this->path)) {
135 ilUtil::makeDirParents($this->path);
136 }
137 return true;
138 }
139
140
147 public function getAbsolutePath()
148 {
149 return $this->path;
150 }
151
157 protected function init()
158 {
159 switch ($this->storage_type) {
161 $this->path = ilUtil::getDataDir();
162 break;
163
165 $this->path = ilUtil::getWebspaceDir();
166 break;
167
169 $this->path = ilUtil::getWebspaceDir();
170 $this->path = ilUtil::removeTrailingPathSeparators($this->path);
171 $this->path .= '/' . self::SECURED_DIRECTORY;
172 break;
173 }
174 $this->path = ilUtil::removeTrailingPathSeparators($this->path);
175 $this->path .= '/';
176
177 // Append path prefix
178 $this->path .= ($this->getPathPrefix() . '/');
179
180 if ($this->path_conversion) {
181 $this->path .= self::_createPathFromId($this->container_id, $this->getPathPostfix());
182 } else {
183 $this->path .= ($this->getPathPostfix() . '_' . $this->container_id);
184 }
185 return true;
186 }
187
195 public function writeToFile($a_data, $a_absolute_path)
196 {
197 if (!$fp = @fopen($a_absolute_path, 'w+')) {
198 return false;
199 }
200 if (@fwrite($fp, $a_data) === false) {
201 @fclose($fp);
202 return false;
203 }
204 @fclose($fp);
205 return true;
206 }
207
215 public function deleteFile($a_abs_name)
216 {
217 if (@file_exists($a_abs_name)) {
218 @unlink($a_abs_name);
219 return true;
220 }
221 return false;
222 }
223
231 public function deleteDirectory($a_abs_name)
232 {
233 if (@file_exists($a_abs_name)) {
234 ilUtil::delDir($a_abs_name);
235 return true;
236 }
237 return false;
238 }
239
240
248 public function delete()
249 {
250 return ilUtil::delDir($this->getAbsolutePath());
251 }
252
253
262 public function copyFile($a_from, $a_to)
263 {
264 if (@file_exists($a_from)) {
265 @copy($a_from, $a_to);
266 return true;
267 }
268 return false;
269 }
270
277 public static function _copyDirectory($a_source, $a_target)
278 {
279 return ilUtil::rCopy($a_source, $a_target);
280 }
281
282 public function appendToPath($a_appendix)
283 {
284 $this->path .= $a_appendix;
285 }
286
287 public function getStorageType()
288 {
289 return $this->storage_type;
290 }
291
295 public function getPath()
296 {
297 return $this->path;
298 }
299}
An exception for terminatinating execution or to throw for unit testing.
getPathPrefix()
Get path prefix.
deleteFile($a_abs_name)
Delete file.
static _createPathFromId($a_container_id, $a_name)
Create a path from an id: e.g 12345 will be converted to 12/34/<name>_5.
copyFile($a_from, $a_to)
Copy files.
getPathPostfix()
Get directory name.
getAbsolutePath()
Get absolute path of storage directory.
static _copyDirectory($a_source, $a_target)
Copy directory and all contents.
deleteDirectory($a_abs_name)
Delete directory.
__construct($a_storage_type, $a_path_conversion, $a_container_id)
Constructor.
writeToFile($a_data, $a_absolute_path)
Write data to file.
static getDataDir()
get data directory (outside webspace)
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static getWebspaceDir($mode="filesystem")
get webspace directory
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
static removeTrailingPathSeparators($path)
$i
Definition: disco.tpl.php:19