initial commit

This commit is contained in:
max/sooulix 2025-02-10 21:24:31 +01:00
commit 43f4d75164
9 changed files with 348 additions and 0 deletions

50
src/add_share.php Normal file
View File

@ -0,0 +1,50 @@
<?php
function current_date_offset($offset)
{
if ($offset == 'U')
{
return "";
}
$date = date_create();
$date->add(new DateInterval("P" . $offset));
return $date->format(DateTimeInterface::ISO8601);
}
function write_line($dirname, $name, $description, $creation, $expiration, $expires)
{
file_put_contents(
tsv_path($dirname),
implode("\t", [
$name,
$description,
$creation,
$expiration,
$expires . "\n"
]),
FILE_APPEND
);
}
if (isset($_POST) && isset($_GET['action']) && $_GET['action'] == 'add')
{
$creation = (new DateTime())->format(DateTimeInterface::ISO8601);
$expires = current_date_offset($_POST['expiration']);
write_line(
$_POST['dirname'],
$_POST['name'],
$_POST['description'],
$creation,
$_POST['expiration'],
$expires
);
$new_share = [
"dir" => $_POST['dirname'],
"name" => $_POST['name'],
"description" => $_POST['description'],
"creation" => $creation,
"expiration" => $_POST['expiration'],
"expires" => current_date_offset($_POST['expiration'])
];
$new_share["link"] = "./read.php?id=" . gen_identifier($new_share);
}
?>

181
src/common.php Normal file
View File

@ -0,0 +1,181 @@
<?php
function gen_identifier($share)
{
return md5(
implode(
"$",
[
$GLOBALS['SANDDIR_SALT'],
$share['dir'],
$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 read_tsv($dir, $path)
{
$lines = explode("\n", file_get_contents($path));
$shares = [];
foreach ($lines as $line)
{
if (!$line)
{
continue;
}
$split = explode("\t", $line);
if (count($split) < 5)
{
throw new Exception("Error with line " . $line);
}
$shares[] = [
"dir" => $dir,
"name" => $split[0],
"description" => $split[1],
"creation" => $split[2],
"expiration" => $split[3],
"expires" => $split[4]
];
}
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 _($var)
{
print($var);
}
?>

4
src/config_sanddir.php Normal file
View File

@ -0,0 +1,4 @@
<?php
$SANDDIR_ROOT_DIR = './share';
$SANDDIR_SALT = 'abcde12345abcde12345';
?>

12
src/dir_list_template.php Normal file
View File

@ -0,0 +1,12 @@
<ul>
<?php
foreach (dir_list($GLOBALS['SANDDIR_ROOT_DIR']) as $share_dir)
{
?>
<li>
<a href="?dir=<?php _($share_dir) ?>"><?php _($share_dir) ?></a>
</li>
<?php
}
?>
</ul>

24
src/index.php Normal file
View File

@ -0,0 +1,24 @@
<html>
<?php
$new_share = NULL;
require_once('./common.php');
require_once('./config_sanddir.php');
include('./add_share.php');
include('./dir_list_template.php');
if ($new_share)
{
include('new_share_template.php');
}
else if (isset($_GET['dir']))
{
$selected_dir = $_GET['dir'];
include('./sanddir.php');
}
?>
</html>

View File

@ -0,0 +1,5 @@
Share "<?php _($new_share["dir"]) ?> with following link : <a href="<?php _($new_share["link"]) ?>"><?php _($new_share["link"]) ?></a>
<?php
print_r($new_share);
?>

16
src/read.php Normal file
View File

@ -0,0 +1,16 @@
<?php
require_once('./common.php');
require_once('./config_sanddir.php');
if (isset($_GET['id']))
{
foreach (share_list($GLOBALS['SANDDIR_ROOT_DIR']) as $share)
{
if (gen_identifier($share) == $_GET['id'])
{
zip_response($share['dir']);
}
}
}
?>

8
src/sanddir.php Normal file
View File

@ -0,0 +1,8 @@
<?php
function share_links($dir)
{
return [];
}
include('./sanddir_template.php');
?>

48
src/sanddir_template.php Normal file
View File

@ -0,0 +1,48 @@
<h1>Sanddir <?php _($selected_dir); ?></h1>
<a href="?dir=<?php _($selected_dir); ?>&action=share">Add a share</a>
<?php
if (isset($_GET['action']) && $_GET['action'] == 'share')
{
?>
<form method="POST" action="index.php?dir=<?php _($selected_dir) ?>&action=add">
<input name="dirname" id="dirname" type="hidden" value="<?php _($selected_dir) ?>" />
<label for="name">Name</label>
<input name="name" id="name" type="text" />
</br>
<label for="description">Description</label>
<input name="description" id="description" type="text" />
</br>
<label for="expiration">Expiration</label>
<select name="expiration" id="expiration">
<option value="1D">1 day</option>
<option value="1W">1 week</option>
<option value="1M">1 month</option>
<option value="1Y">1 year</option>
<option value="U">undefined</option>
</select>
</br>
<input type="submit" />
</form>
<?php
}
?>
<h2>Existing links</h2>
<ul>
<?php
foreach (share_links($selected_dir) as $link)
{
?>
<li>
<a href="<?php _($link) ?>"><?php _($link) ?></a>
</li>
<?php
}
?>
</ul>