1<?php
2/**
3 * DokuWiki Plugin ipmap (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Wheeler <doku@michael-wheeler.org>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15require_once DOKU_PLUGIN.'syntax.php';
16
17class syntax_plugin_ipmap_rendertables extends DokuWiki_Syntax_Plugin {
18    function getType(){ return 'formatting'; }
19    function getAllowedTypes() { return array('formatting','container', 'substition', 'disabled'); }
20    function getSort(){ return 158; }
21
22    function connectTo($mode) {
23      //  $this->Lexer->addSpecialPattern('<ipmap>',$mode,'plugin_ipmap_rendertables');
24      /*  $this->Lexer->addEntryPattern('<ipmap.*?>(?=.*?</ipmap>)',$mode,'plugin_ipmap_rendertables'); */
25        $this->Lexer->addEntryPattern('<ipmap.*>.*',$mode,'plugin_ipmap_rendertables');
26    }
27
28    function postConnect() {
29        $this->Lexer->addExitPattern('</ipmap>','plugin_ipmap_rendertables');
30    }
31
32    function handle($match, $state, $pos, Doku_Handler $handler){
33        switch ($state) {
34          case DOKU_LEXER_ENTER :
35                 list($ip, $net,$subnet) = preg_split("/\//u", substr($match, 7, -1), 3);
36                 return array($state, array($ip, $net, $subnet,strip_tags($match)));
37          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
38          case DOKU_LEXER_EXIT :       return array($state, '');
39        }
40        return array();
41    }
42    function render($mode, Doku_Renderer $renderer, $data) {
43    global $in;
44        if($mode == 'xhtml'){
45            list($state,$match) = $data;
46            switch ($state) {
47              case DOKU_LEXER_ENTER :
48                list($ip, $net, $subnet,$data) = $match;
49                $unused = array();
50                $renderer->doc .= p_render('xhtml',p_get_instructions($this->_maketables($ip, $net, $subnet, $data)), $unused);
51                break;
52              case DOKU_LEXER_UNMATCHED :   break;
53              case DOKU_LEXER_EXIT :        break;
54            }
55            return true;
56        }
57        return false;
58    }
59    function _maketables($ip, $net, $subnet, $data){
60 $subnet = explode(">",$subnet);
61$subnet = $subnet[0];
62        $x = array(
63        9 => 8,
64        8 => 8,
65        7 => 8,
66        6 => 8,
67        5 => 8,
68        4 => 4,
69        3 => 4,
70        2 => 2
71        );
72        $y = array(
73        9 => 64,
74        8 => 32,
75        7 => 16,
76        6 => 8,
77        5 => 4,
78        4 => 4,
79        3 => 2,
80        2 => 2
81        );
82        $subnetsr=array();
83        $subnets = explode("*",$data);
84        foreach ($subnets as &$value) {
85        $subnetu = explode("-",$value);
86        $subneta = explode("/",trim($subnetu[0]));
87        $subnetsr[$subneta[0]] = array("Mask" => $subneta[1], "Desc" => trim($subnetu[1]));
88        }
89
90
91        $diff =  $subnet - $net;
92        $rightbits = 32 - $subnet;
93        for($z = 0; $z < ($rightbits); ++$z) {
94            $rightb .= "1";
95        }
96        $dright = bindec($rightb);
97        $width=$x[$diff];
98        $height=$y[$diff];
99        $dip = ip2long($ip);
100        for($z = 0; $z < ($width); ++$z) {
101            $endrow .= "^";
102        }
103$first = 1;
104/* Are we the first cell in a row - dodgy.  */
105        $output = "^  [[..:main|UP]]  " . $endrow."\n";
106        for($i = 0; $i < ($width*$height); ++$i) {
107              if (($dip + $i * ($dright + 1) > $lasts + $drights ) or ($lasts + $drights == 0)) {
108              $ipout = long2ip($dip + $i * ($dright + 1));
109              $desc = $subnetsr[$ipout]['Desc'];
110              $mask = $subnetsr[$ipout]['Mask'];
111              if ($mask){
112                   $rightbs = "";
113                   $rightbitss = 32 - $mask;
114                   for($z = 0; $z < ($rightbitss); ++$z) {
115                        $rightbs .= "1";
116                   }
117                   $drights = bindec($rightbs);
118                   $sout = $mask;
119                   $lasts = $dip + $i * ($dright + 1);
120                   $lastsb =  $ipout;
121              }  else {
122                   $sout = "$subnet";
123              }
124              }
125
126
127
128              if ($desc){
129                  if ((long2ip($dip + $i * ($dright + 1)) == $ipout) or ($first == 1)){
130                  $output .= "^  [[.:$ipout\_$sout:main|$ipout/$sout]]  \\\\  " . "$desc" . "    ";
131                  $first = 0;
132                  } else {
133                          $output .= "|";
134                  }
135                  } else {
136                    if ((long2ip($dip + $i * ($dright + 1)) == $ipout) or ($first == 1)){
137                   $output .= "|  $ipout/$sout    ";
138                   $first = 0;
139                   } else {
140                           $output .= "|";
141                   }
142
143                  }
144              if (($i + 1)% $width == 0){
145                   $output .= "|\n";
146                   $first = 1;
147              }
148        }
149        return($output);
150    }
151}
152// vim:ts=4:sw=4:et:enc=utf-8:
153