#!/usr/bin/php-cgi -dcgi.force_redirect=0
<?php
# encoding: utf-8
# api: cgi
# type: config
# category: admin
# title: Cat file
# description: Look up filename and redirect to /raw/UUID
# version: 0.1
# state: beta
# depends: php:sqlite
# config: -
#
# Does an indirect lookup by filename,
# then redirects to the /raw/UUID download.
#
# e.g.
# http://fossil/ext/raw/README.md
#
# Will always lookup the trunk/tip uuid.
#
#-- init
$fn = ltrim($_SERVER["PATH_INFO"], "/");
#print_r($_SERVER);
#-- database (== fossil repo)
function db($sql="", $params=[]) {
global $db;
if (empty($db)) {
$db = new PDO("sqlite::memory:");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db->query("ATTACH DATABASE '$_SERVER[FOSSIL_REPOSITORY]' AS 'repo'");
}
if ($params) {
$stmt = $db->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
else {
return $db->query($sql);
}
}
#-- find uuid
if (empty($fn)) {
die("Access as /ext/cat/fn.txt");
}
$r = db(
"SELECT uuid
FROM blob LEFT JOIN mlink ON blob.rid=mlink.fid
LEFT JOIN filename ON mlink.fnid=filename.fnid
WHERE name = ?
ORDER BY rid DESC LIMIT 1",
[$fn]
);
if ($r and $uuid = $r[0]["uuid"]) {
$fn = basename($fn);
header("Location: $_SERVER[FOSSIL_URI]/raw/$uuid?at=$fn");
}
else {
header("Status: 404 Not found");
}