/** * Saves a setting to the configuration file. If the setting already exists, the current * value is overwritten. Otherwise, it will be appended in the end of the file. * <b>NOTE:</b> This method is highly unoptimized because every time that we call saveValue, * we are writing the whole file to disk... Bad ;) But it works, so we'll leave it as it * is for the time being... * * @param name Name of the setting. * @param value Value of the setting. * @return True if success or false otherwise. */ function saveValue($name, $value) { // open the config file $f = new File($this->_configFile); // there was a problem opening the file if (!$f->open("r+")) { return false; } // now we have to process each of the lines $contents = $f->readFile(); $i = 0; $result = array(); $valueString = $this->_getDataString($value); // depending if it's a string or not, we need a different regexp and a // expression that will replace the original if ($this->_getType($value) == TYPE_STRING) { $regexp = "/ *\\\$config\\[\"{$name}\"\\] *= *\"(.*)\"; */"; $replaceWith = "\$config[\"{$name}\"] = {$valueString};"; } else { $regexp = "/ *\\\$config\\[\"{$name}\"\\] *= *(.*); */"; $replaceWith = "\$config[\"{$name}\"] = {$valueString};"; } while ($i < count($contents)) { $line = $contents[$i]; $newline = preg_replace($regexp, $replaceWith, $line); $i++; // for some reason, looks like we're getting some garbage in the end // of the file... couldn't find any better way to do it if ($newline != "?>") { $newline .= "\n"; array_push($result, $newline); } else { array_push($result, "?>"); break; } } // the only thing we have to do know is save the contents of $result // to the output file $f->writeLines($result); return true; }