1<?PHP
2
3/**
4 * Hiding IP address plugin
5 * Avoid IP addresses shown to public.
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     HokkaidoPerson <dosankomali@yahoo.co.jp>
9 */
10
11if(!defined('DOKU_INC')) die();
12
13
14class action_plugin_hidingip extends DokuWiki_Action_Plugin {
15
16    /**
17     * Run this plugin in:
18     * 1. Recent Changes
19     * 2. Old Revisions
20     * 3. Last Modified
21     * 4. Page Locking
22     * 5. Showing Diff
23     */
24    public function register(Doku_Event_Handler $controller) {
25        $whentohide = explode(',', $this->getConf('whenToHide'));
26        $whentohide = array_map('trim', $whentohide);
27        $whentohide = array_unique($whentohide);
28        $whentohide = array_filter($whentohide);
29
30        if (array_search('recent', $whentohide) !== FALSE) $controller->register_hook('HTML_RECENTFORM_OUTPUT', 'BEFORE', $this, 'recentform', array());
31        if (array_search('revision', $whentohide) !== FALSE) $controller->register_hook('HTML_REVISIONSFORM_OUTPUT', 'BEFORE', $this, 'revisionform', array());
32        if (array_search('diff', $whentohide) !== FALSE) $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'tplcontent', array());
33        if (array_search('userlink', $whentohide) !== FALSE ) $controller->register_hook('COMMON_USER_LINK', 'BEFORE', $this, 'userlink', array());
34    }
35
36    /**
37     * In recent changes
38     */
39    public function recentform(Doku_Event $event, $param) {
40
41        $display = $this->getLang('notloggedin');
42        $flag = FALSE;
43        $showip = FALSE;
44
45        // Allowed to see IPs?
46        if ($this->getConf('rightToSeeIP') == 'mg' && auth_ismanager()) $showip = TRUE;
47        if ($this->getConf('rightToSeeIP') == 'sp' && auth_isadmin()) $showip = TRUE;
48
49        // Reminder / メモ書き (en, ja)
50        //
51        // When $event->data->_content['(number)']['class'] has 'user', there is an user name or an IP address in $event->data->_content['(the next number)'].
52        // Thus this plugin detects the 'user' texts, and substitues TRUE for $flag if found.
53        // If the $flag is TRUE, this plugin will check whether or not there is an IP in the texts.  If found, it'll be hidden (but admins can view it even with this plugin).
54        //
55        // $event->data->_content['(任意の数字)']['class']に文字列'user'があった場合、$event->data->_content['(その次の数字)']には、ユーザー名もしくはIPアドレスが保持されています。
56        // なので、このプラグインではその'user'文字列を検出し、見付かった場合、$flag変数にTRUEを代入します。
57        // その$flag変数がTRUEの場合に、文字列内にIPアドレスがあるかチェックします。もしあれば、それを非表示にします(但し、管理人はこのプラグインの介入に関わらずIPアドレスを閲覧出来ます)。
58
59        foreach ($event->data->_content as $key => $ref) {
60            if ($flag == TRUE and strpos($ref,'<bdo dir="ltr">') !== FALSE) {
61                if ($showip == TRUE) {
62                    $event->data->_content[$key] = '<bdi>' . $display . '</bdi> <bdo dir="ltr">(' . substr($ref, strlen('<bdo dir="ltr">'), -6) . ')</bdo>';
63                } else {
64                    $event->data->_content[$key] = '<bdi>' . $display . '</bdi>';
65                }
66            }
67
68            $flag = FALSE;
69
70            if (is_array($ref)) {
71                if (array_key_exists('class', $ref)) {
72                    if ($ref['class'] == 'user') $flag = TRUE;
73                }
74            }
75        }
76    }
77
78    /**
79     * In old revisions
80     */
81    public function revisionform(Doku_Event $event, $param) {
82
83        $display = $this->getLang('notloggedin');
84        $flag = FALSE;
85        $showip = FALSE;
86
87        // Allowed to see IPs?
88        if ($this->getConf('rightToSeeIP') == 'mg' && auth_ismanager()) $showip = TRUE;
89        if ($this->getConf('rightToSeeIP') == 'sp' && auth_isadmin()) $showip = TRUE;
90
91        // Reminder / メモ書き (en, ja)
92        //
93        // The function is very similar to that in recent changes.
94        // But, the first item of the old revision is little different from others, so there is some customizing.
95        //
96        // 「最近の更新」とほぼ同じ機能ですが、以前のリビジョンの最初の項目が他の項目と少し異なるようなので、それに合わせたカスタマイズをしています。
97
98        foreach ($event->data->_content as $key => $ref) {
99            if ($flag == TRUE and strpos($ref,'<bdo dir="ltr">') !== FALSE) {
100                if ($showip == TRUE) {
101                    $event->data->_content[$key] = '<bdi>' . $display . '</bdi> <bdo dir="ltr">(' . substr($ref, strlen('<bdo dir="ltr">'), -6) . ')</bdo>';
102                } else {
103                    $event->data->_content[$key] = '<bdi>' . $display . '</bdi>';
104                }
105            } else if ($flag == TRUE and preg_match('/<bdi>(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])<\/bdi>/' , $ref) == 1) {
106               $event->data->_content[$key] = '<bdi>' . $display . '</bdi>';
107            }
108
109            $flag = FALSE;
110
111            if (is_array($ref)) {
112                if (array_key_exists('class', $ref)) {
113                    if ($ref['class'] == 'user') $flag = TRUE;
114                }
115            }
116        }
117    }
118
119    /**
120     * In showing diffs
121     */
122    public function tplcontent(Doku_Event $event, $param) {
123
124        // Keep out if not managing diffs
125        global $INPUT;
126        if ($INPUT->str('do') != 'diff') return;
127
128
129        $display = $this->getLang('notloggedin');
130        $showip = FALSE;
131
132        // Allowed to see IPs?
133        if ($this->getConf('rightToSeeIP') == 'mg' && auth_ismanager()) $showip = TRUE;
134        if ($this->getConf('rightToSeeIP') == 'sp' && auth_isadmin()) $showip = TRUE;
135
136        // Reminder / メモ書き (en, ja)
137        //
138        // The function is similar to the functions above, but it'll modify HTML directly.
139        //
140        // 上2つと似たような機能ですが、ここではHTMLを直接いじっています。
141
142        $ref = $event->data;
143
144        if (strpos($ref,'<span class="user"><bdo dir="ltr">') !== FALSE) {
145            if ($showip == TRUE) {
146                $event->data = preg_replace('/<span class="user"><bdo dir="ltr">((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))<\/bdo>/', '<span class="user"><bdi>' . $display . '</bdi> <bdo dir="ltr">($1)</bdo>', $ref);
147            } else {
148                $event->data = preg_replace('/<span class="user"><bdo dir="ltr">(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])<\/bdo>/', '<span class="user"><bdi>' . $display . '</bdi>', $ref);
149            }
150        }
151    }
152
153    /**
154     * Modifying user datas (Last Modified and Page Locking)
155     */
156    public function userlink(Doku_Event $event, $param) {
157
158        $display = $this->getLang('notloggedin');
159
160        // Reminder / メモ書き (en, ja)
161        //
162        // If $event->data['username'] is likely to be an IP, the plugin will write $event->data['name'].
163        // You can't use the user name like IPs (that'll be accidentally replaced. e.g.: 3.57.2.13 ).
164        //
165        // $event->data['username']がIPアドレスと思われる場合、$event->data['name']を書き込みます。
166        // IPアドレスっぽいユーザー名だった場合、ログイン中であってもうっかり置き換えられてしまいます。
167        //   例:3.57.2.13
168
169
170        if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/' , $event->data['username']) == 1) $event->data['name'] = $display;
171    }
172
173
174}
175