-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcss.php
88 lines (74 loc) · 1.87 KB
/
css.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Easily compress all your CSS files into one streamlined file.
* This file strips your CSS of all unnecessary line breaks, spaces, and characters.
*
* @author Corey Worrell
* @url http://coreyworrell.com
* @version 1.1
*
* Usage:
* <link rel="stylesheet" type="text/css" href="css.php?reset,grid,styles" />
*
* Example folder structure:
* httpdocs
* - css
* - css.php
* - grid.css
* - reset.css
* - styles.css
* - js
* - img
* - index.html
*/
define('EXT', '.css');
$url = explode('?', $_SERVER['REQUEST_URI'], 2);
$cache = md5($url[1]).EXT;
$files = explode(',', $url[1]);
$cache_exists = file_exists($cache);
$cache_mtime = $cache_exists ? filemtime($cache) : NULL;
$compress = $cache_exists ? FALSE : TRUE;
foreach ($files as $key => $file)
{
$file = $files[$key] = $file.EXT;
if (strpos($file, '://') === FALSE)
{
if ( ! file_exists($file))
{
header('Content-type: text/html');
header('HTTP/1.1 400 Bad Request');
exit('File <code>'.$file.'</code> could not be found.');
}
if ( ! $compress AND filemtime($file) > $cache_mtime)
{
$compress = TRUE;
}
}
}
if ($compress)
{
$replace = array
(
'/\s+/' => ' ',
'/[\s+]?(;|:|{|}|,)[\s+]?/' => '$1',
'/[\t\r\n]/' => '',
'/\/\*(.*?)\*\//' => '',
'/;}/' => '}',
'/}(\s+)?/' => '}',
'/#([\da-f])\1([\da-f])\2([\da-f])\3/i' => '#$1$2$3',
'/([^\d])0(px|em|pt|ex|%|pc|cm|in|mm)/i' => '${1}0',
);
$css = '';
foreach ($files as $file)
{
$css .= file_get_contents($file);
}
$css = trim(preg_replace(array_keys($replace), array_values($replace), $css));
file_put_contents($cache, $css);
}
else
{
$css = file_get_contents($cache);
}
header('Content-type: text/css');
exit($css);