File: web.inc.php

package info (click to toggle)
zoph 1.0.1-3
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 16,480 kB
  • sloc: php: 27,195; javascript: 10,374; sql: 416; sh: 152; makefile: 4
file content (266 lines) | stat: -rw-r--r-- 8,130 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
 * Check if user is logged in, or perform authentication
 *
 * 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 Jason Geiger
 * @author Jeroen Roos
 */

namespace auth;

use conf\conf;
use upgrade\migrations;
use web\request;
use web\session;

use anonymousUser;
use language;
use log;
use settings;
use user;
use userNotFoundException;

/**
 * Check if user is logged in, or perform authentication
 * for users logging in via the web interface
 * This class lets a user pass through if one of the following is true:
 * - a valid username/password was given
 * - a $user object was found in the session
 * - a default user has been defined in config.inc.php
 *
 */
class web implements auth {

    private $session;
    private $request;
    private $user;
    private $lang;

    /**
     * Create a new auth object for web authentication
     * @param web\session Session object
     * @param web\request HTTP request object
     */
    public function __construct(session $session, request $request) {
        $this->session=$session;
        $this->request=$request;
        $migrations=false;
        $this->redirect="logon.php";
        $this->lang = new language(conf::get("interface.language"));
        if (isset($this->session['user'])) {
            $this->user = $this->session['user'];
        }

        if (empty($this->user)) {
            $this->checkRemoteUser();
            $migrations=$this->checkRequireMigrations();
        }

        if ($this->checkAnonymousUser()) {
            return;
        }

        if (!empty($this->user)) {
            if ($migrations) {
                $this->redirect="upgrade.php";
            } else if (!empty($this->request["redirect"])) {
                $this->setRedirect();
            }
        } else if ($migrations) {
            $this->setRedirectLogon("error=ADMINONLY&");
        } else if (!empty($this->request["uname"])) {
            /*  A username was given, but by now, no succesful logon has
                happened so, we'll redirect, with error set to PWDFAIL.
                This will show an error on the login screen.
                There is no indication what the exact problem was (unknown user,
                wrong password, etc.) as this might give an adversary more information
                than we want to give away */
            $this->setRedirectLogon("error=PWDFAIL&");
        } else {
            $this->setRedirectLogon();
        }
    }

    /**
     * Check for remote user authentication
     */
    private function checkRemoteUser() {
        if (conf::get("interface.user.remote") && $this->request->getServerVar("REMOTE_USER")) {
            $username = $this->request->getServerVar("REMOTE_USER");
            try {
                $user = user::getByName($username);
                $this->user = $user;
            } catch (userNotFoundException $e) {
                $this->loginUser();
            }
        } else {
            $this->loginUser();
        }
    }

    /**
     * Check if migrations are needed
     */
    private function checkRequireMigrations() {
        if ((!$this->user instanceof anonymousUser) && $this->user instanceof user && $this->checkMigrations()) {
            if (!$this->user->isAdmin()) {
                $this->user=null;
                unset($this->session["user"]);
            }
            return true;
        }
        return false;
    }

    /**
     * Check for anonymous user authentication
     */
    private function checkAnonymousUser() {
        if ($this->user instanceof anonymousUser) {
            if (!defined("IMAGE_PHP")) {
                $this->user=null;
            } else {
                user::setCurrent($this->user);
            }
            $this->redirect="";
            return true;
        } else if ($this->user instanceof user) {
            $this->loadUser();
            return false;
        }
    }

    /**
     * Get URL to redirect to
     * string|null URL or null when no redirect required
     */
    public function getRedirect() {
        return $this->redirect;
    }

    /**
     * Get language
     * @return language Translation object
     */
    public function getLang() {
        return $this->lang;
    }

    /**
     * Get logged on user
     * @return null|user logged on user or null if nobody succesfully authenticated
     */
    public function getUser() {
        return $this->user;
    }

    /**
     * Logout
     * @codeCoverageIgnore
     */
    public function logout() {
        $this->session->logout();
        $this->redirect="logon.php";
    }

    /**
     * Logon user
     * Authenticate user or perform login for an anonymous user
     */
    private function loginUser() {
        $hash=$this->request["hash"];
        if (defined("IMAGE_PHP") && conf::get("share.enable") && !empty($hash)) {
            $user = new anonymousUser();
        } else if (defined("IMAGE_PHP") && defined("IMAGE_BG")) {
            $user = new anonymousUser();
        } else {
            $uname = $this->request["uname"];
            $pword = $this->request["pword"];
            if (!empty($uname) || conf::get("interface.user.default") !== "0") {
                $validator = new validator($uname, $pword);
                $user = $validator->validate();
            }
        }
        if (isset($user)) {
            $this->user = $user;
            if (!($user instanceof anonymousUser)) {
                $this->session['user'] = $user;
            }
        }
    }

    /**
     * Load user
     * User has been authenticated and data for this user is loaded
     */
    private function loadUser() {
        $user=$this->user;
        $user->lookup();
        $user->lookupPerson();
        $user->lookupPrefs();

        $user->prefs->load();
        $this->lang=$user->loadLanguage(true);

        // Update Last Login Fields
        $user->set("lastlogin", "now()");
        $user->set("lastip", $this->request->getServerVar("REMOTE_ADDR"));
        $user->update();
        $user->lookup();
        $this->user=$user;
        $this->redirect="";
    }

    /**
     * Redirect to Logon screen
     * @param string error code to pass
     */
    private function setRedirectLogon($error = null) {
        $request=$this->request->getServerVar("REQUEST_URI");
        $thisPage=$request ? urlencode(preg_replace("/^\//", "", $request)) : "";
        if (strstr($thisPage, "service%2F")) {
            $page = "../logon.php";
        } else {
            $page = "logon.php";
        }
        $this->redirect=$page . "?" . $error . "redirect=" . $thisPage;
    }

    /**
     * Redirect to Zoph page
     * Redirect to a Zoph page, making sure you are not tricked into opening a link
     * that would damage your database, for example deleting a photo by a url pointing
     * you to the "confirm" action. Just to be extra sure, any action, except "search" is
     * replaced by "display".
     */
    private function setRedirect() {
        $redirect="/" . urldecode($this->request["redirect"]);
        $this->redirect=preg_replace("/action=(?!search).[^&]+/", "action=display", $redirect);
    }

    /**
     * Check if there are any migrations to do
     * if this is the case, only admin users can log on
     * @return bool are there any migrations?
     */
    private function checkMigrations() {
        return (new migrations())->check(conf::get("zoph.version"));
    }
}
?>