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