10 function ChameleonCSS($base, $perm, $temp) {
16 function update($file, $content = '') {
17 if (!is_writable($this->base
. $this->$file)) {
18 $this->error
= $this->$file . ' is not writeable, the file permissions are currently ' . $this->getfilepermissions($this->$file);
22 if (!$fp = fopen($this->base
. $this->$file, 'w')) {
23 $this->error
= 'couldn\'t open file';
26 fwrite($fp, stripslashes($content));
31 function getfilepermissions($file) {
32 return substr(sprintf('%o', fileperms($this->base
. $file)), -4);
36 $permcss = file_get_contents($this->base
. $this->perm
);
37 $tempcss = file_get_contents($this->base
. $this->temp
);
39 if ($permcss === false ||
$tempcss === false) {
40 $this->error
= 'Couldn\'t read file';
44 $permcss = trim($permcss);
45 $tempcss = trim($tempcss);
50 return $this->_merge($permcss, $tempcss);
56 function _merge($permcss, $tempcss) {
57 $csssrcs = array($this->_toobj($permcss), $this->_toobj($tempcss));
61 for ($i = 0; $i < 2; ++
$i) {
62 foreach ($csssrcs[$i] as $sel => $rule) {
64 if (!isset($merged[$sel])) {
65 $merged[$sel] = array();
68 foreach ($rule as $prop => $value) {
69 $merged[$sel][$prop] = $value;
71 if ($i > 0 && !$newsel) {
72 foreach ($merged[$sel] as $prop => $value) {
73 if (!isset($csssrcs[$i][$sel][$prop])) {
74 unset($merged[$sel][$prop]);
80 foreach ($merged as $sel => $value) {
81 if (!isset($csssrcs[$i][$sel])) {
88 return $this->_tostr($merged);
94 function _toobj($cssstr) {
96 $end = strpos($cssstr, '}');
97 while ($end !== false) {
98 $currule = substr($cssstr, 0, $end);
99 $parts = explode('{', $currule);
100 $selector = trim($parts[0]);
101 $rules = trim($parts[1]);
102 $rulesarr = explode(';', $rules);
104 $num = count($rulesarr);
105 for ($i = 0; $i < $num; ++
$i) {
106 if (strpos($rulesarr[$i], ':') === false) {
109 $rule = explode(':', $rulesarr[$i]);
110 $prop = trim($rule[0]);
111 $value = trim($rule[1]);
113 if (!isset($cssobj[$selector])) {
114 $cssobj[$selector] = array();
116 $cssobj[$selector][$prop] = $value;
118 $cssstr = substr($cssstr, $end +
1);
119 $end = strpos($cssstr, '}');
125 function _tostr($cssobj) {
127 foreach ($cssobj as $sel => $rule) {
128 $cssstr .= "$sel {\n";
129 foreach ($rule as $prop => $value) {
130 $cssstr .= " $prop: $value;\n";