Download the file
  1. <?php
  2. # atshi.php : Automatic Transparent Syntax HIlighting
  3. # Matthieu Weber <mweber@free.fr>, 2011
  4. #
  5. # This program is free software: you can redistribute it and/or modify it
  6. # under the terms of the GNU Affero General Public License as published by the
  7. # Free Software Foundation, either version 3 of the License, or (at your
  8. # option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
  13. # for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17.  
  18.  
  19. include_once 'atshi.conf';
  20. include_once 'geshi.php';
  21.  
  22.  
  23. function err_forbidden($path) {
  24. header("Status : 403 Forbidden");
  25. header("HTTP/1.0 403 Forbidden");
  26. ?>
  27. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  28. <html><head>
  29. <title>403 Forbidden</title>
  30. </head><body>
  31. <h1>Not Found</h1>
  32. <p>You don't have permission to access <?php echo $path ?> on this server.</p>
  33. <hr>
  34. <address><?php echo $_SERVER['SERVER_SIGNATURE'] ?></address>
  35. </body></html>
  36. <?php
  37. }
  38.  
  39. function err_not_found($path) {
  40. header("Status : 404 Not Found");
  41. header("HTTP/1.0 404 Not Found");
  42. ?>
  43. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  44. <html><head>
  45. <title>404 Not Found</title>
  46. </head><body>
  47. <h1>Not Found</h1>
  48. <p>The requested URL <?php echo $path ?> was not found on this server.</p>
  49. <hr>
  50. <address><?php echo $_SERVER['SERVER_SIGNATURE'] ?></address>
  51. </body></html>
  52. <?php
  53. }
  54.  
  55. if (! array_key_exists('PATH_INFO', $_SERVER)) {
  56. # File not found
  57. err_forbidden($_SERVER['REQUEST_URI']);
  58. exit;
  59. }
  60.  
  61. $path_info = $_SERVER['PATH_INFO'];
  62. # Paranoid path sanitizing
  63. $path_info = preg_replace("/\.\.\//", "", $path_info);
  64. $path = $base_path . $path_info;
  65. if (! is_file($path)) {
  66. # File not found
  67. err_not_found($path_info);
  68. exit;
  69. }
  70. if (array_key_exists("src", $_GET)) {
  71. # Let the user download the original file
  72. header('Content-Description: File Transfer');
  73. header('Content-Type: application/octet-stream');
  74. header('Content-Disposition: attachment; filename='.basename($path));
  75. header('Content-Transfer-Encoding: binary');
  76. header('Expires: 0');
  77. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  78. header('Pragma: public');
  79. header('Content-Length: ' . filesize($path));
  80. ob_clean();
  81. flush();
  82. readfile($path);
  83. exit;
  84. }
  85. # Let's see if we can recognize the file
  86. $content = file_get_contents($path);
  87. $language = NULL;
  88. $matches = array ();
  89. # Check the file's header
  90. if (preg_match('/^#!(\S+)/m', $content, &$matches)) {
  91. if (array_key_exists($matches[1], $head_lang)) {
  92. $language = $head_lang[$matches[1]];
  93. }
  94. }
  95. # Check the file's extension
  96. if (! $language && preg_match('/\.([^\.]+)$/', $path_info, &$matches)) {
  97. if (array_key_exists($matches[1], $ext_lang)) {
  98. $language = $ext_lang[$matches[1]];
  99. }
  100. }
  101. # Check the file's name
  102. if (! $language && preg_match('/Makefile/', basename($path_info))) {
  103. $language = "make";
  104. }
  105.  
  106. if ($language == NULL) {
  107. # Unknown type, send the file's content with proper content type
  108. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  109. header('Content-Type: '.finfo_file($finfo, $path));
  110. finfo_close($finfo);
  111. header('Content-Length: ' . filesize($path));
  112. ob_clean();
  113. flush();
  114. readfile($path);
  115. exit;
  116. }
  117.  
  118. # Known type, use syntax highlighting
  119. $geshi = new GeSHi($content, $language);
  120. $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
  121. ?>
  122. <html>
  123. <head>
  124. <title><?php echo "Viewing ".$path_info ?></title>
  125. </head>
  126. <body>
  127. <a href="<?php echo $path_info."?src" ?>">Download the file</a>
  128. <?php echo $geshi->parse_code(); ?>
  129. </body>
  130. </html>
  131.