| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class mvblog_IniFileReader extends mvblog_IniParser { |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | protected $filename; |
|---|
| 29 | |
|---|
| 30 | public function __construct($availSettings, $filename) { |
|---|
| 31 | parent::__construct($availSettings); |
|---|
| 32 | $this->filename = $filename; |
|---|
| 33 | $contents = $this->read($this->filename); |
|---|
| 34 | $settings = $this->parse($contents); |
|---|
| 35 | $this->setSettings($settings); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | protected function read($filename) { |
|---|
| 39 | if (($contents = @file_get_contents($filename)) === False) |
|---|
| 40 | throw new mvblog_IniFileReaderException(1, $filename); |
|---|
| 41 | |
|---|
| 42 | return($contents); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | protected function parse($contents) { |
|---|
| 47 | $settings = array(); |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | $contents = str_replace("\r\n", "\n", $contents); |
|---|
| 51 | |
|---|
| 52 | $contents = str_replace("\r", "\n", $contents); |
|---|
| 53 | |
|---|
| 54 | $contents = explode("\n", $contents); |
|---|
| 55 | $section = ""; |
|---|
| 56 | for ($i = 0; $i < count($contents); $i++) { |
|---|
| 57 | |
|---|
| 58 | if (preg_match('/^\[(.*?)\]*$/', $contents[$i], $match)) { |
|---|
| 59 | $section = $match[1]; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | if (!preg_match("/^\s*#.*$/", $contents[$i]) && |
|---|
| 63 | preg_match('/^\s*(.*?)\s*=\s*(.*?)\s*$/', $contents[$i], $match)) { |
|---|
| 64 | $key = $match[1]; |
|---|
| 65 | $value = $match[2]; |
|---|
| 66 | $settings[$section][] = array("key" => $key, "value" => $value, "line" => $i); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | return($settings); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | protected function setSettings($settings) { |
|---|
| 74 | foreach ($settings as $section => $data) { |
|---|
| 75 | foreach($data as $setting) { |
|---|
| 76 | $key = $setting["key"]; |
|---|
| 77 | $value = $setting["value"]; |
|---|
| 78 | $file = $this->filename; |
|---|
| 79 | $line = $setting["line"]; |
|---|
| 80 | |
|---|
| 81 | try { |
|---|
| 82 | $this->setSetting($section, $key, $value); |
|---|
| 83 | } catch (mvblog_IniParserException $e) { |
|---|
| 84 | switch($e->getCode()) { |
|---|
| 85 | case 1: |
|---|
| 86 | throw new mvblog_IniFileReaderException(2, $key, $section, $file, $line); |
|---|
| 87 | break; |
|---|
| 88 | case 2: |
|---|
| 89 | $type = mvblog_IniParser::$typeNames[$this->availSettings[$section][$key]["type"]]; |
|---|
| 90 | throw new mvblog_IniFileReaderException(3, $value, $key, $type, $section, $file, $line); |
|---|
| 91 | break; |
|---|
| 92 | case 3: |
|---|
| 93 | throw new mvblog_IniFileReaderException(4, $key, $value, $section, $file, $line); |
|---|
| 94 | break; |
|---|
| 95 | default: |
|---|
| 96 | throw $e; |
|---|
| 97 | break; |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | ?> |
|---|