prefs->get() * or $application->prefs->set() * */ class phpmole_prefs { // class that loads prefs! // stores data in a global array prefs! var $prefs; function load ($filename="") { // load the Preferences file global $HTTP_ENV_VARS; $p =""; if (!$filename) { if (substr(PHP_OS, 0, 3) == 'WIN') $p = ".WIN"; $filename = dirname(__FILE__)."/Preferences{$p}"; } //echo $filename; $this->data = file($filename); //echo serialize($this->data); $this->parse(); } function parse() { for ($i=0;$i< count($this->data);$i++) { $line = trim($this->data[$i]); if (!$line) continue; if (substr($line,0,1) == "[") { $major = substr($line,1,-1); //print "GOT MAJOR $major\n"; continue; } $eq = strpos( $line,"="); $left = trim(substr($line,0,$eq)); $right = substr($line,$eq+1); $this->prefs[$major."/".$left] = trim($right); //echo "Setting pref $major/$left = $right\n"; } } function write($file) { // map [major/minor] = xxx // to // [major] // minor = xxxx // first convert list into array[major][minor] //echo "WRITING TO $file"; foreach($this->prefs as $k => $v) { $p = strpos($k,"/"); if (!$p) continue; $major = substr($k,0,$p); $minor = substr($k,$p+1); $sorted[$major][$minor] = $v; //echo "STORING $major / $minor = $v\n"; } $fh = fopen($file,"w"); foreach($sorted as $k => $v) { fwrite($fh,"\n[$k]\n"); foreach($v as $minor => $value) fwrite($fh,"$minor = $value\n"); } fclose($fh); } function load_user_prefs() { $homedir = getenv("HOME"); if (substr(PHP_OS, 0, 3) == 'WIN') $homedir = "C:\Program Files"; if (file_exists($homedir."/.".APPNAME."/Preferences")) $this->load($homedir."/.".APPNAME."/Preferences"); } function save_user_prefs() { $homedir = getenv("HOME"); if (substr(PHP_OS, 0, 3) == 'WIN') $homedir = "C:\Program Files"; //echo "MKDIR ". $homedir."/.".APPNAME; if (!is_dir($homedir."/.".APPNAME)) mkdir($homedir."/.".APPNAME,0700); $this->write($homedir."/.".APPNAME."/Preferences"); } function get($a) { return @$this->prefs[$a]; } function set($a,$b) { //echo("PREFS SET $a = $b"); $this->prefs[$a] = $b; } function shunt_array($value, $prefix, $max) { // used for recent files // adds value to 0 // moves 1->2 for ($i=$max;$i>0;$i--) $this->set($prefix . "_" . sprintf("%1d",$i), $this->get ($prefix . "_" . sprintf("%1d",$i-1))); $this->set($prefix ."_0",$value); } function get_array($a) { $i=0; $ret = array(); while (@isset($this->prefs["{$a}_".sprintf("%1d",$i)])) { $ret[$i] = $this->prefs["{$a}_".sprintf("%1d",$i)]; $i++; } return $ret; } function set_array($a,$b) { $this->clear_array($a); foreach($b as $k=>$v) $this->set($a . "_" . sprintf("%1d",$k), $v); } function clear_array($a) { if ($keys = array_keys($this->prefs)) foreach($keys as $k) if ($a == substr($k,0,strlen($a))) unset($this->prefs[$k]); } function prune_array($a,$n) { $ar = $this->get_array($a); // get the last n elements of the array if (count($ar) < $n) return array(); // n =10 count = 12 -> old = 0-2 $old = array_splice($ar,0, count($ar)-$n); // new = 2->12 $new = array_splice($ar,count($ar)-$n); $this->set_array($a,$new); //echo "NEW:".serialize($new); //echo "OLD:".serialize($old); return $old; } function un_set($a) { if (isset($this->prefs[$a])) unset($this->prefs[$a]); } } ?>