Guest User

Untitled

a guest
Nov 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. <?php
  2. define ( '__ABSPATH__', dirname ( __FILE__ ) . '/' );
  3. define ( '__CSSPATH__', 'css/' );
  4. define ( '__JSPATH__', 'js/' );
  5. define ( '__LENGTH__', 60 * 60 * 24 * 14 );
  6.  
  7. class Resources
  8. {
  9.     private $packages = array ( );
  10.    
  11.     public function __construct ( )
  12.     {
  13.         $xml = simplexml_load_file("package-config.xml");
  14.        
  15.         foreach ( $xml -> package as $pckg )
  16.         {
  17.             $type = (string) $pckg [ 'type' ];
  18.             $name = (string) $pckg [ 'name' ];
  19.            
  20.             $minify = $pckg [ 'minify' ];
  21.            
  22.             foreach ( $pckg -> file as $file )
  23.             {
  24.                 $this -> packages [ $type ] [ $name ] [ 'files' ] [ ] = $file [ 'url' ];
  25.             }
  26.  
  27.             $this -> packages [ $type ] [ $name ] [ 'minify' ] = $minify;
  28.         }
  29.     }
  30.    
  31.     public function load_package ( $key, $cache = true )
  32.     {
  33.         if ( ! preg_match ( "/^(\w+\.(?:css|js))$/", $key ) )
  34.         {
  35.             show_404 ( );
  36.         }
  37.        
  38.         $key_bits = explode ( ".", $key );
  39.         $pckg = $key_bits [ 0 ];
  40.         $type = $key_bits [ 1 ];
  41.        
  42.         if ( ! isset ( $this -> packages [ $type ] [ $pckg ] )
  43.             ||
  44.             ! is_array ( $this -> packages [ $type ] [ $pckg ] ) )
  45.         {
  46.             show_404 ( );
  47.         }
  48.        
  49.         $content = array ( );
  50.        
  51.         foreach ( $this -> packages [ $type ] [ $pckg ] [ 'files' ] as $file )
  52.         {
  53.             $file_path = ( ( $type == 'css' ) ? __CSSPATH__ : __JSPATH__ ) . $file;
  54.            
  55.             if ( ! file_exists ( $file_path ) )
  56.             {
  57.                 show_404();
  58.             }
  59.            
  60.             $content [ ] = file_get_contents ( $file_path );
  61.         }
  62.        
  63.         if ( $this -> packages [ $type ] [ $pckg ] [ 'minify' ] == 'true' )
  64.         {
  65.             switch ($type)
  66.             {
  67.                 case 'css':
  68.                     foreach ( $content as $k => $v )
  69.                     {
  70.                         $content [ $k ] = $this -> minify_css ( $v );
  71.                     }
  72.                 break;
  73.                 case 'js':
  74.                     foreach ( $content as $k => $v )
  75.                     {
  76.                         $content [ $k ] = $this -> minify_js ( $v );
  77.                     }
  78.                 break;
  79.             }
  80.         }
  81.        
  82.         $this -> dump_output (
  83.             implode ( ' ', $content ),
  84.             get_mime_by_extension ( $type ),
  85.             $cache
  86.         );
  87.     }
  88.    
  89.     function dump_output ( $output, $mime, $cache = true )
  90.     {
  91.         header ( 'Content-Type: ' . $mime );
  92.        
  93.         if ( $cache === true )
  94.         {
  95.             header ( 'Pragma: public' );
  96.             header ( 'Cache-Control: maxage=' . __LENGTH__ );
  97.             header ( 'Expires: ' . gmdate ( 'D, d M Y H:i:s', time ( ) + __LENGTH__ ) . ' GMT' );
  98.         }
  99.        
  100.         die ( $output );
  101.     }
  102.    
  103.     public function minify_css ( $css )
  104.     {
  105.         // Compress whitespace.
  106.         $css = preg_replace ( '/\s+/', '', $css );
  107.  
  108.         // Remove comments.
  109.         $css = preg_replace ( '/\/\*.*?\*\//', '', $css );
  110.  
  111.         return trim ( $css );
  112.     }
  113.    
  114.     public function minify_js ( $js )
  115.     {
  116.         require_once ( "jsmin.php" );
  117.        
  118.         return ( JSMin :: minify ( $js ) );
  119.     }
  120. }
  121.  
  122. function show_404 ( )
  123. {
  124.     header ( 'HTTP/1.0 404 Not Found' );
  125.     die ( "404, Not found" );
  126. }
  127.  
  128. function get_mime_by_extension ( $ext )
  129. {
  130.     $mimes = array (
  131.         'js'  => 'application/x-javascript',
  132.         'css' => 'text/css',
  133.     );
  134.    
  135.     return ( ( isset ( $mimes [ $ext ] ) ) ? $mimes [ $ext ] : false );
  136. }
  137. ?>
Add Comment
Please, Sign In to add comment