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.

125 lines
3.2 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. if (!$repo->isEmpty()) {
  16. print $c++ ? ',' : '';
  17. print json_encode($repoId, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . ':';
  18. $info = $repo->info();
  19. if (isset($repoData['repositoryUrl'])) {
  20. $info['repositoryUrl'] = $repoData['repositoryUrl'];
  21. }
  22. if (isset($repoData['categoryUrl'])) {
  23. $info['categoryUrl'] = $repoData['categoryUrl'];
  24. }
  25. $info['group'] = $repoData['group'] ?? 'default';
  26. print json_encode($info, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_FORCE_OBJECT);
  27. }
  28. }
  29. print '}';
  30. exit(0);
  31. }
  32. $fullRepoId = $_REQUEST['repo'];
  33. list($repoId, $branchId) = explode('~', $fullRepoId);
  34. if (array_key_exists('lang', $_REQUEST)) {
  35. $fullRepoId .= '~' . $_REQUEST['lang'];
  36. }
  37. if (!array_key_exists($repoId, $allRepositories)) {
  38. Header("HTTP/1.1 404 Repository not found");
  39. exit(0);
  40. }
  41. $repoData = $allRepositories[$repoId];
  42. $repo = getRepo($repoId, $repoData);
  43. if ($branchId) {
  44. try {
  45. $repo->setBranch($branchId);
  46. }
  47. catch (Exception $e) {
  48. Header("HTTP/1.1 404 No such branch");
  49. exit(0);
  50. }
  51. }
  52. if (array_key_exists('file', $_REQUEST)) {
  53. $file = $repo->file_get_contents($_REQUEST['file']);
  54. if ($file === false) {
  55. Header("HTTP/1.1 403 Forbidden");
  56. print "Access denied.";
  57. }
  58. else if ($file === null) {
  59. Header("HTTP/1.1 404 File not found");
  60. print "File not found.";
  61. }
  62. else {
  63. Header("Content-Type: text/plain; charset=utf-8");
  64. print $file;
  65. }
  66. exit(0);
  67. }
  68. $cacheDir = null;
  69. $ts = $repo->timestamp($path);
  70. if (isset($config['cache'])) {
  71. $cacheDir = "{$config['cache']}/repo";
  72. @mkdir($cacheDir);
  73. $cacheTs = filemtime("{$cacheDir}/{$fullRepoId}.json");
  74. if ($cacheTs === $ts) {
  75. Header("Content-Type: application/json; charset=utf-8");
  76. readfile("{$cacheDir}/{$fullRepoId}.json");
  77. exit(0);
  78. }
  79. }
  80. $data = $repo->data($_REQUEST);
  81. $repo->updateLang($data, $_REQUEST);
  82. if (!array_key_exists('index', $data['categories'])) {
  83. $data['categories']['index'] = array(
  84. 'type' => 'index',
  85. 'subCategories' => array_map(
  86. function ($k) {
  87. return array('id' => $k);
  88. }, array_keys($data['categories']))
  89. );
  90. }
  91. if (isset($repoData['repositoryUrl'])) {
  92. $data['repositoryUrl'] = $repoData['repositoryUrl'];
  93. }
  94. if (isset($repoData['categoryUrl'])) {
  95. $data['categoryUrl'] = $repoData['categoryUrl'];
  96. }
  97. $ret = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  98. Header("Content-Type: application/json; charset=utf-8");
  99. print $ret;
  100. if ($cacheDir) {
  101. @mkdir(dirname("{$cacheDir}/{$fullRepoId}"));
  102. file_put_contents("{$cacheDir}/{$fullRepoId}.json", $ret);
  103. touch("{$cacheDir}/{$fullRepoId}.json", $ts);
  104. }