File: app.inc.php

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (248 lines) | stat: -rw-r--r-- 7,342 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
 * App class
 *
 * This file is part of Zoph.
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package Zoph
 * @author Jeroen Roos
 */

use auth\web as auth;
use generic\controllerInterface;
use requirements\check;
use router\routerInterface;
use web\request;
use web\session;
use web\view\viewInterface;

use db\exception as databaseException;

/**
 * App class
 */
abstract class app {
    private auth $auth;
    public ?viewInterface $view;
    public controllerInterface $controller;
    private ?user $user;
    protected static app $app;
    protected static session $session;
    private static routerInterface $router;
    protected static int $mode = self::RUN;
    public static string $page;
    public const RUN = 0;
    public const CLI = 1;
    public const LOGIN = 2;
    public const INSTALL = 4;
    public const TEST = 8;

    protected static $viewFailedRequirements    = \requirements\view\failed::class;
    protected static $viewRedirect              = \web\view\redirect::class;


   /**
    * Create app
    */
    public function __construct(private request $request) {
        static::$router = $this->getRouter($this->request);
        static::$page = $request->getController();

        $action = $request->getAction();
        if (!empty($action)) {
            static::$page .= "/" . $action;
        }

        $this->controller = static::$router->controller;
        if ($this->controller instanceof install\controller) {
            self::setMode(self::INSTALL);
        }
        if ($this->controller instanceof zoph\controller && static::$router->action == "login") {
            self::setMode(self::LOGIN);
        }
        if (self::getMode(self::TEST)) {
            return;
        }

       /**
        * This code takes care of logging in or redirecting to install
        * this cannot be executed by PHP Unit, hence the return above
        * the code called by these lines is tested elsewhere
        */
        // @codeCoverageIgnoreStart

        try {
            self::$session = new session();
            $this->loadSettings();
            self::$session->start();
            $this->auth();
            $this->breadcrumbs();
        } catch (configurationException | databaseException $e) {
            if (!self::getMode(self::INSTALL)) {
                self::setMode(self::INSTALL);
                if (!$this->controller instanceof template\controller) {
                    $this->view = new static::$viewRedirect($this->request);
                    $this->view->setRedirect("install", "Run install script");
                }
            }
        }

        if (!$this->controller instanceof template\controller) {
            $this->checkRequirements();
        }

        // @codeCoverageIgnoreEnd

    }

   /**
    * Get the current execution mode
    * return whether a mode is set
    */
    public static function getMode(int $mode) : bool {
        return (bool) (static::$mode & $mode);
    }

   /**
    * Set the current execution mode
    * multiple modes can be active at the same time, so logically combine the
    * new mode with the existing modes.
    */
    public static function setMode(int $mode) {
        static::$mode |= $mode;
    }

   /**
    * Unset an execution mode
    * multiple modes can be active at the same time, so logically substract the
    * given mode from the existing modes.
    */
    public static function unsetMode(int $mode) : void {
        static::$mode &= ~$mode;
    }

   /**
    * Start authentication process.
    * @codeCoverageIgnore
    */
    private function auth() {
        $this->auth = new auth(self::$session, $this->request);
        $this->user = $this->auth->getUser();
        if ($this->auth->getRedirect() && self::$mode == self::RUN) {
            $this->view = new static::$viewRedirect($this->request);
            $this->view->setRedirect($this->auth->getRedirect());
        }
        if (!$this->user) {
            static::setMode(self::LOGIN);
        }
    }

   /**
    * Run the application
    */
    public function run() {
        if (!isset($this->view)) {
            $this->controller->doAction(static::$router->action);
            $this->view = $this->controller->getView();
        }
    }

   /**
    * Start logout process.
    * @codeCoverageIgnore
    */
    public static function logout(request $request) : void {
        $auth = new auth(self::$session, $request);
        $auth->logout();
    }

   /**
    * Get the basepath, needed if Zoph is installed in a subdir
    */
    public static function getBasePath() : string {
        return static::$router->basepath;
    }

   /**
    * Register breadcrumb, handle clearing or 'eat'ing breadcrumbs
    * @codeCoverageIgnore
    */
    private function breadcrumbs() : void {
        breadcrumb::init($this->request);

        if ($this->request["_clear_crumbs"]) {
            breadcrumb::eat(0);
        } else if ($this->request["_crumb"]) {
            breadcrumb::eat($this->request["_crumb"]);
        }
    }


    private function loadSettings() : void {
        if (!static::getMode(self::CLI)) {
            $i=settings::loadINI();
            settings::parseINI($i);
        }
    }

   /**
    * Load settings for unittest
    * This cannot use autodetection of PHP location
    * because the code is executed from PHPUnit context
    */
    public static function loadSettingsTest(string $instance) : void {
        $i=settings::loadINI($instance);
        settings::parseINI($i, test: true);
    }

    public function checkRequirements() {
        $requirements = new check();
        $requirements->doChecks();
        if (!$requirements->getStatus()) {
            $this->view=new static::$viewFailedRequirements($this->request);
            $this->view->addRequirements($requirements);
        }

    }

    /**
     * Set a token that can be used to prevent CSRF attacks
     * @return the token
     */
    public static function setToken() : string {
        $session = session::getCurrent();
        $session["token"] = hash("sha256", random_bytes(64));
        return $session["token"];
    }

    /**
     * Get the token, if it's not set, return a random token
     */
    public static function getToken() : string {
        $session = session::getCurrent();
        return $session["token"] ?? hash("sha256", random_bytes(64));
    }

    /**
     * Check a given token against the token that is set in the session cookie
     */
    public static function checkToken(string $token) : bool {
        return (!empty($token) && (self::getToken() == $token));
    }


    abstract protected function getRouter(request $request) : routerInterface;
}