sanddir/src/common.php

239 lines
4.6 KiB
PHP

<?php
function checkpw()
{
return (
isset($_SESSION['token']) &&
$_SESSION['token'] == md5($GLOBALS['SANDDIR_USER'] . $GLOBALS['SANDDIR_PASSWORD'])
) ||
isset($_POST['user']) &&
isset($_POST['password']) &&
$_POST['user'] == $GLOBALS['SANDDIR_USER'] &&
$_POST['password'] == $GLOBALS['SANDDIR_PASSWORD'];
}
function gen_identifier($share)
{
return md5(
implode(
"$",
[
$GLOBALS['SANDDIR_SALT'],
$share['dir'],
str_replace(' ', '+', $share['creation'])
]
)
);
}
function dir_list($dir)
{
$children = array();
foreach (scandir($dir) as $child)
{
if ($child[0] != '.')
{
$chidren[] = $child;
}
}
return $chidren;
}
function tsv_path($dir)
{
return implode('/', [
$GLOBALS['SANDDIR_ROOT_DIR'],
'.' . $dir . '.sanddir.tsv'
]);
}
function get_struct($dir, $line)
{
if (!$line)
{
throw new Exception("Line is not compatible");
}
$split = explode("\t", $line);
if (count($split) < 5)
{
throw new Exception("Error with line " . $line);
}
return [
"dir" => $dir,
"name" => $split[0],
"description" => $split[1],
"creation" => $split[2],
"expiration" => $split[3],
"expires" => $split[4]
];
}
function read_tsv($dir, $path)
{
$lines = explode("\n", file_get_contents($path));
$shares = [];
foreach ($lines as $line)
{
if (!$line)
{
continue;
}
$shares[] = get_struct($dir, $line);
}
return $shares;
}
function share_list($dir)
{
return array_reduce(
dir_list($dir),
function($carry, $child)
{
$tsv = tsv_path($child);
if (is_file($tsv))
{
return array_merge($carry, read_tsv($child, $tsv));
}
return $carry;
},
[]
);
}
function zip_dir($dir)
{
$filename = tempnam('./tmp', '');
$zip = new ZipArchive();
$zip->open($filename, ZipArchive::CREATE);
function inner($stack, $result)
{
$path = implode('/', $stack);
if (is_file($path))
{
array_push($result, $path);
}
else
{
foreach (scandir($path) as $elt)
{
if ($elt[0] == '.')
{
continue;
}
else if (is_dir($path))
{
array_push($stack, $elt);
$result = inner($stack, $result);
array_pop($stack);
}
}
}
return $result;
}
$result = inner([$GLOBALS['SANDDIR_ROOT_DIR'] . '/' . $dir], []);
sort($result);
$dirs = [];
$files = [];
foreach ($result as $path)
{
$path_without_root_dir = array_slice(
explode('/', $path), 2
);
$files[] = [
$path,
implode('/', $path_without_root_dir)
];
$dirs[] = array_slice($path_without_root_dir, 0, count($path_without_root_dir) -1);
}
sort($dirs);
$dirs = array_unique(
array_map(
function($item)
{
return implode('/', $item);
},
$dirs
)
);
foreach ($dirs as $dir)
{
$zip->addEmptyDir($dir);
}
foreach ($files as $file)
{
$zip->addFile($file[0], $file[1]);
}
$zip->close();
return $filename;
}
function zip_response($share)
{
$filename = zip_dir($share);
$size = filesize($filename);
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=' . $share . '.zip');
header('Content-Length: ' . $size);
ob_end_clean();
flush();
readfile($filename);
exit;
}
function find_share($dir, $creation)
{
$shares = read_tsv(
$dir,
tsv_path($dir)
);
$identifier = gen_identifier([
'dir' => $dir,
'creation' => $creation
]);
foreach ($shares as $share)
{
if (gen_identifier($share) == $identifier)
{
return $share;
}
}
return NULL;
}
function error_code($int)
{
header("Location: /", TRUE, $int);
exit();
}
function absolute_link($relpath)
{
$scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
$base_uri = $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);
return $scheme . $base_uri . $relpath;
}
function _($var)
{
print($var);
}
?>