xmlparse.inc 10.1 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2
<?php

3
/*
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
Ad Schellevis's avatar
Ad Schellevis committed
27 28
*/

29 30
function listtags()
{
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    /* The following items will be treated as arrays in config.xml */
    $ret = array(
      'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver',
      'bridged', 'build_port_path',
      'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem',
       'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', 'dyndns',
      'element', 'encryption-algorithm-option',
      'field', 'fieldname',
      'gateway_item', 'gateway_group', 'gif', 'gre', 'group',
      'hash-algorithm-option', 'hosts',
      'ifgroupentry', 'igmpentry', 'interface_array', 'item',
      'key',
      'lagg', 'lbpool',
      'member', 'menu', 'mobilekey', 'monitor_type', 'mount',
      'npt', 'ntpserver',
      'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option',
      'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool', 'pages', 'pipe',
      'qinqentry',
      'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule',
      'schedule', 'service', 'servernat', 'servers', 'serversdisabled', 'staticmap', 'subqueue',
      'tab', 'timerange', 'tunnel',
      'user',
      'vip', 'virtual_server', 'vlan',
      'winsserver', 'wolentry', 'widget',
    );

    return array_flip($ret);
Ad Schellevis's avatar
Ad Schellevis committed
58 59
}

60 61 62
function startElement($parser, $name, $attrs)
{
    global $parsedcfg, $depth, $curpath, $havedata, $listtags;
Ad Schellevis's avatar
Ad Schellevis committed
63

64
    array_push($curpath, strtolower($name));
Ad Schellevis's avatar
Ad Schellevis committed
65

66 67 68 69
    $ptr =& $parsedcfg;
    foreach ($curpath as $path) {
        $ptr =& $ptr[$path];
    }
Ad Schellevis's avatar
Ad Schellevis committed
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    /* is it an element that belongs to a list? */
    if (isset($listtags[strtolower($name)])) {
        /* is there an array already? */
        if (!is_array($ptr)) {
            /* make an array */
            $ptr = array();
        }
        array_push($curpath, count($ptr));
    } elseif (isset($ptr)) {
        /* multiple entries not allowed for this element, bail out */
        die(sprintf(gettext('XML error: %s at line %d cannot occur more than once') . "\n",
            $name,
            xml_get_current_line_number($parser)));
    }
    $depth++;
    $havedata = $depth;
Ad Schellevis's avatar
Ad Schellevis committed
87 88
}

89 90 91
function endElement($parser, $name)
{
    global $depth, $curpath, $parsedcfg, $havedata, $listtags;
Ad Schellevis's avatar
Ad Schellevis committed
92

93 94 95 96 97 98 99
    if ($havedata == $depth) {
        $ptr =& $parsedcfg;
        foreach ($curpath as $path) {
            $ptr =& $ptr[$path];
        }
        $ptr = "";
    }
Ad Schellevis's avatar
Ad Schellevis committed
100

101 102 103 104
    array_pop($curpath);
    if (isset($listtags[strtolower($name)])) {
        array_pop($curpath);
    }
Ad Schellevis's avatar
Ad Schellevis committed
105

106
    $depth--;
Ad Schellevis's avatar
Ad Schellevis committed
107 108
}

