ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
19  private $logger = null;
20 
21 
25  public function __construct()
26  {
27  global $DIC;
28 
29  $this->logger = $DIC->logger()->wsrv();
30  $this->logger->logStack();
31 
32  parent::__construct(
34  false,
35  0
36  );
37  }
38 
44  protected function checkWebserviceActivation(\Slim\Http\Request $request, \Slim\Http\Response $response)
45  {
46  global $DIC;
47 
48  $settings = $DIC->settings();
49  if (!$settings->get('soap_user_administration', 0)) {
50  $this->logger->warning('Webservices disabled in administration.');
51 
52  $response = $response
53  ->withHeader('Content-Type', 'text/html')
54  ->withStatus(\Slim\Http\StatusCode::HTTP_FORBIDDEN)
55  ->write('Webservice not enabled.');
56  return $response;
57  }
58  return null;
59  }
60 
64  protected function getPathPrefix()
65  {
66  return 'ilRestFileStorage';
67  }
68 
72  protected function getPathPostfix()
73  {
74  return 'files';
75  }
76 
80  protected function init()
81  {
82  parent::init();
83  $this->create();
84  }
85 
90  public function getFile(\Slim\Http\Request $request, \Slim\Http\Response $response)
91  {
92  $failure = $this->checkWebserviceActivation($request, $response);
93  if ($failure instanceof \Slim\Http\Response) {
94  return $failure;
95  }
96 
97 
98  $file_id = $request->getParam('name');
99 
100  $this->logger->debug('Original file name: ' . $file_id);
101 
102  $real_path = realpath($this->getPath() . '/' . $file_id);
103  if (!$real_path) {
104  $this->logger->warning('No realpath found for ' . $this->getPath() . '/' . $file_id);
105  return $this->responeNotFound($response);
106  }
107  $file_name = basename($real_path);
108  $this->logger->debug('Translated name: ' . $this->getPath() . '/' . $file_name);
109  if (
110  $file_name &&
111  is_file($this->getPath() . '/' . $file_name) &&
112  file_exists($this->getPath() . '/' . $file_name)
113  ) {
114  $this->logger->info('Delivering file: ' . $this->getPath() . '/' . $file_name);
115  $return = file_get_contents($this->getPath() . '/' . $file_name);
116 
117  $this->logger->dump($return);
118 
119  $response = $response
120  ->withStatus(\Slim\Http\StatusCode::HTTP_OK)
121  ->withHeader('Content-Type', 'application/json')
122  ->write($return);
123  return $response;
124  }
125  $this->responeNotFound($response);
126  }
127 
128 
134  protected function responeNotFound(\Slim\Http\Response $response)
135  {
136  return $response
137  ->withHeader('Content-Type', 'text/html')
138  ->withStatus(\Slim\Http\StatusCode::HTTP_NOT_FOUND)
139  ->write('File not found');
140  }
141 
142 
143 
150  public function createFile(\Slim\Http\Request $request, \Slim\Http\Response $response)
151  {
152  $failure = $this->checkWebserviceActivation($request, $response);
153  if ($failure instanceof \Slim\Http\Response) {
154  return $failure;
155  }
156 
157  $request_body = $request->getParam('content');
158 
159  $tmpname = ilUtil::ilTempnam();
160  $path = $this->getPath() . '/' . basename($tmpname);
161 
162  $this->writeToFile($request_body, $path);
163  $return = basename($tmpname);
164 
165  $response = $response
166  ->withHeader('ContentType', 'application/json')
167  ->write($return);
168 
169  return $response;
170  }
171 
172  public function storeFileForRest($content)
173  {
174  $tmpname = ilUtil::ilTempnam();
175  $path = $this->getPath() . '/' . basename($tmpname);
176 
177  $this->writeToFile($content, $path);
178  return basename($tmpname);
179  }
180 
185  public function getStoredFilePath($tmpname)
186  {
187  return $this->getPath() . '/' . $tmpname;
188  }
189 
193  public function deleteDeprecated()
194  {
195  $max_age = time() - self::AVAILABILITY_IN_DAYS * 24 * 60 * 60;
196  $ite = new DirectoryIterator($this->getPath());
197  foreach ($ite as $file) {
198  if ($file->getCTime() <= $max_age) {
199  try {
200  @unlink($file->getPathname());
201  } catch (Exception $e) {
202  $this->logger->warning($e->getMessage());
203  }
204  }
205  }
206  }
207 }
$failure
createFile(\Slim\Http\Request $request, \Slim\Http\Response $response)
Create new file from post.
global $DIC
Definition: saml.php:7
init()
init and create directory
deleteDeprecated()
Delete deprecated files.
getFile(\Slim\Http\Request $request, \Slim\Http\Response $response)
getPathPostfix()
Get path prefix.
writeToFile($a_data, $a_absolute_path)
Write data to file.
checkWebserviceActivation(\Slim\Http\Request $request, \Slim\Http\Response $response)
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
File storage handling.
getPathPrefix()
Get path prefix.
Slim Framework (https://slimframework.com)
Definition: App.php:9
responeNotFound(\Slim\Http\Response $response)
Send 403.
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
$response