A simple single-file comics website engine.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.8KB

  1. <?php
  2. // Change these to suit your site!
  3. $title = "A Simple Comic Site" // The title of your comic
  4. $homepage = "http://example.com"; // The home page of your comic
  5. $comic_dir = "comics"; // The folder where comic image files are kept
  6. $news_dir = "news"; // The folder where post/news HTML files are kept
  7. $digits = 3; // The number of digits in each comic ID. e.g. 3 for 001
  8. $firstlink = "first.gif"; // first page button
  9. $prevlink = "back.gif"; // previous page button
  10. $homelink = "home.gif"; // Home button
  11. $nextlink = "next.gif"; // next page button
  12. $newlink = "newest.gif"; // newest page button
  13. // !! DON'T change any PHP below this line, unless you know what you're doing
  14. // Skip to the end if you need to add styling or move page elements.
  15. // These two functions print out all the comic image IDs as a dropdown menu
  16. function add_option($item, $key, $target_dir) {
  17. if (substr(mime_content_type("$target_dir/$item"),0,5) == 'image') {
  18. $item_id = explode(".", $item)[0];
  19. if ($item_id == $_GET['id']) echo "<option selected>$item_id</option>\n";
  20. else echo "<option>$item_id</option>\n";
  21. }
  22. }
  23. function comics_dropdown($target_dir) {
  24. echo '<form name="comiclist">';
  25. echo '<select name="id" id="id">';
  26. if (filetype($target_dir) == 'dir') {
  27. $file_list = scandir($target_dir,1);
  28. array_walk($file_list, 'add_option', $target_dir);
  29. }
  30. echo '</select><input type="submit" value="Go" method="get"></form>';
  31. }
  32. // Checks whether a page exists for a given ID in the directory
  33. // If it does, returns the file name
  34. function page_exists($idnum, $target_dir) {
  35. global $digits;
  36. if (strlen($idnum) < $digits) $idnum = str_pad($idnum, $digits, '0', STR_PAD_LEFT);
  37. if (filetype($target_dir) == 'dir') {
  38. $flist = glob("$target_dir/$idnum.*");
  39. if (count($flist) > 0) return $flist[0];
  40. else return false;
  41. }
  42. else return false;
  43. }
  44. // Retrieves the newest comic or alphabetically "largest" file
  45. function get_newest($target_dir, $comic) {
  46. // scandir in reverse order, grab the top one for a while loop until it's an image
  47. if (filetype($target_dir) == 'dir') {
  48. $templist = scandir($target_dir,1);
  49. $i=0;
  50. if ($comic) {
  51. foreach($templist as $item) {
  52. if (substr(mime_content_type("$target_dir/$item"),0,5) == 'image') break;
  53. else $i++;
  54. }
  55. }
  56. $newest = explode(".", $templist[$i])[0];
  57. return $newest;
  58. }
  59. else return false;
  60. }
  61. // Displays the navigation buttons
  62. function display_nav($target_dir) {
  63. global $firstlink, $prevlink, $homelink, $nextlink, $newlink, $homepage;
  64. $php_file = $_SERVER['PHP_SELF'];
  65. if ($newest = get_newest($target_dir, true)) {
  66. if (empty($_GET['id'])) {
  67. $prev = $newest-1;
  68. $next = $newest;
  69. $page_id = $newest;
  70. }
  71. else {
  72. $prev = $_GET['id']-1;
  73. $next = $_GET['id']+1;
  74. $page_id = $_GET['id'];
  75. if ($next > $newest) $next = $newest;
  76. }
  77. if ($prev > 1) echo "<a href=\"$php_file?id=1\"><img src=\"$firstlink\" alt=\"First\" class=\"comic_img\" /></a>";
  78. if ($prev > 0) echo "<a href=\"$php_file?id=$prev\"><img src=\"$prevlink\" alt=\"Previous\" class=\"comic_img\" /></a>";
  79. echo "<a href=\"$homepage\"><img src=\"$homelink\" alt=\"Home\" class=\"comic_img\" /></a>";
  80. if ($page_id < $newest) echo "<a href=\"$php_file?id=$next\"><img src=\"$nextlink\" alt=\"Next\" class=\"comic_img\" /></a>";
  81. if ($next < $newest) echo "<a href=\"$php_file?id=$newest\"><img src=\"$newlink\" alt=\"Newest\" class=\"comic_img\" /></a>\n";
  82. }
  83. }
  84. // Displays the comic image for the current page
  85. function display_comic($target_dir) {
  86. if (empty($_GET['id'])) $page_id = get_newest($target_dir, true);
  87. else $page_id = $_GET['id'];
  88. if ($comic_img = page_exists($page_id, $target_dir)) {
  89. echo "<img src=\"$comic_img\" class=\"comic_img\" />";
  90. }
  91. }
  92. // Displays the news file for the current page.
  93. function display_news($target_dir) {
  94. if (empty($_GET['id'])) $page_id = get_newest($target_dir, false);
  95. else $page_id = $_GET['id'];
  96. if ($news_file = page_exists($page_id, $target_dir)) include($news_file);
  97. }
  98. ?>
  99. <html>
  100. <head>
  101. <title><?php echo $title; ?></title>
  102. </head>
  103. <body>
  104. <!--
  105. The style below applies to the comic image and all navigation images.
  106. You can change it or move it to your own stylesheet.
  107. -->
  108. <style>
  109. .comic_img { border:0px; }
  110. </style>
  111. <!--
  112. This is the part that displays the page!
  113. You can add to it as needed for extra menus, to insert style classes, etc.
  114. -->
  115. <div align="center">
  116. <?php display_comic($comic_dir); /*displays the comic image*/ ?>
  117. <br/><br/>
  118. <?php display_nav($comic_dir); /*displays the comic navigation buttons*/ ?>
  119. <?php comics_dropdown($comic_dir); /*displays the archive dropdown*/ ?>
  120. </div>
  121. <div>
  122. <?php display_news($news_dir); /*inserts the news post html*/ ?>
  123. </div>
  124. </body>
  125. </html>