forked from chimeric/dokuwiki-plugin-loadskin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
action.php
232 lines (201 loc) · 8.03 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* DokuWiki Action Plugin LoadSkin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Michael Klier <[email protected]>
* @author Anika Henke <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
if(!defined('DOKU_LF')) define('DOKU_LF', "\n");
require_once(DOKU_PLUGIN.'action.php');
/**
* All DokuWiki plugins to interfere with the event system
* need to inherit from this class
*/
class action_plugin_loadskin extends DokuWiki_Action_Plugin {
// register hook
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, '_handleConf');
$controller->register_hook('MEDIAMANAGER_STARTED', 'BEFORE', $this, '_handleConf');
$controller->register_hook('DETAIL_STARTED', 'BEFORE', $this, '_handleConf');
$controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, '_handleContent', array());
// only needed for not yet up-to-date templates:
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, '_defineConstants');
$controller->register_hook('MEDIAMANAGER_STARTED', 'AFTER', $this, '_defineConstants');
$controller->register_hook('DETAIL_STARTED', 'AFTER', $this, '_defineConstants');
}
/**
* Define DOKU_TPL and DOKU_TPLINC after $conf['template'] has been overwritten
* (this still needs the original constant definition in init.php to be removed)
* @deprecated DOKU_TPL and DOKU_TPLINC are deprecated since Adora Belle
*
* @author Anika Henke <[email protected]>
*/
public function _defineConstants(Doku_Event $event, $param) {
global $conf;
// define Template baseURL
if(!defined('DOKU_TPL'))
define('DOKU_TPL', DOKU_BASE.'lib/tpl/'.$conf['template'].'/');
// define real Template directory
if(!defined('DOKU_TPLINC'))
define('DOKU_TPLINC', DOKU_INC.'lib/tpl/'.$conf['template'].'/');
}
/**
* Overwrites the $conf['template'] setting
*
* @author Michael Klier <[email protected]>
* @author Anika Henke <[email protected]>
*/
public function _handleConf(Doku_Event $event, $param) {
global $conf;
global $ACT;
// store original template in helper attribute
$helper = $this->loadHelper('loadskin', true);
$helper->origTpl = $conf['template'];
// set template
$tpl = $this->_getTpl();
$inAdmin = $ACT == 'admin';
$allowInAdmin = $this->getConf('allowInAdmin');
if($tpl && (!$inAdmin || ($inAdmin && $allowInAdmin))) {
$conf['template'] = $tpl;
}
}
/**
* Output the template switcher if 'automaticOutput' is on
*
* @author Anika Henke <[email protected]>
*/
public function _handleContent(Doku_Event $event, $param){
// @todo: should ideally be in showTemplateSwitcher()
$isOverwrittenByAdmin = !$this->getConf('preferUserChoice') && $this->_getTplPerNamespace();
if ($this->getConf('automaticOutput') && !$isOverwrittenByAdmin) {
$helper = $this->loadHelper('loadskin', true);
$event->data = $helper->showTemplateSwitcher().$event->data;
}
}
/**
* Checks if a given page should use a different template then the default
*
* @author Michael Klier <[email protected]>
* @author Anika Henke <[email protected]>
*/
private function _getTpl() {
$tplPerUser = $this->_getTplPerUser();
$tplPerNamespace = $this->_getTplPerNamespace();
if($this->getConf('preferUserChoice')) {
if($tplPerUser)
return $tplPerUser;
if($tplPerNamespace)
return $tplPerNamespace;
} else {
if($tplPerNamespace)
return $tplPerNamespace;
if($tplPerUser)
return $tplPerUser;
}
return false;
}
/**
* Get template from session and/or user config
*
* @author Anika Henke <[email protected]>
*/
private function _getTplPerUser() {
global $INPUT;
// get all available templates
$helper = $this->loadHelper('loadskin', true);
$tpls = $helper->getTemplates();
$mobileSwitch = $this->getConf('mobileSwitch');
$user = $_SERVER['REMOTE_USER'];
$tplRequest = $INPUT->str('tpl');
$actSelect = $INPUT->str('act') && ($INPUT->str('act') == 'select');
// if template switcher was used
if ($tplRequest && $actSelect && (in_array($tplRequest, $tpls) || ($tplRequest == '*') )) {
// hidden way of deleting the cookie and config values
if ($tplRequest == '*')
$tplRequest = false; // not backwards-compatible, will only work with core PR #1129
// store in cookie
set_doku_pref('loadskinTpl', $tplRequest);
// if registered user, store also in conf file (not for mobile switcher)
if ($user && !$mobileSwitch)
$this->_tplUserConfig('set', $user, $tplRequest);
return $tplRequest;
}
$tplUser = $this->_tplUserConfig('get', $user);// from user conf file
$tplCookie = get_doku_pref('loadskinTpl', '');
// if logged in and user is in conf (not for mobile)
if ($user && $tplUser && in_array($tplUser, $tpls) && !$mobileSwitch){
if ($tplCookie && ($tplCookie == $tplUser))
return $tplCookie;
// store in cookie
set_doku_pref('loadskinTpl', $tplUser);
return $tplUser;
}
// if template is stored in cookie
if ($tplCookie && in_array($tplCookie, $tpls))
return $tplCookie;
// if viewed on a mobile and mobile switcher is used, set mobile template as default
global $INFO;
$mobileTpl = $this->getConf('mobileTemplate');
if ($mobileTpl && $INFO['ismobile']) {
set_doku_pref('loadskinTpl', $mobileTpl);
return $mobileTpl;
}
return false;
}
/**
* Get template from namespace/page and config
*
* @author Michael Klier <[email protected]>
* @author Anika Henke <[email protected]>
*/
private function _getTplPerNamespace() {
global $ID;
$config = DOKU_CONF.'loadskin.conf';
if(@file_exists($config)) {
$data = unserialize(io_readFile($config, false));
$id = $ID;
// remove language path from $id before you check for a match (it would only be at the start)
if ($this->getConf('inheritInTranslations') && !plugin_isdisabled('translation')) {
$transplugin = &plugin_load('helper', 'translation');
$langPath = $transplugin->getLangPart($id).':';
$pos = strpos($id, $langPath);
if (($pos !== false) && ($pos == 0))
$id = str_ireplace($langPath, '', $id);
}
if($data[$id]) return $data[$id];
$path = explode(':', $id);
while(count($path) > 0) {
$id = implode(':', $path);
if($data[$id]) return $data[$id];
array_pop($path);
}
}
return false;
}
/**
* Get/set template for user in config
*
* @author Anika Henke <[email protected]>
*/
private function _tplUserConfig($act, $user, $tpl='') {
$data = array();
$userConf = DOKU_CONF.'loadskin.users.conf';
if(@file_exists($userConf)) {
$data = unserialize(io_readFile($userConf, false));
if ($act == 'get')
return $data[$user];
unset($data[$user]);
}
if ($act == 'get')
return false;
// keep line deleted if $tpl is empty
if ($tpl)
$data[$user] = $tpl;
io_saveFile($userConf, serialize($data));
}
}
// vim:ts=4:sw=4: