possibilité de révoquer un partage

This commit is contained in:
max/sooulix 2025-02-11 00:22:13 +01:00
parent c49abfb35b
commit acd0796f5f
7 changed files with 180 additions and 81 deletions

View File

@ -25,26 +25,23 @@ function write_line($dirname, $name, $description, $creation, $expiration, $expi
); );
} }
if (isset($_POST) && isset($_GET['action']) && $_GET['action'] == 'add') $creation = (new DateTime())->format(DateTimeInterface::ISO8601);
{ $expires = current_date_offset($_POST['expiration']);
$creation = (new DateTime())->format(DateTimeInterface::ISO8601); write_line(
$expires = current_date_offset($_POST['expiration']); $_POST['dirname'],
write_line( $_POST['name'],
$_POST['dirname'], $_POST['description'],
$_POST['name'], $creation,
$_POST['description'], $_POST['expiration'],
$creation, $expires
$_POST['expiration'], );
$expires $new_share = [
); "dir" => $_POST['dirname'],
$new_share = [ "name" => $_POST['name'],
"dir" => $_POST['dirname'], "description" => $_POST['description'],
"name" => $_POST['name'], "creation" => $creation,
"description" => $_POST['description'], "expiration" => $_POST['expiration'],
"creation" => $creation, "expires" => current_date_offset($_POST['expiration'])
"expiration" => $_POST['expiration'], ];
"expires" => current_date_offset($_POST['expiration']) $new_share["link"] = "./read.php?id=" . gen_identifier($new_share);
];
$new_share["link"] = "./read.php?id=" . gen_identifier($new_share);
}
?> ?>

View File

@ -8,7 +8,7 @@ function gen_identifier($share)
[ [
$GLOBALS['SANDDIR_SALT'], $GLOBALS['SANDDIR_SALT'],
$share['dir'], $share['dir'],
$share['creation'] str_replace(' ', '+', $share['creation'])
] ]
) )
); );
@ -37,6 +37,27 @@ function tsv_path($dir)
]); ]);
} }
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) function read_tsv($dir, $path)
{ {
@ -49,19 +70,8 @@ function read_tsv($dir, $path)
{ {
continue; continue;
} }
$split = explode("\t", $line);
if (count($split) < 5) $shares[] = get_struct($dir, $line);
{
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; return $shares;
} }
@ -175,6 +185,27 @@ function zip_response($share)
exit; 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 _($var) function _($var)
{ {
print($var); print($var);

View File

@ -1,3 +1,11 @@
<?php
if (!$selected_dir)
{
?>
Select a directory to share
<?php
}
?>
<ul> <ul>
<?php <?php
foreach (dir_list($GLOBALS['SANDDIR_ROOT_DIR']) as $share_dir) foreach (dir_list($GLOBALS['SANDDIR_ROOT_DIR']) as $share_dir)

View File

@ -1,22 +1,35 @@
<html> <html>
<?php <?php
$new_share = NULL; $new_share = NULL;
$selected_dir = NULL;
require_once('./common.php'); require_once('./common.php');
require_once('./config_sanddir.php'); require_once('./config_sanddir.php');
include('./add_share.php'); if (isset($_GET['dir']))
{
include('./dir_list_template.php'); $selected_dir = $_GET['dir'];
}
else
{
include('./dir_list_template.php');
}
if (isset($_POST) && isset($_GET['action']) && $_GET['action'] == 'add')
{
include('./add_share.php');
}
if ($new_share) if ($new_share)
{ {
include('new_share_template.php'); include('new_share_template.php');
} }
else if (isset($_GET['dir'])) else if ($selected_dir)
{ {
$selected_dir = $_GET['dir']; if (isset($_GET['action']) && $_GET['action'] == 'remove')
{
include('./rm_share.php');
}
include('./sanddir_template.php'); include('./sanddir_template.php');
} }

View File

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

36
src/rm_share.php Normal file
View File

@ -0,0 +1,36 @@
<?php
function remove_share($dir, $creation)
{
$identifier = gen_identifier([
'dir' => $dir,
'creation' => $creation
]);
$tsv = tsv_path($dir);
$keep = [];
$lines = file_get_contents($tsv);
foreach (explode("\n", $lines) as $line)
{
if (!$line)
{
continue;
}
$share = get_struct($dir, $line);
if (!(gen_identifier($share) == $identifier))
{
$keep[] = $line;
}
}
file_put_contents($tsv, implode("\n", $keep));
header("Location: index.php?dir=" . $dir);
}
remove_share($selected_dir, $_GET['creation']);
?>

View File

@ -30,47 +30,61 @@ if (isset($_GET['action']) && $_GET['action'] == 'share')
</form> </form>
<?php <?php
} }
?> $shares = read_tsv($selected_dir, tsv_path($selected_dir));
<h2>Existing shares for <?php _($selected_dir) ?></h2> if (count($shares))
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Creation</th>
<th>Expiration</th>
</tr>
<?php
foreach (read_tsv($selected_dir, tsv_path($selected_dir)) as $share)
{ {
?> ?>
<tr>
<td> <h2>Existing shares for <?php _($selected_dir) ?></h2>
<?php _($share['name']) ?>
</td> <table>
<td> <tr>
<?php _($share['description']) ?> <th>Name</th>
</td> <th>Description</th>
<td> <th>Creation</th>
<?php <th>Expiration</th>
_( <th>Actions</th>
explode('T', $share['creation'])[0] </tr>
); <?php
?> foreach ($shares as $share)
</td> {
<td> ?>
<?php <tr>
_( <td>
$share['expires'] <?php _($share['name']) ?>
? explode('T', $share['expires'])[0] </td>
: 'never' <td>
); <?php _($share['description']) ?>
?> </td>
</td> <td>
</tr> <?php
_(
explode('T', $share['creation'])[0]
);
?>
</td>
<td>
<?php
_(
$share['expires']
? explode('T', $share['expires'])[0]
: 'never'
);
?>
</td>
<td>
<a href="?dir=<?php _($selected_dir) ?>&action=remove&creation=<?php _($share['creation']) ?>">
Delete
</a>
</td>
</tr>
<?php
}
?>
</table>
<?php <?php
} }
?> ?>
</table>