1<?php
2/**
3 * IPGroup Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Sascha Bendix <sascha.bendix@localroot.de>
7 * @author     Marcel Pennewiss <opensource@pennewiss.de>
8 * @author     Peter Grosse <pegro@fem-net.de>
9 * @author     Jonas Licht <jonas.licht@fem.tu-ilmenau.de>
10 */
11
12if(!defined('DOKU_INC')) die();
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
14
15require_once(DOKU_PLUGIN.'admin.php');
16
17class admin_plugin_ipgroup extends DokuWiki_Admin_Plugin {
18
19    /**
20     * This functionality should be available only to administrator
21     */
22    function forAdminOnly() {
23        return true;
24    }
25
26    /**
27     * Handles user request
28     */
29    function handle() {
30        if (isset($_REQUEST['network']) && ($_REQUEST['network'] != '')
31	    && isset($_REQUEST['group']) && ($_REQUEST['group'] != '')) {
32            // network and group should be added to the list of trusted networks
33            // check input
34	    $config_row = $_REQUEST['network'].';'.$_REQUEST['group']."\n";
35            $slash_pos = strpos($_REQUEST['network'],'/');
36            if (($slash_pos) && (filter_var(substr($_REQUEST['network'],0,$slash_pos),FILTER_VALIDATE_IP))) {
37                $filecontent = @file(DOKU_CONF.'ipgroup.conf', FILE_SKIP_EMPTY_LINES);
38                if ($filecontent && (sizeof($filecontent) > 0)) {
39                    if (in_array($config_row, $filecontent)) {
40                        msg($this->getLang('already'), -1);
41                        return;
42                    }
43                }
44                io_saveFile(DOKU_CONF.'ipgroup.conf', $config_row, true);
45            } else {
46                msg($this->getLang('invalid_ip'), -1);
47            }
48        } elseif (isset($_REQUEST['delete']) && is_array($_REQUEST['delete']) && (sizeof($_REQUEST['delete']) > 0)) {
49            // delete network/group-mapping from the list
50	    if (!io_deleteFromFile(DOKU_CONF.'ipgroup.conf', key($_REQUEST['delete'])."\n")) {
51	    	msg($this->getLang('failed'), -1);
52	    }
53        } elseif (isset($_REQUEST['clear'])) {
54            if (file_exists($conf['cachedir'].'/ipgroup')) {
55                @unlink($conf['cachedir'].'/ipgroup');
56            }
57        }
58    }
59
60    /**
61     * Shows edit form
62     */
63    function html() {
64        global $conf;
65
66        print $this->locale_xhtml('intro');
67
68        print $this->locale_xhtml('list');
69        ptln("<div class=\"level2\">");
70        ptln("<form action=\"\" method=\"post\">");
71        formSecurityToken();
72        $networks = @file(DOKU_CONF.'ipgroup.conf', FILE_SKIP_EMPTY_LINES);
73        if ($networks && (sizeof($networks) > 0)) {
74            ptln("<table class=\"inline\">");
75            ptln("<colgroup width=\"250\"></colgroup>");
76            ptln("<colgroup width=\"150\"></colgroup>");
77            ptln("<thead>");
78            ptln("<tr>");
79            ptln("<th>".$this->getLang('network')."</th>");
80            ptln("<th>".$this->getLang('group')."</th>");
81            ptln("<th>".$this->getLang('delete')."</th>");
82            ptln("</tr>");
83            ptln("</thead>");
84            ptln("<tbody>");
85            foreach ($networks as $network) {
86                $network = rtrim($network);
87		list($network, $group) = explode(';', $network);
88                ptln("<tr>");
89                ptln("<td>".rtrim($network)."</td>");
90                ptln("<td>".rtrim($group)."</td>");
91                ptln("<td>");
92                ptln("<input type=\"submit\" name=\"delete[".$network.";".$group."]\" value=\"".$this->getLang('delete')."\" class=\"button\">");
93                ptln("</td>");
94                ptln("</tr>");
95            }
96            ptln("</tbody>");
97            ptln("</table>");
98        } else {
99            ptln("<div class=\"fn\">".$this->getLang('noips')."</div>");
100        }
101        ptln("</form>");
102        ptln("</div>");
103
104        print $this->locale_xhtml('add');
105        ptln("<div class=\"level2\">");
106        ptln("<form action=\"\" method=\"post\">");
107        formSecurityToken();
108        ptln("<label for=\"ip__add\">".$this->getLang('network').":</label>");
109        ptln("<input id=\"ip__add\" name=\"network\" type=\"text\" maxlength=\"44\" class=\"edit\">");
110        ptln("<label for=\"group__add\">".$this->getLang('group').":</label>");
111        ptln("<input id=\"group__add\" name=\"group\" type=\"text\" maxlength=\"64\" class=\"edit\">");
112        ptln("<input type=\"submit\" value=\"".$this->getLang('add')."\" class=\"button\">");
113        ptln("</form>");
114        ptln("</div>");
115
116        if (file_exists($conf['cachedir'].'/ipgroup')) {
117            @unlink($conf['cachedir'].'/ipgroup');
118        }
119    }
120}
121