ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 {
90 $factor = pow(self::FACTOR,$i);
91 if(($tmp = (int) ($num / $factor)) or $found)
92 {
93 $path[] = $tmp;
94 $num = $num % $factor;
95 $found = true;
96 }
97 }
98
99 if(count($path))
100 {
101 $path_string = (implode('/',$path).'/');
102 }
103 return $path_string.$a_name.'_'.$a_container_id;
104 }
105
115 abstract protected function getPathPrefix();
116
127 abstract protected function getPathPostfix();
128
135 public function create()
136 {
137 if(!file_exists($this->path))
138 {
139 ilUtil::makeDirParents($this->path);
140 }
141 return true;
142 }
143
144
151 public function getAbsolutePath()
152 {
153 return $this->path;
154 }
155
161 protected function init()
162 {
163 switch($this->storage_type)
164 {
166 $this->path = ilUtil::getDataDir();
167 break;
168
170 $this->path = ilUtil::getWebspaceDir();
171 break;
172
174 $this->path = ilUtil::getWebspaceDir();
175 $this->path = ilUtil::removeTrailingPathSeparators($this->path);
176 $this->path .= '/'.self::SECURED_DIRECTORY;
177 break;
178 }
179 $this->path = ilUtil::removeTrailingPathSeparators($this->path);
180 $this->path .= '/';
181
182 // Append path prefix
183 $this->path .= ($this->getPathPrefix().'/');
184
185 if($this->path_conversion)
186 {
187 $this->path .= self::_createPathFromId($this->container_id,$this->getPathPostfix());
188 }
189 else
190 {
191 $this->path .= ($this->getPathPostfix().'_'.$this->container_id);
192 }
193 return true;
194 }
195
203 public function writeToFile($a_data,$a_absolute_path)
204 {
205 if(!$fp = @fopen($a_absolute_path,'w+'))
206 {
207 return false;
208 }
209 if(@fwrite($fp,$a_data) === false)
210 {
211 @fclose($fp);
212 return false;
213 }
214 @fclose($fp);
215 return true;
216 }
217
225 public function deleteFile($a_abs_name)
226 {
227 if(@file_exists($a_abs_name))
228 {
229 @unlink($a_abs_name);
230 return true;
231 }
232 return false;
233 }
234
242 function deleteDirectory($a_abs_name)
243 {
244 if(@file_exists($a_abs_name))
245 {
246 ilUtil::delDir($a_abs_name);
247 return true;
248 }
249 return false;
250 }
251
252
260 public function delete()
261 {
262 return ilUtil::delDir($this->getAbsolutePath());
263 }
264
265
274 public function copyFile($a_from,$a_to)
275 {
276 if(@file_exists($a_from))
277 {
278 @copy($a_from,$a_to);
279 return true;
280 }
281 return false;
282 }
283
293 public function _copyDirectory($a_source,$a_target)
294 {
295 return ilUtil::rCopy($a_source,$a_target);
296 }
297
298 public function appendToPath($a_appendix)
299 {
300 $this->path .= $a_appendix;
301 }
302
303 public function getStorageType()
304 {
305 return $this->storage_type;
306 }
307
311 function getPath()
312 {
313 return $this->path;
314 }
315
316}
317
318?>
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.
_copyDirectory($a_source, $a_target)
Copy directory and all contents.
copyFile($a_from, $a_to)
Copy files.
getPathPostfix()
Get directory name.
getAbsolutePath()
Get absolute path of storage directory.
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)