1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* A simple renderer that allows downloading of code and file snippets |
4
|
|
|
* |
5
|
|
|
* @author Andreas Gohr <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
class Doku_Renderer_code extends Doku_Renderer { |
8
|
|
|
protected $_codeblock = 0; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Send the wanted code block to the browser |
12
|
|
|
* |
13
|
|
|
* When the correct block was found it exits the script. |
14
|
|
|
* |
15
|
|
|
* @param string $text |
16
|
|
|
* @param string $language |
17
|
|
|
* @param string $filename |
18
|
|
|
*/ |
19
|
|
|
public function code($text, $language = null, $filename = '') { |
20
|
|
|
global $INPUT; |
21
|
|
|
if(!$language) $language = 'txt'; |
|
|
|
|
22
|
|
|
$language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language); |
23
|
|
|
if(!$filename) $filename = 'snippet.'.$language; |
24
|
|
|
$filename = \dokuwiki\Utf8\PhpString::basename($filename); |
25
|
|
|
$filename = \dokuwiki\Utf8\Clean::stripspecials($filename, '_'); |
26
|
|
|
|
27
|
|
|
// send CRLF to Windows clients |
28
|
|
|
if(strpos($INPUT->server->str('HTTP_USER_AGENT'), 'Windows') !== false) { |
29
|
|
|
$text = str_replace("\n", "\r\n", $text); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if($this->_codeblock == $INPUT->str('codeblock')) { |
33
|
|
|
header("Content-Type: text/plain; charset=utf-8"); |
34
|
|
|
header("Content-Disposition: attachment; filename=$filename"); |
35
|
|
|
header("X-Robots-Tag: noindex"); |
36
|
|
|
echo trim($text, "\r\n"); |
37
|
|
|
exit; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->_codeblock++; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Wraps around code() |
45
|
|
|
* |
46
|
|
|
* @param string $text |
47
|
|
|
* @param string $language |
48
|
|
|
* @param string $filename |
49
|
|
|
*/ |
50
|
|
|
public function file($text, $language = null, $filename = '') { |
51
|
|
|
$this->code($text, $language, $filename); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* This should never be reached, if it is send a 404 |
56
|
|
|
*/ |
57
|
|
|
public function document_end() { |
58
|
|
|
http_status(404); |
59
|
|
|
echo '404 - Not found'; |
60
|
|
|
exit; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the format of the renderer |
65
|
|
|
* |
66
|
|
|
* @returns string 'code' |
67
|
|
|
*/ |
68
|
|
|
public function getFormat() { |
69
|
|
|
return 'code'; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: