1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5
6use dokuwiki\plugin\bez\meta\PermissionDeniedException;
7
8class SubscriptionFactory extends Factory {
9
10    protected function checkToken($token) {
11        $q = "SELECT user_id FROM {$this->get_table_name()} WHERE token=?";
12        $r = $this->model->sqlite->query($q, $token);
13        return $this->model->sqlite->res2single($r);
14    }
15
16    public function mute($token='') {
17        //mute by token
18        if ($token) {
19            $user_id = $this->checkToken($token);
20            if (!$user_id) {
21                throw new PermissionDeniedException();
22            }
23            $q = "UPDATE {$this->get_table_name()} SET mute=1 WHERE user_id=?";
24            $this->model->sqlite->query($q, $user_id);
25        //mute currently loged user
26        } else {
27            if ($this->model->get_level() < BEZ_AUTH_USER) {
28                throw new PermissionDeniedException();
29            }
30            $q = "UPDATE {$this->get_table_name()} SET mute=1 WHERE user_id=?";
31            $this->model->sqlite->query($q, $this->model->user_nick);
32        }
33
34    }
35
36    public function unmute() {
37        if ($this->model->get_level() < BEZ_AUTH_USER) {
38            throw new PermissionDeniedException();
39        }
40        $q = "UPDATE {$this->get_table_name()} SET mute=0 WHERE user_id=?";
41        $this->model->sqlite->query($q, $this->model->user_nick);
42    }
43
44    public function isMuted() {
45        if ($this->model->get_level() < BEZ_AUTH_USER) {
46            throw new PermissionDeniedException();
47        }
48
49        $q = "SELECT mute FROM {$this->get_table_name()} WHERE user_id=?";
50        $r = $this->model->sqlite->query($q, $this->model->user_nick);
51        $mute = $this->model->sqlite->res2single($r);
52        if ($mute === false) {
53            $this->getUserToken();
54            return $this->isMuted();
55        }
56        return $mute;
57    }
58
59    public function getUserToken($user='') {
60        if ($this->model->get_level() < BEZ_AUTH_USER) {
61            throw new PermissionDeniedException();
62        }
63        if (!$user) $user = $this->model->user_nick;
64
65        $q = "SELECT token FROM {$this->get_table_name()} WHERE user_id=?";
66        $r = $this->model->sqlite->query($q, $user);
67        $token = $this->model->sqlite->res2single($r);
68        if ($token === false) {
69            $token = bin2hex(openssl_random_pseudo_bytes(16));
70            $this->model->sqlite->storeEntry($this->get_table_name(),
71                array(  'user_id'         => $user,
72                        'token'           => $token
73                ));
74        }
75        return $token;
76    }
77
78    public function getMutedUsers() {
79        $q = "SELECT user_id FROM {$this->get_table_name()} WHERE mute=1";
80        $r = $this->model->sqlite->query($q);
81        return array_map(function ($row) {
82            return $row['user_id'];
83        }, $this->model->sqlite->res2arr($r));
84    }
85}