File: user.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 (558 lines) | stat: -rw-r--r-- 17,992 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?php
/**
 * A class representing a user of Zoph.
 *
 * 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
 *
 * @author Jason Geiger
 * @author Jeroen Roos
 * @package Zoph
 */

use conf\conf;
use db\select;
use db\clause;
use db\param;
use db\selectHelper;
use organiser\organiserInterface;
use user\prefs;

/**
 * A class representing a user of Zoph
 *
 * @author Jason Geiger
 * @author Jeroen Roos
 * @package Zoph
 */
class user extends zophTable {
    /** @var string The name of the database table */
    protected static $tableName="users";
    /** @var array List of primary keys */
    protected static $primaryKeys=array("user_id");
    /** @var array Fields that may not be empty */
    protected static $notNull=array("user_name");
    /** @var array Fields that are integers */
    protected static $isInteger=array("user_id", "person_id", "lightbox_id");
    /** @var bool keep keys with insert. In most cases the keys are set by
                  the db with auto_increment */
    protected static $keepKeys = false;
    /** @var string URL for this class */
    protected static $url="user?user_id=";

    /** @var user currently logged on user */
    private static $current;

    /** @var person Person linked to this user */
    public $person;
    /** @var prefs Preferences of this user */
    public $prefs;
    /** @var \language Holds translations */
    public $lang;

    /**
     * Insert a new user into the db
     */
    public function insert() {
        $this->set("lastnotify", "now()");
        parent::insert();
        $default=new prefs(-1);
        if ($default->lookup()) {
            $default->set("user_id", $this->getId());
            $this->prefs=$default;
        } else {
            $this->prefs = new prefs($this->getId());
        }
        $this->prefs->insert();
    }

    /**
     * Delete a user from the db
     * also delete the preferences for this user
     */
    public function delete() {
        parent::delete(array("prefs", "groups_users"));
    }

    public function lookup() {
        parent::lookup();
        if ($this->get("lastlogin") == "") {
            $this->set("lastlogin", null);
        }
    }

    /**
     * Is this user anonymous?
     */
    public function isAnonymous() : bool {
        return false;
    }

    /**
     * Lookup the person linked to this user
     */
    public function lookupPerson() {
        $this->person = new person($this->get("person_id"));
        $this->person->lookup();
    }

    /**
     * Lookup the preferences of this user
     */
    public function lookupPrefs() {
        $this->prefs = new prefs($this->getId());
        $this->prefs->lookup();
    }

    /**
     * Is this user an admin?
     * @return bool
     */
    public function isAdmin() {
        $this->lookup();
        return $this->get("user_class") == 0;
    }

    /**
     * Is this user the default user?
     * @return bool
     */
    public function isDefault() {
        $this->lookup();
        return (conf::get("interface.user.default") == $this->getId());
    }

    /**
     * Is this user the creator of this organiser?
     * @return bool
     */
    public function isCreator(organiserInterface $obj) {
        $obj->lookup();
        return ((int) $obj->get("createdby") === $this->getId());
    }

    /**
     * When was this user last notified of new albums?
     * @return string timestamp
     */
    public function getLastNotify() {
        return $this->get("lastnotify");
    }

    /**
     * Get the username
     * @return string name
     */
    public function getName() {
        return $this->get("user_name");
    }

    /**
     * Get groups for this user
     * @return array Groups
     */
    public function getGroups() {
        $qry = new select(array("gu" => "groups_users"));
        $qry->addFields(array("group_id"));
        $qry->where(new clause("user_id=:userid"));
        $qry->addParam(new param(":userid", (int) $this->getId(), PDO::PARAM_INT));

        return group::getRecordsFromQuery($qry);
    }

    /**
     * Get album permissions for this user
     * @param album album to get permissions for
     * @return permissions permissions object
     */
    public function getAlbumPermissions(album $album) {
        // An admin or creator of an album always has full permissions on that album
        if ($this->isAdmin() || $this->isCreator($album)) {
            $gp=new permissions(0, $album->getId());
            $gp->set("access_level", 9);
            $gp->set("watermark_level", 0);
            $gp->set("writable", true);
            return $gp;
        }
        $groups=$this->getGroups();

        $groupIds=array();
        foreach ($groups as $group) {
            $groupIds[]=(int) $group->getId();
        }

        if (is_array($groupIds) && !empty($groupIds)) {
            $qry=new select(array("gp" => "group_permissions"));
            $where = new clause("album_id=:albumid");
            $groups=new param(":groupid", $groupIds, PDO::PARAM_INT);
            $qry->addParams(array(
                new param(":albumid", (int) $album->getId(), PDO::PARAM_INT),
                $groups
            ));
            $where->addAnd(clause::InClause("gp.group_id", $groups));
            $qry->where($where);
            $qry->addOrder("access_level DESC")
                ->addOrder("writable DESC")
                ->addOrder("watermark_level DESC");
            $qry->addLimit(1);
            $aps=permissions::getRecordsFromQuery($qry);
            if (is_array($aps) && !empty($aps)) {
                return $aps[0];
            }
        }

        return null;
    }

