Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('__ABSPATH__', dirname(__FILE__).'/');
- define('__CSSPATH__', 'css/');
- define('__JSPATH__', 'js/' );
- class Resources
- {
- private $packages = array ( );
- public function __construct ( )
- {
- $xml = simplexml_load_file("package-config.xml");
- foreach ( $xml -> package as $package )
- {
- $files = array ( );
- foreach ( $package -> file as $file )
- {
- $files [ ] = $file [ 'url' ];
- }
- $this -> packages [ (string) $package [ 'type' ] ] [ (string) $package [ 'name' ] ] = array ( "files" => $files );
- }
- }
- public function loadResource ( $key )
- {
- $key_bits = explode ( ".", $key );
- $id = UrlShortener::shortToInt ( $key_bits [ 0 ] );
- $type = $key_bits [ 1 ];
- $query = mysql_query ( "SELECT name, built FROM resources_v2 WHERE id = '$id' AND type = '$type' LIMIT 1" );
- if ( mysql_num_rows ( $query ) > 0 )
- {
- $row = mysql_fetch_assoc ( $query );
- if ( is_array ( $this -> packages [ $type ] [ $row [ 'name' ] ] ) )
- {
- header("Cache-Control: max-age=1209600, must-revalidate");
- //header("Last-Modified: " . gmdate('D, d M Y H:i:s', strtotime ( $row [ 'built' ] ) ) . ' GMT' );
- //header("HTTP/1.1 304 Not Modified");
- $path = $this -> getPath ( $type );
- $content = '';
- foreach ( $this -> packages [ $type ] [ $row [ 'name' ] ] [ 'files' ] as $file )
- {
- $content .= file_get_contents ( $path . $file ) . "\n";
- }
- switch ( $type )
- {
- case "js":
- header ( "content-type: application/javascript" );
- echo $this -> minifyJS ( $content );
- break;
- case "css":
- header ( "content-type: text/css" );
- echo $this -> minifyCSS ( $content );
- break;
- }
- return;
- }
- }
- header ( 'HTTP/1.0 404 Not Found' );
- die ( "404, Not found" );
- }
- public function resourceLink ( $type, $package )
- {
- if ( $this -> isValidPackage ( $type, $package ) === TRUE )
- {
- $path = $this -> getPath ( $type );
- $total = 0;
- foreach ( $this -> packages [ $type ] [ $package ] [ 'files' ] as $file )
- {
- if ( ! file_exists ( $path . $file ) )
- {
- // File does not exist... return
- return ( NULL );
- }
- $total += filemtime ( $path . $file );
- }
- if ( ( $data = $this -> resourceRowExists ( $package, $type ) ) !== FALSE )
- {
- if ( $total != $data [ 0 ] [ 'lmodified_total' ] )
- {
- if ( count ( $data ) > 1 )
- {
- $ids = implode(', ', array_map(function ($current) { return $current['id']; }, array_slice ( $data, 1 )));
- $this -> resourceRowDelete ( $ids );
- }
- $this -> resourceRowCreate ( $package, $type, $total );
- }
- }
- else
- {
- $this -> resourceRowCreate ( $package, $type, $total );
- }
- $query = mysql_query ( "SELECT id FROM resources_v2 WHERE name = '$package' AND type = '$type' ORDER BY id DESC LIMIT 1" ) or die ( mysql_error ( ) );
- $row = mysql_fetch_assoc ( $query );
- $key = UrlShortener::intToShort ( $row [ 'id' ] );
- return ( 'rsrc/' . $key . '.' . $type );
- }
- return ( NULL );
- }
- public function css ( $package )
- {
- if ( ( $link = $this -> resourceLink ( "css", $package ) ) != NULL )
- {
- return ( "<link rel=\"stylesheet\" type=\"text/css\" href=\"$link\" />" );
- }
- return ( NULL );
- }
- public function js ( $package )
- {
- if ( ( $link = $this -> resourceLink ( "js", $package ) ) != NULL )
- {
- return ( "<script type=\"text/javascript\" src=\"$link\"></script>" );
- }
- return ( NULL );
- }
- public function minifyCSS ( $css )
- {
- // Compress whitespace.
- $css = preg_replace ( '/\s+/', '', $css );
- // Remove comments.
- $css = preg_replace ( '/\/\*.*?\*\//', '', $css );
- return trim ( $css );
- }
- public function minifyJS ( $js )
- {
- require_once ( "jsmin.php" );
- return ( JSMin :: minify ( $js ) );
- }
- public function getPath ( $type )
- {
- switch ( $type )
- {
- case "css":
- $path = __CSSPATH__;
- break;
- case "js":
- $path = __JSPATH__;
- break;
- }
- return ( $path );
- }
- public function resourceRowDelete ( $ids )
- {
- if ( empty( $ids ) )
- {
- return ( FALSE );
- }
- $query = mysql_query ( "DELETE FROM resources_v2 WHERE id IN($ids)" );
- }
- public function resourceRowCreate ( $package, $type, $total )
- {
- $query = mysql_query ( "INSERT INTO resources_v2 (name, type, lmodified_total, built) VALUES ('$package', '$type', $total, NOW())" ) or die ( mysql_error ( ) );
- }
- // This function does not return true
- // but an array of data
- public function resourceRowExists ( $package, $type )
- {
- $query = mysql_query ( "SELECT id, lmodified_total FROM resources_v2 WHERE name = '$package' AND type = '$type' ORDER BY id DESC" ) or die ( mysql_error ( ) );
- if ( mysql_num_rows ( $query ) > 0 )
- {
- // The newest should be first
- $data = array ( );
- while ( $row = mysql_fetch_assoc ( $query ) )
- {
- $data [ ] = $row;
- }
- return ( $data );
- }
- else
- {
- return ( FALSE );
- }
- }
- public function isValidPackage ( $type, $package )
- {
- if ( isset ( $this -> packages [ $type ] [ $package ] ) && is_array ( $this -> packages [ $type ] [ $package ] ) )
- {
- return ( TRUE );
- }
- return ( FALSE );
- }
- }
- ?>
Add Comment
Please, Sign In to add comment