-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcss.php
More file actions
133 lines (113 loc) · 5.05 KB
/
Copy pathcss.php
File metadata and controls
133 lines (113 loc) · 5.05 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
<?php
class uCSS extends uBasicModule {
static function LinkToDocument($obj,$event,$templateDoc) {
$head = $templateDoc->getElementsByTagName('head')->item(0);
array_sort_subkey(self::$linkFiles,'order');
$beforeRef = $head->getElementsByTagName('link')->length ? $head->getElementsByTagName('link')->item(0) : null;
foreach (self::$linkFiles as $path) {
// already exists?
$exists = false;
foreach ($templateDoc->getElementsByTagName('link') as $l) {
if ($l->getAttribute('rel') == 'stylesheet' && $l->getAttribute('href') == $path['path']) { $exists = true; break; }
}
if ($exists) continue;
$node = $templateDoc->createElement('link');
$node->setAttribute('type','text/css'); $node->setAttribute('rel','stylesheet'); $node->setAttribute('href',$path['path']);
if ($beforeRef) $head->insertBefore($node,$beforeRef);
else $head->appendChild($node);
}
}
static function ProcessDomDocument($obj,$event,$doc) {
$styles = $doc->getElementsByTagName('style');
for ($i = 0; $i < $styles->length; $i++) { // now loop through all scripts, and ensure correct format
$style = $styles->item($i);
if (!$style->hasAttribute('type')) $style->setAttribute('type','text/css');
if (!$style->childNodes->length) continue;
// already commented cdata?
if ($style->childNodes->length == 2 && $style->childNodes->item(0)->nodeType == XML_TEXT_NODE && $style->childNodes->item(1)->nodeType == XML_CDATA_SECTION_NODE) continue;
// single child is not text or cdata
if ($style->childNodes->length != 1 || ($style->childNodes->item(0)->nodeType != XML_TEXT_NODE && $style->childNodes->item(0)->nodeType != XML_CDATA_SECTION_NODE)) continue;
$v = NULL;
try { // attempt to create a fragment from the value - this will check if the data is infact valid xml. if it is then find the cnode child and use it
$v = $style->childNodes->item(0)->nodeValue;
$frag = $doc->createDocumentFragment();
$frag->appendXML($v);
foreach ($frag->childNodes as $node) {
if ($node->nodeType === XML_CDATA_SECTION_NODE) {
$v = trim($node->nodeValue,' /'); break;
}
}
} catch (Exception $e) {} // if it fails, then does not contain cdata so continue as normal
if ($v === NULL) $v = $style->childNodes->item(0)->nodeValue;
$style->removeChild($style->childNodes->item(0));
$style->appendChild($doc->createTextNode("/*"));
$v = preg_replace('/^\s*\*\/\s*/','',$v);
$v = preg_replace('/\s*\/\*\s*$/','',$v);
$style->appendChild($doc->createCDATASection("*/\n" . trim($v) . "\n/*"));
$style->appendChild($doc->createTextNode("*/"));
}
}
public function GetOptions() { return PERSISTENT; }
public static $uuid = 'styles.css';
private static $includeFiles = array();
public static function IncludeFile($path) {
// if running ALERT: CANNOT BE CALLED AT RUN TIME
if (!file_exists($path)) $path = utopia::GetAbsolutePath($path);
if (!file_exists($path)) return;
self::$includeFiles[] = $path;
}
private static $linkFiles = array();
public static function LinkFile($path,$order=null) {
if ($order === null) $order = count(self::$linkFiles);
if (file_exists($path)) $path = utopia::GetRelativePath($path);
foreach (self::$linkFiles as $link) if ($link['path'] == $path) return;
self::$linkFiles[] = array('path'=>$path,'order'=>$order);
}
public static function Initialise() {
uEvents::AddCallback('ProcessDomDocument','uCSS::LinkToDocument');
uEvents::AddCallback('ProcessDomDocument','uCSS::ProcessDomDocument',null,MAX_ORDER);
self::LinkFile('/styles.css',-10);
self::LinkFile(dirname(__FILE__).'/jQuery/jquery-ui.min.css',-99);
self::IncludeFile(PATH_REL_CORE.'default.css');
self::IncludeFile(PATH_REL_ROOT.TEMPLATE_ADMIN.'/global.css');
module_Offline::IgnoreClass(__CLASS__);
}
public function SetupParents() {
$this->SetRewrite(true);
}
public function RunModule() {
utopia::CancelTemplate();
clearstatcache();
$uStr = '';
self::$includeFiles = array_unique(self::$includeFiles);
foreach (self::$includeFiles as $filename) {
//does it exist?
if (!file_exists($filename)) continue;
$uStr .= $filename.filemtime($filename).'-'.filesize($filename);
}
$identifiers = array($_SERVER['REQUEST_URI'],$uStr,count(self::$includeFiles),PATH_REL_CORE);
$etag = utopia::checksum($identifiers);
utopia::Cache_Check($etag,'text/css',$this->GetUUID());
$out = uCache::retrieve($identifiers);
if ($out) {
$out = file_get_contents($out);
} else {
$out = self::BuildCSS(true);
uCache::store($identifiers,$out);
}
utopia::Cache_Output($out,$etag,'text/css',$this->GetUUID());
}
static function BuildCSS($minify=true) {
$body = '';
foreach (self::$includeFiles as $filename) {
if (!file_exists($filename)) continue;
$contents = file_get_contents($filename);
// convert relative url paths into absolute ones.
$subDir = utopia::GetRelativePath(dirname($filename));
$contents = preg_replace('/url\("?\'?([^"\']+)"?\'?\)/Ui','url(' . $subDir . '/$1)',$contents);
$body .= $contents."\n\n";
}
if ($minify) $body = cssMin::minify($body);
return $body;
}
}