*/ if(!defined('DOKU_INC')) die(); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'admin.php'); class admin_plugin_iptrust extends DokuWiki_Admin_Plugin { /** * Return some info */ function getInfo() { return array( 'author' => 'Andriy Lesyuk', 'email' => 'andriy.lesyuk@softjourn.com', 'date' => '2009-04-09', 'name' => 'IPTrust Admin Plugin', 'desc' => 'Lets admin to specify which IPs to trust.' ); } /** * This functionality should be available only ot administrator */ function forAdminOnly() { return false; } /** * Handles user request */ function handle() { if (isset($_REQUEST['ip']) && ($_REQUEST['ip'] != '')) { if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $_REQUEST['ip']) && (ip2long($_REQUEST['ip']) !== false)) { $ip = long2ip(ip2long($_REQUEST['ip'])); $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES); if ($ips && (sizeof($ips) > 0)) { if (in_array($ip."\n", $ips)) { msg($this->getLang('already'), -1); return; } } io_saveFile(DOKU_CONF.'iptrust.conf', $ip."\n", true); } else { msg($this->getLang('invalid_ip'), -1); } } elseif (isset($_REQUEST['delete']) && is_array($_REQUEST['delete']) && (sizeof($_REQUEST['delete']) > 0)) { $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES); if ($ips && (sizeof($ips) > 0)) { $count = sizeof($ips); foreach ($_REQUEST['delete'] as $ip => $dummy) { if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip) && (ip2long($ip) !== false)) { $ip = long2ip(ip2long($ip)); $i = array_search($ip."\n", $ips); if ($i !== false) { array_splice($ips, $i, 1); } } else { msg($this->getLang('invalid_ip'), -1); break; } } if (sizeof($ips) < $count) { io_saveFile(DOKU_CONF.'iptrust.conf', join('', $ips)); } } else { msg($this->getLang('failed'), -1); } } elseif (isset($_REQUEST['add']) && is_array($_REQUEST['add']) && (sizeof($_REQUEST['add']) > 0)) { $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES); foreach ($_REQUEST['add'] as $ip => $dummy) { if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip) && (ip2long($ip) !== false)) { $ip = long2ip(ip2long($ip)); if ($ips && (sizeof($ips) > 0)) { if (in_array($ip."\n", $ips)) { msg($this->getLang('already'), -1); return; } } io_saveFile(DOKU_CONF.'iptrust.conf', $ip."\n", true); } } } elseif (isset($_REQUEST['clear'])) { if (file_exists($conf['cachedir'].'/iptrust')) { @unlink($conf['cachedir'].'/iptrust'); } } } /** * Shows edit form */ function html() { global $conf; print $this->locale_xhtml('intro'); print $this->locale_xhtml('list'); ptln("
"); ptln("
"); formSecurityToken(); $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES); if ($ips && (sizeof($ips) > 0)) { usort($ips, array($this, 'sort_ip')); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); foreach ($ips as $ip) { $ip = rtrim($ip); ptln(""); ptln(""); ptln(""); ptln(""); } ptln(""); ptln("
".$this->getLang('ip_addr')."".$this->getLang('delete')."
".rtrim($ip).""); ptln("getLang('delete')."\" class=\"button\">"); ptln("
"); } else { ptln("
".$this->getLang('noips')."
"); } ptln("
"); ptln("
"); print $this->locale_xhtml('add'); ptln("
"); ptln("
"); formSecurityToken(); ptln(""); ptln(""); ptln("getLang('add')."\" class=\"button\">"); ptln("
"); ptln("
"); if ($this->getConf('log_networks')) { print $this->locale_xhtml('access'); ptln("
"); ptln("
"); formSecurityToken(); $logins = @file($conf['cachedir'].'/iptrust', FILE_SKIP_EMPTY_LINES); if ($logins && (sizeof($logins) > 0)) { usort($logins, array($this, 'sort_login')); $count = sizeof($logins); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); for ($i = 0; $i < sizeof($logins);) { list($user, $ip, $date) = explode("\t", $logins[$i]); if ($ips && in_array($ip."\n", $ips)) { array_splice($logins, $i, 1); } else { ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); ptln(""); $i++; } } ptln(""); ptln("
".$this->getLang('ip_addr')."".$this->getLang('user')."".$this->getLang('date')."".$this->getLang('add')."
".$ip."".$user."".strftime($conf['dformat'], $date).""); ptln("getLang('add')."\" class=\"button\">"); ptln("
"); ptln("getLang('clear')."\" class=\"button\">"); if (sizeof($logins) < $count) { io_saveFile($conf['cachedir'].'/iptrust', join('', $logins)); } } ptln("
"); ptln("
"); } else { if (file_exists($conf['cachedir'].'/iptrust')) { @unlink($conf['cachedir'].'/iptrust'); } } } /** * Sorts IP addresses array */ function sort_ip($a, $b) { return (ip2long($a) - ip2long($b)); } /** * Sorts logins array */ function sort_login($a, $b) { list($auser, $aip, $adate) = explode("\t", $a); list($buser, $bip, $bdate) = explode("\t", $b); return ($bdate - $adate); } }