xref: /dokuwiki/inc/parser/code.php (revision 56bd9509ab2037512829392fda6427af7f390724)
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 Gohrif(!defined('DOKU_INC')) die('meh.');
83d491f75SAndreas Gohr
93d491f75SAndreas Gohrclass Doku_Renderer_code extends Doku_Renderer {
103d491f75SAndreas Gohr    var $_codeblock = 0;
113d491f75SAndreas Gohr
123d491f75SAndreas Gohr    /**
133d491f75SAndreas Gohr     * Send the wanted code block to the browser
143d491f75SAndreas Gohr     *
153d491f75SAndreas Gohr     * When the correct block was found it exits the script.
16f50a239bSTakamura     *
17f50a239bSTakamura     * @param string $text
18f50a239bSTakamura     * @param string $language
19f50a239bSTakamura     * @param string $filename
203d491f75SAndreas Gohr     */
210ea51e63SMatt Perry    function code($text, $language = null, $filename = '') {
22f0859d4bSTom N Harris        global $INPUT;
233d491f75SAndreas Gohr        if(!$language) $language = 'txt';
24*56bd9509SPhy        $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language);
253d491f75SAndreas Gohr        if(!$filename) $filename = 'snippet.'.$language;
263009a773SAndreas Gohr        $filename = utf8_basename($filename);
272d3f0c16SAndreas Gohr        $filename = utf8_stripspecials($filename, '_');
283d491f75SAndreas Gohr
29ece4159bSAndreas Gohr        // send CRLF to Windows clients
30ece4159bSAndreas Gohr        if(strpos($INPUT->server->str('HTTP_USER_AGENT'), 'Windows') !== false) {
31ece4159bSAndreas Gohr            $text = str_replace("\n", "\r\n", $text);
32ece4159bSAndreas Gohr        }
33ece4159bSAndreas Gohr
34f0859d4bSTom N Harris        if($this->_codeblock == $INPUT->str('codeblock')) {
353d491f75SAndreas Gohr            header("Content-Type: text/plain; charset=utf-8");
363d491f75SAndreas Gohr            header("Content-Disposition: attachment; filename=$filename");
373d491f75SAndreas Gohr            header("X-Robots-Tag: noindex");
383d491f75SAndreas Gohr            echo trim($text, "\r\n");
393d491f75SAndreas Gohr            exit;
403d491f75SAndreas Gohr        }
413d491f75SAndreas Gohr
423d491f75SAndreas Gohr        $this->_codeblock++;
433d491f75SAndreas Gohr    }
443d491f75SAndreas Gohr
453d491f75SAndreas Gohr    /**
463d491f75SAndreas Gohr     * Wraps around code()
47f50a239bSTakamura     *
48f50a239bSTakamura     * @param string $text
49f50a239bSTakamura     * @param string $language
50f50a239bSTakamura     * @param string $filename
513d491f75SAndreas Gohr     */
520ea51e63SMatt Perry    function file($text, $language = null, $filename = '') {
53f9d4952bSAndreas Gohr        $this->code($text, $language, $filename);
543d491f75SAndreas Gohr    }
553d491f75SAndreas Gohr
563d491f75SAndreas Gohr    /**
573d491f75SAndreas Gohr     * This should never be reached, if it is send a 404
583d491f75SAndreas Gohr     */
593d491f75SAndreas Gohr    function document_end() {
609d2e1be6SAndreas Gohr        http_status(404);
613d491f75SAndreas Gohr        echo '404 - Not found';
623d491f75SAndreas Gohr        exit;
633d491f75SAndreas Gohr    }
643d491f75SAndreas Gohr
653d491f75SAndreas Gohr    /**
663d491f75SAndreas Gohr     * Return the format of the renderer
673d491f75SAndreas Gohr     *
683d491f75SAndreas Gohr     * @returns string 'code'
693d491f75SAndreas Gohr     */
703d491f75SAndreas Gohr    function getFormat() {
713d491f75SAndreas Gohr        return 'code';
723d491f75SAndreas Gohr    }
733d491f75SAndreas Gohr}
74