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