1<?php 2/** 3 * IPTrust Plugin 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andriy Lesyuk <andriy.lesyuk@softjourn.com> 7 */ 8 9if(!defined('DOKU_INC')) die(); 10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 11 12require_once(DOKU_PLUGIN.'admin.php'); 13 14class admin_plugin_iptrust2 extends DokuWiki_Admin_Plugin { 15 16 17 /** 18 * This functionality should be available only ot administrator 19 */ 20 function forAdminOnly() { 21 return false; 22 } 23 24 /** 25 * Handles user request 26 */ 27 function handle() { 28 if (isset($_REQUEST['ip']) && ($_REQUEST['ip'] != '')) { 29 if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_REQUEST['ip']) && 30 (ip2long($_REQUEST['ip']) !== false)) { 31 $ip = long2ip(ip2long($_REQUEST['ip'])); 32 $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES); 33 if ($ips && (sizeof($ips) > 0)) { 34 if (in_array($ip."\n", $ips)) { 35 msg($this->getLang('already'), -1); 36 return; 37 } 38 } 39 io_saveFile(DOKU_CONF.'iptrust.conf', $ip."\n", true); 40 } else { 41 msg($this->getLang('invalid_ip'), -1); 42 } 43 } elseif (isset($_REQUEST['delete']) && is_array($_REQUEST['delete']) && (sizeof($_REQUEST['delete']) > 0)) { 44 $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES); 45 if ($ips && (sizeof($ips) > 0)) { 46 $count = sizeof($ips); 47 foreach ($_REQUEST['delete'] as $ip => $dummy) { 48 if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip) && 49 (ip2long($ip) !== false)) { 50 $ip = long2ip(ip2long($ip)); 51 $i = array_search($ip."\n", $ips); 52 if ($i !== false) { 53 array_splice($ips, $i, 1); 54 } 55 } else { 56 msg($this->getLang('invalid_ip'), -1); 57 break; 58 } 59 } 60 if (sizeof($ips) < $count) { 61 io_saveFile(DOKU_CONF.'iptrust.conf', join('', $ips)); 62 } 63 } else { 64 msg($this->getLang('failed'), -1); 65 } 66 } elseif (isset($_REQUEST['add']) && is_array($_REQUEST['add']) && (sizeof($_REQUEST['add']) > 0)) { 67 $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES); 68 foreach ($_REQUEST['add'] as $ip => $dummy) { 69 if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip) && 70 (ip2long($ip) !== false)) { 71 $ip = long2ip(ip2long($ip)); 72 if ($ips && (sizeof($ips) > 0)) { 73 if (in_array($ip."\n", $ips)) { 74 msg($this->getLang('already'), -1); 75 return; 76 } 77 } 78 io_saveFile(DOKU_CONF.'iptrust.conf', $ip."\n", true); 79 } 80 } 81 } elseif (isset($_REQUEST['clear'])) { 82 if (file_exists($conf['cachedir'].'/iptrust')) { 83 @unlink($conf['cachedir'].'/iptrust'); 84 } 85 } 86 } 87 88 /** 89 * Shows edit form 90 */ 91 function html() { 92 global $conf; 93 94 print $this->locale_xhtml('intro'); 95 96 print $this->locale_xhtml('list'); 97 ptln("<div class=\"level2\">"); 98 ptln("<form action=\"\" method=\"post\">"); 99 formSecurityToken(); 100 $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES); 101 if ($ips && (sizeof($ips) > 0)) { 102 usort($ips, array($this, 'sort_ip')); 103 ptln("<table class=\"inline\">"); 104 ptln("<colgroup width=\"250\"></colgroup>"); 105 ptln("<thead>"); 106 ptln("<tr>"); 107 ptln("<th>".$this->getLang('ip_addr')."</th>"); 108 ptln("<th>".$this->getLang('delete')."</th>"); 109 ptln("</tr>"); 110 ptln("</thead>"); 111 ptln("<tbody>"); 112 foreach ($ips as $ip) { 113 $ip = rtrim($ip); 114 ptln("<tr>"); 115 ptln("<td>".rtrim($ip)."</td>"); 116 ptln("<td>"); 117 ptln("<input type=\"submit\" name=\"delete[".$ip."]\" value=\"".$this->getLang('delete')."\" class=\"button\">"); 118 ptln("</td>"); 119 ptln("</tr>"); 120 } 121 ptln("</tbody>"); 122 ptln("</table>"); 123 } else { 124 ptln("<div class=\"fn\">".$this->getLang('noips')."</div>"); 125 } 126 ptln("</form>"); 127 ptln("</div>"); 128 129 print $this->locale_xhtml('add'); 130 ptln("<div class=\"level2\">"); 131 ptln("<form action=\"\" method=\"post\">"); 132 formSecurityToken(); 133 ptln("<label for=\"ip__add\">".$this->getLang('ip_addr').":</label>"); 134 ptln("<input id=\"ip__add\" name=\"ip\" type=\"text\" maxlength=\"32\" class=\"edit\">"); 135 ptln("<input type=\"submit\" value=\"".$this->getLang('add')."\" class=\"button\">"); 136 ptln("</form>"); 137 ptln("</div>"); 138 139 if ($this->getConf('log_networks')) { 140 print $this->locale_xhtml('access'); 141 ptln("<div class=\"level2\">"); 142 ptln("<form action=\"\" method=\"post\">"); 143 formSecurityToken(); 144 $logins = @file($conf['cachedir'].'/iptrust', FILE_SKIP_EMPTY_LINES); 145 if ($logins && (sizeof($logins) > 0)) { 146 usort($logins, array($this, 'sort_login')); 147 $count = sizeof($logins); 148 ptln("<table class=\"inline\">"); 149 ptln("<thead>"); 150 ptln("<tr>"); 151 ptln("<th>".$this->getLang('ip_addr')."</th>"); 152 ptln("<th>".$this->getLang('user')."</th>"); 153 ptln("<th>".$this->getLang('date')."</th>"); 154 ptln("<th>".$this->getLang('add')."</th>"); 155 ptln("</tr>"); 156 ptln("</thead>"); 157 ptln("<tbody>"); 158 for ($i = 0; $i < sizeof($logins);) { 159 list($user, $ip, $date) = explode("\t", $logins[$i]); 160 if ($ips && in_array($ip."\n", $ips)) { 161 array_splice($logins, $i, 1); 162 } else { 163 ptln("<tr>"); 164 ptln("<td>".$ip."</td>"); 165 ptln("<td>".$user."</td>"); 166 ptln("<td>".strftime($conf['dformat'], $date)."</td>"); 167 ptln("<td>"); 168 ptln("<input type=\"submit\" name=\"add[".$ip."]\" value=\"".$this->getLang('add')."\" class=\"button\">"); 169 ptln("</td>"); 170 ptln("</tr>"); 171 $i++; 172 } 173 } 174 ptln("</tbody>"); 175 ptln("</table>"); 176 ptln("<input type=\"submit\" name=\"clear\" value=\"".$this->getLang('clear')."\" class=\"button\">"); 177 if (sizeof($logins) < $count) { 178 io_saveFile($conf['cachedir'].'/iptrust', join('', $logins)); 179 } 180 } 181 ptln("</form>"); 182 ptln("</div>"); 183 } else { 184 if (file_exists($conf['cachedir'].'/iptrust')) { 185 @unlink($conf['cachedir'].'/iptrust'); 186 } 187 } 188 } 189 190 /** 191 * Sorts IP addresses array 192 */ 193 function sort_ip($a, $b) { 194 return (ip2long($a) - ip2long($b)); 195 } 196 197 /** 198 * Sorts logins array 199 */ 200 function sort_login($a, $b) { 201 list($auser, $aip, $adate) = explode("\t", $a); 202 list($buser, $bip, $bdate) = explode("\t", $b); 203 return ($bdate - $adate); 204 } 205 206} 207