109 110 111
function cData($parser, $data)
{
    global $depth, $curpath, $parsedcfg, $havedata;
Ad Schellevis's avatar
Ad Schellevis committed
112

113
    $data = trim($data, "\t\n\r");
Ad Schellevis's avatar
Ad Schellevis committed
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128
    if ($data != "") {
        $ptr =& $parsedcfg;
        foreach ($curpath as $path) {
            $ptr =& $ptr[$path];
        }
        if (is_string($ptr)) {
            $ptr .= html_entity_decode($data);
        } else {
            if (trim($data, " ") != "") {
                $ptr = html_entity_decode($data);
                $havedata++;
            }
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
129 130
}

131 132 133 134 135 136
function parse_xml_config($cffile, $rootobj, $isstring = "false")
{
    global $listtags;
    $listtags = listtags();
    if (isset($GLOBALS['custom_listtags'])) {
        foreach($GLOBALS['custom_listtags'] as $tag) {
Ad Schellevis's avatar
Ad Schellevis committed
137 138
            $listtags[$tag] = $tag;
        }
139 140
    }
    return parse_xml_config_raw($cffile, $rootobj, $isstring);
Ad Schellevis's avatar
Ad Schellevis committed
141 142
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
function parse_xml_config_raw($cffile, $rootobj, $isstring = "false")
{
    global $depth, $curpath, $parsedcfg, $havedata, $listtags;
    $parsedcfg = array();
    $curpath = array();
    $depth = 0;
    $havedata = 0;

    $xml_parser = xml_parser_create();

    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "cdata");
    xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE, 1);

    if (!($fp = fopen($cffile, "r"))) {
        log_error(gettext("Error: could not open XML input") . "\n");
        return -1;
    }

    while ($data = fread($fp, 4096)) {
        if (!xml_parse($xml_parser, $data, feof($fp))) {
            log_error(sprintf(gettext('XML error: %s at line %d in %s') . "\n",
                  xml_error_string(xml_get_error_code($xml_parser)),
                  xml_get_current_line_number($xml_parser),
                  $cffile));
            return -1;
        }
    }
    xml_parser_free($xml_parser);
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    if ($rootobj) {
        if (!is_array($rootobj)) {
            $rootobj = array($rootobj);
        }
        foreach ($rootobj as $rootobj_name) {
            if ($parsedcfg[$rootobj_name]) {
                break;
            }
        }
        if (!$parsedcfg[$rootobj_name]) {
            log_error(sprintf(gettext("XML error: no %s object found!") . "\n", implode(" or ", $rootobj)));
            return -1;
        }
        return $parsedcfg[$rootobj_name];
    } else {
        return $parsedcfg;
    }
Ad Schellevis's avatar
Ad Schellevis committed
190 191
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
function dump_xml_config_sub($arr, $indent)
{
  global $listtags;

  $xmlconfig = "";

    foreach ($arr as $ent => $val) {
        if (is_array($val)) {
            /* is it just a list of multiple values? */
            if (isset($listtags[strtolower($ent)])) {
                foreach ($val as $cval) {
                    if (is_array($cval)) {
                        if (empty($cval)) {
                            $xmlconfig .= str_repeat("\t", $indent);
                            $xmlconfig .= "<$ent/>\n";
                        } else {
                            $xmlconfig .= str_repeat("\t", $indent);
                            $xmlconfig .= "<$ent>\n";
                            $xmlconfig .= dump_xml_config_sub($cval, $indent + 1);
                            $xmlconfig .= str_repeat("\t", $indent);
                            $xmlconfig .= "</$ent>\n";
                        }
                    } else {
                        if ($cval === false) {
                            continue;
                        }
                        $xmlconfig .= str_repeat("\t", $indent);
                        if ((is_bool($cval) && $cval == true) || ($cval === "")) {
                            $xmlconfig .= "<$ent/>\n";
                        } elseif ((substr($ent, 0, 5) == "descr")
                          || (substr($ent, 0, 6) == "detail")
                          || (substr($ent, 0, 12) == "login_banner")
                          || (substr($ent, 0, 9) == "ldap_attr")
                          || (substr($ent, 0, 9) == "ldap_bind")
                          || (substr($ent, 0, 11) == "ldap_basedn")
                          || (substr($ent, 0, 18) == "ldap_authcn")
                          || (substr($ent, 0, 19) == "ldap_extended_query")) {
                            $xmlconfig .= "<$ent><![CDATA[" . htmlentities($cval) . "]]></$ent>\n";
                        } else {
                            $xmlconfig .= "<$ent>" . htmlentities($cval) . "</$ent>\n";
                        }
                    }
                }
            } elseif (empty($val)) {
                $xmlconfig .= str_repeat("\t", $indent);
                $xmlconfig .= "<$ent/>\n";
            } else {
                /* it's an array */
                $xmlconfig .= str_repeat("\t", $indent);
                $xmlconfig .= "<$ent>\n";
                $xmlconfig .= dump_xml_config_sub($val, $indent + 1);
                $xmlconfig .= str_repeat("\t", $indent);
                $xmlconfig .= "</$ent>\n";
            }
          } else {
            if ((is_bool($val) && ($val == true)) || ($val === "")) {
                $xmlconfig .= str_repeat("\t", $indent);
                $xmlconfig .= "<$ent/>\n";
            } elseif (!is_bool($val)) {
                $xmlconfig .= str_repeat("\t", $indent);
                if ((substr($ent, 0, 5) == "descr")
                  || (substr($ent, 0, 6) == "detail")
                  || (substr($ent, 0, 12) == "login_banner")
                  || (substr($ent, 0, 9) == "ldap_attr")
                  || (substr($ent, 0, 9) == "ldap_bind")
                  || (substr($ent, 0, 11) == "ldap_basedn")
                  || (substr($ent, 0, 18) == "ldap_authcn")
                  || (substr($ent, 0, 19) == "ldap_extended_query")) {
                    $xmlconfig .= "<$ent><![CDATA[" . htmlentities($val) . "]]></$ent>\n";
                } else {
                    $xmlconfig .= "<$ent>" . htmlentities($val) . "</$ent>\n";
                }
            }
        }
    }
Ad Schellevis's avatar
Ad Schellevis committed
267

268
    return $xmlconfig;
Ad Schellevis's avatar
Ad Schellevis committed
269 270
}

271 272 273 274 275 276
function dump_xml_config($arr, $rootobj)
{
    global $listtags;
    $listtags = listtags();
    if (isset($GLOBALS['custom_listtags'])) {
        foreach($GLOBALS['custom_listtags'] as $tag) {
Ad Schellevis's avatar
Ad Schellevis committed
277 278
            $listtags[$tag] = $tag;
        }
279 280
    }
    return dump_xml_config_raw($arr, $rootobj);
Ad Schellevis's avatar
Ad Schellevis committed
281 282
}

283 284 285 286 287 288 289
function dump_xml_config_raw($arr, $rootobj)
{
    $xmlconfig = "<?xml version=\"1.0\"?" . ">\n";
    $xmlconfig .= "<$rootobj>\n";
    $xmlconfig .= dump_xml_config_sub($arr, 1);
    $xmlconfig .= "</$rootobj>\n";
    return $xmlconfig;
Ad Schellevis's avatar
Ad Schellevis committed
290 291
}
?>