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.

71 lines
1.8 KiB

  1. <?php include "conf.php"; /* load a local configuration */ ?>
  2. <?php session_start(); ?>
  3. <?php require 'vendor/autoload.php'; /* composer includes */ ?>
  4. <?php include "modulekit/loader.php"; /* loads all php-includes */ ?>
  5. <?php include "node_modules/json-multiline-strings/src/json-multiline-strings.php"; ?>
  6. <?php call_hooks("init"); /* initialize submodules */ ?>
  7. <?php
  8. if (!isset($repositories)) {
  9. $repositories = array(
  10. 'default' => array(
  11. 'path' => $config['categoriesDir'],
  12. ),
  13. );
  14. }
  15. if (isset($_REQUEST['repo'])) {
  16. $repo = $_REQUEST['repo'];
  17. }
  18. if (!array_key_exists($repo, $repositories)) {
  19. Header("HTTP/1.1 404 Repository not found");
  20. exit(0);
  21. }
  22. $path = $repositories[$repo]['path'];
  23. function newestTimestamp ($path) {
  24. $ts = 0;
  25. $d = opendir($path);
  26. while ($f = readdir($d)) {
  27. $t = filemtime("{$path}/{$f}");
  28. if ($t > $ts) {
  29. $ts = $t;
  30. }
  31. }
  32. closedir($d);
  33. return $ts;
  34. }
  35. $cacheDir = null;
  36. $ts = newestTimestamp($path);
  37. if (isset($config['cache'])) {
  38. $cacheDir = "{$config['cache']}/repo";
  39. @mkdir($cacheDir);
  40. $cacheTs = filemtime("{$cacheDir}/{$repo}.json");
  41. if ($cacheTs === $ts) {
  42. Header("Content-Type: application/json; charset=utf-8");
  43. readfile("{$cacheDir}/{$repo}.json");
  44. exit(0);
  45. }
  46. }
  47. $data = array();
  48. $d = opendir($path);
  49. while ($f = readdir($d)) {
  50. if (preg_match("/^([0-9a-zA-Z_\-]+)\.json$/", $f, $m) && $f !== 'package.json') {
  51. $d1 = json_decode(file_get_contents("{$path}/{$f}"), true);
  52. $data[$m[1]] = jsonMultilineStringsJoin($d1, array('exclude' => array(array('const'))));
  53. }
  54. }
  55. closedir($d);
  56. $ret = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  57. Header("Content-Type: application/json; charset=utf-8");
  58. print $ret;
  59. file_put_contents("{$cacheDir}/{$repo}.json", $ret);
  60. touch("{$cacheDir}/{$repo}.json", $ts);