00001 <?php
00002
00015
00016
00017
00018 class ConfigParser {
00019
00021 var $content;
00022
00026 function ConfigParser() {
00027 }
00028
00035 function &readConfig($file) {
00036 $configData = array();
00037 $currentSection = false;
00038 $falseValue = false;
00039
00040 if (!file_exists($file) || !is_readable($file)) {
00041 return $falseValue;
00042 }
00043
00044 $fp = fopen($file, 'rb');
00045 if (!$fp) {
00046 return $falseValue;
00047 }
00048
00049 while (!feof($fp)) {
00050 $line = fgets($fp, 1024);
00051 $line = trim($line);
00052 if ($line === '' || strpos($line, ';') === 0) {
00053
00054 continue;
00055 }
00056
00057 if (preg_match('/^\[(.+)\]/', $line, $matches)) {
00058
00059 $currentSection = $matches[1];
00060 if (!isset($configData[$currentSection])) {
00061 $configData[$currentSection] = array();
00062 }
00063
00064 } else if (strpos($line, '=') !== false) {
00065
00066 list($key, $value) = explode('=', $line, 2);
00067 $key = trim($key);
00068 $value = trim($value);
00069
00070
00071 if (preg_match('/^[\"\'](.*)[\"\']$/', $value, $matches)) {
00072
00073 $value = stripslashes($matches[1]);
00074
00075 } else {
00076 preg_match('/^([\S]*)/', $value, $matches);
00077 $value = $matches[1];
00078
00079
00080 if ($value === '') {
00081 $value = null;
00082
00083 } else if (is_numeric($value)) {
00084 if (strstr($value, '.')) {
00085
00086 $value = (float) $value;
00087 } else if (substr($value, 0, 2) == '0x') {
00088
00089 $value = intval($value, 16);
00090 } else if (substr($value, 0, 1) == '0') {
00091
00092 $value = intval($value, 8);
00093 } else {
00094
00095 $value = (int) $value;
00096 }
00097
00098 } else if (strtolower($value) == 'true' || strtolower($value) == 'on') {
00099 $value = true;
00100
00101 } else if (strtolower($value) == 'false' || strtolower($value) == 'off') {
00102 $value = false;
00103
00104 } else if (defined($value)) {
00105
00106 $value = constant($value);
00107 }
00108 }
00109
00110 if ($currentSection === false) {
00111 $configData[$key] = $value;
00112
00113 } else if (is_array($configData[$currentSection])) {
00114 $configData[$currentSection][$key] = $value;
00115 }
00116 }
00117 }
00118
00119 fclose($fp);
00120
00121 return $configData;
00122 }
00123
00132 function updateConfig($file, $params) {
00133 if (!file_exists($file) || !is_readable($file)) {
00134 return false;
00135 }
00136
00137 $this->content = '';
00138 $lines = file($file);
00139
00140
00141 for ($i=0, $count=count($lines); $i < $count; $i++) {
00142 $line = $lines[$i];
00143
00144 if (preg_match('/^;/', $line) || preg_match('/^\s*$/', $line)) {
00145
00146 $this->content .= $line;
00147
00148 } else if (preg_match('/^\s*\[(\w+)\]/', $line, $matches)) {
00149
00150 $currentSection = $matches[1];
00151 $this->content .= $line;
00152
00153 } else if (preg_match('/^\s*(\w+)\s*=/', $line, $matches)) {
00154
00155 $key = $matches[1];
00156
00157 if (!isset($currentSection) && array_key_exists($key, $params) && !is_array($params[$key])) {
00158
00159 $value = $params[$key];
00160
00161 } else if (isset($params[$currentSection]) && is_array($params[$currentSection]) && array_key_exists($key, $params[$currentSection])) {
00162
00163 $value = $params[$currentSection][$key];
00164
00165 } else {
00166
00167 $this->content .= $line;
00168 continue;
00169 }
00170
00171 if (preg_match('/[^\w\-\/]/', $value)) {
00172
00173 $valueString = '"' . $value . '"';
00174 } else {
00175 $valueString = $value;
00176 }
00177
00178 $this->content .= "$key = $valueString\n";
00179
00180 } else {
00181 $this->content .= $line;
00182 }
00183 }
00184
00185 return true;
00186 }
00187
00193 function writeConfig($file) {
00194 if (!(file_exists($file) && is_writable($file))
00195 && !(!file_exists($file) && is_dir(dirname($file)) && is_writable(dirname($file)))) {
00196
00197 return false;
00198 }
00199
00200 $fp = @fopen($file, 'wb');
00201 if (!$fp) {
00202 return false;
00203 }
00204
00205 fwrite($fp, $this->content);
00206 fclose($fp);
00207 return true;
00208 }
00209
00214 function getFileContents() {
00215 return $this->content;
00216 }
00217
00218 }
00219
00220 ?>