13d491f75SAndreas Gohr<?php 2*faf3f01bSAndreas Gohr 3*faf3f01bSAndreas Gohruse dokuwiki\Utf8\Clean; 4*faf3f01bSAndreas Gohruse dokuwiki\Utf8\PhpString; 5*faf3f01bSAndreas Gohr 63d491f75SAndreas Gohr/** 73d491f75SAndreas Gohr * A simple renderer that allows downloading of code and file snippets 83d491f75SAndreas Gohr * 93d491f75SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 103d491f75SAndreas Gohr */ 11*faf3f01bSAndreas Gohrclass Doku_Renderer_code extends Doku_Renderer 12*faf3f01bSAndreas Gohr{ 13de369923SAndreas Gohr protected $_codeblock = 0; 143d491f75SAndreas Gohr 153d491f75SAndreas Gohr /** 163d491f75SAndreas Gohr * Send the wanted code block to the browser 173d491f75SAndreas Gohr * 183d491f75SAndreas Gohr * When the correct block was found it exits the script. 19f50a239bSTakamura * 20f50a239bSTakamura * @param string $text 21f50a239bSTakamura * @param string $language 22f50a239bSTakamura * @param string $filename 233d491f75SAndreas Gohr */ 24*faf3f01bSAndreas Gohr public function code($text, $language = null, $filename = '') 25*faf3f01bSAndreas Gohr { 26f0859d4bSTom N Harris global $INPUT; 273d491f75SAndreas Gohr if (!$language) $language = 'txt'; 2856bd9509SPhy $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language); 293d491f75SAndreas Gohr if (!$filename) $filename = 'snippet.' . $language; 30*faf3f01bSAndreas Gohr $filename = PhpString::basename($filename); 31*faf3f01bSAndreas Gohr $filename = Clean::stripspecials($filename, '_'); 323d491f75SAndreas Gohr 33ece4159bSAndreas Gohr // send CRLF to Windows clients 34ece4159bSAndreas Gohr if (strpos($INPUT->server->str('HTTP_USER_AGENT'), 'Windows') !== false) { 35ece4159bSAndreas Gohr $text = str_replace("\n", "\r\n", $text); 36ece4159bSAndreas Gohr } 37ece4159bSAndreas Gohr 38f0859d4bSTom N Harris if ($this->_codeblock == $INPUT->str('codeblock')) { 393d491f75SAndreas Gohr header("Content-Type: text/plain; charset=utf-8"); 403d491f75SAndreas Gohr header("Content-Disposition: attachment; filename=$filename"); 413d491f75SAndreas Gohr header("X-Robots-Tag: noindex"); 423d491f75SAndreas Gohr echo trim($text, "\r\n"); 433d491f75SAndreas Gohr exit; 443d491f75SAndreas Gohr } 453d491f75SAndreas Gohr 463d491f75SAndreas Gohr $this->_codeblock++; 473d491f75SAndreas Gohr } 483d491f75SAndreas Gohr 493d491f75SAndreas Gohr /** 503d491f75SAndreas Gohr * Wraps around code() 51f50a239bSTakamura * 52f50a239bSTakamura * @param string $text 53f50a239bSTakamura * @param string $language 54f50a239bSTakamura * @param string $filename 553d491f75SAndreas Gohr */ 56*faf3f01bSAndreas Gohr public function file($text, $language = null, $filename = '') 57*faf3f01bSAndreas Gohr { 58f9d4952bSAndreas Gohr $this->code($text, $language, $filename); 593d491f75SAndreas Gohr } 603d491f75SAndreas Gohr 613d491f75SAndreas Gohr /** 623d491f75SAndreas Gohr * This should never be reached, if it is send a 404 633d491f75SAndreas Gohr */ 64*faf3f01bSAndreas Gohr public function document_end() 65*faf3f01bSAndreas Gohr { 669d2e1be6SAndreas Gohr http_status(404); 673d491f75SAndreas Gohr echo '404 - Not found'; 683d491f75SAndreas Gohr exit; 693d491f75SAndreas Gohr } 703d491f75SAndreas Gohr 713d491f75SAndreas Gohr /** 723d491f75SAndreas Gohr * Return the format of the renderer 733d491f75SAndreas Gohr * 743d491f75SAndreas Gohr * @returns string 'code' 753d491f75SAndreas Gohr */ 76*faf3f01bSAndreas Gohr public function getFormat() 77*faf3f01bSAndreas Gohr { 783d491f75SAndreas Gohr return 'code'; 793d491f75SAndreas Gohr } 803d491f75SAndreas Gohr} 81