1<?php
2
3if(!defined('DOKU_INC')) die();
4
5class action_plugin_extranet extends DokuWiki_Action_Plugin {
6
7	public function register(Doku_Event_Handler $controller) {
8		if($this->_isclientfromextranet()) {
9			$controller->register_hook('AUTH_LOGIN_CHECK', 'AFTER', $this, '_disableactions');
10			$controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, '_createextranetcache');
11			$controller->register_hook('IO_WIKIPAGE_READ', 'AFTER', $this, '_displayhidemessageifrestricted');
12			$controller->register_hook('MEDIA_SENDFILE', 'BEFORE', $this, '_hidemediasifrestricted');
13		}
14	}
15
16	function _isclientfromextranet() {
17		$client_ip = $_SERVER[$this->getConf('server_ip_key')];
18
19		if(!empty($this->getConf('extranet_ip_list'))) {
20			$restricted_ip_list = explode(',', $this->getConf('extranet_ip_list'));
21			foreach($restricted_ip_list as $restricted_ip) {
22				if($client_ip === $restricted_ip) {
23					return true;
24				}
25			}
26		}
27
28		if(!empty($this->getConf('extranet_ip_regex'))) {
29			preg_match($this->getConf('extranet_ip_regex'), $client_ip, $matches);
30			return !empty($matches[0]);
31		}
32
33		return false;
34	}
35
36	function _iscontentrestrictedfromextranet($content) {
37		return strpos($content, '~~NOEXTRANET~~') !== false;
38	}
39
40	function _isnamespacerestrictedfromextranet() {
41		global $ID;
42
43		if(!empty($this->getConf('hide_regex'))) {
44			preg_match($this->getConf('hide_regex'), $ID, $matches);
45			return !empty($matches[0]);
46		}
47
48		return false;
49	}
50
51	function _ismediarestrictedfromextranet($media) {
52		if(!empty($this->getConf('hide_regex'))) {
53			preg_match($this->getConf('hide_regex'), $media, $matches);
54			return !empty($matches[0]);
55		}
56
57		return false;
58	}
59
60    function _disableactions(Doku_Event &$event, $param) {
61		if(!empty($this->getConf('disable_actions'))) {
62			global $conf;
63			$conf['disableactions'] = (!empty($conf['disableactions'])? $conf['disableactions'].',' : '').$this->getConf('disable_actions');
64		}
65    }
66
67    function _createextranetcache(Doku_Event &$event, $param) {
68		$cache = $event->data;
69        $cache->key .= '#extranet';
70        $cache->cache = getCacheName($cache->key, $cache->ext);
71    }
72
73    function _displayhidemessageifrestricted(Doku_Event &$event, $param) {
74		$isPageRestricted = $this->_iscontentrestrictedfromextranet($event->result)
75			|| $this->_isnamespacerestrictedfromextranet();
76
77		if($isPageRestricted) {
78			$result = '';
79
80			if($this->getConf('preserve_first_title')) {
81				$titlePattern = '/(?:^|\v)(={2,6}.+={2,})(?:\v|$)/';
82				preg_match($titlePattern, $event->result, $matches);
83
84				if(!empty($matches[0])) {
85					$result .= $matches[0]."\r\n";
86				}
87			}
88			$result .= $this->getConf('message_prefix').$this->getLang('hidden_message').$this->getConf('message_suffix');
89
90			$event->result = $result;
91		}
92    }
93
94    function _hidemediasifrestricted(Doku_Event &$event, $param) {
95		$isMediaRestricted = $this->getConf('hide_files') && $this->_ismediarestrictedfromextranet($event->data['media']);
96
97		if($isMediaRestricted) {
98			$event->data['file'] = dirname(__FILE__).'/images/restricted.png';
99			$event->data['status'] = 403;
100			$event->data['mime'] = 'image/png';
101			$event->data['download'] = false;
102		}
103    }
104}
105