ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
4include_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?>
print $file
static getInstance( $name='default')
Get Slim application with name.
Definition: Slim.php:254
writeToFile($a_data, $a_absolute_path)
Write data to file.
File storage handling.
getPathPrefix()
Get path prefix.
checkWebserviceActivation()
Check if soap administration is enabled.
getPathPostfix()
Get path prefix.
responeNotFound()
Send not found response.
init()
init and create directory
deleteDeprecated()
Delete deprecated files.
createFile()
Get file by md5 hash.
getFile($name)
Get file by md5 hash.
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276