Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

sendResource()

  • Loading branch information...
commit 5a2c4c0aa024756fa122ba20ce11b2a6f4a50918 1 parent aecd7f4
@korbinus authored
Showing with 18 additions and 16 deletions.
  1. +18 −16 src/rsc.php
View
34 src/rsc.php
@@ -17,8 +17,7 @@
*
*/
-/* @todo: write a function for avoiding to have the same code twice
- * @todo: deal with if-modified-since
+/* @todo: deal with if-modified-since
*/
function error404() {
@@ -27,6 +26,16 @@ function error404() {
echo "The page that you have requested could not be found.";
exit();
}
+function sendResource ($path, $filename, $contentType) {
+ $file = $path . $filename;
+ if (file_exists($file)) {
+ header('HTTP/1.1 203'); //send a success header
+ header('Content-type: ' . $contentType); //send the content-type
+ include $file; //simply drop the content
+ } else { //otherwise
+ error404(); //not found
+ }
+}
$pathJS = '../js/'; //path to js/ directory
$pathCSS = '../css/'; //path to css/directory
@@ -39,22 +48,15 @@ function error404() {
$file = htmlspecialchars($split[1]);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
+
if (preg_match('/.js$/',$file) > 0) { //javascript file
- if(file_exists($pathJS . $file)) { //check that the file exists
- header('HTTP/1.1 203'); //send a success header
- header('Content-type: text/javascript; charset=UTF-8'); //send the content-type
- include $pathJS . $file; //simply drop the content
- } else {
- error404();
- }
+
+ sendResource($pathJS, $file, 'text/javascript');
+
} else if (preg_match('/.css$/',$file) > 0) { //css file
- if(file_exists($pathCSS . $file)) { //check that the file exists
- header('HTTP/1.1 203'); //send a success header
- header('Content-type: text/css; charset=UTF-8'); //send the content-type
- include $pathCSS . $file; //simply drop the content
- } else {
- error404();
- }
+
+ sendResource($pathCSS, $file, 'text/css');
+
} else {
error404();
}
Please sign in to comment.
Something went wrong with that request. Please try again.