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