Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// +----------------------------------------------------------------------+
3
// | PHP Version 4                                                        |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 1997-2003 The PHP Group                                |
6
// +----------------------------------------------------------------------+
7
// | This source file is subject to version 2.0 of the PHP license,       |
8
// | that is bundled with this package in the file LICENSE, and is        |
9
// | available at through the world-wide-web at                           |
10
// | http://www.php.net/license/2_02.txt.                                 |
11
// | If you did not receive a copy of the PHP license and are unable to   |
12
// | obtain it through the world-wide-web, please send a note to          |
13
// | license@php.net so we can mail you a copy immediately.               |
14
// +----------------------------------------------------------------------+
15
// | Author: Bertrand Mansion <bmansion@mamasam.com>                      |
16
// +----------------------------------------------------------------------+
17
//
18
// $Id: Apache.php 203595 2005-12-24 02:34:39Z aashley $
19
 
20
/**
21
* Simple config parser for apache httpd.conf files
22
* A more complex version could handle directives as
23
* associative arrays.
24
*
25
* @author      Bertrand Mansion <bmansion@mamasam.com>
26
* @package     Config
27
*/
28
class Config_Container_Apache {
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
    * @access public
42
    * @param    string  $options    (optional)Options to be used by renderer
43
    */
44
    function Config_Container_Apache($options = array())
45
    {
46
        $this->options = $options;
47
    } // end constructor
48
 
49
    /**
50
    * Parses the data of the given configuration file
51
    *
52
    * @access public
53
    * @param string $datasrc    path to the configuration file
54
    * @param object $obj        reference to a config object
55
    * @return mixed returns a PEAR_ERROR, if error occurs or true if ok
56
    */
57
    function &parseDatasrc($datasrc, &$obj)
58
    {
59
        $return = true;
60
        if (!is_readable($datasrc)) {
61
            return PEAR::raiseError("Datasource file cannot be read.", null, PEAR_ERROR_RETURN);
62
        }
63
        $lines = file($datasrc);
64
        $n = 0;
65
        $lastline = '';
66
        $sections[0] =& $obj->container;
67
        foreach ($lines as $line) {
68
            $n++;
69
            if (!preg_match('/^\s*#/', $line) &&
70
                 preg_match('/^\s*(.*)\s+\\\$/', $line, $match)) {
71
                // directive on more than one line
72
                $lastline .= $match[1].' ';
73
                continue;
74
            }
75
            if ($lastline != '') {
76
                $line = $lastline.trim($line);
77
                $lastline = '';
78
            }
79
            if (preg_match('/^\s*#+\s*(.*?)\s*$/', $line, $match)) {
80
                // a comment
81
                $currentSection =& $sections[count($sections)-1];
82
                $currentSection->createComment($match[1]);
83
            } elseif (trim($line) == '') {
84
                // a blank line
85
                $currentSection =& $sections[count($sections)-1];
86
                $currentSection->createBlank();
87
            } elseif (preg_match('/^\s*(\w+)(?:\s+(.*?)|)\s*$/', $line, $match)) {
88
                // a directive
89
                $currentSection =& $sections[count($sections)-1];
90
                $currentSection->createDirective($match[1], $match[2]);
91
            } elseif (preg_match('/^\s*<(\w+)(?:\s+([^>]*)|\s*)>\s*$/', $line, $match)) {
92
                // a section opening
93
                if (!isset($match[2]))
94
                    $match[2] = '';
95
                $currentSection =& $sections[count($sections)-1];
96
                $attributes = explode(' ', $match[2]);
97
                $sections[] =& $currentSection->createSection($match[1], $attributes);
98
            } elseif (preg_match('/^\s*<\/(\w+)\s*>\s*$/', $line, $match)) {
99
                // a section closing
100
                $currentSection =& $sections[count($sections)-1];
101
                if ($currentSection->name != $match[1]) {
102
                    return PEAR::raiseError("Section not closed in '$datasrc' at line $n.", null, PEAR_ERROR_RETURN);
103
                }
104
                array_pop($sections);
105
            } else {
106
                return PEAR::raiseError("Syntax error in '$datasrc' at line $n.", null, PEAR_ERROR_RETURN);
107
            }
108
        }
109
        return $return;
110
    } // end func parseDatasrc
111
 
112
    /**
113
    * Returns a formatted string of the object
114
    * @param    object  $obj    Container object to be output as string
115
    * @access   public
116
    * @return   string
117
    */
118
    function toString(&$obj)
119
    {
120
        static $deep = -1;
121
        $ident = '';
122
        if (!$obj->isRoot()) {
123
            // no indent for root
124
            $deep++;
125
            $ident = str_repeat('  ', $deep);
126
        }
127
        if (!isset($string)) {
128
            $string = '';
129
        }
130
        switch ($obj->type) {
131
            case 'blank':
132
                $string = "\n";
133
                break;
134
            case 'comment':
135
                $string = $ident.'# '.$obj->content."\n";
136
                break;
137
            case 'directive':
138
                $string = $ident.$obj->name.' '.$obj->content."\n";
139
                break;
140
            case 'section':
141
                if (!$obj->isRoot()) {
142
                    $string = $ident.'<'.$obj->name;
143
                    if (is_array($obj->attributes) && count($obj->attributes) > 0) {
144
                        foreach ($obj->attributes as $attr => $val) {
145
                            $string .= ' '.$val;
146
                        }
147
                    }
148
                    $string .= ">\n";
149
                }
150
                if (count($obj->children) > 0) {
151
                    for ($i = 0; $i < count($obj->children); $i++) {
152
                        $string .= $this->toString($obj->getChild($i));
153
                    }
154
                }
155
                if (!$obj->isRoot()) {
156
                    // object is not root
157
                    $string .= $ident.'</'.$obj->name.">\n";
158
                }
159
                break;
160
            default:
161
                $string = '';
162
        }
163
        if (!$obj->isRoot()) {
164
            $deep--;
165
        }
166
        return $string;
167
    } // end func toString
168
} // end class Config_Container_Apache
169
?>