root/trunk/common/mvblog_inifilereader.php

Revision 776, 2.8 KB (checked in by michiel, 9 months ago)

update copyright year.

Closes #180

Line 
1<?php
2/**
3 * MvBlog -- An open source no-nosense blogtool
4 *
5 * Copyright (C) 2005-2008, Michiel van Baak
6 * Michiel van Baak <mvanbaak@users.sourceforge.net>
7 *
8 * See http://www.mvblog.org for more information on MvBlog.
9 * That page also provides Bugtrackers, Filereleases etc.
10 *
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License Version 2. See the LICENSE file
13 * at the top of the source tree.
14 *
15 * @package MvBlog
16 * @author Ferry Boender
17 * @version %%VERSION%%
18 * @copyright 2005-2008 Ferry Boender
19 */
20
21/**
22 * Class to read an ini file
23 */
24class mvblog_IniFileReader extends mvblog_IniParser {
25    /**
26     * @var string $filename The ini file to read
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    // Custom parser so we get file &line numbers
46    protected function parse($contents) {
47        $settings = array();
48
49        // Find and replace dos line endings
50        $contents = str_replace("\r\n", "\n", $contents);
51        // Find and replace mac line endings
52        $contents = str_replace("\r", "\n", $contents);
53
54        $contents = explode("\n", $contents);
55        $section = "";
56        for ($i = 0; $i < count($contents); $i++) {
57            // Look for a section
58            if (preg_match('/^\[(.*?)\]*$/', $contents[$i], $match)) {
59                $section = $match[1];
60            }
61
62            if (!preg_match("/^\s*#.*$/", $contents[$i]) && // Comment
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?>
Note: See TracBrowser for help on using the browser.