1<?php
2/**
3 * DokuWiki Plugin anonip (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12require_once DOKU_PLUGIN.'action.php';
13
14class action_plugin_anonip extends DokuWiki_Action_Plugin {
15
16    public function register(Doku_Event_Handler $controller) {
17         $controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'handle_dokuwiki_started');
18    }
19
20    public function handle_dokuwiki_started(Doku_Event &$event, $param) {
21        // is the incoming IP already anonymized by the webserver?
22        if($_SERVER['REMOTE_ADDR'] == '127.0.0.1'){
23            // try to use the session ID as identifier
24            $ses = session_id();
25            if(!$ses){
26                // no session running, randomize
27                $ses = mt_rand();
28            }
29            $uid = md5($ses);
30        }else{
31            // Use IP + Browser Data
32            $uid = md5(auth_browseruid());
33        }
34
35        // build pseudo IPv6 (local)
36        $ip = 'fe80:'.substr($uid,0,4).
37                  ':'.substr($uid,4,4).
38                  ':'.substr($uid,8,4).
39                  ':'.substr($uid,12,4).
40                  ':'.substr($uid,16,4).
41                  ':'.substr($uid,20,4).
42                  ':'.substr($uid,24,4);
43
44        // reset server variables
45        $_SERVER['REMOTE_ADDR'] = $ip;
46        if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) unset($_SERVER['HTTP_X_FORWARDED_FOR']);
47        if(isset($_SERVER['HTTP_X_REAL_IP'])) unset($_SERVER['HTTP_X_REAL_IP']);
48    }
49}
50
51// vim:ts=4:sw=4:et:
52