fix #8131 - chinese translations
[Pman.Core] / Asset.php
index 0ac4ba8..3e49f8e 100644 (file)
--- a/Asset.php
+++ b/Asset.php
 require_once 'Pman.php';
 
 class Pman_Core_Asset extends Pman {
-    
-    
+     
     var $types = array(
         'css' => 'text/css',
         'js' => 'text/javascript',
+        'map' => 'application/json'
     );
     
     function getAuth()
@@ -35,35 +35,41 @@ class Pman_Core_Asset extends Pman {
     }
     
     
-    function get($s='')
+    function get($s='', $opts = Array())
     {
-       
+        $this->sessionState(0);
+        
         $bits = explode('/', $s);
         
         if (empty($bits[0]) || empty($bits[1])  || !isset($this->types[$bits[0]])) {
             $this->jerr("invalid url");
         }
        
+        $ext = $bits[0];
+        if (preg_match('/\.map$/',$_SERVER['REQUEST_URI'])) {
+            $ext = 'map';
+        }
+       
         $s = str_replace('/', '-', $bits[1]);
         
+        
         $ui = posix_getpwuid(posix_geteuid());
         $ff = HTML_FlexyFramework::get();
         
-        $compile = session_save_path() . '/' .
-                $ui['name'] . '-' . $ff->project . '-' . $ff->version .  '-'. $bits[0] . 'compile';
-     
-        $fn = $compile . '/'. $s .'.'. $bits[0];
-        
-        
+        $compile = self::getCompileDir($bits[0], '', false);
         
+        $fn = $compile . '/'. $s .'.'. $ext;
         
         if (!file_exists($fn)) {
-            header('Content-Type: '. $this->types[$bits[0]]);
+            header('Content-Type: '. $this->types[$ext]);
         
             echo "// compiled file not found = $fn";
             exit;
         }
         
+        $supportsGzip = !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;
+
+        
         $last_modified_time = filemtime($fn);
         
         
@@ -85,289 +91,116 @@ class Pman_Core_Asset extends Pman {
         
         
         header("Pragma: public");
-        header('Content-Length: '. filesize($fn));
+        
         header('Cache-Control: max-age=2592000, public');
         header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
         header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
         header('Etag: '. md5($fn)); 
         
-        $fh = fopen($fn,'r');
-        fpassthru($fh);
-        fclose($fh);
+         if ( $supportsGzip ) {
+            $content = gzencode( file_get_contents($fn) , 9);
+            
+            header('Content-Encoding: gzip');
+            header('Vary: Accept-Encoding');
+            header('Content-Length: '. strlen($content));
+            
+            echo $content;
+            
+        } else {
+            
+            
+            $fh = fopen($fn,'r');
+            fpassthru($fh);
+            fclose($fh);
+             
+        }
+        
+        
+        
         exit;
         
     }
+    
     function post($s='') {
+        if(!empty($_REQUEST['_clear_cache'])) {
+            $this->clearCompiledFilesCache();
+        }
+        
         die('invalid');
     }
      
-    
-}
-
-// a little experimental... we are going to use the same name as the class. for these..
-
-trait Pman_Core_Asset_Trait {
-    
-    
-    
-     /**
-     * usage in template
-     * {outputJavascriptDir(#Hydra#,#Hydra.js",#.......#)}
-     *
-     * call_user
-     * 
-     */
-    
-    function outputJavascriptDir($path)
+    static function getCompileDir($type, $module = '', $is_mkdir = true)
     {
-        
-        $relpath = $this->rootURL . '/' . $path .'/';
         $ff = HTML_FlexyFramework::get();
-        $dir =   $this->rootDir.'/' . $path;
-        
-        $args = func_get_args();
-        $ar = array();
-        if (count($args) < 2) {
-            $ar = glob($dir . '/*.js');
-        } else {
-            array_shift($args);
-            foreach($args as $f) {
-                if (strpos($f,'*') > -1) {
-                    $ar = array_merge($ar ,  glob($dir . '/'. $f));
-                    continue;
-                }
-                
-                $ar[] = $dir .'/'. $f;
-            }
-          
-        }
-         // cached version?? - how do we decide if it's expired?
-        // while scanning the directory is slow... - it's faster than serving every file...
-        
-        
-        //$path = $this->rootURL ."/Pman/$mod/";
-        
-        
-        
-        $files = array();
-        $arfiles = array();
-        $maxtime = 0;
-        $mtime = 0;
-        foreach($ar as $fn) {
-            $f = basename($fn);
-            // got the 'module file..'
-            $mtime = filemtime($dir . '/'. $f);
-            $maxtime = max($mtime, $maxtime);
-            $arfiles[$fn] = $mtime;
-            $files[] = $relpath  . $f . '?ts='.$mtime;
-        }
-        
-        ksort($arfiles); // just sort by name so it's consistant for serialize..
         
         $ui = posix_getpwuid(posix_geteuid());
-       
         
-        $compiledir = session_save_path() . '/' .
-                $ui['name'] . '-' . $ff->project . '-' . $ff->version . '-jscompile';
+        $compile_dir = session_save_path() . "/";
         
-        if (!file_exists($compiledir)) {
-            mkdir($compiledir,0700,true);
+        if (empty($module)) {
+            $module = $ff->project . (isset($ff->appNameShort) ?  '_' . $ff->appNameShort : '');
         }
         
-         
-        
-        $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
-        usort($files, $lsort);
         
-        
-        if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) {
-            echo "<!-- Javascript compile turned off (isDev on) -->\n";
-            $this->assetArrayToHtml($files,'js');
-            return;
+        switch($type) {
+            case 'js':
+            case 'css':
+            case 'scss':
+                $compile_dir .= implode("-", array(
+                    $ui['name'],
+                    $module,
+                    $ff->version,
+                    "{$type}compile"
+                ));
+                break;
+            // template config?
+            default:
+                return false;
         }
         
         
-        $smod = str_replace('/','.',$path);
-        
-        $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
-         
-         
+        if (file_exists($compile_dir)) {
+            return $compile_dir;
+        }
         
-        // where are we going to write all of this..
-        // This has to be done via a 
-        if (!file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
-            require_once 'Pman/Core/JsCompile.php';
-            $x = new Pman_Core_JsCompile();
-            
-            $x->pack($arfiles,$compiledir.'/'.$output, false);
-            clearstatcache();
-            if (!file_exists($compiledir.'/'.$output) ||
-                !filesize($compiledir.'/'.$output)) {
-                echo "<!-- compile did not generate files : {$compiledir}/{$output} -->\n";
-                $this->assetArrayToHtml($files,'js');
-                return;
-            } 
-            
-        } else {
-         //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
+        if(!$is_mkdir) {
+            return false;
         }
-        //$this->arrayToJsInclude(  $files);
-        $this->assetArrayToHtml(  array(
-            $this->baseURL.'/Asset/js/'. $output,
-          
-        ), 'js');
         
-    }
-    
-    function assetArrayToHtml($ar, $type)
-    {
-        foreach($ar as $f) {
-            switch( $type) {
-                case 'js':
-                    echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
-                    break;
-                case 'css':
-                    echo '<link rel="stylesheet" href="'. $f. '"/>'."\n";
-                    break;
-       
-            }
-            
+        if(mkdir($compile_dir, 0700, true)) {
+            return $compile_dir;
         }
+        
+        return false;
     }
     
-    
-    /**
-     * usage in template
-     * {outputCssDir(#{Hydra/templates/images/css/#,#Hydra.js",#.......#)}
-     */
-    
-    function outputCssDir($path)
+    function clearCompiledFilesCache()
     {
-          
-        $relpath = $this->rootURL . '/' . $path .'/';
-        $ff = HTML_FlexyFramework::get();
-        $dir =   $this->rootDir.'/' . $path;
-        
-        $args = func_get_args();
-        $ar = array();
-        if (count($args) < 2) {
-            $ar = glob($dir . '/*.css');
-        } else {
-            array_shift($args);
-            foreach($args as $f) {
-                if (strpos($f,'*') > -1) {
-                    $ar = array_merge($ar ,  glob($dir . '/'. $f));
-                    continue;
-                }
-                // what if the fiel does not exist???
-                $ar[] = $dir .'/'. $f;
-            }
-          
+        $au = $this->getAuthUser();
+        if (!$au && !in_array($_SERVER['REMOTE_ADDR'] , array('127.0.0.1','::1'))) {
+            $this->jerr("Cache can only be cleared by authenticated users");
         }
         
+        require_once 'System.php';
+        $ff = HTML_FlexyFramework::get();
         
-         // cached version?? - how do we decide if it's expired?
-        // while scanning the directory is slow... - it's faster than serving every file...
-        
+        $mods = $this->modulesList();
+        $mods[] = $ff->project; // Pman - this was the old format...
+        $mods[] = ''; // Pman + appshortname..
         
-        //$path = $this->rootURL ."/Pman/$mod/";
+        foreach ($mods as $module) {
+            $compile_dir = $this->getCompileDir('js', $module, false);
         
-        //print_R($ar);exit;
-        $missing_files  = false;
-        $files = array();
-        $arfiles = array();
-        $relfiles = array(); // array of files without the path part...
-        $maxtime = 0;
-        $mtime = 0;
-        foreach($ar as $fn) {
-            $relfiles[] = substr($fn, strlen($dir)+1);
-            $f = basename($fn);
-            // got the 'module file..'
-            
-            if (!file_exists($dir . '/'. $f)) {
-                echo "<!-- missing {$relpath}{$f} -->\n";
-                $files[] = $relpath  . $f . '?ts=0';
-                $missing_files = true;
-                continue;
+            if(!empty($compile_dir)) {
+                System::rm(array('-r', $compile_dir));
             }
-            
-            $mtime = filemtime($dir . '/'. $f);
-            $maxtime = max($mtime, $maxtime);
-            $arfiles[$fn] = $mtime;
-            $files[] = $relpath  . $f . '?ts='.$mtime;
-            
-            
-            
-        }
-        if ($missing_files) {
-            $this->assetArrayToHtml($files, 'css');
-            return;
-            
-        }
-        
-         
-        //print_r($relfiles);
-        
-        $ui = posix_getpwuid(posix_geteuid());
-       
-        
-        $compiledir = session_save_path() . '/' .
-                $ui['name'] . '-' . $ff->project . '-'. $ff->version . '-csscompile';
-        
-        if (!file_exists($compiledir)) {
-            mkdir($compiledir,0700,true);
-        }
+            $compile_dir = $this->getCompileDir('css', $module, false);
         
-         
-        // no sorting???
-        //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
-        //usort($files, $lsort);
-        
-        
-        if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) {
-            echo "<!-- CSS compile turned off (isDev on) -->\n";
-            $this->assetArrayToHtml($files,'css');
-            return;
+            if(!empty($compile_dir)) {
+                System::rm(array('-r', $compile_dir));
+            }
         }
-        
-        
-        $smod = str_replace('/','.',$path);
-        
-        $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.css';
          
-         
-        
-        // where are we going to write all of this..
-        // This has to be done via a 
-        if (true || !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
-            
-            
-            
-            require_once 'HTML/CSS/Minify.php';
-            $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
-            
-            file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.'/Asset/css'));
-            clearstatcache();
-            if (!file_exists($compiledir.'/'.$output) ||
-                !filesize($compiledir.'/'.$routput)) {
-                echo "<!-- compile did not generate files : " . basename($compiledir) . "/{$output} -->\n";
-                $this->assetArrayToHtml($files,'css');
-                return;
-            } 
-            
-        } else {
-         //   echo "<!-- file already exists: {$basedir}/{$output} -->\n";
-        }
-        //$this->arrayToJsInclude(  $files);
-        $this->assetArrayToHtml(  array(
-            $this->baseURL.'/Asset/css/'. $output,
-          
-        ),'css');
-        
+        $this->jok('DONE');
     }
-    
-    
-    
-}
\ No newline at end of file
+}