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     var $types = array(
27         'css' => 'text/css',
28         'js' => 'text/javascript',
29     );
30     
31     function getAuth()
32     {
33         return true;
34     }
35     
36     
37     function get($s='', $opts = Array())
38     {
39         
40         $this->sessionState(0);
41         
42         $bits = explode('/', $s);
43         
44         if (empty($bits[0]) || empty($bits[1])  || !isset($this->types[$bits[0]])) {
45             $this->jerr("invalid url");
46         }
47        
48         $s = str_replace('/', '-', $bits[1]);
49         
50         $ui = posix_getpwuid(posix_geteuid());
51         $ff = HTML_FlexyFramework::get();
52         
53         $compile = session_save_path() . '/' .
54                 $ui['name'] . '-' . $ff->project . '-' . $ff->version .  '-'. $bits[0] . 'compile';
55      
56         $fn = $compile . '/'. $s .'.'. $bits[0];
57         
58         
59         
60         
61         if (!file_exists($fn)) {
62             header('Content-Type: '. $this->types[$bits[0]]);
63         
64             echo "// compiled file not found = $fn";
65             exit;
66         }
67         
68         $last_modified_time = filemtime($fn);
69         
70         
71         if (
72             (
73                 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
74                 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
75             )
76             ||
77             (
78                  isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
79                 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
80             )) { 
81             header("HTTP/1.1 304 Not Modified");
82             exit;
83         }
84         
85         header('Content-Type: '. $this->types[$bits[0]]);
86         
87         
88         header("Pragma: public");
89         header('Content-Length: '. filesize($fn));
90         header('Cache-Control: max-age=2592000, public');
91         header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
92         header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
93         header('Etag: '. md5($fn)); 
94         
95         $fh = fopen($fn,'r');
96         fpassthru($fh);
97         fclose($fh);
98         exit;
99         
100     }
101     function post($s='') {
102         die('invalid');
103     }
104      
105     
106 }