pupupu

simple static CMS for crappy servers
git clone https://git.ce9e.org/pupupu.git

commit
b2f44bb2e184a0b6bf85cb1aa1c1181fb5039031
parent
363862e50717edbcda3ce7d90aa6eaa507206235
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2018-10-20 22:04
render error page

Diffstat

M index.php 40 +++++++++++++++++++++++++++-------------
A templates/error.html 5 +++++

2 files changed, 32 insertions, 13 deletions


diff --git a/index.php b/index.php

@@ -4,6 +4,8 @@ require __DIR__ . '/vendor/autoload.php';
    4     4 use Symfony\Component\Yaml\Yaml;
    5     5 use Michelf\MarkdownExtra;
    6     6 
   -1     7 class HttpException extends Exception {}
   -1     8 
    7     9 function trans($s)
    8    10 {
    9    11     return $s;
@@ -82,8 +84,7 @@ function validatePath($path)
   82    84         substr($path, -1) === '/' ||
   83    85         strpos($path, '..') !== false
   84    86     ) {
   85    -1         http_response_code(400);
   86    -1         die();
   -1    87         throw new HttpException(trans('Not Found'), 404);
   87    88     } else {
   88    89         return $path;
   89    90     }
@@ -316,7 +317,7 @@ function filesView($pupupu, $twig)
  316   317         $pupupu->rmFile($path . '/' . $_POST['name']);
  317   318         header('Location: ', true, 302);
  318   319     } else {
  319    -1         http_response_code(400);
   -1   320         throw new HttpException(trans('Invalid request'), 400);
  320   321     }
  321   322 }
  322   323 
@@ -349,8 +350,7 @@ function pageView($pupupu, $twig)
  349   350             ));
  350   351         } elseif (isset($_POST['delete'])) {
  351   352             if ($path === '') {
  352    -1                 http_response_code(400);
  353    -1                 die();
   -1   353                 throw new HttpException(trans('Cannot delete root'), 400);
  354   354             }
  355   355             $pupupu->rm($path);
  356   356             $target = pathDirname($path);
@@ -365,6 +365,14 @@ function pageView($pupupu, $twig)
  365   365     }
  366   366 }
  367   367 
   -1   368 function errorView($pupupu, $twig, $error)
   -1   369 {
   -1   370     http_response_code($error->getCode());
   -1   371     echo $twig->render('error.html', array(
   -1   372         'error' => $error,
   -1   373     ));
   -1   374 }
   -1   375 
  368   376 $pupupu = new Pupupu('..', '..', '..');
  369   377 
  370   378 if (isset($_SERVER['REQUEST_METHOD'])) {
@@ -374,14 +382,20 @@ if (isset($_SERVER['REQUEST_METHOD'])) {
  374   382     $twig = new Twig_Environment($loader);
  375   383     $twig->addFilter(new Twig_Filter('trans', 'trans'));
  376   384 
  377    -1     if (empty($_GET['path']) && $_GET['path'] !== '') {
  378    -1         pagesView($pupupu, $twig);
  379    -1     } elseif ($_GET['path'] === '_site') {
  380    -1         siteView($pupupu, $twig);
  381    -1     } elseif (substr($_GET['path'], 0, 6) === '_files') {
  382    -1         filesView($pupupu, $twig);
  383    -1     } else {
  384    -1         pageView($pupupu, $twig);
   -1   385     try {
   -1   386         if (empty($_GET['path']) && $_GET['path'] !== '') {
   -1   387             pagesView($pupupu, $twig);
   -1   388         } elseif ($_GET['path'] === '_site') {
   -1   389             siteView($pupupu, $twig);
   -1   390         } elseif (substr($_GET['path'], 0, 6) === '_files') {
   -1   391             filesView($pupupu, $twig);
   -1   392         } else {
   -1   393             pageView($pupupu, $twig);
   -1   394         }
   -1   395     } catch (Twig_Error_Loader $e) {
   -1   396         errorView($pupupu, $twig, new HttpException($e->getMessage(), 500));
   -1   397     } catch (HttpException $e) {
   -1   398         errorView($pupupu, $twig, $e);
  385   399     }
  386   400 } else {
  387   401     $pupupu->renderAll(true);

diff --git a/templates/error.html b/templates/error.html

@@ -0,0 +1,5 @@
   -1     1 {% extends 'base.html' %}
   -1     2 
   -1     3 {% block main %}
   -1     4     <h1>{{ error.code }} - {{ error.message }}</h1>
   -1     5 {% endblock %}