Guest User

CSS and JS Optimize class

a guest
May 11th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.46 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Uporabnik doda JS in CSS datoteke, razred jih združi po tipu datoteke in morebitni navedeni sekciji ter ustvari cache.
  5.  *
  6.  * Opozorilo (potencialni exploit): datoteke dodajaj ročno oziroma brez user inputa
  7.  *
  8.  * Drugače je razred dokaj optimiziran in varen.
  9.  */
  10.  
  11. // Optimize::addFile("css", array("css/default.css")); // doda 1 css datoteko
  12. // Optimize::addFile("js", array("js/jquery/v1.7.2.js", "druga/js/datoteka.js));
  13. // Optimize::addFile("js", array("js/datoteka/za/specificno/podstran.js), "specificna");
  14. // Optimize::getHTML(); // vrne končni html
  15.  
  16. define("WWW_PATH" . "/srv/path/webpage.com/"); // odkomentiraj in nastavi (obvezno končna poševnica)
  17.  
  18. class OptimizeCssJs {
  19.     private static $files = array();
  20.     public static $types = array();
  21.     private static $content = array();
  22.    
  23.     /*
  24.      * Konfiguracija tipov datotek.
  25.      */
  26.     public static function setTypes() {
  27.         self::$types = array(
  28.             "css" => array(
  29.                 "header" => "text/css",
  30.                 "ext" => "css",
  31.                 "html" => '<link rel="stylesheet" type="text/css" href="##LINK##" />'
  32.             ),
  33.             "js" => array(
  34.                 "header" => "text/javascript",
  35.                 "ext" => "js",
  36.                 "html" => '<script type="text/javascript" src="##LINK##"></script>'
  37.             )
  38.         );
  39.     }
  40.    
  41.     /*
  42.      * Metoda za dodajanje datotek.
  43.      *
  44.      * $type - eden izmed zgoraj navedenih tipov datotek (prvi index)
  45.      * $files - ena (string)  ali več datotek (enodimenzionalen array)
  46.      * $section - privzeto main, lahko se doda dodatna sekcija. Za vsako sekcijo se ustvari svoja cache datoteka (main za datoteke, ki se vedno postavijo na strani in poljubno število drugih za v večini nepomembne datoteke)
  47.      */
  48.     public static function addFile($type, $files, $section = "main") {
  49.         // type must be set
  50.         if (!isset(self::$types[$type]) || empty($files))
  51.             return FALSE;
  52.        
  53.         // if files are not in array, we make array
  54.         if (!is_array($files))
  55.             $files = array($files);
  56.            
  57.         foreach ($files AS $file)
  58.             if (!isset(self::$files[$type][$section]) || !in_array($file, self::$files[$type][$section]))
  59.                 self::$files[$type][$section][] = $file;
  60.     }
  61.    
  62.     /*
  63.      * Izvrši php datoteko (oziroma prikaže source druge) in vrne rezultat.
  64.      *
  65.      * $file - php datoteka, ki mora biti izvršena (kakšni dinamični css stili / js datoteke)
  66.      * $args - morebitno posredovanje argumentov
  67.      */
  68.     private static function runPHPFile($file, $args) {
  69.         ob_start();
  70.         include $file;
  71.         return ob_get_clean();
  72.     }
  73.    
  74.     /*
  75.      * Generira končni HTML.
  76.      */
  77.     public static function getHTML() {
  78.         $html = array();
  79.        
  80.         foreach (self::$types AS $type => $conf) {
  81.             $dir = "cache/" . $type . "/";
  82.                
  83.             // create cache dir
  84.             if (!is_dir(WWW_PATH . $dir))
  85.                 mkdir(WWW_PATH . $dir, 777, TRUE);
  86.                
  87.             // foreach filetype as section
  88.             if (isset(self::$files[$type]))
  89.             foreach (self::$files[$type] AS $section) {
  90.                 $hash = sha1(implode($section));
  91.                 $filename = $hash . "." . self::$types[$type]['ext'];
  92.                
  93.                 // if file doesn't exist, we create it
  94.                 if (!is_file(WWW_PATH . $dir . $filename) || self::isOldCache($section, WWW_PATH . $dir . $filename)) {
  95.                     @unlink(WWW_PATH . $dir . $filename);
  96.                    
  97.                     $fileContent = NULL;
  98.                     // foreach section as file
  99.                     foreach ($section AS $key => $file)
  100.                         if (end(explode(".", $file)) == $conf['ext']
  101.                             && $content = file_get_contents(WWW_PATH . $file)) {
  102.                             $fileContent .= $content;
  103.                         } else if (end(explode(".", $file)) == "php"
  104.                             || end(explode(".", $key)) == "php") {
  105.                                
  106.                             if (is_file(WWW_PATH . $file)) {
  107.                                 $fileContent .= self::runPHPFile(WWW_PATH . $file, NULL);
  108.                             } else if (is_file(WWW_PATH . $key)) {
  109.                                 $fileContent .= self::runPHPFile(WWW_PATH . $key, $file);
  110.                             }
  111.                         }
  112.                    
  113.                     // save file
  114.                     file_put_contents(WWW_PATH . $dir . $filename, $fileContent);
  115.                 }
  116.                
  117.                 $html[] = str_replace("##LINK##", "/" . $dir . $filename, self::$types[$type]['html']);
  118.             }
  119.         }
  120.        
  121.         return implode("\n", $html);
  122.     }
  123.    
  124.     /*
  125.      * Preveri starost cachea
  126.      *
  127.      * $section - ime sekcije
  128.      * $filename - ime datoteke
  129.      */
  130.     public static function isOldCache($section, $filename) {
  131.         if (is_file($filename) && is_readable($filename))
  132.             foreach ($section AS $key => $file)
  133.                 if (is_file(WWW_PATH . $file) && is_readable(WWW_PATH . $file))
  134.                     if (filemtime(WWW_PATH . $file) > filemtime($filename))
  135.                         return TRUE;
  136.         else
  137.             return TRUE;
  138.        
  139.         return FALSE;
  140.     }
  141. }
  142.  
  143. OptimizeJsCss::setTypes();
  144.  
  145. ?>
Advertisement
Add Comment
Please, Sign In to add comment