]> git.bts.cx Git - cx.git/blobdiff - cx/lib/template.php
Use the correct file request call
[cx.git] / cx / lib / template.php
index 76e5318eba3477605dd9a069149db6bb10e46ce9..638c6008de3177ce4b07453a5d00907db1ea6ff7 100644 (file)
@@ -1,5 +1,52 @@
 <?php
 
 <?php
 
+cx_require('third_party', 'mime', 'mime_types.php');
+
+function cx_template_content_path($class, $path) {
+       $segments = array(CX_PATH, 'templates', $class, 'assets', $path);
+       $path = join(DIRECTORY_SEPARATOR, $segments);
+       return $path;
+}
+
+function cx_template_has_content($class, $path) {
+       return is_file(cx_template_content_path($class, $path));
+}
+
+function cx_template_output_content($class, $path, $cache_age = 30 * 86400) {
+       $full_path = cx_template_content_path($class, $path);
+
+       $extension = pathinfo($path, PATHINFO_EXTENSION);
+
+       if (isset(MIME_REVERSE_MAP[$extension])) {
+               $mime_type = MIME_REVERSE_MAP[$extension][0];
+       } else {
+               $finfo = finfo_open(FILEINFO_MIME_TYPE);
+               $mime_type = finfo_file($finfo, $full_path);
+               //finfo_close($finfo); - Deprecated
+       }
+
+       $file_size = filesize($full_path);
+       $file_modified_time = filemtime($full_path);
+
+       header('Content-Type: ' . $mime_type);
+       header('Content-Length: ' . $file_size);
+       header('Cache-Control: public, max-age=604800');
+       header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_age) . ' GMT');
+       header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $file_modified_time) . ' GMT');
+
+       if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
+               $request_modification = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
+               if ($request_modification >= $file_modified_time) {
+                       http_response_code(304);
+                       return;
+               }
+       }
+
+       $fp = fopen($full_path, 'rb');
+       fpassthru($fp);
+       fclose($fp);
+}
+
 function cx_template_render($class, $name, $variables = null) {
        global $cx_template_base, $cx_template_content;
        
 function cx_template_render($class, $name, $variables = null) {
        global $cx_template_base, $cx_template_content;
        
@@ -7,7 +54,7 @@ function cx_template_render($class, $name, $variables = null) {
 
        $output = '';
        while ($name != null) {
 
        $output = '';
        while ($name != null) {
-               $segments = array(CX_PATH, 'templates', $class, $name . '.php');
+               $segments = array(CX_PATH, 'templates', $class, 'pages', $name . '.php');
                $path = join(DIRECTORY_SEPARATOR, $segments);
                
                $base_template = null;
                $path = join(DIRECTORY_SEPARATOR, $segments);
                
                $base_template = null;