    /**
     * Get permissions for a specific photo, for this user
     * @param photo Photo to get permissions for
     * @return permissions permissions object
     */
    public function getPhotoPermissions(photo $photo) {
        $permissions=null;
        foreach ($photo->getAlbums() as $album) {
            if ($this->isCreator($album)) {
                $permissions=new permissions(0, $album->getId());
                $permissions->set("access_level", 9);
                $permissions->set("watermark_level", 0);
                $permissions->set("writable", true);
                return $permissions;
            }
        }

        $qry=new select(array("p" => "photos"));
        $qry->addFields(array("photo_id"));

        $where=new clause("p.photo_id = :photoid");
        $qry->addParam(new param(":photoid", (int) $photo->getId(), PDO::PARAM_INT));
        $qry->addParam(new param(":userid", (int) $this->getId(), PDO::PARAM_INT));

        $qry->join(array("pa" => "photo_albums"), "pa.photo_id=p.photo_id");
        $qry->join(array("gp" => "group_permissions"), "gp.album_id=pa.album_id");
        $qry->join(array("gu" => "groups_users"), "gp.group_id=gu.group_id");

        $where->addAnd(new clause("gp.access_level>=p.level"));
        $where->addAnd(new clause("gu.user_id=:userid"));
        $qry->addFields(array("gp.*"));
        $qry->addLimit(1);
        // do ordering to grab entry with most permissions
        $qry->addOrder("gp.access_level DESC")->addOrder("writable DESC")->addOrder("watermark_level DESC");
        $qry->where($where);

        $gps = permissions::getRecordsFromQuery($qry);
        if ($gps && !empty($gps)) {
            $permissions=$gps[0];
        }

        if ($this->canSeeAllPhotos()) {
            if ($permissions instanceof permissions) {
                $permissions->set("access_level", 9);
            } else {
                $permissions=new permissions();
                $permissions->set("access_level", 9);
                $permissions->set("watermark_level", 0);
                $permissions->set("writable", 0);
            }

            if ($this->isAdmin()) {
                $permissions->set("writable", 1);
            }
        }


        return $permissions;
    }

    /**
     * Check whether this user can see hidden circles
     * @return bool user can see hidden circles
     */
    public function canSeeHiddenCircles() {
        return ($this->isAdmin() || $this->get("see_hidden_circles"));
    }

    /**
     * Check whether this user can see all photos.
     * This means that the permissions checking is bypassed for this user,
     * as if it is an admin user, but without giving full admin rights
     * @return bool user can see all photos
     */
    public function canSeeAllPhotos() {
        return ($this->isAdmin() || $this->get("view_all_photos"));
    }

    /**
     * Check whether this user is allowed to leave comments
     * @return bool user can leave comments
     */
    public function canLeaveComments() {
        return ($this->isAdmin() || $this->get("leave_comments"));
    }

    /**
     * Check whether this user is allowed to delete photos
     * @return bool user can delete photos
     */
    public function canDeletePhotos() {
        return ($this->isAdmin() || $this->get("delete_photos"));
    }

    /**
     * Check whether this user can edit, add and delete albums, categories, places and people
     * @return bool user can add, edit and delete albums, categories, places and people
     */
    public function canEditOrganisers() {
        return ($this->isAdmin() || $this->get("edit_organizers"));
    }

    /**
     * Check whether this user can browse people
     * @return bool user can see the list of people that are in photos this user can see
     */
    public function canBrowsePeople() {
        return ($this->canEditOrganisers() || $this->get("browse_people"));
    }

    /**
     * Check whether this user can see details of people (such as address, birthdate, etc.)
     * @return bool user can see details of people
     */
    public function canSeePeopleDetails() {
        return ($this->canEditOrganisers() || $this->get("detailed_people"));
    }

    /**
     * Check whether this user can browse places
     * @return bool user can see the list of places where photos this user can see were taken
     */
    public function canBrowsePlaces() {
        return ($this->canEditOrganisers() || $this->get("browse_places"));
    }

    /**
     * Check whether this user can browse tracks
     * @return bool user can see tracks
     */
    public function canBrowseTracks() {
        return ($this->isAdmin() || $this->get("browse_tracks"));
    }

    /**
     * Check whether this user can see details of places (such as address)
     * @return bool user can see details of places
     */
    public function canSeePlaceDetails() {
        return ($this->canEditOrganisers() || $this->get("detailed_places"));
    }

    /**
     * Check wheter this user can rate photos
     * @return bool user can rate photos
     */
    public function canRatePhotos() {
        return ($this->isAdmin() || $this->get("allow_rating"));
    }

    /**
     * Check wheter this user can import
     * @return bool user can import
     */
    public function canImport() : bool {
        return ($this->isAdmin() || $this->get("import"));
    }

