index.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. include("config.php");
  3. include("ppub.php");
  4. define("INDEX_TYPE_MAIN", 0);
  5. define("INDEX_TYPE_TAG", 1);
  6. define("INDEX_TYPE_SEARCH", 2);
  7. function get_ppub_file_list() {
  8. if(USE_PPIX) {
  9. include_once("ppix.php");
  10. $ppix = new Ppix(fopen(PUBLICATION_DIR . "/lib.ppix", 'rb'));
  11. if(isset($_GET["q"])) {
  12. $ids = $ppix->do_search(strtolower(str_replace("/", "", $_GET["q"])));
  13. $list = array();
  14. for ($i=0; $i < count($ids); $i++) {
  15. $list[$i] = $ppix->get_publication_by_id($ids[$i]);
  16. }
  17. return $list;
  18. } else if(isset($_GET["tag"])) {
  19. $tag = str_replace("/", "", $_GET["tag"]);
  20. $tags = $ppix->get_tags();
  21. $col = $tags[$tag];
  22. if($col === null) {
  23. return array();
  24. }
  25. $ids = $ppix->get_collection_by_id($col);
  26. $list = array();
  27. for ($i=0; $i < count($ids); $i++) {
  28. $list[$i] = $ppix->get_publication_by_id($ids[$i]);
  29. }
  30. return $list;
  31. } else {
  32. $count = $ppix->get_publication_count();
  33. $list = array();
  34. for ($i=0; $i < $count; $i++) {
  35. $list[$i] = $ppix->get_publication_by_id($i);
  36. }
  37. return $list;
  38. }
  39. }
  40. else {
  41. $dir = opendir(PUBLICATION_DIR . "/");
  42. $list = array();
  43. while($file = readdir($dir)){
  44. if ($file != '.' and $file != '..' and $file != "lib.ppix"){
  45. $ctime = filectime(PUBLICATION_DIR . "/" . $file) . ',' . $file;
  46. $list[$ctime] = $file;
  47. }
  48. }
  49. closedir($dir);
  50. krsort($list);
  51. return $list;
  52. }
  53. }
  54. function get_tag_list() {
  55. if(USE_PPIX) {
  56. include_once("ppix.php");
  57. $ppix = new Ppix(fopen(PUBLICATION_DIR . "/lib.ppix", 'rb'));
  58. return array_keys($ppix->get_tags());
  59. }
  60. else {
  61. return array();
  62. }
  63. }
  64. $file = $_GET["ppub"];
  65. $asset = urldecode($_GET["asset"]);
  66. error_log($_SERVER['REQUEST_URI']);
  67. if($asset == '' and $_SERVER['REQUEST_URI'][-1] != '/') {
  68. header("location: " . $_SERVER['REQUEST_URI'] . "/");
  69. }
  70. if($file == "" or $file == "/" or $file == "feed.rss") {
  71. if($file == "feed.rss") {
  72. header("content-type: application/rss+xml");
  73. include("rss_template.php");
  74. }
  75. else {
  76. header("content-type: text/html");
  77. include("index_template.php");
  78. }
  79. $index_type = INDEX_TYPE_MAIN;
  80. $index_arg = null;
  81. if(isset($_GET["q"])) {
  82. $index_type = INDEX_TYPE_SEARCH;
  83. $index_arg = str_replace("/", "", $_GET["q"]);
  84. }
  85. if(isset($_GET["tag"])) {
  86. $index_type = INDEX_TYPE_TAG;
  87. $index_arg = str_replace("/", "", $_GET["tag"]);
  88. }
  89. if($index_type == INDEX_TYPE_MAIN) {
  90. $index_arg = get_tag_list();
  91. }
  92. index_start($index_type, $index_arg);
  93. $list = get_ppub_file_list();
  94. foreach ($list as $file) {
  95. $ppub = new Ppub();
  96. $ppub->read_file(PUBLICATION_DIR . "/".$file);
  97. index_listing($ppub, $file);
  98. }
  99. if(count($list) == 0) {
  100. index_no_content($index_type, $index_arg);
  101. }
  102. index_end();
  103. exit();
  104. }
  105. $file = str_replace("/", "", $file);
  106. $file_name = $file;
  107. $file = PUBLICATION_DIR . "/" . $file;
  108. if(!file_exists($file)){
  109. header('HTTP/1.1 404 Not Found');
  110. include("404.php");
  111. exit();
  112. }
  113. $accepts = $_SERVER["HTTP_ACCEPT"];
  114. if(($asset == "" or $asset == "/") and strpos($accepts, "application/x-ppub") !== false or isset($_GET["download"])) {
  115. header("content-type: application/x-ppub");
  116. if(isset($_GET["download"])) {
  117. header("Content-Disposition: attachment; filename=\"".$file_name."\"");
  118. }
  119. echo(readfile($file));
  120. exit();
  121. }
  122. $ppub = new Ppub();
  123. $ppub->read_file($file);
  124. if($asset == "" or $asset == "/") {
  125. $asset = $ppub->asset_list[1];
  126. }
  127. else {
  128. $asset = $ppub->asset_index[$asset];
  129. }
  130. if(strpos($accepts, "text/html") !== false && $asset->mimetype == "text/markdown") {
  131. header("content-type: text/html");
  132. include("content_template.php");
  133. include("Parsedown.php");
  134. $pd = new Parsedown();
  135. content_start($ppub, $file_name);
  136. content_html($pd->text($ppub->read_asset($asset)));
  137. content_end($ppub);
  138. }
  139. else {
  140. header("content-type: " . $asset->mimetype);
  141. echo($ppub->read_asset($asset));
  142. }
  143. ?>