You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

81 lines
2.0 KiB

  1. <?php
  2. use dokuwiki\Utf8\Clean;
  3. use dokuwiki\Utf8\PhpString;
  4. /**
  5. * A simple renderer that allows downloading of code and file snippets
  6. *
  7. * @author Andreas Gohr <andi@splitbrain.org>
  8. */
  9. class Doku_Renderer_code extends Doku_Renderer
  10. {
  11. protected $_codeblock = 0;
  12. /**
  13. * Send the wanted code block to the browser
  14. *
  15. * When the correct block was found it exits the script.
  16. *
  17. * @param string $text
  18. * @param string $language
  19. * @param string $filename
  20. */
  21. public function code($text, $language = null, $filename = '')
  22. {
  23. global $INPUT;
  24. if (!$language) $language = 'txt';
  25. $language = preg_replace(PREG_PATTERN_VALID_LANGUAGE, '', $language);
  26. if (!$filename) $filename = 'snippet.' . $language;
  27. $filename = PhpString::basename($filename);
  28. $filename = Clean::stripspecials($filename, '_');
  29. // send CRLF to Windows clients
  30. if (strpos($INPUT->server->str('HTTP_USER_AGENT'), 'Windows') !== false) {
  31. $text = str_replace("\n", "\r\n", $text);
  32. }
  33. if ($this->_codeblock == $INPUT->str('codeblock')) {
  34. header("Content-Type: text/plain; charset=utf-8");
  35. header("Content-Disposition: attachment; filename=$filename");
  36. header("X-Robots-Tag: noindex");
  37. echo trim($text, "\r\n");
  38. exit;
  39. }
  40. $this->_codeblock++;
  41. }
  42. /**
  43. * Wraps around code()
  44. *
  45. * @param string $text
  46. * @param string $language
  47. * @param string $filename
  48. */
  49. public function file($text, $language = null, $filename = '')
  50. {
  51. $this->code($text, $language, $filename);
  52. }
  53. /**
  54. * This should never be reached, if it is send a 404
  55. */
  56. public function document_end()
  57. {
  58. http_status(404);
  59. echo '404 - Not found';
  60. exit;
  61. }
  62. /**
  63. * Return the format of the renderer
  64. *
  65. * @returns string 'code'
  66. */
  67. public function getFormat()
  68. {
  69. return 'code';
  70. }
  71. }