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.

98 lines
2.6 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. $allRepositories = getRepositories();
  9. if (!isset($_REQUEST['repo'])) {
  10. Header("Content-Type: application/json; charset=utf-8");
  11. print '{';
  12. $c = 0;
  13. foreach ($allRepositories as $repoId => $repoData) {
  14. $repo = getRepo($repoId, $repoData);
  15. print $c++ ? ',' : '';
  16. print json_encode($repoId, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . ':';
  17. $info = $repo->info();
  18. if (isset($repoData['repositoryUrl'])) {
  19. $info['repositoryUrl'] = $repoData['repositoryUrl'];
  20. }
  21. if (isset($repoData['categoryUrl'])) {
  22. $info['categoryUrl'] = $repoData['categoryUrl'];
  23. }
  24. print json_encode($info, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_FORCE_OBJECT);
  25. }
  26. print '}';
  27. exit(0);
  28. }
  29. $fullRepoId = $_REQUEST['repo'];
  30. list($repoId, $branchId) = explode('~', $fullRepoId);
  31. if (!array_key_exists($repoId, $allRepositories)) {
  32. Header("HTTP/1.1 404 Repository not found");
  33. exit(0);
  34. }
  35. $repoData = $allRepositories[$repoId];
  36. $repo = getRepo($repoId, $repoData);
  37. if ($branchId) {
  38. try {
  39. $repo->setBranch($branchId);
  40. }
  41. catch (Exception $e) {
  42. Header("HTTP/1.1 404 No such branch");
  43. exit(0);
  44. }
  45. }
  46. $cacheDir = null;
  47. $ts = $repo->timestamp($path);
  48. if (isset($config['cache'])) {
  49. $cacheDir = "{$config['cache']}/repo";
  50. @mkdir($cacheDir);
  51. $cacheTs = filemtime("{$cacheDir}/{$fullRepoId}.json");
  52. if ($cacheTs === $ts) {
  53. Header("Content-Type: application/json; charset=utf-8");
  54. readfile("{$cacheDir}/{$fullRepoId}.json");
  55. exit(0);
  56. }
  57. }
  58. $data = $repo->data();
  59. if (!array_key_exists('index', $data['categories'])) {
  60. $data['categories']['index'] = array(
  61. 'type' => 'index',
  62. 'subCategories' => array_map(
  63. function ($k) {
  64. return array('id' => $k);
  65. }, array_keys($data['categories']))
  66. );
  67. }
  68. if (isset($repoData['repositoryUrl'])) {
  69. $data['repositoryUrl'] = $repoData['repositoryUrl'];
  70. }
  71. if (isset($repoData['categoryUrl'])) {
  72. $data['categoryUrl'] = $repoData['categoryUrl'];
  73. }
  74. $ret = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  75. Header("Content-Type: application/json; charset=utf-8");
  76. print $ret;
  77. if ($cacheDir) {
  78. @mkdir(dirname("{$cacheDir}/{$fullRepoId}"));
  79. file_put_contents("{$cacheDir}/{$fullRepoId}.json", $ret);
  80. touch("{$cacheDir}/{$fullRepoId}.json", $ts);
  81. }