xref: /dokuwiki/inc/parser/code.php (revision e1f856bac8f154dbb5a51c739630e38115fbbe0b)
1<?php
2/**
3 * A simple renderer that allows downloading of code and file snippets
4 *
5 * @author Andreas Gohr <andi@splitbrain.org>
6 */
7if(!defined('DOKU_INC')) die('meh.');
8
9class Doku_Renderer_code extends Doku_Renderer {
10    var $_codeblock = 0;
11
12    /**
13     * Send the wanted code block to the browser
14     *
15     * When the correct block was found it exits the script.
16     */
17    function code($text, $language = null, $filename = '') {
18        global $INPUT;
19        if(!$language) $language = 'txt';
20        if(!$filename) $filename = 'snippet.'.$language;
21        $filename = utf8_basename($filename);
22        $filename = utf8_stripspecials($filename, '_');
23
24        if($this->_codeblock == $INPUT->str('codeblock')) {
25            header("Content-Type: text/plain; charset=utf-8");
26            header("Content-Disposition: attachment; filename=$filename");
27            header("X-Robots-Tag: noindex");
28            echo trim($text, "\r\n");
29            exit;
30        }
31
32        $this->_codeblock++;
33    }
34
35    /**
36     * Wraps around code()
37     */
38    function file($text, $language = null, $filename = '') {
39        $this->code($text, $language, $filename);
40    }
41
42    /**
43     * This should never be reached, if it is send a 404
44     */
45    function document_end() {
46        http_status(404);
47        echo '404 - Not found';
48        exit;
49    }
50
51    /**
52     * Return the format of the renderer
53     *
54     * @returns string 'code'
55     */
56    function getFormat() {
57        return 'code';
58    }
59}
60