pub / rememori

Simple file-based bookmarking and notes application
git clone https://src.jayvii.de/pub/rememori.git
Home | Log | Files | Exports | Refs | README | RSS

index.php (5829B)


      1 <!-- SPDX-License-Identifier: AGPL-3.0-or-later
      2      SPDX-FileCopyrightText: 2024 JayVii <jayvii[AT]posteo[DOT]de>
      3 -->
      4 
      5 <?php
      6 
      7     /* Load library functions */
      8     foreach (glob("./lib/*.php") as $lib) {
      9         include($lib);
     10     }
     11 
     12     /* Load configurations */
     13     foreach (glob("./config/*.php") as $conf) {
     14         include($conf);
     15     }
     16 
     17     /* Gather POST input and validate if necessary */
     18     $action = gather_post("action");
     19     $category = validate_input_string(gather_post("category"));
     20     $filename = validate_input_string(gather_post("filename"));
     21     $filepath_t1 = gather_post("filepath_t1");
     22     $content = gather_post("content");
     23     $query = gather_post("query");
     24     $user = validate_input_string(gather_post("user"));
     25     $pass = gather_post("pass");
     26     $token = ""; // initilise empty
     27     $cookie = (int)validate_input_string(gather_post("cookie")); // cast to int
     28 
     29     /* if password is given, but token is not, create the token! */
     30     $token = "";
     31     if (!empty($pass)) {
     32         $token = create_password_hash($pass);
     33     }
     34 
     35     /* read information from cookie */
     36     if (array_key_exists("session", $_COOKIE)) {
     37         $user = explode("|", $_COOKIE["session"])[0];
     38         $token = explode("|", $_COOKIE["session"])[1];
     39     }
     40 
     41     /* if no action is given, set it to "categories" initially */
     42     if (empty($action)) {
     43         $action = "categories";
     44     }
     45 
     46     /* Check authentification of user */
     47     $auth = auth_user($user, $token);
     48     if ($auth !== true) {
     49         $action = "login";
     50     } else {
     51         set_auth_cookie($user, $token, $cookie);
     52     }
     53 
     54     /* ensure the given filename (if it is set!) has a valid extension */
     55     if (
     56         (count(preg_grep("/\.txt$/", array($filename), PREG_GREP_INVERT)) > 0)
     57         &&
     58         (!empty($filename))
     59     ){
     60         $filename = $filename . ".txt";
     61     }
     62 
     63 ?>
     64 
     65 <?php
     66 
     67     /* Actions Block 1: Actions that do not print */
     68 
     69     /* Logout action */
     70     if ($action == "logout") {
     71         destroy_session();
     72         /* set action to "login", so we return to the login screen again */
     73         $action = "login";
     74     }
     75 
     76     /* New Password action */
     77     if ($action == "add_password") {
     78         add_auth(
     79             $user,
     80             gather_post("new_pass")
     81         );
     82         $action = "account";
     83     }
     84 
     85     /* Update tokens action */
     86     if ($action == "update_tokens") {
     87         update_tokens($user);
     88         $action = "account";
     89     }
     90 
     91     /* Edit action */
     92     if ($action == "edit") {
     93         /* if no filename is given, try to come up with one ourselves */
     94         if (empty($filename)) {
     95             $filename = generate_filename($content);
     96         }
     97         if (empty($category)) {
     98             $category = "unknown";
     99         }
    100         /* edit note */
    101         $filename = edit_note(
    102             $user,
    103             $category,
    104             $filename,
    105             $filepath_t1,
    106             $content
    107         );
    108         /* set action to "show", so the new file will be shown afterwards */
    109         $action = "show";
    110     }
    111 
    112     /* Deletion action */
    113     if ($action == "delete") {
    114         delete_note(
    115             $user,
    116             $category,
    117             $filename
    118         );
    119         /* set action to "list", so we return to the main view again */
    120         $action = "list";
    121     }
    122 
    123 ?>
    124 
    125 <!DOCTYPE html>
    126 <html>
    127 
    128     <head>
    129         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    130         <title>Rememori</title>
    131         <link rel="icon" type="image/png" href="/assets/img/favicon.png">
    132         <link rel="icon" type="image/png" sizes="16x16" href="/assets/img/favicon_16.png">
    133         <link rel="icon" type="image/png" sizes="32x32" href="/assets/img/favicon_32.png">
    134         <link rel="icon" type="image/png" sizes="64x64" href="/assets/img/favicon_64.png">
    135         <link rel="icon" type="image/png" sizes="128x128" href="/assets/img/favicon_128.png">
    136         <link rel="icon" type="x-image/ico" sizes="32x32" href="/assets/img/favicon.ico">
    137         <link rel="apple-touch-icon" type="image/png" href="/assets/img/favicon.png">
    138         <link rel=stylesheet href=/assets/css/simple.min.css media=all>
    139         <link rel=stylesheet href=/assets/css/custom.css>
    140         <link crossorigin="use-credentials" rel="manifest" href="/manifest.json">
    141         <script async src="/assets/js/deletion.js"></script>
    142         <meta name="viewport" content="width=device-width, initial-scale=1.0">
    143     </head>
    144 
    145     <body>
    146 
    147 <?php
    148 
    149     /* Only show Header if we are not at login */
    150     if ($action != "login") {
    151 
    152 ?>
    153 
    154     <header>
    155         <!-- Buttons -->
    156         <?php top_navigation(); ?>
    157         <!-- Headline -->
    158         <h1>Rememori</h1>
    159         <!-- Search bar -->
    160         <?php search_bar($query); ?>
    161     </header>
    162 
    163 <?php
    164 
    165     } else {
    166 
    167 ?>
    168 
    169     <header>
    170         <!-- Buttons -->
    171         <nav>
    172             <a href="https://src.jayvii.de/pub/rememori/" target="_blank">
    173                 <?php echo $GLOBALS["i18n_development"]; ?>
    174             </a>
    175         </nav>
    176 
    177         <!-- Headline -->
    178         <h1>Rememori</h1>
    179     </header>
    180 
    181 <?php
    182 
    183     } // if-statement
    184 
    185     /* Actions Block 2: Actions that print */
    186 
    187     /* Login action */
    188     if ($action == "login") {
    189         show_login_form("categories");
    190         die(); // ensure the process stops after this
    191     }
    192 
    193     /* Account action */
    194     if ($action == "account") {
    195         show_account($user, $token);
    196     }
    197 
    198     /* Listing action */
    199     if ($action == "list") {
    200         list_notes($user, $category);
    201     }
    202 
    203     if ($action == "categories") {
    204         list_categories($user);
    205     }
    206 
    207     /* Show action */
    208     if ($action == "show") {
    209         show_note(
    210             $user,
    211             $category,
    212             $filename
    213         );
    214     }
    215 
    216     /* Search action */
    217     if ($action == "search") {
    218         search_notes(
    219             $user,
    220             $query
    221         );
    222     }
    223 
    224 ?>
    225 
    226     </body>
    227 </html>