router.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. require_once 'vendor/mustangostang/spyc/Spyc.php';
  3. require_once 'vendor/autoload.php';
  4. if ($_SERVER['HTTP_HOST'] == 'bimba.tk') {
  5. header('Location: https://apiote.tk/programs/bimba', true, 303);
  6. die();
  7. }
  8. $path = ltrim(@$_GET["path"], "/");
  9. if (preg_match("/^[\/a-zA-Z_\-0-9]+.(svg|woff2|css|txt|webp|png|js)$/", $path) === 1) {
  10. $types = ['css' => 'text/css', 'js' => 'application/javascript', 'woff2' => 'font/woff2', 'png' => 'image/png', 'txt' => 'text/plain', 'svg' => 'image/svg+xml', 'webp' => 'image/webp'];
  11. $ext = pathinfo($path)['extension'];
  12. header("Content-Type: $types[$ext]");
  13. readfile($path);
  14. die;
  15. }
  16. if (preg_match("/^[\/a-zA-Z]+.sig$/", $path) === 1) {
  17. $yaml = Spyc::YAMLLoad(dirname($path) .'/'. basename($path, '.sig') . '.yml');
  18. $name = basename($path, '.sig');
  19. header('Content-Type: application/yaml');
  20. header("Content-Disposition: attachment; filename=\"$name.sig.yml\"");
  21. echo Spyc::YAMLDump($yaml['signature'], false, 0, true);
  22. die;
  23. }
  24. if (preg_match("/^[\/a-zA-Z]+.(php)$/", $path) === 1) {
  25. include $path;
  26. die;
  27. }
  28. if (preg_match("/^[\/a-zA-Z]*$/", $path) === 0) {
  29. http_response_code(404);
  30. die('404');
  31. }
  32. if ($path == '') {
  33. $path = './';
  34. }
  35. if (is_dir($path)) {
  36. doFile("${path}index");
  37. } else {
  38. doFile($path);
  39. }
  40. function doFile($path) {
  41. $path = strtolower($path);
  42. if (file_exists("$path.yml")) {
  43. $yaml = Spyc::YAMLLoad("$path.yml");
  44. head($yaml['style'], $yaml['title'], $yaml['script']);
  45. switch($yaml['type']) {
  46. case 'file':
  47. $path = dirname($path);
  48. main("$path/$yaml[file]", array_key_exists('signature', $yaml));
  49. break;
  50. case 'listing':
  51. listing(dirname($path));
  52. }
  53. foot();
  54. } else {
  55. http_response_code(404);
  56. $quotes = Spyc::YAMLLoad("quotes.yml");
  57. $index = rand(0, count($quotes)-1);
  58. head(['main.css'], 'Not Found', []);
  59. echo '<h1><span class="error">error:</span> 404</h1>';
  60. echo $quotes[$index];
  61. foot();
  62. die;
  63. }
  64. }
  65. function listing($path) {
  66. $dir = scandir($path);
  67. $dir = array_filter($dir, function($v) {
  68. return preg_match('/^.*\.yml$/', $v) === 1 &&
  69. $v != 'index.yml';
  70. });
  71. $yaml = Spyc::YAMLLoad("$path/index.yml");
  72. echo "<h1>$yaml[title]</h1>\n";
  73. echo "<p>$yaml[description]</p>\n";
  74. echo "<ul class=\"listing\">\n";
  75. foreach($dir as $file) {
  76. $yaml = Spyc::YAMLLoad("$path/$file");
  77. $a = "/$path/".basename($file, '.yml');
  78. echo "<li><a href=\"$a\"><span class=\"listing-title\">$yaml[title]</span></a><br/>";
  79. echo "<span class=\"listing-description\">$yaml[description]</span></li>";
  80. }
  81. echo "</ul>\n";
  82. }
  83. function head($styles, $title, $scripts) {
  84. echo <<<HEAD
  85. <!DOCTYPE html>
  86. <html>
  87. <head>
  88. <title>$title</title>
  89. <meta charset="utf-8">
  90. <meta name="viewport" content="width=device-width, initial-scale=1">
  91. <link rel="icon" type="image/svg+xml" href="/img/logo.svg">
  92. <meta name="description" content="Apiote’s website">
  93. <meta name="og:description" content="Apiote’s website">
  94. <meta name="og:title" content="Apiote">
  95. <meta name="og:image" content="/img/preview.webp">
  96. <meta name="referrer" content="no-referrer">
  97. <meta name="theme-color" content="#911f1b">
  98. <link href="/feed/?format=atom" type="application/atom+xml" rel="alternate" title="Atom feed" />
  99. <link href="/feed/?format=json" type="application/json" rel="alternate" title="JSON feed" />
  100. <link rel="stylesheet" href="/style/plex.css">
  101. <link rel="stylesheet" href="/style/material.css">
  102. <link rel="stylesheet" href="/style/fontawesome.css">
  103. <link rel="stylesheet" href="/style/fira.css">
  104. HEAD;
  105. foreach ($styles as $style) {
  106. echo " <link rel=\"stylesheet\" href=\"/style/$style\">\n";
  107. }
  108. if ($scripts != []) {
  109. foreach ($scripts as $script) {
  110. echo " <script src=\"/js/$script\"></script>\n"; # integrity
  111. }
  112. }
  113. echo <<<HEAD
  114. </head>
  115. <body>
  116. <header>
  117. <div id="logo">
  118. <a href="/"><img src="/img/logo.svg" alt="logo"></a>
  119. </div>
  120. <div>
  121. <a href="/"><div id="title">
  122. Apiote
  123. </div></a>
  124. <nav>
  125. <a href="/about">:about</a>
  126. <a href="/programs/">:programs</a>
  127. <a href="/manifestos/">:manifestos</a>
  128. <a href="/donate">:donate</a>
  129. <a href="/credits">:credits</a>
  130. <a href="/contact">:contact</a>
  131. </nav>
  132. </div>
  133. </header>
  134. <main>
  135. <article>
  136. HEAD;
  137. }
  138. function foot() {
  139. echo <<<FOOT
  140. </article>
  141. <hr id="aside-separator">
  142. <aside>
  143. <p>Computer science student. Fed up with business agenda.</p>
  144. <ul>
  145. <li><i class="list-icon fas fa-code-branch"></i><a href="https://notabug.org/apiote">apiote</a></li>
  146. <li><i class="list-icon fab fa-keybase"></i><a href="https://keybase.io/apiote">apiote</a></li>
  147. <li><i class="list-icon material-icons">fingerprint</i><a href="https://keyserver.ubuntu.com/pks/lookup?op=vindex&search=0x2442e27776e0a578&fingerprint=on">0x2442e27776e0a578</a></li>
  148. <li><i class="list-icon fab fa-mastodon"></i><a href="https://floss.social/@apiote">@apiote@floss.social</a></li>
  149. <li class="wrap"><i class="list-icon fab fa-monero"></i><code>46siw2guCQWQiNAdTBCAvVY1uGYhsFQBuEStBgh4shrfBETMNsMDi9MHoMWyjrVYxEPNKFHsodZ31EcC9FM2R2nMCF21bAJ</code></li>
  150. <li class="wrap"><img src="/img/zcash_logo.svg" alt="zcash" class="list-icon"><code>zcTTKeUpznTKe3nuZ182t3GnWEyhRGwnCWm613k7PYgMCGvKebekV3px844x8S2KGhA7JVFYHb7cn1Xm2ZpK2QnWzcmrnMm</code></li>
  151. </ul>
  152. <p>The license for this website is <a href="https://www.gnu.org/licenses/agpl-3.0.en.html">AGPL</a></p>
  153. <p class="anouncement">The domain ‘adamsptogs.tk’ will not be prolonged and will cease to exist in April 2019. To still be able to visit this website head to <a href="https://apiote.tk">apiote.tk</a></p>
  154. </aside>
  155. </main>
  156. </body>
  157. </html>
  158. FOOT;
  159. }
  160. function main($filename, $signature) {
  161. if ($signature) {
  162. $basename = basename($filename, '.rst');
  163. $basename = basename($basename, '.html');
  164. $sig = dirname($filename, 2). '/' . $basename . '.sig';
  165. echo "<a href=\"$sig\"><i class=\"sig small-icon material-icons\">lock</i> this document is signed</a>";
  166. }
  167. switch(pathinfo($filename)['extension']) {
  168. case 'html':
  169. readfile($filename);
  170. break;
  171. case 'rst':
  172. $parser = new Gregwar\RST\Parser;
  173. $rst = file_get_contents($filename);
  174. $document = $parser->parse($rst);
  175. echo $document;
  176. break;
  177. }
  178. }
  179. ?>