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