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