1: <?php
2:
3: namespace Liberty;
4:
5:
6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
32:
33:
34: class INI {
35:
36:
37: 38: 39:
40: public static function read($filename) {
41: $ini = array();
42: $lines = file($filename);
43: $section = 'default';
44: $multi = '';
45: foreach($lines as $line) {
46: if (substr($line, 0, 1) !== ';') {
47: $line = str_replace("\r", "", str_replace("\n", "", $line));
48: if (preg_match('/^\[(.*)\]/', $line, $m)) {
49: $section = $m[1];
50: } else if ($multi === '' && preg_match('/^([a-z0-9_.\[\]-]+)\s*=\s*(.*)$/i', $line, $m)) {
51: $key = $m[1];
52: $val = $m[2];
53: if (substr($val, -1) !== "\\") {
54: $val = trim($val);
55: INI::manage_keys($ini[$section], $key, $val);
56: $multi = '';
57: } else {
58: $multi = substr($val, 0, -1)."\n";
59: }
60: } else if ($multi !== '') {
61: if (substr($line, -1) === "\\") {
62: $multi .= substr($line, 0, -1)."\n";
63: } else {
64: INI::manage_keys($ini[$section], $key, $multi.$line);
65: $multi = '';
66: }
67: }
68: }
69: }
70:
71: $buf = get_defined_constants(true);
72: $consts = array();
73: 74: 75: 76: 77: 78: 79:
80: return $ini;
81: }
82:
83:
84:
85: 86: 87:
88: public static function write($filename, $ini) {
89: $string = '';
90: foreach(array_keys($ini) as $key) {
91: $string .= '['.$key."]\n";
92: $string .= INI::write_get_string($ini[$key], '')."\n";
93: }
94: file_put_contents($filename, $string);
95: }
96:
97:
98:
99: 100: 101:
102: private static function write_get_string(& $ini, $prefix) {
103: $string = '';
104: ksort($ini);
105: foreach($ini as $key => $val) {
106: if (is_array($val)) {
107: $string .= INI::write_get_string($ini[$key], $prefix.$key.'.');
108: } else {
109: $string .= $prefix.$key.' = '.str_replace("\n", "\\\n", INI::set_value($val))."\n";
110: }
111: }
112: return $string;
113: }
114:
115:
116: 117: 118:
119: private static function set_value($val) {
120: if ($val === true) { return 'true'; }
121: else if ($val === false) { return 'false'; }
122: return $val;
123: }
124:
125:
126:
127: 128: 129:
130: private static function get_value($val) {
131: if (preg_match('/^-?[0-9]$/i', $val)) { return intval($val); }
132: else if (strtolower($val) === 'true') { return true; }
133: else if (strtolower($val) === 'false') { return false; }
134: else if (preg_match('/^"(.*)"$/i', $val, $m)) { return $m[1]; }
135: else if (preg_match('/^\'(.*)\'$/i', $val, $m)) { return $m[1]; }
136: return $val;
137: }
138:
139:
140: 141: 142:
143: private static function get_key($val) {
144: if (preg_match('/^[0-9]$/i', $val)) { return intval($val); }
145: return $val;
146: }
147:
148:
149: 150: 151:
152: private static function manage_keys(& $ini, $key, $val) {
153: if (preg_match('/^([a-z0-9_-]+)\.(.*)$/i', $key, $m)) {
154: INI::manage_keys($ini[$m[1]], $m[2], $val);
155: } else if (preg_match('/^([a-z0-9_-]+)\[(.*)\]$/i', $key, $m)) {
156: if ($m[2] !== '') {
157: $ini[$m[1]][INI::get_key($m[2])] = INI::get_value($val);
158: } else {
159: $ini[$m[1]][] = INI::get_value($val);
160: }
161: } else {
162: $ini[INI::get_key($key)] = INI::get_value($val);
163: }
164: }
165:
166:
167: 168: 169:
170: private static function replace_consts(& $item, $key, $consts) {
171: if (is_string($item)) {
172: $item = strtr($item, $consts);
173: }
174: }
175: }
176:
177: ?>