/***************************************************************************************** * This class provides functions for parsing, storing and returning CSS stylesheets. * It stores the CSS styles in a MySql database with an owner id and a theme id. * The main purpose for this class is to provide different CSS styles for different user * and themes. * The constructor expects an MySQL link identifier and the name of the MySql-Database as * arguments. (See example) * * * Copyright (C) 2004 Nico de Haen * * $Id: dynamic_CSS.class.php,v 1.0 2004/02/01 * *******************************************************************************************/ class dynamic_CSS { var $warnings = ""; var $mysql_host = ""; var $mysql_user = ""; var $mysql_pass = ""; var $mysql_db = ""; var $style_arr; //multi leveled array to store all the CSS styles /** * dynamic_CSS::dynamic_CSS() * Constructor this class - define public connection parameters and * call the connect method * * @param $host * @param $user * @param $password * @param $database */ function dynamic_CSS(){ if(func_num_args()<1){ if(!$this->mysql_check()){ echo "\n\n"; } } else { $this->mysql_host = func_get_arg(0); $this->mysql_user = func_get_arg(1); $this->mysql_pass = func_get_arg(2); $this->mysql_db = func_get_arg(3); } } function mysql_check(){ if(!@mysql_ping()){ @mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass); @mysql_select_db($this->mysql_db); if(!@mysql_ping()){ $this->warnings.=mysql_error()."\n"; return false; } } return true; } function parse_css($str){ $temp_arr = split('}',$str); array_splice($temp_arr,-1,1); for($i=0;$iwarnings.="No CSS styles found!\n"; return; } $this->style_arr = $style_arr; return $style_arr; } /** * stores the passed style array in the database * primary keys are the the id and the owner id. * both can be chosen freely * * @param $id * @param $owner * @param $db * */ function save_to_db($id, $owner, $db){ if(!$this->mysql_check()){ return false; } if(func_num_args()==3){ $style_arr = $this->style_arr; } if(count($style_arr)<1){ $this->warnings.="No styles in array!\n"; return; } if($id=='' || $owner==''){ $this->warnings.="No style id or owner id given\n"; return; } $result = @mysql_list_fields($db,"styles"); if(!$anz = @mysql_num_fields($result)){ $this->warnings.=mysql_error(); return; } $properties = array(); for($x=0;$x<$anz;$x++){ array_push($properties,mysql_field_name($result,$x)); } while (list ($key, $val) = each ($style_arr)) { //for each style $sql = "SELECT * FROM styles WHERE id='$id' AND name='$key' AND owner='$owner'"; $result = mysql_query($sql); if(mysql_num_rows($result)>0){ $sql = "UPDATE styles set "; //if id already exists while (list ($key1, $val1) = each ($val)) { $key1 = str_replace('-','_',$key1); if(array_search(trim($key1),$properties)){ $sql.= "$key1 = '$val1',"; } else $this->warnings.="Property $key1 in style $key not supported!\n"; } $sql = substr($sql,0,strlen($sql)-1); $sql.=" WHERE id = '$id' AND name = '$key' AND owner ='$owner'"; } else { $sql = "INSERT INTO styles "; $key_str = "name, id, owner"; $val_str = "'$key','$id', '$owner'"; while (list ($key1, $val1) = each ($val)) { $key1 = str_replace('-','_',$key1); if(array_search($key1,$properties,"strict")){ $key_str.=",".$key1; $val_str.=",'$val1'"; } else $this->warnings.="Property x$key1 in style x$key not supported!\n"; } $sql.= "( $key_str ) VALUES ( $val_str )"; } mysql_query($sql); } } function print_css($id, $owner){ if(!$this->mysql_check()){ return; } $result = mysql_query("SELECT * FROM styles WHERE id = '$id' AND owner = '$owner'"); if(mysql_num_rows($result)>0){ echo "\n"; } else echo "\n"; } function debug(){ if($this->warnings==""){ return ""; } else return ""; } }