ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
34 abstract class ilFileSystemStorage
35 {
36  const STORAGE_WEB = 1;
37  const STORAGE_DATA = 2;
38 
39  const FACTOR = 100;
40  const MAX_EXPONENT = 3;
41 
42 
43  private $container_id;
44  private $storage_type;
45  private $path_conversion = false;
46 
47  protected $path;
48 
58  public function __construct($a_storage_type,$a_path_conversion,$a_container_id)
59  {
60  $this->storage_type = $a_storage_type;
61  $this->path_conversion = $a_path_conversion;
62  $this->container_id = $a_container_id;
63 
64  // Get path info
65  $this->init();
66  }
67 
68  public function getContainerId()
69  {
70  return $this->container_id;
71  }
72 
82  public static function _createPathFromId($a_container_id,$a_name)
83  {
84  $path = array();
85  $found = false;
86  $num = $a_container_id;
87  for($i = self::MAX_EXPONENT; $i > 0;$i--)
88  {
89  $factor = pow(self::FACTOR,$i);
90  if(($tmp = (int) ($num / $factor)) or $found)
91  {
92  $path[] = $tmp;
93  $num = $num % $factor;
94  $found = true;
95  }
96  }
97 
98  if(count($path))
99  {
100  $path_string = (implode('/',$path).'/');
101  }
102  return $path_string.$a_name.'_'.$a_container_id;
103  }
104 
114  abstract protected function getPathPrefix();
115 
126  abstract protected function getPathPostfix();
127 
134  public function create()
135  {
136  if(!file_exists($this->path))
137  {
138  ilUtil::makeDirParents($this->path);
139  }
140  return true;
141  }
142 
143 
150  public function getAbsolutePath()
151  {
152  return $this->path;
153  }
154 
160  protected function init()
161  {
162  switch($this->storage_type)
163  {
164  case self::STORAGE_DATA:
165  $this->path = ilUtil::getDataDir();
166  break;
167 
168  case self::STORAGE_WEB:
169  $this->path = ilUtil::getWebspaceDir();
170  break;
171  }
172  $this->path = ilUtil::removeTrailingPathSeparators($this->path);
173  $this->path .= '/';
174 
175  // Append path prefix
176  $this->path .= ($this->getPathPrefix().'/');
177 
178  if($this->path_conversion)
179  {
180  $this->path .= self::_createPathFromId($this->container_id,$this->getPathPostfix());
181  }
182  else
183  {
184  $this->path .= ($this->getPathPostfix().'_'.$this->container_id);
185  }
186  return true;
187  }
188 
196  public function writeToFile($a_data,$a_absolute_path)
197  {
198  if(!$fp = @fopen($a_absolute_path,'w+'))
199  {
200  return false;
201  }
202  if(@fwrite($fp,$a_data) === false)
203  {
204  @fclose($fp);
205  return false;
206  }
207  @fclose($fp);
208  return true;
209  }
210 
218  public function deleteFile($a_abs_name)
219  {
220  if(@file_exists($a_abs_name))
221  {
222  @unlink($a_abs_name);
223  return true;
224  }
225  return false;
226  }
227 
235  function deleteDirectory($a_abs_name)
236  {
237  if(@file_exists($a_abs_name))
238  {
239  ilUtil::delDir($a_abs_name);
240  return true;
241  }
242  return false;
243  }
244 
245 
253  public function delete()
254  {
255  return ilUtil::delDir($this->getAbsolutePath());
256  }
257 
258 
267  public function copyFile($a_from,$a_to)
268  {
269  if(@file_exists($a_from))
270  {
271  @copy($a_from,$a_to);
272  return true;
273  }
274  return false;
275  }
276 
286  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 ?>