index.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. include("config.php");
  3. include("ppub.php");
  4. $file = $_GET["ppub"];
  5. $asset = urldecode($_GET["asset"]);
  6. error_log($_SERVER['REQUEST_URI']);
  7. if($asset == '' and $_SERVER['REQUEST_URI'][-1] != '/') {
  8. header("location: " . $_SERVER['REQUEST_URI'] . "/");
  9. }
  10. if($file == "" or $file == "/" or $file == "feed.rss") {
  11. if($file == "feed.rss") {
  12. header("content-type: application/rss+xml");
  13. include("rss_template.php");
  14. }
  15. else {
  16. header("content-type: text/html");
  17. include("index_template.php");
  18. }
  19. index_start();
  20. $dir = opendir(PUBLICATION_DIR . "/");
  21. $list = array();
  22. while($file = readdir($dir)){
  23. if ($file != '.' and $file != '..'){
  24. $ctime = filectime(PUBLICATION_DIR . "/" . $file) . ',' . $file;
  25. $list[$ctime] = $file;
  26. }
  27. }
  28. closedir($dir);
  29. krsort($list);
  30. foreach ($list as $file) {
  31. $ppub = new Ppub();
  32. $ppub->read_file(PUBLICATION_DIR . "/".$file);
  33. index_listing($ppub, $file);
  34. }
  35. index_end();
  36. exit();
  37. }
  38. $file = str_replace("/", "", $file);
  39. $file_name = $file;
  40. $file = PUBLICATION_DIR . "/" . $file;
  41. if(!file_exists($file)){
  42. header('HTTP/1.1 404 Not Found');
  43. include("404.php");
  44. exit();
  45. }
  46. $accepts = $_SERVER["HTTP_ACCEPT"];
  47. if(($asset == "" or $asset == "/") and strpos($accepts, "application/x-ppub") !== false or isset($_GET["download"])) {
  48. header("content-type: application/x-ppub");
  49. if(isset($_GET["download"])) {
  50. header("Content-Disposition: attachment; filename=\"".$file_name."\"");
  51. }
  52. echo(readfile($file));
  53. exit();
  54. }
  55. $ppub = new Ppub();
  56. $ppub->read_file($file);
  57. if($asset == "" or $asset == "/") {
  58. $asset = $ppub->asset_list[1];
  59. }
  60. else {
  61. $asset = $ppub->asset_index[$asset];
  62. }
  63. if(strpos($accepts, "text/html") !== false && $asset->mimetype == "text/markdown") {
  64. header("content-type: text/html");
  65. include("content_template.php");
  66. include("Parsedown.php");
  67. $pd = new Parsedown();
  68. content_start($ppub);
  69. content_html($pd->text($ppub->read_asset($asset)));
  70. content_end($ppub);
  71. }
  72. else {
  73. header("content-type: " . $asset->mimetype);
  74. echo($ppub->read_asset($asset));
  75. }
  76. ?>