ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilRestFileStorage.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
29 {
30  private const AVAILABILITY_IN_DAYS = 1;
31 
32  private $logger;
33 
34  protected ilSetting $settings;
35 
39  public function __construct()
40  {
41  global $DIC;
42 
43  $this->settings = $DIC->settings();
44  $this->logger = $DIC->logger()->wsrv();
47  false,
48  0
49  );
50  }
51 
52  protected function checkWebserviceActivation(Request $request, Response $response): ?Response
53  {
54  if (!$this->settings->get('soap_user_administration', '0')) {
55  $this->logger->warning('Webservices disabled in administration.');
56 
57  return $response
58  ->withHeader('Content-Type', 'text/html')
59  ->withStatus(\Slim\Http\StatusCode::HTTP_FORBIDDEN)
60  ->write('Webservice not enabled.');
61  }
62  return null;
63  }
64 
65  protected function getPathPrefix(): string
66  {
67  return 'ilRestFileStorage';
68  }
69 
70  protected function getPathPostfix(): string
71  {
72  return 'files';
73  }
74 
78  protected function init(): bool
79  {
80  parent::init();
81  $this->create();
82  return true;
83  }
84 
85  public function getFile(Request $request, Response $response): Response
86  {
87  $failure = $this->checkWebserviceActivation($request, $response);
88  if ($failure instanceof \Slim\Http\Response) {
89  return $failure;
90  }
91 
92  $file_id = $request->getParam('name');
93 
94  $this->logger->debug('Original file name: ' . $file_id);
95 
96  $real_path = realpath($this->getPath() . '/' . $file_id);
97  if (!$real_path) {
98  $this->logger->warning('No realpath found for ' . $this->getPath() . '/' . $file_id);
99  return $this->responeNotFound($response);
100  }
101  $file_name = basename($real_path);
102  $this->logger->debug('Translated name: ' . $this->getPath() . '/' . $file_name);
103  if (
104  $file_name &&
105  is_file($this->getPath() . '/' . $file_name) &&
106  file_exists($this->getPath() . '/' . $file_name)
107  ) {
108  $this->logger->info('Delivering file: ' . $this->getPath() . '/' . $file_name);
109  $return = file_get_contents($this->getPath() . '/' . $file_name);
110 
111  $this->logger->dump($return);
112 
113  return $response
114  ->withStatus(\Slim\Http\StatusCode::HTTP_OK)
115  ->withHeader('Content-Type', 'application/json')
116  ->write($return);
117  }
118  return $this->responeNotFound($response);
119  }
120 
121 
127  protected function responeNotFound(Response $response): Response
128  {
129  return $response
130  ->withHeader('Content-Type', 'text/html')
131  ->withStatus(\Slim\Http\StatusCode::HTTP_NOT_FOUND)
132  ->write('File not found');
133  }
134 
135 
139  public function createFile(Request $request, Response $response): Response
140  {
141  $failure = $this->checkWebserviceActivation($request, $response);
142  if ($failure instanceof \Slim\Http\Response) {
143  return $failure;
144  }
145 
146  $request_body = $request->getParam('content');
147 
148  $tmpname = ilFileUtils::ilTempnam();
149  $path = $this->getPath() . '/' . basename($tmpname);
150 
151  $this->writeToFile($request_body, $path);
152  $return = basename($tmpname);
153 
154  return $response
155  ->withHeader('ContentType', 'application/json')
156  ->write($return);
157  }
158 
159  public function storeFileForRest(string $content): string
160  {
161  $tmpname = ilFileUtils::ilTempnam();
162  $path = $this->getPath() . '/' . basename($tmpname);
163 
164  $this->writeToFile($content, $path);
165  return basename($tmpname);
166  }
167 
168  public function getStoredFilePath(string $tmpname): string
169  {
170  return $this->getPath() . '/' . $tmpname;
171  }
172 
176  public function deleteDeprecated(): void
177  {
178  $max_age = time() - self::AVAILABILITY_IN_DAYS * 24 * 60 * 60;
179  $ite = new DirectoryIterator($this->getPath());
180  foreach ($ite as $file) {
181  if ($file->getCTime() <= $max_age) {
182  try {
183  unlink($file->getPathname());
184  } catch (Exception $e) {
185  $this->logger->warning($e->getMessage());
186  }
187  }
188  }
189  }
190 
191  public function writeToFile($a_data, $a_absolute_path): bool
192  {
193  if (!$fp = fopen($a_absolute_path, 'wb+')) {
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 }
getFile(Request $request, Response $response)
writeToFile($a_data, $a_absolute_path)
checkWebserviceActivation(Request $request, Response $response)
createFile(Request $request, Response $response)
Create new file from post.
responeNotFound(Response $response)
Send 403.
getStoredFilePath(string $tmpname)
init()
init and create directory
$response
Definition: xapitoken.php:93
deleteDeprecated()
Delete deprecated files.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:22
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
File storage handling.
__construct(Container $dic, ilPlugin $plugin)
storeFileForRest(string $content)