|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00017 class cssparser { 00018 private $css; 00019 private $html; 00020 00021 function cssparser($html = true) { 00022 // Register "destructor" 00023 register_shutdown_function(array(&$this, "finalize")); 00024 $this->html = ($html != false); 00025 $this->Clear(); 00026 } 00027 00028 function finalize() { 00029 unset($this->css); 00030 } 00031 00032 function Clear() { 00033 unset($this->css); 00034 $this->css = array(); 00035 if($this->html) { 00036 $this->Add("ADDRESS", ""); 00037 $this->Add("APPLET", ""); 00038 $this->Add("AREA", ""); 00039 $this->Add("A", "text-decoration : underline; color : Blue;"); 00040 $this->Add("A:visited", "color : Purple;"); 00041 $this->Add("BASE", ""); 00042 $this->Add("BASEFONT", ""); 00043 $this->Add("BIG", ""); 00044 $this->Add("BLOCKQUOTE", ""); 00045 $this->Add("BODY", ""); 00046 $this->Add("BR", ""); 00047 $this->Add("B", "font-weight: bold;"); 00048 $this->Add("CAPTION", ""); 00049 $this->Add("CENTER", ""); 00050 $this->Add("CITE", ""); 00051 $this->Add("CODE", ""); 00052 $this->Add("DD", ""); 00053 $this->Add("DFN", ""); 00054 $this->Add("DIR", ""); 00055 $this->Add("DIV", ""); 00056 $this->Add("DL", ""); 00057 $this->Add("DT", ""); 00058 $this->Add("EM", ""); 00059 $this->Add("FONT", ""); 00060 $this->Add("FORM", ""); 00061 $this->Add("H1", ""); 00062 $this->Add("H2", ""); 00063 $this->Add("H3", ""); 00064 $this->Add("H4", ""); 00065 $this->Add("H5", ""); 00066 $this->Add("H6", ""); 00067 $this->Add("HEAD", ""); 00068 $this->Add("HR", ""); 00069 $this->Add("HTML", ""); 00070 $this->Add("IMG", ""); 00071 $this->Add("INPUT", ""); 00072 $this->Add("ISINDEX", ""); 00073 $this->Add("I", "font-style: italic;"); 00074 $this->Add("KBD", ""); 00075 $this->Add("LINK", ""); 00076 $this->Add("LI", ""); 00077 $this->Add("MAP", ""); 00078 $this->Add("MENU", ""); 00079 $this->Add("META", ""); 00080 $this->Add("OL", ""); 00081 $this->Add("OPTION", ""); 00082 $this->Add("PARAM", ""); 00083 $this->Add("PRE", ""); 00084 $this->Add("P", ""); 00085 $this->Add("SAMP", ""); 00086 $this->Add("SCRIPT", ""); 00087 $this->Add("SELECT", ""); 00088 $this->Add("SMALL", ""); 00089 $this->Add("STRIKE", ""); 00090 $this->Add("STRONG", ""); 00091 $this->Add("STYLE", ""); 00092 $this->Add("SUB", ""); 00093 $this->Add("SUP", ""); 00094 $this->Add("TABLE", ""); 00095 $this->Add("TD", ""); 00096 $this->Add("TEXTAREA", ""); 00097 $this->Add("TH", ""); 00098 $this->Add("TITLE", ""); 00099 $this->Add("TR", ""); 00100 $this->Add("TT", ""); 00101 $this->Add("UL", ""); 00102 $this->Add("U", "text-decoration : underline;"); 00103 $this->Add("VAR", ""); 00104 } 00105 } 00106 00107 function SetHTML($html) { 00108 $this->html = ($html != false); 00109 } 00110 00111 function Add($key, $codestr) { 00112 $key = strtolower($key); 00113 $codestr = strtolower($codestr); 00114 if(!isset($this->css[$key])) { 00115 $this->css[$key] = array(); 00116 } 00117 $codes = explode(";",$codestr); 00118 if(count($codes) > 0) { 00119 $codekey=''; $codevalue=''; 00120 foreach($codes as $code) { 00121 $code = trim($code); 00122 $this->assignValues(explode(":",$code),$codekey,$codevalue); 00123 if(strlen($codekey) > 0) { 00124 $this->css[$key][trim($codekey)] = trim($codevalue); 00125 } 00126 } 00127 } 00128 } 00129 00130 private function assignValues($arr,&$val1,&$val2) { 00131 $n = count($arr); 00132 if ($n > 0) { 00133 $val1=$arr[0]; 00134 $val2=($n > 1) ? $arr[1] : ''; 00135 } 00136 } 00137 function Get($key, $property) { 00138 $key = strtolower($key); 00139 $property = strtolower($property); 00140 $tag='';$subtag='';$class='';$id=''; 00141 $this->assignValues(explode(":",$key),$tag,$subtag); 00142 $this->assignValues(explode(".",$tag),$tag,$class); 00143 $this->assignValues(explode("#",$tag),$tag,$id); 00144 $result = ""; 00145 $_subtag=''; $_class=''; $_id=''; 00146 foreach($this->css as $_tag => $value) { 00147 $this->assignValues(explode(":",$_tag),$_tag,$_subtag); 00148 $this->assignValues(explode(".",$_tag),$_tag,$_class); 00149 $this->assignValues(explode("#",$_tag),$_tag,$_id); 00150 00151 $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0); 00152 $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0); 00153 $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0); 00154 $idmatch = (strcmp($id, $_id) == 0); 00155 00156 if($tagmatch & $subtagmatch & $classmatch & $idmatch) { 00157 $temp = $_tag; 00158 if((strlen($temp) > 0) & (strlen($_class) > 0)) { 00159 $temp .= ".".$_class; 00160 } elseif(strlen($temp) == 0) { 00161 $temp = ".".$_class; 00162 } 00163 if((strlen($temp) > 0) & (strlen($_subtag) > 0)) { 00164 $temp .= ":".$_subtag; 00165 } elseif(strlen($temp) == 0) { 00166 $temp = ":".$_subtag; 00167 } 00168 if(isset($this->css[$temp][$property])) { 00169 $result = $this->css[$temp][$property]; 00170 } 00171 } 00172 } 00173 return $result; 00174 } 00175 00176 function GetSection($key) { 00177 $key = strtolower($key); 00178 $tag='';$subtag='';$class='';$id=''; 00179 $_subtag=''; $_class=''; $_id=''; 00180 00181 $this->assignValues(explode(":",$key),$tag,$subtag); 00182 $this->assignValues(explode(".",$tag),$tag,$class); 00183 $this->assignValues(explode("#",$tag),$tag,$id); 00184 $result = array(); 00185 foreach($this->css as $_tag => $value) { 00186 $this->assignValues(explode(":",$_tag),$_tag,$_subtag); 00187 $this->assignValues(explode(".",$_tag),$_tag,$_class); 00188 $this->assignValues(explode("#",$_tag),$_tag,$_id); 00189 00190 $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0); 00191 $subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0); 00192 $classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0); 00193 $idmatch = (strcmp($id, $_id) == 0); 00194 00195 if($tagmatch & $subtagmatch & $classmatch & $idmatch) { 00196 $temp = $_tag; 00197 if((strlen($temp) > 0) & (strlen($_class) > 0)) { 00198 $temp .= ".".$_class; 00199 } elseif(strlen($temp) == 0) { 00200 $temp = ".".$_class; 00201 } 00202 if((strlen($temp) > 0) & (strlen($_subtag) > 0)) { 00203 $temp .= ":".$_subtag; 00204 } elseif(strlen($temp) == 0) { 00205 $temp = ":".$_subtag; 00206 } 00207 foreach($this->css[$temp] as $property => $value) { 00208 $result[$property] = $value; 00209 } 00210 } 00211 } 00212 return $result; 00213 } 00214 00215 function ParseStr($str) { 00216 $this->Clear(); 00217 // Remove comments 00218 $str = preg_replace("/\/\*(.*)?\*\//Usi", "", $str); 00219 // Parse this damn csscode 00220 $parts = explode("}",$str); 00221 if(count($parts) > 0) { 00222 foreach($parts as $part) { 00223 $keystr='';$codestr=''; 00224 $this->assignValues(explode("{",$part),$keystr,$codestr); 00225 $keys = explode(",",trim($keystr)); 00226 if(count($keys) > 0) { 00227 foreach($keys as $key) { 00228 if(strlen($key) > 0) { 00229 $key = str_replace("\n", "", $key); 00230 $key = str_replace("\\", "", $key); 00231 $this->Add($key, trim($codestr)); 00232 } 00233 } 00234 } 00235 } 00236 } 00237 // 00238 return (count($this->css) > 0); 00239 } 00240 00241 function Parse($filename) { 00242 $this->Clear(); 00243 if(file_exists($filename)) { 00244 return $this->ParseStr(file_get_contents($filename)); 00245 } else { 00246 return false; 00247 } 00248 } 00249 00250 function GetCSS() { 00251 $result = ""; 00252 foreach($this->css as $key => $values) { 00253 $result .= $key." {\n"; 00254 foreach($values as $key => $value) { 00255 $result .= " $key: $value;\n"; 00256 } 00257 $result .= "}\n\n"; 00258 } 00259 return $result; 00260 } 00261 }