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