forked from BeehiveForum/BeehiveForum
/
bh_check_styles.php
218 lines (143 loc) · 6.12 KB
/
bh_check_styles.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*======================================================================
Copyright Project BeehiveForum 2002
This file is part of BeehiveForum.
BeehiveForum is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
BeehiveForum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Beehive; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
======================================================================*/
date_default_timezone_set('UTC');
define("BH_INCLUDE_PATH", "./forum/include/");
define("BEEHIVEMODE_LIGHT", true);
require_once BH_INCLUDE_PATH. 'errorhandler.inc.php';
require_once BH_INCLUDE_PATH. 'format.inc.php';
$exclude_files_array = array('start_main.css', 'style_ie6.css', 'gallery.css');
$exclude_dirs_array = array('forum/styles/default');
function get_file_list(&$file_list_array, $path, $extension)
{
$extension_preg = preg_quote($extension, '/');
if (!is_array($file_list_array)) $file_list_array = array();
if (($dir_handle = opendir($path))) {
while (($file_name = readdir($dir_handle)) !== false) {
if ($file_name != "." && $file_name != "..") {
if (@is_dir("$path/$file_name") && !in_array("$path/$file_name", $GLOBALS['exclude_dirs_array'])) {
get_file_list($file_list_array, "$path/$file_name", $extension);
} else if ((preg_match("/$extension_preg$/iu", $file_name) > 0) && !in_array($file_name, $GLOBALS['exclude_files_array'])) {
$file_list_array[] = "$path/$file_name";
}
}
}
}
}
function parse_css_to_array($css_file_contents)
{
$css_rules_array = array();
preg_match_all('/([^}]+){([^}]+)}/im', $css_file_contents, $rule_matches_array, PREG_SET_ORDER);
foreach ($rule_matches_array as $rule_match) {
$selector = preg_replace('/ +/', ' ', preg_replace("/\r|\r\n|\n/", " ", trim($rule_match[1])));
$css_rules_array[$selector] = array();
$attributes_array = array_filter(array_map('trim', explode(';', $rule_match[2])), 'strlen');
foreach ($attributes_array as $attribute_line) {
list($attribute, $value) = explode(':', $attribute_line);
$css_rules_array[$selector][trim($attribute)] = trim($value);
}
}
return $css_rules_array;
}
function selector_sort($a, $b)
{
if ($a == 'html' && $b == 'body') return -1;
if ($a == 'body' && $b == 'html') return 1;
if ($a == 'html') return -1;
if ($b == 'html') return 1;
if ($a == 'body') return -1;
if ($b == 'body') return 1;
if (substr($a, 0, 1) == '.' && substr($b, 0, 1) != '.') return 1;
if (substr($a, 0, 1) != '.' && substr($b, 0, 1) == '.') return -1;
return strcmp($a, $b);
}
function sort_array_by_array($array, $sort_by)
{
$common_keys = array_intersect_key(array_flip($sort_by), $array);
$common_key_values = array_intersect_key($array, $common_keys);
return array_merge($common_keys, $common_key_values);
}
function parse_array_to_css($css_rules_array)
{
$css_file_contents = '';
foreach ($css_rules_array as $selector => $rules_set) {
ksort($rules_set);
$selector = implode(",\n", array_map('trim', explode(',', $selector)));
$css_file_contents.= sprintf(
"%s {\n %s;\n}\n\n",
$selector,
implode_assoc($rules_set, ': ', ";\n ")
);
}
return trim($css_file_contents);
}
function array_diff_key_recursive($array1, $array2)
{
foreach ($array1 as $key => $value) {
if (is_array($value) && array_key_exists($key, $array2)) {
$result[$key] = array_diff_key_recursive($array1[$key], $array2[$key]);
} else if (is_array($value)) {
$result[$key] = $array1[$key];
} else {
$result = array_diff_key($array1, $array2);
}
if (is_array($result) && isset($result[$key]) && is_array($result[$key]) && count($result[$key]) == 0) {
unset($result[$key]);
}
}
return $result;
}
set_time_limit(0);
header('Content-Type: text/plain');
$css_rules_array = array();
get_file_list($file_list, 'forum/styles', 'style.css');
get_file_list($file_list, 'forum/styles', 'mobile.css');
foreach ($file_list as $css_filepath) {
$css_rules_array[$css_filepath] = parse_css_to_array(file_get_contents($css_filepath));
}
foreach ($css_rules_array as $css_filepath => $css_rules_set) {
$default_css_filepath = sprintf(
'forum/styles/default/%s',
basename($css_filepath)
);
$default_css_rules = parse_css_to_array(file_get_contents($default_css_filepath));
file_put_contents($default_css_filepath, parse_array_to_css($default_css_rules));
$default_css_rules = parse_css_to_array(file_get_contents($default_css_filepath));
foreach ($default_css_rules as $selector => $default_rules_set) {
if (!isset($css_rules_set[$selector])) {
$css_rules_set[$selector] = $default_rules_set;
} else {
foreach ($default_rules_set as $rule_name => $value) {
if (preg_match('/(#[0-9A-F]{3,6}|rgba?)/i', $value) > 0) {
continue;
}
if (preg_match('/color/i', $rule_name) > 0) {
continue;
}
$css_rules_set[$selector][$rule_name] = $value;
}
}
}
foreach ($css_rules_set as $selector => $css_rules) {
if (!isset($default_css_rules[$selector])) {
unset($css_rules_set[$selector]);
}
}
$css_rules_set = sort_array_by_array($css_rules_set, array_keys($default_css_rules));
file_put_contents($css_filepath, parse_array_to_css($css_rules_set));
}
?>