paperweb/web/index.php

141 lines
5.4 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("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>
<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>
<section id="result">
<?php
if (array_key_exists('doQuery', $_REQUEST)) {
$pattern='/(?<=")(?:[^"]|\\\\")*[^" ](?:[^"]|\\\\")*(?=")|[^ "]+/';
preg_match_all($pattern, $_REQUEST['D'], $matches);
$dates = escapeshellarg(implode('|', $matches[0]));
preg_match_all($pattern, $_REQUEST['L'], $matches);
$labels = escapeshellarg(implode('|', $matches[0]));
preg_match_all($pattern, $_REQUEST['K'], $matches);
$words = escapeshellarg(implode('|', $matches[0]));
$json = exec("sudo -u {$CONF['USER']} {$CONF['PATH']} -Q -d {$dates} -l {$labels} -k {$words} -i");
unset($_REQUEST['doThumbnails']);
unset($_REQUEST['thumbnailsDone']);
unset($_REQUEST['currentDoc']);
} else {
$json = @$_REQUEST['queryDone'];
}
if ($json) {
if ($json == '[]') {
?>
<p>No result.</p>
<?php
} else {
$current = (array_key_exists('doThumbnails', $_REQUEST) ? $_REQUEST['doThumbnails'] : @$_REQUEST['currentDoc']);
?>
<h2>Documents found</h2>
<input type="hidden" name="queryDone" value="<?php echo htmlentities($json); ?>"/>
<?php
foreach (json_decode($json, true) as $doc) {
?>
<button type="submit" name="doThumbnails" value="<?php echo htmlentities($doc['folder']); ?>"<?php
if ($doc['folder'] == $current) {
echo ' disabled="disabled"';
}
?>>
<h3><?php echo preg_replace('/^(....)(..)(..).*$/', '$1-$2-$3', $doc['folder']); ?></h3>
<p><?php echo $doc['count']; ?> pages (<?php echo ($doc['type']=='pdf'?'PDF':'images'); ?>)</p>
<?php if (count($doc['labels'])) { ?>
<ul>
<?php foreach ($doc['labels'] as $lab) { ?>
<li><?php echo htmlentities($lab); ?></li>
<?php } ?>
</ul>
<?php } ?>
</button>
<?php
}
}
}
?>
</section>
<section id="thumbs">
<?php
if (array_key_exists('doThumbnails', $_REQUEST)) {
$date = $_REQUEST['doThumbnails'];
$datearg = escapeshellarg($date);
$json = exec("sudo -u {$CONF['USER']} {$CONF['PATH']} -T {$datearg}");
} else {
$json = @$_REQUEST['thumbnailsDone'];
$date = @$_REQUEST['currentDoc'];
}
if ($json) {
?>
<h2>Pages</h2>
<input type="hidden" name="thumbnailsDone" value="<?php echo htmlentities($json); ?>"/>
<input type="hidden" name="currentDoc" value="<?php echo htmlentities($date); ?>"/>
<?php
foreach (json_decode($json, true) as $n => $p) {
$nump = $n+1;
?>
<a target="_blank" href="?<?php echo htmlentities("doDownload=1&date={$date}&page={$nump}"); ?>"><img
src="data:<?php echo $p['mime']; ?>;base64,<?php echo $p['data']; ?>"
width="<?php echo $p['width']; ?>" height="<?php echo $p['height']; ?>"
alt="Page <?php echo $nump; ?>"/></a>
<?php
}
}
?>
</section>
</form>
<footer><p>PaperWeb - Copyright 2016 Yves Gablin - Based on Paperwork by jflesch.</p></footer>
</body></html>
<?php
}