File: xmllib.php

package info (click to toggle)
ipplan 4.92a-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,256 kB
  • ctags: 11,121
  • sloc: php: 44,277; sh: 387; perl: 257; xml: 97; makefile: 57; sql: 32
file content (172 lines) | stat: -rwxr-xr-x 5,223 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
172
<?php

// IPplan v4.92a
// Aug 24, 2001
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

// parse an xml document and return an array with all the attributes 
// and values for a given tag. Two levels of nesting are supported in
// the given tag
class xml  {
    var $parser;
    var $blocktag="";
    var $gottag=0;
    var $index=-1;
    var $result=array();

    var $depth=0;

    function xml($blocktag) {
        if(extension_loaded("xml")) {
            $this->blocktag=$blocktag;
            $this->parser = xml_parser_create("");
            xml_set_object($this->parser,$this);
            xml_set_element_handler($this->parser,"tag_open","tag_close");
            //xml_set_character_data_handler($this->parser,"cdata");
        }
    }

    function parse($data) { 
        xml_parse($this->parser,$data);
        return $this->result;
    }

    function tag_open($parser,$tag,$attributes) { 
        if ($tag==$this->blocktag) {
            $this->gottag=1;
            $this->index++;
            return;
        }
        if ($this->gottag) {
            // only increase depth if inside blocktag
            $this->depth++;
            if ($this->depth==2) {
                $this->result[$this->index][$tag][]=$attributes;
            }
            else {
                /* subsequent values appear as sub arrays

  ["ADDRESS"]=>
  array(3) {
    ["ADDR"]=>
    string(11) "192.168.0.1"
    ["ADDRTYPE"]=>
    string(4) "ipv4"
    [0]=>
    array(3) {
      ["ADDR"]=>
      string(17) "00:09:5B:DE:A8:6A"
      ["ADDRTYPE"]=>
      string(3) "mac"
      ["VENDOR"]=>
      string(7) "Netgear"
    }
  }
                */
                if (isset($this->result[$this->index][$tag])) {
                    $this->result[$this->index][$tag][]=$attributes;
                }
                else {
                    $this->result[$this->index][$tag]=$attributes;
                }
            }
            //var_dump($parser,$tag,$attributes); 
        }
    }

    function cdata($parser,$cdata) { 
        //var_dump($parser,$cdata);
    }

    function tag_close($parser,$tag) { 
        if ($tag==$this->blocktag) {
            $this->gottag=0;
            return;
            //var_dump($parser,$tag); 
        }
        // only decrase depth if inside blocktag
        if ($this->gottag) {
            $this->depth--;
        }
    }

} // end of class xml

// nmap parser always returns arrays for tags of form
// [tagname][0...x][element]
// array index will mostly be zero if one as most results
// return 1 tag
class xmlnmap extends xml {

    function tag_open($parser,$tag,$attributes) { 
        if ($tag==$this->blocktag) {
            $this->gottag=1;
            $this->index++;
            return;
        }
        if ($this->gottag) {
            // only increase depth if inside blocktag
            $this->depth++;
            // dupes like this would have been overwritten! so do not overwrite
            // by always creating an array
            // <address addr="192.168.0.1" addrtype="ipv4" />
            // <address addr="00:09:5B:DE:A8:6A" addrtype="mac" vendor="Netgear" />
            $this->result[$this->index][$tag][]=$attributes;
            //var_dump($parser,$tag,$attributes); 
        }
    }

}

// a simple class to do template replacements on a file. syntax is
// similar to XSL, but only handles <xsl:value-of select="variable"/>
// replacements currently - all other tags are stripped out. this is 
// to not require the XSLT php module
class myTemplate {
   var $template;

   // reads entire file into a variable
   function get($filename) {
      $data=@file($filename);
      if (empty($data)) {   // error
         return FALSE;
      }
      else {
         $this->template=implode("",$data);
         return TRUE;
      }
   }

   // replaces in template for each instance of the fields array
   // fields is indexed by reference
   function process($fields) {
       foreach($fields as $key=>$value) {
          $this->template=str_replace("<xsl:value-of select=\"$key\"/>",
                                      $value, $this->template);
       }
       // strip out other tags that don't end in newlines
       $this->template=preg_replace("'<[\/\!]*?[^<>]*?>'si", "", 
                                    $this->template);
       // strip out all remaining tags that end in newline
       $this->template=preg_replace("'<[\/\!]*?[^<>]*?>\n'si", "", 
                                    $this->template);

       return $this->template;
   }
} // end of class myTemplate

?>