This repository has been archived on 2021-11-29. You can view files and clone it, but cannot push or open issues or pull requests.
simple-gitv/index.php

81 lines
2.2 KiB
PHP

<?php
/*
simple-gitv - a simple, libre web-based Git project viewer
Copyright (C) 2020
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// get version
$version = trim(file_get_contents("docs/version"));
// include assorted important utilities
include "src/utils.php";
// read arguments from user config
$config = parse_ini_file("config.ini");
$repos = strip_trailing_slash($config["repositories"]);
$title = $config["title"];
$theme = $config["theme"];
// get file to serve based on URL
$url = strip_trailing_slash(substr($_SERVER['REQUEST_URI'], 1));
$page_type = get_page_type($url);
if ($page_type === "invalid") {
include "src/errors/404.php";
die();
}
// homepage does not need to check for Git repo
if ($page_type === "home") {
include "src/pages/home.php";
die();
}
// get Git repo
$project = before_first_slash($url);
$git_dir = get_git_dir("$repos/$project");
// 404 if not a git directory
if (! $git_dir) {
include "src/errors/404.php";
die();
}
// use cached version if it exists, otherwise make it
$last_commit = get_last_commit_time($git_dir);
if (file_exists("cache/$url.html") && $last_commit < filemtime("cache/$url.html")) {
include "cache/$url.html";
} else {
// create directory in cache if it doesn't exist
$directory = "cache/" . before_last_slash($url);
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
// create cached file
ob_start();
// get default branch
$branch = get_main_git_branch($git_dir);
include "src/pages/$page_type.php";
$cache_file = fopen("cache/$url.html", 'w');
fwrite($cache_file, ob_get_contents());
fclose($cache_file);
ob_end_flush();
}
?>