/**
handles paper identifiers for automated upload by myself & redirect to clients
usage:
upload: https://www.monperrus.net/martin/alleviating-patch-overfitting-emse18.pdf
paper list: https://www.monperrus.net/martin/papers
Martin Monperrus
Friday, March 15 2013
*/
class Paper {
var $slug='';
var $redirect='';
var $creation_time='';
}
global $suffix;
$suffix = '.paper';
define('PAPER_SUFFIX', $suffix);
function show() {
include('simplephpauth.php');
echo '
';
$papers = array();
foreach (glob('*'.PAPER_SUFFIX) as $k) {
$metadata = json_decode(file_get_contents($k), true);
$metadata['name'] = $k;
$papers[$metadata["creation_time"]] = $metadata;
}
krsort($papers);
foreach ($papers as $xx=>$metadata) {
$k=$metadata['name'];
$slug = substr($k,0,-strlen(PAPER_SUFFIX));
echo '';
echo ''."$slug.pdf".' | ';
$status = "pb";
if (isset($metadata['redirect'])) {
$status = "redirect";
if (preg_match('/^overleaf:(.*)/', $metadata['redirect'], $matches)) {
$status = "overleaf";
}
}
if (file_exists(actual_file($slug))) {
$status = "local";
if (isset($metadata['redirect'])) {
$status = "OOPS both redirect and local";
}
}
echo ''.$status.' | ';
// echo ''.json_encode($metadata).' | ';
// echo ''."del".' | ';
echo '
';
}
echo '
';
}
function form($paper=NULL) {
?>
New paper:
New paper with redirect / new redirect:
}
function actual_file($slug) {
$dir='pubdir';
return $dir.'/'.$slug.'.pdf';
};
function upload() {
$PASSWORD='yvbzPS7*z7zz4';
if (!isset($_REQUEST['slug'])) {
die ('$_REQUEST[\'slug\'] is not set');
}
if (!isset($_FILES[$PASSWORD])) {
die ("no files passed as parameter\nusage: upload-paper-to-monperrus.net slug file.pdf");
}
$slug=$_REQUEST['slug'];
// security check
$v = $_FILES[$PASSWORD];
if (!file_exists($slug.'.paper')) {
die("paper not registered!\nRegister at:\nhttp://www.monperrus.net/martin/papers?action=add&slug=".$slug."\n");
}
$where = actual_file($slug) ;
//print_r($v);
rename($v['tmp_name'],$where);
chmod($where,0755);
header('Content-type: text/plain');
echo "uploaded in ".$where." !\n";
echo "link: https://www.monperrus.net/martin/".$slug.".pdf\n";
} // end function upload
function metadata_file($slug) {
return $slug.PAPER_SUFFIX;
}
/** adds a paper in the database */
function add_paper() {
include('simplephpauth.php');
if (!isset($_REQUEST['slug']) || strlen($_REQUEST['slug'])==0) { die('no slug');}
if (preg_match('/pdf$/i',$_REQUEST['slug'])) { die('slug should not end with PDF');}
$slug = $_REQUEST['slug'];
if (file_exists(metadata_file($slug))) {
$data = $data=json_decode(file_get_contents(metadata_file($slug)),true);
} else {
$data = array();
}
if (!isset($data['creation_time'])) {
$data['creation_time'] = date('Ymd-Gi');
}
if (isset($_REQUEST['newredirect'])) {
$data['redirect'] = @$_REQUEST['newredirect'];
// remove the file if set up the redirect
if (file_exists(actual_file($_REQUEST['slug']))) {
unlink(actual_file($_REQUEST['slug']));
}
}
file_put_contents(metadata_file($_REQUEST['slug']), json_encode($data));
header('Location: http://www.monperrus.net/martin/papers');
exit;
}
function redirect($where) {
header("HTTP/1.1 301 Moved Permanently");
header('Location: '.$where);
exit;
}
function serve($slug) {
//echo metadata_file($slug);
$data=json_decode(file_get_contents(metadata_file($slug)),true);
if (isset($data['redirect'])) {
if (preg_match('/^http/', $data['redirect'])) {
redirect($data['redirect']);
exit;
}
if (preg_match('/^overleaf:(.*)/', $data['redirect'], $matches)) {
header('Content-type: application/pdf');
echo file_get_contents("https://www.monperrus.net/martin/get-overleaf-pdf.py?paper_id=".$matches[1]);
//redirect("https://www.monperrus.net/martin/get-overleaf-pdf.py?paper_id=".$matches[1]);
exit;
}
} else {
// TODO log
$phpmodlog_param=$_SERVER['REQUEST_URI'];
//echo PHPMODLOG_LOGFILENAME;
//phpmodlog_log($phpmodlog_param);
//if (!function_exists('phpmodlog_log')) echo 'couo';
include('phpmodlog.php');
//echo "cou";
header('Content-type: application/pdf');
readfile(actual_file($slug));
}
}
function dispatch() {
global $suffix;
if (isset($_REQUEST['serve'])) {
serve($_REQUEST['serve']);
exit;
}
if (isset($_REQUEST['newredirect'])) {
add_paper();
exit;
}
if (isset($_REQUEST['upload'])) {
upload();
exit;
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'add') {
add_paper();
exit;
}
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'del') {
unlink($_REQUEST['slug'].$suffix);
header('Location: http://www.monperrus.net/martin/papers');
exit;
}
show();
form();
}
dispatch();
?>