1<?php
2/**
3 * DokuWiki Plugin phprestrict (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author	Robert Woodhead <trebor@animeigo.com>
7 *
8 * Version 1.0 - Initial release
9 *
10 * Version 1.1 - Added disable of revision history on PHP pages.
11 */
12
13if(!defined('DOKU_INC')) die(); // must be run within Dokuwiki
14
15class action_plugin_phprestrict_action extends DokuWiki_Action_Plugin {
16
17	/**
18	 * Registers a callback function for a given event
19	 *
20	 * @param Doku_Event_Handler $controller DokuWiki's event controller object
21	 * @return void
22	 */
23
24	public function register(Doku_Event_Handler $controller) {
25
26		// Future feature: To kill PHP in non-enabled pages, will need to hook IO_WIKIPAGE_READ
27
28		$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_action');
29
30	}
31
32	/**
33	 * adjust conf to permit php if namespace/page matches
34	 */
35
36	public function handle_action(Doku_Event &$event, $param) {
37
38		global $conf;
39		global $INFO;
40		global $REV;
41
42		// default to php being disabled on all pages
43
44		$conf['phpok'] = 0;
45
46		// php is NEVER OK on revision pages
47
48		if ($REV) {
49			return;
50
51		}
52
53		// check for path match and enable PHP if found
54
55		$paths = explode(',',
56						 str_replace(array("\r\n","\r","\n"),',',
57									 $this->getConf('paths')
58						));
59
60		$id = $INFO['id'];
61
62		foreach ($paths as $path) {
63
64			switch (substr($path,-1)) {
65
66				case false:		// empty string
67
68					continue;	// try next path
69
70				case ':':
71
72					$phpok = (strpos($id,$path) === 0);
73					break;
74
75				case '*':
76
77					$phpok = (strpos($id,substr($path,0,-1)) === 0);
78					break;
79
80				default:
81
82					$phpok = ($id === $path);
83					break;
84
85			}
86
87			if ($phpok) {
88
89				$conf['phpok'] = 1;
90
91				// source display/export/revisions can be disabled on potential PHP pages
92
93				if ($this->getConf('hide') === 1) {
94					if (strpos($conf['disableactions'],'source') === false) $conf['disableactions'] .= ',source';
95					if (strpos($conf['disableactions'],'export_raw') === false) $conf['disableactions'] .= ',export_raw';
96					if (strpos($conf['disableactions'],'revisions') === false) $conf['disableactions'] .= ',revisions';
97				}
98
99				return;
100
101			}
102
103		}
104
105		// fall through to default of no php!
106
107	}
108
109}
110
111// vim:ts=4:sw=4:et:
112