Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Part of the PEAR Config package
4
 *
5
 * PHP Version 4
6
 *
7
 * @category Configuration
8
 * @package  Config
9
 * @author   Bertrand Mansion <bmansion@mamasam.com>
10
 * @license  http://www.php.net/license PHP License
11
 * @link     http://pear.php.net/package/Config
12
 */
13
 
14
/**
15
 * Config parser for PHP .ini files
16
 * Faster because it uses parse_ini_file() but get rid of comments,
17
 * quotes, types and converts On, Off, True, False, Yes, No to 0 and 1.
18
 *
19
 * Empty lines and comments are not preserved.
20
 *
21
 * @category Configuration
22
 * @package  Config
23
 * @author   Bertrand Mansion <bmansion@mamasam.com>
24
 * @license  http://www.php.net/license PHP License
25
 * @link     http://pear.php.net/package/Config
26
 */
27
class Config_Container_IniFile
28
{
29
 
30
    /**
31
     * This class options
32
     * Not used at the moment
33
     *
34
     * @var array
35
    */
36
    var $options = array();
37
 
38
    /**
39
     * Constructor
40
     *
41
     * @param string $options (optional)Options to be used by renderer
42
     *
43
     * @access public
44
     */
45
    function Config_Container_IniFile($options = array())
46
    {
47
        $this->options = $options;
48
    } // end constructor
49
 
50
    /**
51
     * Parses the data of the given configuration file
52
     *
53
     * @param string $datasrc path to the configuration file
54
     * @param object &$obj    reference to a config object
55
     *
56
     * @return mixed Returns a PEAR_ERROR, if error occurs or true if ok
57
     *
58
     * @access public
59
     */
60
    function &parseDatasrc($datasrc, &$obj)
61
    {
62
        $return = true;
63
        if (!file_exists($datasrc)) {
64
            return PEAR::raiseError(
65
                "Datasource file does not exist.",
66
                null, PEAR_ERROR_RETURN
67
            );
68
        }
69
        $currentSection =& $obj->container;
70
        $confArray = parse_ini_file($datasrc, true);
71
        if (!$confArray) {
72
            return PEAR::raiseError(
73
                "File '$datasrc' does not contain configuration data.",
74
                null, PEAR_ERROR_RETURN
75
            );
76
        }
77
        foreach ($confArray as $key => $value) {
78
            if (is_array($value)) {
79
                $currentSection =& $obj->container->createSection($key);
80
                foreach ($value as $directive => $content) {
81
                    // try to split the value if comma found
82
                    if (!is_array($content) && strpos($content, '"') === false) {
83
                        $values = preg_split('/\s*,\s+/', $content);
84
                        if (count($values) > 1) {
85
                            foreach ($values as $k => $v) {
86
                                $currentSection->createDirective($directive, $v);
87
                            }
88
                        } else {
89
                            $currentSection->createDirective($directive, $content);
90
                        }
91
                    } else {
92
                        $currentSection->createDirective($directive, $content);
93
                    }
94
                }
95
            } else {
96
                $currentSection->createDirective($key, $value);
97
            }
98
        }
99
        return $return;
100
    } // end func parseDatasrc
101
 
102
    /**
103
     * Returns a formatted string of the object
104
     *
105
     * @param object &$obj Container object to be output as string
106
     *
107
     * @return string
108
     *
109
     * @access public
110
     */
111
    function toString(&$obj)
112
    {
113
        static $childrenCount, $commaString;
114
 
115
        if (!isset($string)) {
116
            $string = '';
117
        }
118
        switch ($obj->type) {
119
        case 'blank':
120
            $string = "\n";
121
            break;
122
        case 'comment':
123
            $string = ';'.$obj->content."\n";
124
            break;
125
        case 'directive':
126
            $count = $obj->parent->countChildren('directive', $obj->name);
127
            $content = $obj->content;
128
            if (!is_array($content)) {
129
                $content = $this->contentToString($content);
130
                if ($count > 1) {
131
                    // multiple values for a directive are separated by a comma
132
                    if (isset($childrenCount[$obj->name])) {
133
                        $childrenCount[$obj->name]++;
134
                    } else {
135
                        $childrenCount[$obj->name] = 0;
136
                        $commaString[$obj->name] = $obj->name.'=';
137
                    }
138
                    if ($childrenCount[$obj->name] == $count-1) {
139
                        // Clean the static for future calls to toString
140
                        $string .= $commaString[$obj->name].$content."\n";
141
                        unset($childrenCount[$obj->name]);
142
                        unset($commaString[$obj->name]);
143
                    } else {
144
                        $commaString[$obj->name] .= $content.', ';
145
                    }
146
                } else {
147
                    $string = $obj->name.'='.$content."\n";
148
                }
149
            } else {
150
                //array
151
                $string = '';
152
                $n = 0;
153
                foreach ($content as $contentKey => $contentValue) {
154
                    if (is_integer($contentKey) && $contentKey == $n) {
155
                        $stringKey = '';
156
                        ++$n;
157
                    } else {
158
                        $stringKey = $contentKey;
159
                    }
160
                    $string .= $obj->name . '[' . $stringKey . ']='
161
                        . $this->contentToString($contentValue) . "\n";
162
                }
163
            }
164
            break;
165
        case 'section':
166
            if (!$obj->isRoot()) {
167
                $string = '['.$obj->name."]\n";
168
            }
169
            if (count($obj->children) > 0) {
170
                for ($i = 0; $i < count($obj->children); $i++) {
171
                    $string .= $this->toString($obj->getChild($i));
172
                }
173
            }
174
            break;
175
        default:
176
            $string = '';
177
        }
178
        return $string;
179
    } // end func toString
180
 
181
 
182
 
183
    /**
184
     * Converts a given content variable to a string that can
185
     * be used as value in a ini file
186
     *
187
     * @param mixed $content Value
188
     *
189
     * @return string $content String to be used as ini value
190
     */
191
    function contentToString($content)
192
    {
193
        if ($content === false) {
194
            $content = '0';
195
        } else if ($content === true) {
196
            $content = '1';
197
        } else if (strlen(trim($content)) < strlen($content)
198
            || strpos($content, ',') !== false
199
            || strpos($content, ';') !== false
200
            || strpos($content, '=') !== false
201
            || strpos($content, '"') !== false
202
            || strpos($content, '%') !== false
203
            || strpos($content, '~') !== false
204
            || strpos($content, '!') !== false
205
            || strpos($content, '|') !== false
206
            || strpos($content, '&') !== false
207
            || strpos($content, '(') !== false
208
            || strpos($content, ')') !== false
209
            || $content === 'none'
210
        ) {
211
            $content = '"'.addslashes($content).'"';
212
        }
213
        return $content;
214
    }
215
 
216
} // end class Config_Container_IniFile
217
?>