    /**
     * Get array to display information about this user
     * @return array of properties to display
     */
    public function getDisplayArray() {
        $this->lookupPerson();
        $da = array(
            translate("username")   => $this->get("user_name"),
            translate("person")     => $this->person->getLink(),
            translate("class")      => $this->get("user_class") == 0 ? "Admin" : "User"
        );
        $desc=$this->getAccessRightsDescription();
        foreach ($this->getAccessRightsArray() as $field => $value) {
            $da[$desc[$field]] = $value == 1 ? translate("Yes") : translate("No");
        }
        $da = array_merge($da, array(
            translate("last login")         => $this->get("lastlogin"),
            translate("last ip address")    => $this->get("lastip")
        ));

        if ($this->getLightbox() && $this->getLightbox()->get("album")) {
            $da[translate("lightbox album")] = $this->getLightbox()->get("album");
        }

        return $da;
    }

    public function getLightbox() {
        if ($this->get("lightbox_id")) {
            $lightbox = new album($this->get("lightbox_id"));
            $lightbox->lookup();
            return $lightbox;
        }
    }

    public function getAccessRightsArray() {
        return array(
            "view_all_photos"       => $this->get("view_all_photos"),
            "delete_photos"         => $this->get("delete_photos"),
            "browse_people"         => $this->get("browse_people"),
            "browse_places"         => $this->get("browse_places"),
            "browse_tracks"         => $this->get("browse_tracks"),
            "edit_organizers"       => $this->get("edit_organizers"),
            "detailed_people"       => $this->get("detailed_people"),
            "see_hidden_circles"    => $this->get("see_hidden_circles"),
            "detailed_places"       => $this->get("detailed_places"),
            "import"                => $this->get("import"),
            "download"              => $this->get("download"),
            "leave_comments"        => $this->get("leave_comments"),
            "allow_rating"          => $this->get("allow_rating"),
            "allow_multirating"     => $this->get("allow_multirating"),
            "allow_share"           => $this->get("allow_share")
        );
    }

    public static function getAccessRightsDescription() {
        return array(
            "view_all_photos"       => translate("can view all photos"),
            "delete_photos"         => translate("can delete photos"),
            "browse_people"         => translate("can browse people"),
            "browse_places"         => translate("can browse places"),
            "browse_tracks"         => translate("can browse tracks"),
            "edit_organizers"       => translate("can edit albums, categories, places and people"),
            "detailed_people"       => translate("can view details of people"),
            "see_hidden_circles"    => translate("can view hidden circles"),
            "detailed_places"       => translate("can view details of places"),
            "import"                => translate("can import"),
            "download"              => translate("can download zipfiles"),
            "leave_comments"        => translate("can leave comments"),
            "allow_rating"          => translate("can rate photos"),
            "allow_multirating"     => translate("can rate the same photo multiple times"),
            "allow_share"           => translate("can share photos")
        );
    }

    /**
     * Load language
     * This loads the translations of Zoph's web gui
     * @param bool Even load when already loaded
     */
    public function loadLanguage($force = 0) {
        $langs=array();

        if (!$force && $this->lang != null) {
            return $this->lang;
        }

        if ($this->prefs != null && $this->prefs->get("language") != null) {
            $langs[] = $this->prefs->get("language");
        }

        $langs=array_merge($langs, language::httpAccept());

        $this->lang=language::load($langs);
        return $this->lang;
    }

    /**
     * Create a graph of the ratings this user has made
     */
    public function getRatingGraph() {
        return rating::getGraphArrayForUser($this);
    }

    /**
     * Get the comments this user has placed
     * @return array comments
     */
    public function getComments() {
        return comment::getRecords("comment_date", array("user_id" => (int) $this->getId()));
    }

    /**
     * Get user object by searching for username
     * @param string name
     * @return user user object
     */
    public static function getByName($name) {
        $users=static::getRecords(null, array("user_name" => $name));
        if (sizeof($users)==1) {
            return $users[0];
        } else if (empty($users)) {
            throw new userNotFoundException("User not found");
        } else {
            throw new userMultipleFoundException("Multiple users with the same name found");
        }
    }

    /**
     * Check if user exists
     * @param string username
     * @return bool user exists
     */
    public static function exists(string $name) {
        try {
            self::getByName($name);
        } catch (userNotFoundException $e) {
            return false;
        } catch (userMultipleFoundException $e) {
            return true;
        }
        return true;
    }

    /**
     * Get all users
     * @param string sort order
     * @return array Array of all users
     */
    public static function getAll($order = "user_name") {
        return static::getRecords($order);
    }

    /**
     * Set currently logged in user
     * (log in)
     * @param user user object
     */
    public static function setCurrent(user $user) {
        $user->lookup();
        $user->lookupPrefs();
        $user->lookupPerson();
        static::$current=$user;
    }

    /**
     * Delete currently logged in user
     * (Log out)
     */
    public static function unsetCurrent() {
        static::$current=null;
    }

    /**
     * Get currently logged in user
     */
    public static function getCurrent() {
        return static::$current;
    }

}
?>