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.'action.php');
13
14class action_plugin_iptrust extends DokuWiki_Action_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 Action Plugin',
25            'desc'   => 'Allows read-only access only to given set of IPs.'
26        );
27    }
28
29    /**
30     * Register event handlers
31     */
32    function register(&$controller) {
33        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_act_preprocess', array());
34    }
35
36    /**
37     * Check if content should be shown
38     */
39    function handle_act_preprocess(&$event, $param) {
40        global $conf;
41        if (!isset($_SERVER['REMOTE_USER'])) {
42            if (!in_array($event->data, array('login', 'register', 'resendpwd'))) {
43                $ip = clientIP(true);
44                $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES);
45                if (!$ips || !in_array($ip."\n", $ips)) {
46                    $event->data = 'login';
47                }
48            }
49        } else {
50            if ($event->data == 'login') {
51                $nets = $this->getConf('log_networks');
52                if ($nets) {
53                    $ip = clientIP(true);
54                    $ips = @file(DOKU_CONF.'iptrust.conf', FILE_SKIP_EMPTY_LINES);
55                    if (!$ips || !in_array($ip."\n", $ips)) {
56                        $nets = preg_split('/, */', $nets);
57                        foreach ($nets as $net) {
58                            if (strpos($ip, $net) === 0) {
59                                $i = 0;
60                                $logins = @file($conf['cachedir'].'/iptrust', FILE_SKIP_EMPTY_LINES);
61                                if ($logins) {
62                                    for ($i = 0; $i < sizeof($logins); $i++) {
63                                        list($login, $host, $date) = explode("\t", $logins[$i]);
64                                        if ($ip == $host) {
65                                            break;
66                                        }
67                                    }
68                                } else {
69                                    $logins = array();
70                                }
71                                $logins[$i] = $_SERVER['REMOTE_USER']."\t".$ip."\t".time()."\n";
72                                io_saveFile($conf['cachedir'].'/iptrust', join('', $logins));
73                                break;
74                            }
75                        }
76                    }
77                }
78            }
79        }
80    }
81
82}
83