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.

74 lines
2.0 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. function getRepo ($repoId, $repoData) {
  16. switch (array_key_exists('type', $repoData) ? $repoData['type'] : 'dir') {
  17. case 'git':
  18. $repo = new RepositoryGit($repoId, $repoData);
  19. break;
  20. default:
  21. $repo = new RepositoryDir($repoId, $repoData);
  22. }
  23. return $repo;
  24. }
  25. if (!isset($_REQUEST['repo'])) {
  26. Header("Content-Type: application/json; charset=utf-8");
  27. print '{';
  28. $c = 0;
  29. foreach ($repositories as $repoId => $repoData) {
  30. $repo = getRepo($repoId, $repoData);
  31. print $c++ ? ',' : '';
  32. print json_encode($repoId) . ':';
  33. print json_encode($repo->info(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_FORCE_OBJECT);
  34. }
  35. print '}';
  36. exit(0);
  37. }
  38. $repoId = $_REQUEST['repo'];
  39. if (!array_key_exists($repoId, $repositories)) {
  40. Header("HTTP/1.1 404 Repository not found");
  41. exit(0);
  42. }
  43. $repo = getRepo($repoId, $repositories[$repoId]);
  44. $cacheDir = null;
  45. $ts = $repo->newestTimestamp($path);
  46. if (isset($config['cache'])) {
  47. $cacheDir = "{$config['cache']}/repo";
  48. @mkdir($cacheDir);
  49. $cacheTs = filemtime("{$cacheDir}/{$repoId}.json");
  50. if ($cacheTs === $ts) {
  51. Header("Content-Type: application/json; charset=utf-8");
  52. readfile("{$cacheDir}/{$repoId}.json");
  53. exit(0);
  54. }
  55. }
  56. $data = $repo->data();
  57. $ret = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  58. Header("Content-Type: application/json; charset=utf-8");
  59. print $ret;
  60. file_put_contents("{$cacheDir}/{$repoId}.json", $ret);
  61. touch("{$cacheDir}/{$repoId}.json", $ts);