index.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. include("config.php");
  3. include("ppub.php");
  4. include("theme.php");
  5. define("INDEX_TYPE_MAIN", 0);
  6. define("INDEX_TYPE_TAG", 1);
  7. define("INDEX_TYPE_SEARCH", 2);
  8. function get_ppub_file_list() {
  9. if(USE_PPIX) {
  10. include_once("ppix.php");
  11. $ppix = new Ppix(fopen(PUBLICATION_DIR . "/lib.ppix", 'rb'));
  12. if(isset($_GET["q"])) {
  13. $ids = $ppix->do_search(strtolower(str_replace("/", "", $_GET["q"])));
  14. $list = array();
  15. for ($i=0; $i < count($ids); $i++) {
  16. $list[$i] = $ppix->get_publication_by_id($ids[$i]);
  17. }
  18. return $list;
  19. } else if(isset($_GET["tag"])) {
  20. $tag = str_replace("/", "", $_GET["tag"]);
  21. $tags = $ppix->get_tags();
  22. $col = $tags[$tag];
  23. if($col === null) {
  24. return array();
  25. }
  26. $ids = $ppix->get_collection_by_id($col);
  27. $list = array();
  28. for ($i=0; $i < count($ids); $i++) {
  29. $list[$i] = $ppix->get_publication_by_id($ids[$i]);
  30. }
  31. return $list;
  32. } else {
  33. $count = $ppix->get_publication_count();
  34. $list = array();
  35. for ($i=0; $i < $count; $i++) {
  36. $list[$i] = $ppix->get_publication_by_id($i);
  37. }
  38. return $list;
  39. }
  40. }
  41. else {
  42. $dir = opendir(PUBLICATION_DIR . "/");
  43. $list = array();
  44. while($file = readdir($dir)){
  45. if ($file != '.' and $file != '..' and $file != "lib.ppix"){
  46. $ctime = filectime(PUBLICATION_DIR . "/" . $file) . ',' . $file;
  47. $list[$ctime] = $file;
  48. }
  49. }
  50. closedir($dir);
  51. krsort($list);
  52. return $list;
  53. }
  54. }
  55. function get_tag_list() {
  56. if(USE_PPIX) {
  57. include_once("ppix.php");
  58. $ppix = new Ppix(fopen(PUBLICATION_DIR . "/lib.ppix", 'rb'));
  59. return array_keys($ppix->get_tags());
  60. }
  61. else {
  62. return array();
  63. }
  64. }
  65. $file = $_GET["ppub"];
  66. $asset = urldecode($_GET["asset"]);
  67. error_log($_SERVER['REQUEST_URI']);
  68. if($asset == '' and $_SERVER['REQUEST_URI'][-1] != '/') {
  69. header("location: " . $_SERVER['REQUEST_URI'] . "/");
  70. }
  71. if($file == "" or $file == "/" or $file == "feed.rss") {
  72. $start = "index_start";
  73. $no_content = "index_no_content";
  74. $listing = "index_listing";
  75. $end = "index_end";
  76. if($file == "feed.rss") {
  77. header("content-type: application/rss+xml");
  78. $start = "rss_start";
  79. $listing = "rss_listing";
  80. $no_content = "rss_no_content";
  81. $end = "rss_end";
  82. }
  83. else {
  84. header("content-type: text/html");
  85. }
  86. $index_type = INDEX_TYPE_MAIN;
  87. $index_arg = null;
  88. if(isset($_GET["q"])) {
  89. $index_type = INDEX_TYPE_SEARCH;
  90. $index_arg = str_replace("/", "", $_GET["q"]);
  91. }
  92. if(isset($_GET["tag"])) {
  93. $index_type = INDEX_TYPE_TAG;
  94. $index_arg = str_replace("/", "", $_GET["tag"]);
  95. }
  96. if($index_type == INDEX_TYPE_MAIN) {
  97. $index_arg = get_tag_list();
  98. }
  99. $start($index_type, $index_arg);
  100. $list = get_ppub_file_list();
  101. foreach ($list as $file) {
  102. $ppub = new Ppub();
  103. $ppub->read_file(PUBLICATION_DIR . "/".$file);
  104. $listing($ppub, $file);
  105. }
  106. if(count($list) == 0) {
  107. $no_content($index_type, $index_arg);
  108. }
  109. $end();
  110. exit();
  111. }
  112. $file = str_replace("/", "", $file);
  113. $file_name = $file;
  114. $file = PUBLICATION_DIR . "/" . $file;
  115. if(!file_exists($file)){
  116. header('HTTP/1.1 404 Not Found');
  117. include("404.php");
  118. exit();
  119. }
  120. $accepts = $_SERVER["HTTP_ACCEPT"];
  121. if(($asset == "" or $asset == "/") and strpos($accepts, "application/x-ppub") !== false or isset($_GET["download"])) {
  122. header("content-type: application/x-ppub");
  123. if(isset($_GET["download"])) {
  124. header("Content-Disposition: attachment; filename=\"".$file_name."\"");
  125. }
  126. echo(readfile($file));
  127. exit();
  128. }
  129. $ppub = new Ppub();
  130. $ppub->read_file($file);
  131. if($asset == "" or $asset == "/") {
  132. $asset = $ppub->asset_list[1];
  133. }
  134. else {
  135. $asset = $ppub->asset_index[$asset];
  136. }
  137. if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  138. if(strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) < filectime(PUBLICATION_DIR . "/" . $file)) {
  139. header('HTTP/1.1 304 Not Modified');
  140. exit;
  141. }
  142. }
  143. header("Cache-Control: public, max-age=604800");
  144. if(strpos($accepts, "text/html") !== false && $asset->mimetype == "text/markdown" && !isset($_GET["raw"])) {
  145. header("content-type: text/html");
  146. include("Parsedown.php");
  147. $pd = new Parsedown();
  148. content_start($ppub, $file_name);
  149. content_html($pd->text($ppub->read_asset($asset)));
  150. content_end($ppub);
  151. }
  152. else if(strpos($accepts, "text/html") !== false && $asset->mimetype == "application/x-ppvm" && !isset($_GET["raw"])) {
  153. header("content-type: text/html");
  154. include("ppvm.php");
  155. include("Parsedown.php");
  156. $pd = new Parsedown();
  157. $video = new Ppvm();
  158. $video->from_string($ppub->read_asset($asset));
  159. include("video_player.php");
  160. if(isset($_GET["embed"])) {
  161. ppvm_player($ppub, $file_name, $video);
  162. }
  163. else {
  164. video_start($ppub, $file_name, $video);
  165. $description = $ppub->asset_index[$video->metadata["description"]];
  166. video_html($pd->text($ppub->read_asset($description)));
  167. video_end($ppub);
  168. }
  169. }
  170. else {
  171. header("content-type: " . $asset->mimetype);
  172. if($ppub->can_stream_asset($asset)) {
  173. $file_size = $ppub->get_asset_size($asset);
  174. $start = 0;
  175. $end = $file_size;
  176. header('Accept-Ranges: bytes', true);
  177. ### Parse Content-Range header for byte offsets, looks like "bytes=11525-" OR "bytes=11525-12451"
  178. if( isset($_SERVER['HTTP_RANGE']) && preg_match('%bytes=(\d+)-(\d+)?%i', $_SERVER['HTTP_RANGE'], $match) )
  179. {
  180. $start = (int)$match[1];
  181. $finish_bytes = 0;
  182. if( isset($match[2]) ){
  183. $finish_bytes = (int)$match[2];
  184. $end = $finish_bytes + 1;
  185. } else {
  186. $finish_bytes = $file_size - 1;
  187. }
  188. $cr_header = sprintf('Content-Range: bytes %d-%d/%d', $start, $finish_bytes, $file_size);
  189. if($start > $file_size || $end > $file_size) {
  190. header("HTTP/1.1 416 Range Not Satisfiable");
  191. header($cr_header);
  192. exit;
  193. }
  194. header("HTTP/1.1 206 Partial content");
  195. header($cr_header);
  196. }
  197. header(sprintf('Content-Length: %d', $end - $start));
  198. $ppub->stream_asset($asset, $start, $end);
  199. }
  200. else {
  201. echo($ppub->read_asset($asset));
  202. }
  203. }
  204. ?>