Skip to content

Commit

Permalink
/download ask for credentials if visitor close #385
Browse files Browse the repository at this point in the history
+ add download button in home view when possible.
  • Loading branch information
vincent-peugnet committed Jan 19, 2024
1 parent 0d66967 commit 2e8e73f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
39 changes: 24 additions & 15 deletions app/class/Controllerpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function importpage(): bool
public function pageconnect(string $route)
{
if ($this->user->isvisitor()) {
http_response_code(401);
$this->showtemplate('connect', ['route' => $route, 'id' => $this->page->id()]);
exit;
}
Expand Down Expand Up @@ -302,23 +303,31 @@ public function download($page)
{
$this->setpage($page, 'pagedownload');

if ($this->importpage() && $this->canedit($this->page)) {
$file = Model::PAGES_DIR . Config::pagetable() . DIRECTORY_SEPARATOR . $page . '.json';

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/json; charset=utf-8');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
} else {
if (!$this->importpage()) {
$this->routedirect('pageread', ['page' => $page]);
}

$this->pageconnect('pagedownload');

if (!$this->canedit($this->page)) {
http_response_code(403);
$this->showtemplate('forbidden', ['id' => $this->page->id(), 'route' => 'pagedownload']);
exit;
}

$file = Model::PAGES_DIR . Config::pagetable() . DIRECTORY_SEPARATOR . $page . '.json';

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/json; charset=utf-8');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/view/templates/forbidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
<h1>Forbidden</h1>

<span>
<?= $user->level() ?>
<?= $this->e($user->level()) ?>
</span>

<?php if($user->isinvite()) { ?>
<p>
Sorry <?= $user->name() ?>, you are not allowed to do this.
Sorry <?= $this->e($user->name()) ?>, you are not allowed to do this.
</p>
<?php } ?>

<?php
if(in_array($route, ['pageedit', 'pageread', 'pageadd'])) {
if(in_array($route, ['pageedit', 'pageread', 'pageadd', 'pagedownload'])) {
echo '<p><a href="' . $this->upage('pageread', $id) . '">back to page read view</a></p>';
} else {
echo '<p><a href="' . $this->url('home') . '">Go back to home</a>';
Expand Down
13 changes: 5 additions & 8 deletions app/view/templates/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,13 @@
<th class="edit"></th>
<th class="read"></th>
<th class="delete" title="delete page"></th>
<?php if ($user->issupereditor()) { ?>
<th class="download" title="download page as json"></th>
<?php }
if ($columns['tag']) { ?>
<?php if ($columns['tag']) { ?>
<th class="tag">
<a href="<?= $opt->sortbyorder('tag') ?>">tag</a>
<?= $this->insert('macro_tablesort', ['opt' => $opt, 'th' => 'tag']) ?>
</th>
<?php }
if ($columns['title']) { ?>
<?php } if ($columns['title']) { ?>
<th class="title">
<a href="<?= $opt->sortbyorder('title') ?>">title</a>
<?= $this->insert('macro_tablesort', ['opt' => $opt, 'th' => 'title']) ?>
Expand Down Expand Up @@ -296,14 +293,14 @@
</a>
<?php } ?>
</td>
<?php if ($user->issupereditor()) { ?>
<td class="download">
<?php if ($this->caneditpage($item)) { ?>
<a href="<?= $this->upage('pagedownload', $item->id()) ?>" download>
<i class="fa fa-download"></i>
</a>
<?php } ?>
</td>
<?php }
if ($columns['tag']) { ?>
<?php if ($columns['tag']) { ?>
<td class="tag"><?= $opt->taglinks($item->tag('array')) ?></td>
<?php }
if ($columns['title']) { ?>
Expand Down

0 comments on commit 2e8e73f

Please sign in to comment.