cache_styles.php
5.63 KB
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
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: cache_styles.php 34182 2013-10-29 08:48:22Z nemohou $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
function build_cache_styles() {
global $_G;
$stylevars = $styledata = array();
$defaultstyleid = $_G['setting']['styleid'];
foreach(C::t('common_stylevar')->range() as $var) {
$stylevars[$var['styleid']][$var['variable']] = $var['substitute'];
}
foreach(C::t('common_style')->fetch_all_data(true) as $data) {
$data['tpldir'] = $data['directory'];
$data = array_merge($data, (array)$stylevars[$data['styleid']]);
$datanew = array();
$data['imgdir'] = $data['imgdir'] ? $data['imgdir'] : STATICURL.'image/common';
$data['styleimgdir'] = $data['styleimgdir'] ? $data['styleimgdir'] : $data['imgdir'];
foreach($data as $k => $v) {
if(substr($k, -7, 7) == 'bgcolor') {
$newkey = substr($k, 0, -7).'bgcode';
$datanew[$newkey] = setcssbackground($data, $k);
}
}
$data = array_merge($data, $datanew);
if(strstr($data['boardimg'], ',')) {
$flash = explode(",", $data['boardimg']);
$flash[0] = trim($flash[0]);
$flash[0] = preg_match('/^http:\/\//i', $flash[0]) ? $flash[0] : $data['styleimgdir'].'/'.$flash[0];
$data['boardlogo'] = "<embed src=\"".$flash[0]."\" width=\"".trim($flash[1])."\" height=\"".trim($flash[2])."\" type=\"application/x-shockwave-flash\" wmode=\"transparent\"></embed>";
} else {
$data['boardimg'] = preg_match('/^http:\/\//i', $data['boardimg']) ? $data['boardimg'] : $data['styleimgdir'].'/'.$data['boardimg'];
$data['boardlogo'] = "<img src=\"$data[boardimg]\" alt=\"".$_G['setting']['bbname']."\" border=\"0\" />";
}
$data['bold'] = $data['nobold'] ? 'normal' : 'bold';
$contentwidthint = intval($data['contentwidth']);
$contentwidthint = $contentwidthint ? $contentwidthint : 600;
if($data['extstyle']) {
list($data['extstyle'], $data['defaultextstyle']) = explode('|', $data['extstyle']);
$extstyle = explode("\t", $data['extstyle']);
$data['extstyle'] = array();
foreach($extstyle as $dir) {
if(file_exists($extstylefile = DISCUZ_ROOT.$data['tpldir'].'/style/'.$dir.'/style.css')) {
if($data['defaultextstyle'] == $dir) {
$data['defaultextstyle'] = $data['tpldir'].'/style/'.$dir;
}
$content = file_get_contents($extstylefile);
if(preg_match('/\[name\](.+?)\[\/name\]/i', $content, $r1) && preg_match('/\[iconbgcolor](.+?)\[\/iconbgcolor]/i', $content, $r2)) {
$data['extstyle'][] = array($data['tpldir'].'/style/'.$dir, $r1[1], $r2[1]);
}
}
}
}
$data['verhash'] = random(3);
$styledata[] = $data;
}
foreach($styledata as $data) {
savecache('style_'.$data['styleid'], $data);
if($defaultstyleid == $data['styleid']) {
savecache('style_default', $data);
}
writetocsscache($data);
}
}
function setcssbackground(&$data, $code) {
$codes = explode(' ', $data[$code]);
$css = $codevalue = '';
for($i = 0; $i < count($codes); $i++) {
if($i < 2) {
if($codes[$i] != '') {
if($codes[$i]{0} == '#') {
$css .= strtoupper($codes[$i]).' ';
$codevalue = strtoupper($codes[$i]);
} elseif(preg_match('/^http:\/\//i', $codes[$i])) {
$css .= 'url("'.$codes[$i].'") ';
} else {
$css .= 'url("'.$data['styleimgdir'].'/'.$codes[$i].'") ';
}
}
} else {
$css .= $codes[$i].' ';
}
}
$data[$code] = $codevalue;
$css = trim($css);
return $css ? 'background: '.$css : '';
}
function writetocsscache($data) {
global $_G;
$dir = DISCUZ_ROOT.'./template/default/common/';
$dh = opendir($dir);
$data['staticurl'] = STATICURL;
while(($entry = readdir($dh)) !== false) {
if(fileext($entry) == 'css') {
$cssfile = DISCUZ_ROOT.'./'.$data['tpldir'].'/common/'.$entry;
!file_exists($cssfile) && $cssfile = $dir.$entry;
$cssdata = @implode('', file($cssfile));
if(file_exists($cssfile = DISCUZ_ROOT.'./'.$data['tpldir'].'/common/extend_'.$entry)) {
$cssdata .= @implode('', file($cssfile));
}
if(is_array($_G['setting']['plugins']['available']) && $_G['setting']['plugins']['available']) {
foreach($_G['setting']['plugins']['available'] as $plugin) {
if(file_exists($cssfile = DISCUZ_ROOT.'./source/plugin/'.$plugin.'/template/extend_'.$entry)) {
$cssdata .= @implode('', file($cssfile));
}
}
}
$cssdata = preg_replace("/\{([A-Z0-9]+)\}/e", '\$data[strtolower(\'\1\')]', $cssdata);
$cssdata = preg_replace("/<\?.+?\?>\s*/", '', $cssdata);
$cssdata = !preg_match('/^http:\/\//i', $data['styleimgdir']) ? preg_replace("/url\(([\"'])?".preg_quote($data['styleimgdir'], '/')."/i", "url(\\1$_G[siteurl]$data[styleimgdir]", $cssdata) : $cssdata;
$cssdata = !preg_match('/^http:\/\//i', $data['imgdir']) ? preg_replace("/url\(([\"'])?".preg_quote($data['imgdir'], '/')."/i", "url(\\1$_G[siteurl]$data[imgdir]", $cssdata) : $cssdata;
$cssdata = !preg_match('/^http:\/\//i', $data['staticurl']) ? preg_replace("/url\(([\"'])?".preg_quote($data['staticurl'], '/')."/i", "url(\\1$_G[siteurl]$data[staticurl]", $cssdata) : $cssdata;
if($entry == 'module.css') {
$cssdata = preg_replace('/\/\*\*\s*(.+?)\s*\*\*\//', '[\\1]', $cssdata);
}
$cssdata = preg_replace(array('/\s*([,;:\{\}])\s*/', '/[\t\n\r]/', '/\/\*.+?\*\//'), array('\\1', '',''), $cssdata);
if(@$fp = fopen(DISCUZ_ROOT.'./data/cache/style_'.$data['styleid'].'_'.$entry, 'w')) {
fwrite($fp, $cssdata);
fclose($fp);
} else {
exit('Can not write to cache files, please check directory ./data/ and ./data/cache/ .');
}
}
}
}
?>