index.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 if(USE_PPCL) {
  42. include_once("ppcl.php");
  43. $ppcl = new Ppcl();
  44. $ppcl->from_string(file_get_contents(PUBLICATION_DIR . "/collection.ppcl"));
  45. $list = array();
  46. foreach($ppcl->publications as $pub) {
  47. array_push($list, $pub->name);
  48. }
  49. return array_reverse($list);
  50. }
  51. else {
  52. $dir = opendir(PUBLICATION_DIR . "/");
  53. $list = array();
  54. while($file = readdir($dir)){
  55. if ($file != '.' and $file != '..' and $file != "lib.ppix"){
  56. $ctime = filemtime(PUBLICATION_DIR . "/" . $file) . ',' . $file;
  57. $list[$ctime] = $file;
  58. }
  59. }
  60. closedir($dir);
  61. krsort($list);
  62. return $list;
  63. }
  64. }
  65. function get_tag_list() {
  66. if(USE_PPIX) {
  67. include_once("ppix.php");
  68. $ppix = new Ppix(fopen(PUBLICATION_DIR . "/lib.ppix", 'rb'));
  69. return array_keys($ppix->get_tags());
  70. }
  71. else {
  72. return array();
  73. }
  74. }
  75. $file = $_GET["ppub"];
  76. $asset = urldecode($_GET["asset"]);
  77. error_log($_SERVER['REQUEST_URI']);
  78. if($asset == '' and $_SERVER['REQUEST_URI'][-1] != '/') {
  79. header("location: " . $_SERVER['REQUEST_URI'] . "/");
  80. }
  81. if($file == "" or $file == "/" or $file == "feed.rss") {
  82. $start = "index_start";
  83. $no_content = "index_no_content";
  84. $listing = "index_listing";
  85. $end = "index_end";
  86. if($file == "feed.rss") {
  87. header("content-type: application/rss+xml");
  88. $start = "rss_start";
  89. $listing = "rss_listing";
  90. $no_content = "rss_no_content";
  91. $end = "rss_end";
  92. }
  93. else {
  94. header("content-type: text/html");
  95. }
  96. $index_type = INDEX_TYPE_MAIN;
  97. $index_arg = null;
  98. if(isset($_GET["q"])) {
  99. $index_type = INDEX_TYPE_SEARCH;
  100. $index_arg = str_replace("/", "", $_GET["q"]);
  101. }
  102. if(isset($_GET["tag"])) {
  103. $index_type = INDEX_TYPE_TAG;
  104. $index_arg = str_replace("/", "", $_GET["tag"]);
  105. }
  106. if($index_type == INDEX_TYPE_MAIN) {
  107. $index_arg = get_tag_list();
  108. }
  109. $start($index_type, $index_arg);
  110. $list = get_ppub_file_list();
  111. foreach ($list as $file) {
  112. error_log("File: " . $file);
  113. $ppub = new Ppub();
  114. $ppub->read_file(PUBLICATION_DIR . "/".$file);
  115. $listing($ppub, $file);
  116. }
  117. if(count($list) == 0) {
  118. $no_content($index_type, $index_arg);
  119. }
  120. $end();
  121. exit();
  122. }
  123. $file = str_replace("/", "", $file);
  124. $file_name = $file;
  125. $file = PUBLICATION_DIR . "/" . $file;
  126. $ppcl_pub = null;
  127. if(USE_PPCL) {
  128. include_once("ppcl.php");
  129. $ppcl = new Ppcl();
  130. $ppcl->from_string(file_get_contents(PUBLICATION_DIR . "/collection.ppcl"));
  131. $ppcl_pub = $ppcl->get_publication($file_name);
  132. }
  133. // Don't serve non-published files if using PPCL
  134. if(!file_exists($file) || (USE_PPCL && $ppcl_pub == null)){
  135. header('HTTP/1.1 404 Not Found');
  136. include("404.php");
  137. exit();
  138. }
  139. $accepts = $_SERVER["HTTP_ACCEPT"];
  140. if(($asset == "" or $asset == "/") and strpos($accepts, "application/x-ppub") !== false or isset($_GET["download"])) {
  141. header("content-type: application/x-ppub");
  142. if(isset($_GET["download"])) {
  143. header("Content-Disposition: attachment; filename=\"".$file_name."\"");
  144. }
  145. echo(readfile($file));
  146. exit();
  147. }
  148. $ppub = new Ppub();
  149. $ppub->read_file($file);
  150. if($asset == "" or $asset == "/") {
  151. $asset = $ppub->asset_list[1];
  152. }
  153. else {
  154. $asset = $ppub->asset_index[$asset];
  155. }
  156. if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  157. if(strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) < filectime(PUBLICATION_DIR . "/" . $file)) {
  158. header('HTTP/1.1 304 Not Modified');
  159. exit;
  160. }
  161. }
  162. header("Cache-Control: public, max-age=604800");
  163. if(strpos($accepts, "text/html") !== false && $asset->mimetype == "text/markdown" && !isset($_GET["raw"])) {
  164. header("content-type: text/html");
  165. include("Parsedown.php");
  166. $pd = new Parsedown();
  167. content_start($ppub, $file_name);
  168. content_html($pd->text($ppub->read_asset($asset)));
  169. content_end($ppub);
  170. }
  171. else if(strpos($accepts, "text/html") !== false && $asset->mimetype == "application/x-ppvm" && !isset($_GET["raw"])) {
  172. header("content-type: text/html");
  173. include("ppvm.php");
  174. include("Parsedown.php");
  175. $pd = new Parsedown();
  176. $video = new Ppvm();
  177. $video->from_string($ppub->read_asset($asset));
  178. include("video_player.php");
  179. if(isset($_GET["embed"])) {
  180. ppvm_player($ppub, $file_name, $video);
  181. }
  182. else {
  183. video_start($ppub, $file_name, $video);
  184. $description = $ppub->asset_index[$video->metadata["description"]];
  185. video_html($pd->text($ppub->read_asset($description)));
  186. video_end($ppub);
  187. }
  188. }
  189. else {
  190. header("content-type: " . $asset->mimetype);
  191. if($ppub->can_stream_asset($asset)) {
  192. $file_size = $ppub->get_asset_size($asset);
  193. $start = 0;
  194. $end = $file_size;
  195. header('Accept-Ranges: bytes', true);
  196. ### Parse Content-Range header for byte offsets, looks like "bytes=11525-" OR "bytes=11525-12451"
  197. if( isset($_SERVER['HTTP_RANGE']) && preg_match('%bytes=(\d+)-(\d+)?%i', $_SERVER['HTTP_RANGE'], $match) )
  198. {
  199. $start = (int)$match[1];
  200. $finish_bytes = 0;
  201. if( isset($match[2]) ){
  202. $finish_bytes = (int)$match[2];
  203. $end = $finish_bytes + 1;
  204. } else {
  205. $finish_bytes = $file_size - 1;
  206. }
  207. $cr_header = sprintf('Content-Range: bytes %d-%d/%d', $start, $finish_bytes, $file_size);
  208. if($start > $file_size || $end > $file_size) {
  209. header("HTTP/1.1 416 Range Not Satisfiable");
  210. header($cr_header);
  211. exit;
  212. }
  213. header("HTTP/1.1 206 Partial content");
  214. header($cr_header);
  215. }
  216. header(sprintf('Content-Length: %d', $end - $start));
  217. $ppub->stream_asset($asset, $start, $end);
  218. }
  219. else {
  220. echo($ppub->read_asset($asset));
  221. }
  222. }
  223. ?>