root/trunk/common/mvblog_iniparser.php

Revision 776, 3.7 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
21class 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        // Find and replace dos line endings
43        $contents = str_replace("\r\n", "\n", $contents);
44        // Find and replace mac line endings
45        $contents = str_replace("\r", "\n", $contents);
46        // Set section to nothing by default
47        $section = "";
48
49        $contents = explode("\n", $contents);
50        for ($i = 0; $i < count($contents); $i++) {
51            // Look for a section
52            if (preg_match('/^\[(.*?)\]*$/', $contents[$i], $match)) {
53                $section = $match[1];
54            }
55
56            if (!preg_match("/^\s*[#;].*$/", $contents[$i]) && // Comment
57                preg_match('/^\s*(.*?)\s*=\s*(.*?)\s*$/', $contents[$i], $match)) {
58                $key = $match[1];
59                $value = $match[2];
60                $values[$section][$key] = $value; // FIXME: Strip
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        //if section is empty return the complete settings array
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?>
Note: See TracBrowser for help on using the browser.