Asset.php
[Pman.Core] / Asset.php
1 <?php
2  
3  /**
4   * Generic cached assset server...
5   *
6   * Hydra generates a url for the compressed files as /Asset/css/xxxx.yyy.zzz
7   *
8   */
9  
10 require_once 'Pman.php';
11
12 class Pman_Core_Asset extends Pman {
13     
14     
15     var $types = array(
16         'css' => 'text/css',
17         'js' => 'text/javascript',
18     );
19     
20     
21     function get($s='')
22     {
23        
24         $bits = explode('/', $s);
25         
26         if (empty($bits[0]) || empty($bits[1])  || !isset($this->types[$bits[0]])) {
27             $this->jerr("invalid url");
28         }
29        
30         $s = str_replace('/', '-', $bits[1]);
31         
32         $ui = posix_getpwuid(posix_geteuid());
33         $ff = HTML_FlexyFramework::get();
34         
35         $compile = session_save_path() . '/' .
36                 $ui['name'] . '-' . $ff->project . '-' . $ff->version .  '-'. $bits[0] . 'compile';
37      
38         $fn = $compile . '/'. $s .'.'. $bits[0];
39         
40         
41         
42         
43         if (!file_exists($fn)) {
44             header('Content-Type: '. $this->types[$bits[0]]);
45         
46             echo "// compiled file not found = $fn";
47             exit;
48         }
49         
50         $last_modified_time = filemtime($fn);
51         
52         
53         if (
54             (
55                 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
56                 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
57             )
58             ||
59             (
60                  isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
61                 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
62             )) { 
63             header("HTTP/1.1 304 Not Modified");
64             exit;
65         }
66         
67         header('Content-Type: '. $this->types[$bits[0]]);
68         
69         
70         header("Pragma: public");
71         header('Content-Length: '. filesize($fn));
72         header('Cache-Control: max-age=2592000, public');
73         header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
74         header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
75         header('Etag: '. md5($fn)); 
76         
77         $fh = fopen($fn,'r');
78         fpassthru($fh);
79         fclose($fh);
80         exit;
81         
82     }
83     function post($s='') {
84         die('invalid');
85     }
86      
87     
88 }