1<?php
2/**
3 * DokuWiki Plugin yourip (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Artem Sidorenko <artem@2realities.com>
7 * 2019-09 target urls changed by Hella Breitkopf, https://www.unixwitch.de
8 * 2022-08 enable localization by Hella Breitkopf, https://www.unixwitch.de
9 */
10
11// must be run within Dokuwiki
12if (!defined('DOKU_INC')) die();
13
14if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
15if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
16if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17
18require_once DOKU_PLUGIN.'syntax.php';
19
20class syntax_plugin_yourip extends DokuWiki_Syntax_Plugin {
21
22    public function getType() {
23        return 'substition';
24    }
25
26    public function getPType() {
27        return 'block';
28    }
29
30    public function getSort() {
31        return 99;
32    }
33
34    public function connectTo($mode) {
35        $this->Lexer->addSpecialPattern('~~YOURIP_.*?~~',$mode,'plugin_yourip');
36    }
37
38    public function handle($match, $state, $pos, Doku_Handler $handler){
39        $data = array("yourip_type"=>"");
40        $match = substr($match, 9, -2);
41
42        if ($match == 'BOX')
43            $data['yourip_type'] = 'box';
44        elseif ($match == 'LINE')
45            $data['yourip_type'] = 'line';
46        elseif ($match == 'IPONLY')
47            $data['yourip_type'] = 'iponlyline';
48
49        return $data;
50    }
51
52    public function render($mode, Doku_Renderer $renderer, $data) {
53        if($mode != 'xhtml') return false;
54
55        $ip = getenv ("REMOTE_ADDR");
56        $type=false;
57        if (substr_count($ip,":") > 1 && substr_count($ip,".") == 0)
58            $type='ipv6';
59        else
60            $type='ipv4';
61
62        #show the things, here info in the box
63        $text=false;
64        if($data['yourip_type']=="box"){
65            $text="\n<div id='yourip' class='$type'>";
66        if($type=='ipv6') {
67            $text .= $this->getLang('you_use_v6'); // "You've got IPv6"
68            $text .= "<br/> ";
69            $text .= sprintf( $this->getLang('ipv6_from'), $ip); // "IPv6 connection from $ip"
70        }
71        else
72        {
73            $text .= $this->getLang('you_use_v4'); // "You use old fashioned IPv4"
74            $text .= "<br/>";
75            $text .= sprintf( $this->getLang('ipv4_from'), $ip); // "IPv4 connection from $ip"
76        }
77        $text .="</div>\n";
78        $renderer->doc .= $text;
79        return true;
80
81        #info as line
82       }elseif($data['yourip_type']=="line"){
83           $text="<p id='yourip' class='$type'>";
84           if($type=='ipv6')
85               $text .= sprintf( $this->getLang('ipv6_from'), $ip); // "IPv6 connection from $ip"
86           else
87               $text .= sprintf( $this->getLang('ipv4_from'), $ip); // "IPv4 connection from $ip"
88           $text .="</p>\n";
89           $renderer->doc .= $text;
90           return true;
91
92        #info without text
93        }elseif($data['yourip_type']=="iponlyline"){
94                $text = "<p id='yourip' class='$type'>";
95                $text .= "$ip" ;
96                $text .= "</p>\n" ;
97            $renderer->doc .= $text;
98            return true;
99        }
100        else return false;
101
102    } // end function render
103
104} // end class
105
106// vim:ts=4:sw=4:et:
107