ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilFileSystemStorage5069.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
31{
32 const STORAGE_WEB = 1;
33 const STORAGE_DATA = 2;
34 const STORAGE_SECURED = 3;
35
36 const FACTOR = 100;
37 const MAX_EXPONENT = 3;
38 const SECURED_DIRECTORY = "sec";
39
42 private $path_conversion = false;
43
44 protected $path;
45
55 public function __construct($a_storage_type,$a_path_conversion,$a_container_id)
56 {
57 $this->storage_type = $a_storage_type;
58 $this->path_conversion = $a_path_conversion;
59 $this->container_id = $a_container_id;
60
61 // Get path info
62 $this->init();
63 }
64
65 public function getContainerId()
66 {
68 }
69
79 public static function _createPathFromId($a_container_id,$a_name)
80 {
81 $path = array();
82 $found = false;
83 $num = $a_container_id;
84 for($i = self::MAX_EXPONENT; $i > 0;$i--)
85 {
86 $factor = pow(self::FACTOR,$i);
87 if(($tmp = (int) ($num / $factor)) or $found)
88 {
89 $path[] = $tmp;
90 $num = $num % $factor;
91 $found = true;
92 }
93 }
94
95 if(count($path))
96 {
97 $path_string = (implode('/',$path).'/');
98 }
99 return $path_string.$a_name.'_'.$a_container_id;
100 }
101
111 abstract protected function getPathPrefix();
112
123 abstract protected function getPathPostfix();
124
131 public function create()
132 {
133 if(!file_exists($this->path))
134 {
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)
160 {
162 $this->path = ilUtil::getDataDir();
163 break;
164
166 $this->path = ilUtil::getWebspaceDir();
167 break;
168
170 $this->path = ilUtil::getWebspaceDir();
171 $this->path = ilUtil::removeTrailingPathSeparators($this->path);
172 $this->path .= '/'.self::SECURED_DIRECTORY;
173 break;
174 }
175 $this->path = ilUtil::removeTrailingPathSeparators($this->path);
176 $this->path .= '/';
177
178 // Append path prefix
179 $this->path .= ($this->getPathPrefix().'/');
180
181 if($this->path_conversion)
182 {
183 $this->path .= self::_createPathFromId($this->container_id,$this->getPathPostfix());
184 }
185 else
186 {
187 $this->path .= ($this->getPathPostfix().'_'.$this->container_id);
188 }
189 return true;
190 }
191
199 public function writeToFile($a_data,$a_absolute_path)
200 {
201 if(!$fp = @fopen($a_absolute_path,'w+'))
202 {
203 return false;
204 }
205 if(@fwrite($fp,$a_data) === false)
206 {
207 @fclose($fp);
208 return false;
209 }
210 @fclose($fp);
211 return true;
212 }
213
221 public function deleteFile($a_abs_name)
222 {
223 if(@file_exists($a_abs_name))
224 {
225 @unlink($a_abs_name);
226 return true;
227 }
228 return false;
229 }
230
238 function deleteDirectory($a_abs_name)
239 {
240 if(@file_exists($a_abs_name))
241 {
242 ilUtil::delDir($a_abs_name);
243 return true;
244 }
245 return false;
246 }
247
248
256 public function delete()
257 {
258 return ilUtil::delDir($this->getAbsolutePath());
259 }
260
261
270 public function copyFile($a_from,$a_to)
271 {
272 if(@file_exists($a_from))
273 {
274 @copy($a_from,$a_to);
275 return true;
276 }
277 return false;
278 }
279
286 static public function _copyDirectory($a_source,$a_target)
287 {
288 return ilUtil::rCopy($a_source,$a_target);
289 }
290
291 public function appendToPath($a_appendix)
292 {
293 $this->path .= $a_appendix;
294 }
295
296 public function getStorageType()
297 {
298 return $this->storage_type;
299 }
300
304 function getPath()
305 {
306 return $this->path;
307 }
308
309}
310
311?>
An exception for terminatinating execution or to throw for unit testing.
static _copyDirectory($a_source, $a_target)
Copy directory and all contents.
getPathPostfix()
Get directory name.
copyFile($a_from, $a_to)
Copy files.
deleteDirectory($a_abs_name)
Delete directory.
getPathPrefix()
Get path prefix.
__construct($a_storage_type, $a_path_conversion, $a_container_id)
Constructor.
writeToFile($a_data, $a_absolute_path)
Write data to 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.
getAbsolutePath()
Get absolute path of storage directory.
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)