diff --git a/source/BuildsRenderer.php b/source/BuildsRenderer.php index 07217f0..01a86a3 100644 --- a/source/BuildsRenderer.php +++ b/source/BuildsRenderer.php @@ -8,6 +8,31 @@ class BuildsRenderer { $this->builds = $builds; } + public function renderJSON() { + /* Return a JSON formatted list of builds. */ + $builds_json = array(); + foreach ($this->builds as $build) { + $builds_json[] = array( + 'url' => $this->getFileNameURL($build), + 'app' => $build->app, + 'version' => $build->version, + 'risk_id' => $build->risk_id, + 'branch' => $build->branch, + 'patch' => $build->patch, + 'hash' => $build->hash, + 'platform' => $build->platform, + 'architecture' => $build->architecture, + 'bitness' => $build->bitness, + 'file_mtime' => $build->file_mtime, + 'file_name' => $build->file_name, + 'file_size' => $build->file_size, + 'file_extension' => $build->file_extension, + 'release_cycle' => $build->release_cycle, + ); + } + echo(json_encode($builds_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + } + public function renderPage() { $platform_filters = array( 'windows', @@ -257,7 +282,7 @@ class BuildsRenderer { $request_uri = ($_SERVER['REQUEST_URI'] == "/" || stringContains($_SERVER['REQUEST_URI'], "bpy")) ? - "/download/daily" : $_SERVER['REQUEST_URI']; + "/download/daily" : strtok($_SERVER['REQUEST_URI'], '?'); $env_id = getEnvId(); diff --git a/source/main.php b/source/main.php index 8a49039..6f37bd2 100644 --- a/source/main.php +++ b/source/main.php @@ -13,6 +13,29 @@ function handleNotFoundRequest() { return; } +function renderDownloadResponseAsJSON($lister) { + header('Content-Type: application/json; charset=utf-8'); + // Require v=1 to be specified + if (!isset($_GET['v']) || $_GET['v'] != '1') { + $data = [ 'error' => 'Missing supported format version, for example v=1']; + echo json_encode( $data ); + http_response_code(400); + return; + } + $builds = $lister->getBuilds(); + $renderer = new BuildsRenderer($builds); + $renderer->renderJSON(); + return; +} + +function renderDownloadResponseAsHTML($lister) { + // Serve the directory listing as HTML. + require 'templates/header.php'; + // Logic inside content_build_lister.php will render the list of builds + require 'templates/content_build_lister.php'; + require 'templates/footer.php'; +} + function handleDownloadRequest() { $lister = createListerForCurrentRequest(); $is_valid = $lister->isValid(); @@ -29,11 +52,14 @@ function handleDownloadRequest() { return; } - // Serve the directory listing. - require 'templates/header.php'; - // Logic inside content_build_lister.php will render the list of builds - require 'templates/content_build_lister.php'; - require 'templates/footer.php'; + // Check if we are requesting a JSON formatted view. + if (isset($_GET['format']) && $_GET['format'] == 'json') { + renderDownloadResponseAsJSON($lister); + return; + } + + renderDownloadResponseAsHTML($lister); + } function handleDashboardRequest() {