| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | class mvblog_IniParser { |
|---|
| 22 | const TYPE_STRING = 1; |
|---|
| 23 | const TYPE_BOOL = 2; |
|---|
| 24 | const TYPE_INT = 3; |
|---|
| 25 | |
|---|
| 26 | public static $typeNames = array( |
|---|
| 27 | mvblog_IniParser::TYPE_STRING => "String", |
|---|
| 28 | mvblog_IniParser::TYPE_BOOL => "Boolean", |
|---|
| 29 | mvblog_IniParser::TYPE_INT => "Integer", |
|---|
| 30 | ); |
|---|
| 31 | protected $availSettings = array(); |
|---|
| 32 | protected $settings = array(); |
|---|
| 33 | |
|---|
| 34 | public function __construct($availSettings) { |
|---|
| 35 | $this->availSettings = $availSettings; |
|---|
| 36 | $this->setDefaults($availSettings); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | protected function parse($contents) { |
|---|
| 40 | $values = array(); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | $contents = str_replace("\r\n", "\n", $contents); |
|---|
| 44 | |
|---|
| 45 | $contents = str_replace("\r", "\n", $contents); |
|---|
| 46 | |
|---|
| 47 | $section = ""; |
|---|
| 48 | |
|---|
| 49 | $contents = explode("\n", $contents); |
|---|
| 50 | for ($i = 0; $i < count($contents); $i++) { |
|---|
| 51 | |
|---|
| 52 | if (preg_match('/^\[(.*?)\]*$/', $contents[$i], $match)) { |
|---|
| 53 | $section = $match[1]; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | if (!preg_match("/^\s*[#;].*$/", $contents[$i]) && |
|---|
| 57 | preg_match('/^\s*(.*?)\s*=\s*(.*?)\s*$/', $contents[$i], $match)) { |
|---|
| 58 | $key = $match[1]; |
|---|
| 59 | $value = $match[2]; |
|---|
| 60 | $values[$section][$key] = $value; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | return($values); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | protected function setDefaults($availSettings) { |
|---|
| 68 | foreach($availSettings as $section => $data) |
|---|
| 69 | foreach ($data as $key => $value) |
|---|
| 70 | if (array_key_exists("default", $value)) |
|---|
| 71 | $this->setSetting($section, $key, $value["default"]); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | protected function setSetting($section, $key, $value) { |
|---|
| 75 | if (!array_key_exists($key, $this->availSettings[$section])) |
|---|
| 76 | throw new mvblog_IniParserException(1, $key, $section); |
|---|
| 77 | |
|---|
| 78 | $type = $this->availSettings[$section][$key]["type"]; |
|---|
| 79 | switch($type) { |
|---|
| 80 | case mvblog_IniParser::TYPE_STRING: |
|---|
| 81 | $this->settings[$section][$key] = (string)$value; |
|---|
| 82 | break; |
|---|
| 83 | case mvblog_IniParser::TYPE_BOOL: |
|---|
| 84 | if (strcasecmp($value, "on") === 0 || |
|---|
| 85 | strcasecmp($value, "true") === 0 || |
|---|
| 86 | strcasecmp($value, "yes") === 0 || |
|---|
| 87 | $value == '1') { |
|---|
| 88 | $this->settings[$section][$key] = True; |
|---|
| 89 | } else |
|---|
| 90 | if (strcasecmp($value, "off") === 0 || |
|---|
| 91 | strcasecmp($value, "false") === 0 || |
|---|
| 92 | strcasecmp($value, "no") === 0 || |
|---|
| 93 | $value == '0') { |
|---|
| 94 | $this->settings[$section][$key] = False; |
|---|
| 95 | } else { |
|---|
| 96 | throw new mvblog_IniParserException(2, $value, $key, mvblog_IniParser::$typeNames[$type], $section); |
|---|
| 97 | } |
|---|
| 98 | break; |
|---|
| 99 | case mvblog_IniParser::TYPE_INT: |
|---|
| 100 | if (is_int($value) || ctype_digit($value)) |
|---|
| 101 | $this->settings[$section][$key] = (int)$value; |
|---|
| 102 | else |
|---|
| 103 | throw new mvblog_IniParserException(2, $value, $key, mvblog_IniParser::$typeNames[$type], $section); |
|---|
| 104 | break; |
|---|
| 105 | default: |
|---|
| 106 | throw new mvblog_IniParserException(3, $this->availSettings[$section][$key]); |
|---|
| 107 | break; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | public function getSetting($section, $key) { |
|---|
| 112 | if (array_key_exists($key, $this->settings[$section])) |
|---|
| 113 | return($this->settings[$section][$key]); |
|---|
| 114 | else |
|---|
| 115 | return(null); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | public function getSettings($section = "") { |
|---|
| 119 | |
|---|
| 120 | if (!$section) |
|---|
| 121 | return $this->settings; |
|---|
| 122 | else |
|---|
| 123 | if (array_key_exists($section, $this->settings)) |
|---|
| 124 | return $this->settings[$section]; |
|---|
| 125 | else |
|---|
| 126 | return null; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | ?> |
|---|