-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaction.php
executable file
·353 lines (305 loc) · 11.8 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
if (!defined('DOKU_INC'))
die();
/**
* Fastwiki plugin, used for inline section editing, and loading of do= actions without a page refresh.
*
* @see http://dokuwiki.org/plugin:fastwiki
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Eli Fenton
*/
class action_plugin_fastwiki extends DokuWiki_Action_Plugin {
protected $m_inPartial = false;
protected $m_no_content = false;
protected $m_preload_head = '====47hsjwycv782nwncv8b920m8bv72jmdm3929bno3b3====';
protected $m_orig_act;
/**
* Register callback functions
*
* @param {Doku_Event_Handler} $controller DokuWiki's event controller object
*/
public function register(Doku_Event_Handler $controller) {
// Listed in order of when they happen.
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_start');
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'override_loadskin');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_before');
$controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'unknown_action');
$controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'block_redirect');
$controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, 'block_headers');
$controller->register_hook('ACTION_HEADERS_SEND', 'AFTER', $this, 'instead_of_template');
$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'pre_render');
}
/**
* Start processing the request. This happens after doku init.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function handle_start(Doku_Event &$event, $param) {
global $conf, $INPUT, $ACT;
$this->m_orig_act = $ACT;
if ($INPUT->str('partial') == '1') {
$this->m_inPartial = true;
// Because so much is declared in global scope in doku.php, it's impossible to call tpl_content() without
// rendering the whole template. This hack loads a blank template, so we only render the page's inner content.
$conf['template'] = '../plugins/fastwiki/tplblank';
}
else {
global $lang, $JSINFO;
$JSINFO['fastwiki'] = array(
// Configuration
'secedit' => $this->getConf('secedit'),
'preview' => $this->getConf('preview'),
'fastpages' => $this->getConf('fastpages'),
'save' => $this->getConf('save'),
'fastshow' => $this->getConf('fastshow'),
'fastshow_same_ns' => $this->getConf('fastshow_same_ns'),
'fastshow_include' => $this->getConf('fastshow_include'),
'fastshow_exclude' => $this->getConf('fastshow_exclude'),
'preload' => function_exists('curl_init') ? $this->getConf('preload') : false,
'preload_head' => $this->m_preload_head,
'preload_batchsize'=> $this->getConf('preload_batchsize'),
'preload_per_page' => $this->getConf('preload_per_page'),
// Needed for the initialization of the partial edit page.
'locktime' => $conf['locktime'] - 60,
'usedraft' => $conf['usedraft'] ? $conf['usedraft'] : '0',
// Miscellaneous
'text_btn_show' => $lang['btn_show'],
'templatename' => $conf['template']
);
}
}
/**
* The Loadskin plugin changes $conf['template'] in multiple places. Make sure we cover them all.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function override_loadskin(Doku_Event &$event, $param) {
global $conf;
if ($this->m_inPartial)
$conf['template'] = '../plugins/fastwiki/tplblank';
}
/**
* Define special actions.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function unknown_action(Doku_Event &$event, $param) {
if ($event->data == 'fastwiki_preload')
$event->preventDefault();
}
/**
* Hook into the pre-processor for the action handler to catch subscribe sub-actions before the action name changes.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function handle_action_before(Doku_Event &$event, $param) {
if (!$this->m_inPartial)
return;
global $ACT, $INPUT;
// For partials, we don't want output from subscribe actions -- just success/error messages.
if ($this->m_orig_act == 'subscribe' && $INPUT->str('sub_action'))
$this->m_no_content = true;
else if ($this->getConf('preload') && $this->m_orig_act == 'fastwiki_preload')
$event->preventDefault();
}
/**
* Don't output headers while proxying preload pages.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function block_headers(Doku_Event &$event, $param) {
global $INPUT;
if ($INPUT->str('fastwiki_preload_proxy'))
$event->preventDefault();
}
/**
* Some actions, like save and subscribe, normally redirect. Block that for partials.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
function block_redirect(Doku_Event &$event, $param) {
if ($this->m_inPartial)
$event->preventDefault();
}
/**
* Handle the "partial" action, using the blank template to deliver nothing but the inner page content.
* This happens right before the template code would normally execute.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function instead_of_template(Doku_Event &$event, $param) {
if (!$this->m_inPartial)
return;
global $ACT, $INPUT, $ID, $INFO;
$preload = $this->getConf('preload') && $this->m_orig_act == 'fastwiki_preload';
// Output error messages.
html_msgarea();
$compareid = $INPUT->str('fastwiki_compareid');
if ($compareid && (auth_quickaclcheck($ID) != auth_quickaclcheck($compareid)))
echo 'PERMISSION_CHANGE';
// Some partials only want an error message.
else if (!$this->m_no_content) {
// Update revision numbers for section edit, in case the file was saved.
if ($this->m_orig_act == 'save')
$INFO['lastmod'] = @filemtime($INFO['filepath']);
// Preload page content.
else if ($preload)
$this->_preload_pages();
else {
//global $_COOKIE;
//$cookies = array();
//foreach ($_COOKIE as $name=>$value)
// array_push($cookies, $name . '=' . addslashes($value));
//$cookies = join('; ', $cookies);
//echo "[{$_SERVER["REMOTE_USER"]}, $cookies]";
}
// Section save. This won't work, unless I return new "range" inputs for all sections.
// $secedit = $ACT == 'show' && $INPUT->str('target') == 'section' && ($INPUT->str('prefix') || $INPUT->str('suffix'));
// if ($secedit)
// $this->render_text($INPUT->str('wikitext')); //+++ render_text isn't outputting anything.
// else
if (!$preload)
tpl_content($ACT == 'show');
}
}
/**
* The template is about to render the main content area. Plop in a marker div so the javascript can
* figure out where the main content area is. NOTE: Templates that don't wrap tpl_content()
* in an HTML tag won't work with this plugin.
*
* @param {Doku_Event} $event - The DokuWiki event object.
* @param {mixed} $param - The fifth argument to register_hook().
*/
public function pre_render(Doku_Event &$event, $param) {
global $ACT, $INPUT, $ID;
if (!$this->m_inPartial)
print '<div class="plugin_fastwiki_marker" style="display:none"></div>';
}
/**
* Preload pages based on URL parameters, and return them.
*/
protected function _preload_pages() {
global $INPUT, $_COOKIE, $ID;
$maxpages = $this->getConf('preload_batchsize');
$pages = explode(',', $INPUT->str('fastwiki_preload_pages'));
$count = min($maxpages, count($pages));
$headers = getallheaders();
$requests = array();
$filtered = array();
for ($x=0; $x<$count; $x++) {
$newid = cleanID($pages[$x]);
// ACL must be exactly the same.
if (page_exists($newid) && (auth_quickaclcheck($ID) == auth_quickaclcheck($newid)))
$filtered[] = $newid;
}
$pages = $filtered;
$count = count($pages);
if (function_exists('curl_init')) {
for ($x=0; $x<$count; $x++) {
$newid = $pages[$x];
// Because there's no way to call doku recursively, curl is the only way to get a fresh context.
// Without a fresh context, there's no easy way to get action plugins to run or TOC to render properly.
/*
From include plugin. Interesting.
extract($page);
$id = $page['id'];
$exists = $page['exists'];
Or maybe open a new doku process with popen?
*/
$ch = curl_init(DOKU_URL.'doku.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "id={$newid}&partial=1&fastwiki_preload_proxy=1");
curl_setopt($ch, CURLOPT_COOKIE, $headers['Cookie']);
curl_setopt($ch, CURLOPT_USERAGENT, $headers['User-Agent']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Language: ' . $headers['Accept-Language']));
curl_setopt($ch, CURLOPT_REFERER, $headers['Referer']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); // Ignore redirects. TODO: Really? What about redirect plugin?
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
array_push($requests, array($ch, $newid));
}
// Request URLs with multiple threads.
// TODO: This currently hangs. Enable the array_push above, and remove curl_exec, to test.
if (count($requests) > 0) {
$multicurl = curl_multi_init();
foreach ($requests as $req)
curl_multi_add_handle($multicurl, $req[0]);
$active = null;
// Strange loop becuase php 5.3.18 broke curl_multi_select
do {
do {
$mrc = curl_multi_exec($multicurl, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// Wait 10ms to fix a bug where multi_select returns -1 forever.
usleep(10000);
} while(curl_multi_select($multicurl) === -1);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($multicurl) != -1) {
do {
$mrc = curl_multi_exec($multicurl, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($requests as $idx=>$req) {
if ($idx > 0)
print $this->m_preload_head;
print $req[1] . "\n";
echo curl_multi_getcontent($req[0]);
curl_multi_remove_handle($multicurl, $req[0]);
}
curl_multi_close($multicurl);
}
}
// TODO: WORKING
// Fallback when curl isn't installed. Not parallelized, but it works!
// Note that this will not work with connections that do chunking.
//TODO DOCUMENT: Needs allow_url_fopen.
//TODO Replicate client's User-Agent, Accept-Language header. Copy COOKIE header instead of reconstructing.
//TODO: This is VERY slow.
else {
return;
global $_SERVER;
$hostname = $_SERVER['SERVER_NAME'];
for ($x=0; $x<$count; $x++) {
$newid = $pages[$x];
$headers = array(
"POST " . DOKU_URL . "doku.php HTTP/1.1",
"Host: " . $hostname,
"Cookie: " . $cookies,
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
//"Accept: text/plain, */*",
"", "");
$body = "id={$newid}&partial=1&fastwiki_preload_proxy=1";
print implode("\r\n", $headers) . "id={$newid}&partial=1&fastwiki_preload_proxy=1\n\n\n";
continue;
$remote = fsockopen($hostname, 80, $errno, $errstr, 5);
fwrite($remote, implode("\r\n", $headers) . $body);
$response = '';
while (!feof($remote))
$response .= fread($remote, 8192);
fclose($remote);
if ($x > 0)
print $this->m_preload_head;
print "$newid\n";
echo $response;
}
}
}
}
if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = '';
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_')
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
return $headers;
}
}