Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Uporabnik doda JS in CSS datoteke, razred jih združi po tipu datoteke in morebitni navedeni sekciji ter ustvari cache.
- *
- * Opozorilo (potencialni exploit): datoteke dodajaj ročno oziroma brez user inputa
- *
- * Drugače je razred dokaj optimiziran in varen.
- */
- // Optimize::addFile("css", array("css/default.css")); // doda 1 css datoteko
- // Optimize::addFile("js", array("js/jquery/v1.7.2.js", "druga/js/datoteka.js));
- // Optimize::addFile("js", array("js/datoteka/za/specificno/podstran.js), "specificna");
- // Optimize::getHTML(); // vrne končni html
- define("WWW_PATH" . "/srv/path/webpage.com/"); // odkomentiraj in nastavi (obvezno končna poševnica)
- class OptimizeCssJs {
- private static $files = array();
- public static $types = array();
- private static $content = array();
- /*
- * Konfiguracija tipov datotek.
- */
- public static function setTypes() {
- self::$types = array(
- "css" => array(
- "header" => "text/css",
- "ext" => "css",
- "html" => '<link rel="stylesheet" type="text/css" href="##LINK##" />'
- ),
- "js" => array(
- "header" => "text/javascript",
- "ext" => "js",
- "html" => '<script type="text/javascript" src="##LINK##"></script>'
- )
- );
- }
- /*
- * Metoda za dodajanje datotek.
- *
- * $type - eden izmed zgoraj navedenih tipov datotek (prvi index)
- * $files - ena (string) ali več datotek (enodimenzionalen array)
- * $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)
- */
- public static function addFile($type, $files, $section = "main") {
- // type must be set
- if (!isset(self::$types[$type]) || empty($files))
- return FALSE;
- // if files are not in array, we make array
- if (!is_array($files))
- $files = array($files);
- foreach ($files AS $file)
- if (!isset(self::$files[$type][$section]) || !in_array($file, self::$files[$type][$section]))
- self::$files[$type][$section][] = $file;
- }
- /*
- * Izvrši php datoteko (oziroma prikaže source druge) in vrne rezultat.
- *
- * $file - php datoteka, ki mora biti izvršena (kakšni dinamični css stili / js datoteke)
- * $args - morebitno posredovanje argumentov
- */
- private static function runPHPFile($file, $args) {
- ob_start();
- include $file;
- return ob_get_clean();
- }
- /*
- * Generira končni HTML.
- */
- public static function getHTML() {
- $html = array();
- foreach (self::$types AS $type => $conf) {
- $dir = "cache/" . $type . "/";
- // create cache dir
- if (!is_dir(WWW_PATH . $dir))
- mkdir(WWW_PATH . $dir, 777, TRUE);
- // foreach filetype as section
- if (isset(self::$files[$type]))
- foreach (self::$files[$type] AS $section) {
- $hash = sha1(implode($section));
- $filename = $hash . "." . self::$types[$type]['ext'];
- // if file doesn't exist, we create it
- if (!is_file(WWW_PATH . $dir . $filename) || self::isOldCache($section, WWW_PATH . $dir . $filename)) {
- @unlink(WWW_PATH . $dir . $filename);
- $fileContent = NULL;
- // foreach section as file
- foreach ($section AS $key => $file)
- if (end(explode(".", $file)) == $conf['ext']
- && $content = file_get_contents(WWW_PATH . $file)) {
- $fileContent .= $content;
- } else if (end(explode(".", $file)) == "php"
- || end(explode(".", $key)) == "php") {
- if (is_file(WWW_PATH . $file)) {
- $fileContent .= self::runPHPFile(WWW_PATH . $file, NULL);
- } else if (is_file(WWW_PATH . $key)) {
- $fileContent .= self::runPHPFile(WWW_PATH . $key, $file);
- }
- }
- // save file
- file_put_contents(WWW_PATH . $dir . $filename, $fileContent);
- }
- $html[] = str_replace("##LINK##", "/" . $dir . $filename, self::$types[$type]['html']);
- }
- }
- return implode("\n", $html);
- }
- /*
- * Preveri starost cachea
- *
- * $section - ime sekcije
- * $filename - ime datoteke
- */
- public static function isOldCache($section, $filename) {
- if (is_file($filename) && is_readable($filename))
- foreach ($section AS $key => $file)
- if (is_file(WWW_PATH . $file) && is_readable(WWW_PATH . $file))
- if (filemtime(WWW_PATH . $file) > filemtime($filename))
- return TRUE;
- else
- return TRUE;
- return FALSE;
- }
- }
- OptimizeJsCss::setTypes();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment