Guest User

Untitled

a guest
Nov 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.33 KB | None | 0 0
  1. <?php
  2. define('__ABSPATH__', dirname(__FILE__).'/');
  3. define('__CSSPATH__', 'css/');
  4. define('__JSPATH__', 'js/' );
  5.  
  6. class Resources
  7. {
  8.     private $packages = array ( );
  9.    
  10.     public function __construct ( )
  11.     {
  12.         $xml = simplexml_load_file("package-config.xml");
  13.        
  14.         foreach ( $xml -> package as $package )
  15.         {
  16.             $files = array ( );
  17.            
  18.             foreach ( $package -> file as $file )
  19.             {
  20.                 $files [ ] = $file [ 'url' ];
  21.             }
  22.            
  23.             $this -> packages [ (string) $package [ 'type' ] ] [ (string) $package [ 'name' ] ] = array ( "files" => $files );
  24.         }
  25.     }
  26.    
  27.     public function loadResource ( $key )
  28.     {  
  29.         $key_bits = explode ( ".", $key );
  30.  
  31.         $id = UrlShortener::shortToInt ( $key_bits [ 0 ] );
  32.         $type = $key_bits [ 1 ];
  33.        
  34.         $query = mysql_query ( "SELECT name, built FROM resources_v2 WHERE id = '$id' AND type = '$type' LIMIT 1" );
  35.        
  36.         if ( mysql_num_rows ( $query ) > 0 )
  37.         {
  38.             $row = mysql_fetch_assoc ( $query );
  39.            
  40.             if ( is_array ( $this -> packages [ $type ] [ $row [ 'name' ] ] ) )
  41.             {
  42.                 header("Cache-Control: max-age=1209600, must-revalidate");
  43.                 //header("Last-Modified: " . gmdate('D, d M Y H:i:s', strtotime ( $row [ 'built' ] ) ) . ' GMT' );
  44.                 //header("HTTP/1.1 304 Not Modified");
  45.                
  46.                 $path = $this -> getPath ( $type );
  47.  
  48.                 $content = '';
  49.                
  50.                 foreach ( $this -> packages [ $type ] [ $row [ 'name' ] ] [ 'files' ] as $file )
  51.                 {
  52.                     $content .= file_get_contents ( $path . $file ) . "\n";
  53.                 }
  54.            
  55.                 switch ( $type )
  56.                 {
  57.                     case "js":
  58.                         header ( "content-type: application/javascript" );
  59.                         echo $this -> minifyJS ( $content );
  60.                         break;
  61.                     case "css":
  62.                         header ( "content-type: text/css" );
  63.                         echo $this -> minifyCSS ( $content );
  64.                         break;
  65.                 }
  66.                
  67.                 return;
  68.             }
  69.         }
  70.        
  71.         header ( 'HTTP/1.0 404 Not Found' );
  72.         die ( "404, Not found" );
  73.     }
  74.  
  75.     public function resourceLink ( $type, $package )
  76.     {
  77.         if ( $this -> isValidPackage ( $type, $package ) === TRUE )
  78.         {
  79.             $path = $this -> getPath ( $type );
  80.            
  81.             $total = 0;
  82.            
  83.             foreach ( $this -> packages [ $type ] [ $package ] [ 'files' ] as $file )
  84.             {
  85.                 if ( ! file_exists ( $path . $file ) )
  86.                 {
  87.                     // File does not exist... return
  88.                     return ( NULL );
  89.                 }
  90.                
  91.                 $total += filemtime ( $path . $file );
  92.             }
  93.            
  94.             if ( ( $data = $this -> resourceRowExists ( $package, $type ) ) !== FALSE )
  95.             {
  96.                 if ( $total != $data [ 0 ] [ 'lmodified_total' ] )
  97.                 {
  98.                     if ( count ( $data ) > 1 )
  99.                     {
  100.                         $ids = implode(', ', array_map(function ($current) { return $current['id']; }, array_slice ( $data, 1 )));
  101.                        
  102.                         $this -> resourceRowDelete ( $ids );
  103.                     }
  104.                    
  105.                     $this -> resourceRowCreate ( $package, $type, $total );
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 $this -> resourceRowCreate ( $package, $type, $total );
  111.             }
  112.            
  113.             $query = mysql_query ( "SELECT id FROM resources_v2 WHERE name = '$package' AND type = '$type' ORDER BY id DESC LIMIT 1" ) or die ( mysql_error ( ) );
  114.            
  115.             $row = mysql_fetch_assoc ( $query );
  116.            
  117.             $key = UrlShortener::intToShort ( $row [ 'id' ] );
  118.            
  119.             return ( 'rsrc/' . $key . '.' . $type );
  120.         }
  121.        
  122.         return ( NULL );
  123.     }
  124.    
  125.     public function css ( $package )
  126.     {
  127.         if ( ( $link = $this -> resourceLink ( "css", $package ) ) != NULL )
  128.         {
  129.             return ( "<link rel=\"stylesheet\" type=\"text/css\" href=\"$link\" />" );
  130.         }
  131.        
  132.         return ( NULL );
  133.     }
  134.    
  135.     public function js ( $package )
  136.     {
  137.         if ( ( $link = $this -> resourceLink ( "js", $package ) ) != NULL )
  138.         {
  139.             return ( "<script type=\"text/javascript\" src=\"$link\"></script>" );
  140.         }
  141.        
  142.         return ( NULL );
  143.     }
  144.    
  145.     public function minifyCSS ( $css )
  146.     {
  147.         // Compress whitespace.
  148.         $css = preg_replace ( '/\s+/', '', $css );
  149.  
  150.         // Remove comments.
  151.         $css = preg_replace ( '/\/\*.*?\*\//', '', $css );
  152.  
  153.         return trim ( $css );
  154.     }
  155.    
  156.     public function minifyJS ( $js )
  157.     {
  158.         require_once ( "jsmin.php" );
  159.        
  160.         return ( JSMin :: minify ( $js ) );
  161.     }
  162.    
  163.     public function getPath ( $type )
  164.     {
  165.         switch ( $type )
  166.         {
  167.             case "css":
  168.                 $path = __CSSPATH__;
  169.                 break;
  170.             case "js":
  171.                 $path = __JSPATH__;
  172.                 break;
  173.         }
  174.        
  175.         return ( $path );
  176.     }
  177.    
  178.     public function resourceRowDelete ( $ids )
  179.     {
  180.         if ( empty( $ids ) )
  181.         {
  182.             return ( FALSE );
  183.         }
  184.  
  185.         $query = mysql_query ( "DELETE FROM resources_v2 WHERE id IN($ids)" );
  186.     }
  187.    
  188.     public function resourceRowCreate ( $package, $type, $total )
  189.     {
  190.         $query = mysql_query ( "INSERT INTO resources_v2 (name, type, lmodified_total, built) VALUES ('$package', '$type', $total, NOW())" ) or die ( mysql_error ( ) );
  191.     }
  192.    
  193.     // This function does not return true
  194.         // but an array of data
  195.     public function resourceRowExists ( $package, $type )
  196.     {
  197.         $query = mysql_query ( "SELECT id, lmodified_total FROM resources_v2 WHERE name = '$package' AND type = '$type' ORDER BY id DESC" ) or die ( mysql_error ( ) );
  198.    
  199.         if ( mysql_num_rows ( $query ) > 0 )
  200.         {
  201.             // The newest should be first
  202.             $data = array ( );
  203.            
  204.             while ( $row = mysql_fetch_assoc ( $query ) )
  205.             {
  206.                 $data [ ] = $row;
  207.             }
  208.            
  209.             return ( $data );
  210.         }
  211.         else
  212.         {
  213.             return ( FALSE );  
  214.         }
  215.     }
  216.    
  217.     public function isValidPackage ( $type, $package )
  218.     {  
  219.         if ( isset ( $this -> packages [ $type ] [ $package ] ) && is_array ( $this -> packages [ $type ] [ $package ] ) )
  220.         {
  221.             return ( TRUE );
  222.         }
  223.        
  224.         return ( FALSE );
  225.     }
  226. }
  227. ?>
Add Comment
Please, Sign In to add comment