ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
40  private $container_id;
41  private $storage_type;
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  {
67  return $this->container_id;
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  $factor = pow(self::FACTOR, $i);
86  if (($tmp = (int) ($num / $factor)) or $found) {
87  $path[] = $tmp;
88  $num = $num % $factor;
89  $found = true;
90  }
91  }
92 
93  if (count($path)) {
94  $path_string = (implode('/', $path) . '/');
95  }
96  return $path_string . $a_name . '_' . $a_container_id;
97  }
98 
108  abstract protected function getPathPrefix();
109 
120  abstract protected function getPathPostfix();
121 
128  public function create()
129  {
130  if (!file_exists($this->path)) {
131  ilUtil::makeDirParents($this->path);
132  }
133  return true;
134  }
135 
136 
143  public function getAbsolutePath()
144  {
145  return $this->path;
146  }
147 
153  protected function init()
154  {
155  switch ($this->storage_type) {
156  case self::STORAGE_DATA:
157  $this->path = ilUtil::getDataDir();
158  break;
159 
160  case self::STORAGE_WEB:
161  $this->path = ilUtil::getWebspaceDir();
162  break;
163 
164  case self::STORAGE_SECURED:
165  $this->path = ilUtil::getWebspaceDir();
166  $this->path = ilUtil::removeTrailingPathSeparators($this->path);
167  $this->path .= '/' . self::SECURED_DIRECTORY;
168  break;
169  }
170  $this->path = ilUtil::removeTrailingPathSeparators($this->path);
171  $this->path .= '/';
172 
173  // Append path prefix
174  $this->path .= ($this->getPathPrefix() . '/');
175 
176  if ($this->path_conversion) {
177  $this->path .= self::_createPathFromId($this->container_id, $this->getPathPostfix());
178  } else {
179  $this->path .= ($this->getPathPostfix() . '_' . $this->container_id);
180  }
181  return true;
182  }
183 
191  public function writeToFile($a_data, $a_absolute_path)
192  {
193  if (!$fp = @fopen($a_absolute_path, 'w+')) {
194  return false;
195  }
196  if (@fwrite($fp, $a_data) === false) {
197  @fclose($fp);
198  return false;
199  }
200  @fclose($fp);
201  return true;
202  }
203 
211  public function deleteFile($a_abs_name)
212  {
213  if (@file_exists($a_abs_name)) {
214  @unlink($a_abs_name);
215  return true;
216  }
217  return false;
218  }
219 
227  public function deleteDirectory($a_abs_name)
228  {
229  if (@file_exists($a_abs_name)) {
230  ilUtil::delDir($a_abs_name);
231  return true;
232  }
233  return false;
234  }
235 
236 
244  public function delete()
245  {
246  return ilUtil::delDir($this->getAbsolutePath());
247  }
248 
249 
258  public function copyFile($a_from, $a_to)
259  {
260  if (@file_exists($a_from)) {
261  @copy($a_from, $a_to);
262  return true;
263  }
264  return false;
265  }
266 
273  public static function _copyDirectory($a_source, $a_target)
274  {
275  return ilUtil::rCopy($a_source, $a_target);
276  }
277 
278  public function appendToPath($a_appendix)
279  {
280  $this->path .= $a_appendix;
281  }
282 
283  public function getStorageType()
284  {
285  return $this->storage_type;
286  }
287 
291  public function getPath()
292  {
293  return $this->path;
294  }
295 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
static _copyDirectory($a_source, $a_target)
Copy directory and all contents.
static _createPathFromId($a_container_id, $a_name)
Create a path from an id: e.g 12345 will be converted to 12/34/<name>_5.
deleteDirectory($a_abs_name)
Delete directory.
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
deleteFile($a_abs_name)
Delete file.
getPathPostfix()
Get directory name.
copyFile($a_from, $a_to)
Copy files.
writeToFile($a_data, $a_absolute_path)
Write data to file.
getAbsolutePath()
Get absolute path of storage directory.
static removeTrailingPathSeparators($path)
Create styles array
The data for the language used.
getPathPrefix()
Get path prefix.
static getDataDir()
get data directory (outside webspace)
$i
Definition: disco.tpl.php:19
__construct($a_storage_type, $a_path_conversion, $a_container_id)
Constructor.
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