View difference between Paste ID: iv2m7h48 and Xr45FbwB
SHOW: | | - or go back to the newest paste.
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
 * http://schtr4jh.net
11
 */
12
13
// Optimize::addFile("css", array("css/default.css")); // doda 1 css datoteko
14
// Optimize::addFile("js", array("js/jquery/v1.7.2.js", "druga/js/datoteka.js));
15
// Optimize::addFile("js", array("js/datoteka/za/specificno/podstran.js), "specificna");
16
// Optimize::getHTML(); // vrne končni html
17
18
define("WWW_PATH" . "/srv/path/webpage.com/"); // odkomentiraj in nastavi (obvezno končna poševnica)
19
20
class OptimizeCssJs {
21
	private static $files = array();
22
	public static $types = array();
23
	private static $content = array();
24
	
25
	/*
26
	 * Konfiguracija tipov datotek.
27
	 */
28
	public static function setTypes() {
29
		self::$types = array(
30
			"css" => array(
31
				"header" => "text/css",
32
				"ext" => "css",
33
				"html" => '<link rel="stylesheet" type="text/css" href="##LINK##" />'
34
			),
35
			"js" => array(
36
				"header" => "text/javascript",
37
				"ext" => "js",
38
				"html" => '<script type="text/javascript" src="##LINK##"></script>'
39
			)
40
		);
41
	}
42
	
43
	/*
44
	 * Metoda za dodajanje datotek.
45
	 * 
46
	 * $type - eden izmed zgoraj navedenih tipov datotek (prvi index)
47
	 * $files - ena (string)  ali več datotek (enodimenzionalen array)
48
	 * $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)
49
	 */
50
	public static function addFile($type, $files, $section = "main") {
51
		// type must be set
52
		if (!isset(self::$types[$type]) || empty($files))
53
			return FALSE;
54
		
55
		// if files are not in array, we make array
56
		if (!is_array($files))
57
			$files = array($files);
58
			
59
		foreach ($files AS $file)
60
			if (!isset(self::$files[$type][$section]) || !in_array($file, self::$files[$type][$section]))
61
				self::$files[$type][$section][] = $file;
62
	}
63
	
64
	/*
65
	 * Izvrši php datoteko (oziroma prikaže source druge) in vrne rezultat.
66
	 * 
67
	 * $file - php datoteka, ki mora biti izvršena (kakšni dinamični css stili / js datoteke)
68
	 * $args - morebitno posredovanje argumentov
69
	 */
70
	private static function runPHPFile($file, $args) {
71
        ob_start();
72
        include $file;
73
        return ob_get_clean();
74
	}
75
	
76
	/*
77
	 * Generira končni HTML.
78
	 */
79
	public static function getHTML() {
80
		$html = array();
81
		
82
		foreach (self::$types AS $type => $conf) {
83
			$dir = "cache/" . $type . "/";
84
				
85
			// create cache dir
86
			if (!is_dir(WWW_PATH . $dir))
87
				mkdir(WWW_PATH . $dir, 777, TRUE);
88
				
89
			// foreach filetype as section
90
			if (isset(self::$files[$type]))
91
			foreach (self::$files[$type] AS $section) {
92
				$hash = sha1(implode($section));
93
				$filename = $hash . "." . self::$types[$type]['ext'];
94
				
95
				// if file doesn't exist, we create it
96
				if (!is_file(WWW_PATH . $dir . $filename) || self::isOldCache($section, WWW_PATH . $dir . $filename)) {
97
					@unlink(WWW_PATH . $dir . $filename);
98
					
99
					$fileContent = NULL;
100
					// foreach section as file
101
					foreach ($section AS $key => $file)
102
						if (end(explode(".", $file)) == $conf['ext']
103
							&& $content = file_get_contents(WWW_PATH . $file)) {
104
							$fileContent .= $content;
105
						} else if (end(explode(".", $file)) == "php"
106
							|| end(explode(".", $key)) == "php") {
107
								
108
							if (is_file(WWW_PATH . $file)) {
109
						    	$fileContent .= self::runPHPFile(WWW_PATH . $file, NULL);
110
						    } else if (is_file(WWW_PATH . $key)) {
111
								$fileContent .= self::runPHPFile(WWW_PATH . $key, $file);
112
						    }
113
						}
114
					
115
					// save file
116
					file_put_contents(WWW_PATH . $dir . $filename, $fileContent);
117
				}
118
				
119
				$html[] = str_replace("##LINK##", "/" . $dir . $filename, self::$types[$type]['html']);
120
			}
121
		}
122
		
123
		return implode("\n", $html);
124
	}
125
	
126
	/*
127
	 * Preveri starost cachea
128
	 * 
129
	 * $section - ime sekcije
130
	 * $filename - ime datoteke
131
	 */
132
	public static function isOldCache($section, $filename) {
133
		if (is_file($filename) && is_readable($filename))
134
			foreach ($section AS $key => $file)
135
				if (is_file(WWW_PATH . $file) && is_readable(WWW_PATH . $file))
136
					if (filemtime(WWW_PATH . $file) > filemtime($filename))
137
						return TRUE;
138
		else
139
			return TRUE;
140
		
141
		return FALSE;
142
	}
143
}
144
145
OptimizeJsCss::setTypes();
146
147
?>