index.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  131. if(strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) < filectime(PUBLICATION_DIR . "/" . $file)) {
  132. header('HTTP/1.1 304 Not Modified');
  133. exit;
  134. }
  135. }
  136. header("Cache-Control: public, max-age=604800");
  137. if(strpos($accepts, "text/html") !== false && $asset->mimetype == "text/markdown") {
  138. header("content-type: text/html");
  139. include("content_template.php");
  140. include("Parsedown.php");
  141. $pd = new Parsedown();
  142. content_start($ppub, $file_name);
  143. content_html($pd->text($ppub->read_asset($asset)));
  144. content_end($ppub);
  145. }
  146. else if(strpos($accepts, "text/html") !== false && $asset->mimetype == "application/x-ppvm") {
  147. header("content-type: text/html");
  148. include("ppvm.php");
  149. include("Parsedown.php");
  150. $pd = new Parsedown();
  151. $video = new Ppvm();
  152. $video->from_string($ppub->read_asset($asset));
  153. if(isset($_GET["embed"])) {
  154. include("video_player.php");
  155. ppvm_player($ppub, $file_name, $video);
  156. }
  157. else {
  158. include("video_template.php");
  159. content_start($ppub, $file_name, $video);
  160. $description = $ppub->asset_index[$video->metadata["description"]];
  161. content_html($pd->text($ppub->read_asset($description)));
  162. content_end($ppub);
  163. }
  164. }
  165. else {
  166. header("content-type: " . $asset->mimetype);
  167. if($ppub->can_stream_asset($asset)) {
  168. $file_size = $ppub->get_asset_size($asset);
  169. $start = 0;
  170. $end = $file_size;
  171. header('Accept-Ranges: bytes', true);
  172. ### Parse Content-Range header for byte offsets, looks like "bytes=11525-" OR "bytes=11525-12451"
  173. if( isset($_SERVER['HTTP_RANGE']) && preg_match('%bytes=(\d+)-(\d+)?%i', $_SERVER['HTTP_RANGE'], $match) )
  174. {
  175. $start = (int)$match[1];
  176. $finish_bytes = 0;
  177. if( isset($match[2]) ){
  178. $finish_bytes = (int)$match[2];
  179. $end = $finish_bytes + 1;
  180. } else {
  181. $finish_bytes = $file_size - 1;
  182. }
  183. $cr_header = sprintf('Content-Range: bytes %d-%d/%d', $start, $finish_bytes, $file_size);
  184. header("HTTP/1.1 206 Partial content");
  185. header($cr_header);
  186. }
  187. header(sprintf('Content-Length: %d', $end - $start));
  188. $ppub->stream_asset($asset, $start, $end);
  189. }
  190. else {
  191. echo($ppub->read_asset($asset));
  192. }
  193. }
  194. ?>