5757
5858namespace GlpiPlugin \Example ;
5959
60+ use Glpi \Exception \Http \NotFoundHttpException ;
61+ use Glpi \Exception \Http \HttpException ;
6062use Document as GlpiDocument ;
6163
64+ use function Safe \filemtime ;
65+ use function Safe \filesize ;
66+ use function Safe \fopen ;
67+ use function Safe \fread ;
68+ use function Safe \preg_match ;
69+ use function Safe \set_time_limit ;
70+
6271class Document extends GlpiDocument
6372{
6473 /**
@@ -142,8 +151,7 @@ protected function sendFile()
142151
143152 // Ensure the file exists
144153 if (!file_exists ($ streamSource ) || !is_file ($ streamSource )) {
145- header ('HTTP/1.0 404 Not Found ' );
146- exit (0 );
154+ throw new NotFoundHttpException ();
147155 }
148156
149157 // Download range defaults to the full file
@@ -157,8 +165,7 @@ protected function sendFile()
157165 // Open the file
158166 $ fileHandle = @fopen ($ streamSource , 'rb ' );
159167 if (!$ fileHandle ) {
160- header ('HTTP/1.0 500 Internal Server Error ' );
161- exit (0 );
168+ throw new HttpException (500 , 'Internal Server Error ' );
162169 }
163170
164171 // set range if specified by the client
@@ -174,8 +181,7 @@ protected function sendFile()
174181 // seek to the begining of the range
175182 $ currentPosition = $ begin ;
176183 if (fseek ($ fileHandle , $ begin , SEEK_SET ) < 0 ) {
177- header ('HTTP/1.0 500 Internal Server Error ' );
178- exit (0 );
184+ throw new HttpException (500 , 'Internal Server Error ' );
179185 }
180186
181187 // send headers to ensure the client is able to detect a corrupted download
@@ -206,9 +212,8 @@ protected function sendFile()
206212 // allow a few seconds to send a few KB.
207213 set_time_limit (10 );
208214 $ content = fread ($ fileHandle , min (1024 * 16 , $ end - $ currentPosition + 1 ));
209- if ($ content === false ) {
210- header ('HTTP/1.0 500 Internal Server Error ' , true ); // Replace previously sent headers
211- exit (0 );
215+ if (empty ($ content )) {
216+ throw new HttpException (500 , 'Internal Server Error ' );
212217 } else {
213218 print $ content ;
214219 }
@@ -217,6 +222,6 @@ protected function sendFile()
217222 }
218223
219224 // End now to prevent any unwanted bytes
220- exit ( 0 ) ;
225+ return ;
221226 }
222227}
0 commit comments