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 // send CRLF to Windows clients 25 if(strpos($INPUT->server->str('HTTP_USER_AGENT'), 'Windows') !== false) { 26 $text = str_replace("\n", "\r\n", $text); 27 } 28 29 if($this->_codeblock == $INPUT->str('codeblock')) { 30 header("Content-Type: text/plain; charset=utf-8"); 31 header("Content-Disposition: attachment; filename=$filename"); 32 header("X-Robots-Tag: noindex"); 33 echo trim($text, "\r\n"); 34 exit; 35 } 36 37 $this->_codeblock++; 38 } 39 40 /** 41 * Wraps around code() 42 */ 43 function file($text, $language = null, $filename = '') { 44 $this->code($text, $language, $filename); 45 } 46 47 /** 48 * This should never be reached, if it is send a 404 49 */ 50 function document_end() { 51 http_status(404); 52 echo '404 - Not found'; 53 exit; 54 } 55 56 /** 57 * Return the format of the renderer 58 * 59 * @returns string 'code' 60 */ 61 function getFormat() { 62 return 'code'; 63 } 64} 65