ILIAS  release_8 Revision v8.24
class.ilRestFileStorage.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
6
7use Slim\Http\Request;
8use Slim\Http\Response;
9
15{
16 private const AVAILABILITY_IN_DAYS = 1;
17
18 private $logger;
19
21
25 public function __construct()
26 {
27 global $DIC;
28
29 $this->settings = $DIC->settings();
30 $this->logger = $DIC->logger()->wsrv();
33 false,
34 0
35 );
36 }
37
38 protected function checkWebserviceActivation(Request $request, Response $response): ?Response
39 {
40 if (!$this->settings->get('soap_user_administration', '0')) {
41 $this->logger->warning('Webservices disabled in administration.');
42
43 return $response
44 ->withHeader('Content-Type', 'text/html')
45 ->withStatus(\Slim\Http\StatusCode::HTTP_FORBIDDEN)
46 ->write('Webservice not enabled.');
47 }
48 return null;
49 }
50
51 protected function getPathPrefix(): string
52 {
53 return 'ilRestFileStorage';
54 }
55
56 protected function getPathPostfix(): string
57 {
58 return 'files';
59 }
60
64 protected function init(): bool
65 {
66 parent::init();
67 $this->create();
68 return true;
69 }
70
71 public function getFile(Request $request, Response $response): Response
72 {
73 $failure = $this->checkWebserviceActivation($request, $response);
74 if ($failure instanceof \Slim\Http\Response) {
75 return $failure;
76 }
77
78 $file_id = $request->getParam('name');
79
80 $this->logger->debug('Original file name: ' . $file_id);
81
82 $real_path = realpath($this->getPath() . '/' . $file_id);
83 if (!$real_path) {
84 $this->logger->warning('No realpath found for ' . $this->getPath() . '/' . $file_id);
85 return $this->responeNotFound($response);
86 }
87 $file_name = basename($real_path);
88 $this->logger->debug('Translated name: ' . $this->getPath() . '/' . $file_name);
89 if (
90 $file_name &&
91 is_file($this->getPath() . '/' . $file_name) &&
92 file_exists($this->getPath() . '/' . $file_name)
93 ) {
94 $this->logger->info('Delivering file: ' . $this->getPath() . '/' . $file_name);
95 $return = file_get_contents($this->getPath() . '/' . $file_name);
96
97 $this->logger->dump($return);
98
99 return $response
100 ->withStatus(\Slim\Http\StatusCode::HTTP_OK)
101 ->withHeader('Content-Type', 'application/json')
102 ->write($return);
103 }
104 return $this->responeNotFound($response);
105 }
106
107
113 protected function responeNotFound(Response $response): Response
114 {
115 return $response
116 ->withHeader('Content-Type', 'text/html')
117 ->withStatus(\Slim\Http\StatusCode::HTTP_NOT_FOUND)
118 ->write('File not found');
119 }
120
121
125 public function createFile(Request $request, Response $response): Response
126 {
127 $failure = $this->checkWebserviceActivation($request, $response);
128 if ($failure instanceof \Slim\Http\Response) {
129 return $failure;
130 }
131
132 $request_body = $request->getParam('content');
133
134 $tmpname = ilFileUtils::ilTempnam();
135 $path = $this->getPath() . '/' . basename($tmpname);
136
137 $this->writeToFile($request_body, $path);
138 $return = basename($tmpname);
139
140 return $response
141 ->withHeader('ContentType', 'application/json')
142 ->write($return);
143 }
144
145 public function storeFileForRest(string $content): string
146 {
147 $tmpname = ilFileUtils::ilTempnam();
148 $path = $this->getPath() . '/' . basename($tmpname);
149
150 $this->writeToFile($content, $path);
151 return basename($tmpname);
152 }
153
154 public function getStoredFilePath(string $tmpname): string
155 {
156 return $this->getPath() . '/' . $tmpname;
157 }
158
162 public function deleteDeprecated(): void
163 {
164 $max_age = time() - self::AVAILABILITY_IN_DAYS * 24 * 60 * 60;
165 $ite = new DirectoryIterator($this->getPath());
166 foreach ($ite as $file) {
167 if ($file->getCTime() <= $max_age) {
168 try {
169 unlink($file->getPathname());
170 } catch (Exception $e) {
171 $this->logger->warning($e->getMessage());
172 }
173 }
174 }
175 }
176
177 public function writeToFile($a_data, $a_absolute_path): bool
178 {
179 if (!$fp = fopen($a_absolute_path, 'wb+')) {
180 return false;
181 }
182 if (fwrite($fp, $a_data) === false) {
183 fclose($fp);
184 return false;
185 }
186 fclose($fp);
187 return true;
188 }
189}
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
File storage handling.
getPathPrefix()
Get path prefix.
getStoredFilePath(string $tmpname)
getPathPostfix()
Get directory name.
responeNotFound(Response $response)
Send 403.
init()
init and create directory
deleteDeprecated()
Delete deprecated files.
writeToFile($a_data, $a_absolute_path)
storeFileForRest(string $content)
checkWebserviceActivation(Request $request, Response $response)
createFile(Request $request, Response $response)
Create new file from post.
getFile(Request $request, Response $response)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$response