paperweb/web/index.php

69 lines
2.9 KiB
PHP

<?php
# PAPERWEB - GPLv3 licence
# Copyright 2016 Yves Gablin
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
$CONF = parse_ini_file('paperweb.ini.php');
if (array_key_exists('doDownload', $_REQUEST)) {
$date = @$_REQUEST['date'];
$page = @$_REQUEST['page'];
$datearg = escapeshellarg($date);
$pagearg = escapeshellarg($page);
# -M and -R are used instead of -D to avoid storing the data in RAM
$json = exec("sudo -u {$CONF['USER']} {$CONF['PATH']} -M {$datearg} -p {$pagearg}");
if ($json) {
$meta = json_decode($json, true);
$knownetag = trim(@$_SERVER['HTTP_IF_NONE_MATCH'], ' "');
if ($meta['etag'] == $knownetag) {
header('HTTP/1.1 304 Not Modified');
} else {
$ext = explode('/', $meta['mime'])[1];
header("Content-Type: {$meta['mime']}");
header("ETag: \"{$meta['etag']}\"");
header("Cache-Control: public");
header("Content-Disposition: inline; filename=\"{$date}_{$page}.{$ext}\"");
passthru("sudo -u {$CONF['USER']} {$CONF['PATH']} -R {$datearg} -p {$pagearg}");
}
}
} else {
?>
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<title>PaperWeb, a simple web interface for Paperwork</title>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="paperweb.js"></script>
<link rel="stylesheet" href="paperweb.css"/>
</head><body>
<form method="POST">
<header>
<h1>PaperWeb, a simple web interface for Paperwork</h1>
<p><label>Space-separated date prefixes (<var>YYYY</var>, <var>YYYYMM</var> or <var>YYYYMMDD</var>, eg: <samp>201512 2016</samp>):
<input name="D" value="<?php echo htmlentities(@$_REQUEST['D']); ?>"></label></p>
<p><label>Space-separated substrings of labels (quotes allowed, eg: <samp>invoice "state tax"</samp>):
<input name="L" value="<?php echo htmlentities(@$_REQUEST['L']); ?>"/></label></p>
<p><label>Space-separated substrings of keywords (quotes allowed, eg: <samp>"check account" contact</samp>):
<input name="K" value="<?php echo htmlentities(@$_REQUEST['K']); ?>"/></label></p>
<p><button type="submit" name="doQuery" value="1">Refresh</button></p>
</header>
<?php
require('query.inc.php');
require('thumbs.inc.php');
?>
</form>
<footer><p>PaperWeb - Copyright 2016 Yves Gablin - Based on Paperwork by jflesch.</p></footer>
</body></html>
<?php
}