xref: /dokuwiki/inc/parser/code.php (revision de369923ccbbc3c1e79fd7b4d03677397e03876a)
13d491f75SAndreas Gohr<?php
23d491f75SAndreas Gohr/**
33d491f75SAndreas Gohr * A simple renderer that allows downloading of code and file snippets
43d491f75SAndreas Gohr *
53d491f75SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
63d491f75SAndreas Gohr */
73d491f75SAndreas Gohrclass Doku_Renderer_code extends Doku_Renderer {
8*de369923SAndreas Gohr    protected $_codeblock = 0;
93d491f75SAndreas Gohr
103d491f75SAndreas Gohr    /**
113d491f75SAndreas Gohr     * Send the wanted code block to the browser
123d491f75SAndreas Gohr     *
133d491f75SAndreas Gohr     * When the correct block was found it exits the script.
14f50a239bSTakamura     *
15f50a239bSTakamura     * @param string $text
16f50a239bSTakamura     * @param string $language
17f50a239bSTakamura     * @param string $filename
183d491f75SAndreas Gohr     */
19*de369923SAndreas Gohr    public function code($text, $language = null, $filename = '') {
20f0859d4bSTom N Harris        global $INPUT;
213d491f75SAndreas Gohr        if(!$language) $language = 'txt';
2256bd9509SPhy        $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language);
233d491f75SAndreas Gohr        if(!$filename) $filename = 'snippet.'.$language;
243009a773SAndreas Gohr        $filename = utf8_basename($filename);
252d3f0c16SAndreas Gohr        $filename = utf8_stripspecials($filename, '_');
263d491f75SAndreas Gohr
27ece4159bSAndreas Gohr        // send CRLF to Windows clients
28ece4159bSAndreas Gohr        if(strpos($INPUT->server->str('HTTP_USER_AGENT'), 'Windows') !== false) {
29ece4159bSAndreas Gohr            $text = str_replace("\n", "\r\n", $text);
30ece4159bSAndreas Gohr        }
31ece4159bSAndreas Gohr
32f0859d4bSTom N Harris        if($this->_codeblock == $INPUT->str('codeblock')) {
333d491f75SAndreas Gohr            header("Content-Type: text/plain; charset=utf-8");
343d491f75SAndreas Gohr            header("Content-Disposition: attachment; filename=$filename");
353d491f75SAndreas Gohr            header("X-Robots-Tag: noindex");
363d491f75SAndreas Gohr            echo trim($text, "\r\n");
373d491f75SAndreas Gohr            exit;
383d491f75SAndreas Gohr        }
393d491f75SAndreas Gohr
403d491f75SAndreas Gohr        $this->_codeblock++;
413d491f75SAndreas Gohr    }
423d491f75SAndreas Gohr
433d491f75SAndreas Gohr    /**
443d491f75SAndreas Gohr     * Wraps around code()
45f50a239bSTakamura     *
46f50a239bSTakamura     * @param string $text
47f50a239bSTakamura     * @param string $language
48f50a239bSTakamura     * @param string $filename
493d491f75SAndreas Gohr     */
50*de369923SAndreas Gohr    public function file($text, $language = null, $filename = '') {
51f9d4952bSAndreas Gohr        $this->code($text, $language, $filename);
523d491f75SAndreas Gohr    }
533d491f75SAndreas Gohr
543d491f75SAndreas Gohr    /**
553d491f75SAndreas Gohr     * This should never be reached, if it is send a 404
563d491f75SAndreas Gohr     */
57*de369923SAndreas Gohr    public function document_end() {
589d2e1be6SAndreas Gohr        http_status(404);
593d491f75SAndreas Gohr        echo '404 - Not found';
603d491f75SAndreas Gohr        exit;
613d491f75SAndreas Gohr    }
623d491f75SAndreas Gohr
633d491f75SAndreas Gohr    /**
643d491f75SAndreas Gohr     * Return the format of the renderer
653d491f75SAndreas Gohr     *
663d491f75SAndreas Gohr     * @returns string 'code'
673d491f75SAndreas Gohr     */
68*de369923SAndreas Gohr    public function getFormat() {
693d491f75SAndreas Gohr        return 'code';
703d491f75SAndreas Gohr    }
713d491f75SAndreas Gohr}
72