ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilRestFileStorage.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once './Services/FileSystem/classes/class.ilFileSystemStorage.php';
5 
13 {
15 
16 
20  public function __construct()
21  {
22  parent::__construct(
24  false,
25  0
26  );
27  }
28 
32  protected function checkWebserviceActivation()
33  {
34  $settings = $GLOBALS['ilSetting'];
35  if(!$settings->get('soap_user_administration',0))
36  {
37  Slim::getInstance()->response()->header('Content-Type','text/html');
38  Slim::getInstance()->response()->status(403);
39  Slim::getInstance()->response()->body('Webservices not enabled.');
40  return false;
41  }
42  return true;
43  }
44 
48  protected function getPathPrefix()
49  {
50  return 'ilRestFileStorage';
51  }
52 
56  protected function getPathPostfix()
57  {
58  return 'files';
59  }
60 
64  protected function init()
65  {
66  parent::init();
67  $this->create();
68  }
69 
74  public function getFile($name)
75  {
76  if(!$this->checkWebserviceActivation())
77  {
78  return false;
79  }
80 
81  $GLOBALS['ilLog']->write(__METHOD__.' original name: '.$this->getPath().'/'.$name);
82 
83  $real_path = realpath($this->getPath().'/'.$name);
84  if(!$real_path)
85  {
86  $GLOBALS['ilLog']->write(__METHOD__.' no realpath found for: '.$this->getPath().'/'.$name);
87  $this->responeNotFound();
88  return;
89  }
90  $file_name = basename($real_path);
91  $GLOBALS['ilLog']->write(__METHOD__.' translated name: '.$this->getPath().'/'.$file_name);
92  if(
93  $file_name &&
94  is_file($this->getPath().'/'.$file_name) &&
95  file_exists($this->getPath().'/'.$file_name)
96  )
97  {
98  $GLOBALS['ilLog']->write(__METHOD__.' delivering file: ' . $this->getPath().'/'.$file_name);
99  $return = file_get_contents($this->getPath().'/'.$file_name);
100  // Response header
101  Slim::getInstance()->response()->header('Content-Type', 'application/json');
102  Slim::getInstance()->response()->body($return);
103  return;
104  }
105 
106  $this->responeNotFound();
107  }
108 
109 
113  protected function responeNotFound()
114  {
115  $GLOBALS['ilLog']->write(__METHOD__.' file not found.');
116  Slim::getInstance()->response()->header('Content-Type','text/html');
117  Slim::getInstance()->response()->status(404);
118  Slim::getInstance()->response()->body('Not found');
119  }
120 
125  public function createFile()
126  {
127  if(!$this->checkWebserviceActivation())
128  {
129  return false;
130  }
131 
132  $request = Slim::getInstance()->request();
133  $body = $request->post("content");
134 
135  $tmpname = ilUtil::ilTempnam();
136  $path = $this->getPath().'/'.basename($tmpname);
137 
138  $this->writeToFile($body, $path);
139  $return = basename($tmpname);
140 
141  $GLOBALS['ilLog']->write(__METHOD__.' Writing to path '.$path);
142 
143  Slim::getInstance()->response()->header('Content-Type', 'application/json');
144  Slim::getInstance()->response()->body($return);
145  }
146 
147  public function storeFileForRest($content)
148  {
149  $tmpname = ilUtil::ilTempnam();
150  $path = $this->getPath().'/'.basename($tmpname);
151 
152  $this->writeToFile($content, $path);
153  return basename($tmpname);
154  }
155 
156  public function getStoredFilePath($tmpname)
157  {
158  return $this->getPath().'/'.$tmpname;
159  }
160 
164  public function deleteDeprecated()
165  {
166  $max_age = time() - self::AVAILABILITY_IN_DAYS * 24 * 60 * 60;
167  $ite = new DirectoryIterator($this->getPath());
168  foreach($ite as $file)
169  {
170  if($file->getCTime() <= $max_age)
171  {
172  try {
173  @unlink($file->getPathname());
174  }
175  catch(Exception $e) {
176  $GLOBALS['ilLog']->write(__METHOD__.' '. $e->getMessage());
177  }
178  }
179  }
180  }
181 }
182 ?>
static getInstance( $name='default')
Get Slim application with name.
Definition: Slim.php:254
createFile()
Get file by md5 hash.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
init()
init and create directory
deleteDeprecated()
Delete deprecated files.
$real_path
Definition: aliased.php:29
getFile($name)
Get file by md5 hash.
getPathPostfix()
Get path prefix.
writeToFile($a_data, $a_absolute_path)
Write data to file.
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
File storage handling.
getPathPrefix()
Get path prefix.
responeNotFound()
Send not found response.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
checkWebserviceActivation()
Check if soap administration is enabled.