GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d1e15a...62e4a9 )
by François
06:16 queued 44s
created

ConnectionLog::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
3
namespace fkooman\VPN\Server;
4
5
use PDO;
6
use RuntimeException;
7
use fkooman\IO\IO;
8
9
/**
10
 * With this we store events from client-connect and client-disconnect
11
 * from the OpenVPN server.
12
 */
13
class ConnectionLog
14
{
15
    /** @var PDO */
16
    private $db;
17
18
    /** @var \fkooman\IO\IO */
19
    private $io;
20
21
    /** @var string */
22
    private $prefix;
23
24
    public function __construct(PDO $db, IO $io = null, $prefix = '')
25
    {
26
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
27
        $this->db = $db;
28
        if (is_null($io)) {
29
            $io = new IO();
30
        }
31
        $this->io = $io;
32
        $this->prefix = $prefix;
33
    }
34
35
    /**
36
     * Handle connecting client.
37
     *
38
     * @param array $v the environment variables from the connect event
39
     *
40
     * @throws RuntimeException if the insert fails
41
     */
42
    public function connect(array $v)
43
    {
44
        $stmt = $this->db->prepare(
45
            sprintf(
46
                'INSERT INTO %s (
47
                    common_name,
48
                    time_unix,
49
                    ifconfig_pool_remote_ip,
50
                    ifconfig_ipv6_remote
51
                 ) 
52
                 VALUES(
53
                    :common_name, 
54
                    :time_unix, 
55
                    :ifconfig_pool_remote_ip, 
56
                    :ifconfig_ipv6_remote
57
                 )',
58
                $this->prefix.'connections'
59
            )
60
        );
61
62
        $stmt->bindValue(':common_name', $v['common_name'], PDO::PARAM_STR);
63
        $stmt->bindValue(':time_unix', intval($v['time_unix']), PDO::PARAM_INT);
64
        $stmt->bindValue(':ifconfig_pool_remote_ip', $v['ifconfig_pool_remote_ip'], PDO::PARAM_STR);
65
        $stmt->bindValue(':ifconfig_ipv6_remote', $v['ifconfig_ipv6_remote'], PDO::PARAM_STR);
66
        $stmt->execute();
67
68
        if (1 !== $stmt->rowCount()) {
69
            throw new RuntimeException('unable to insert');
70
        }
71
    }
72
73
    /**
74
     * Handle disconnecting client.
75
     *
76
     * @param array $v the environment variables from the disconnect event
77
     *
78
     * @return bool whether the update succeeded, returns false if there is no
79
     *              matching 'connect' in the database
80
     */
81
    public function disconnect(array $v)
82
    {
83
        $stmt = $this->db->prepare(
84
            sprintf(
85
                'UPDATE %s 
86
                    SET bytes_received = :bytes_received, bytes_sent = :bytes_sent, disconnect_time_unix = :disconnect_time_unix
87
                    WHERE common_name = :common_name 
88
                        AND time_unix = :time_unix
89
                        AND ifconfig_pool_remote_ip = :ifconfig_pool_remote_ip
90
                        AND ifconfig_ipv6_remote = :ifconfig_ipv6_remote',
91
                $this->prefix.'connections'
92
            )
93
        );
94
95
        $stmt->bindValue(':common_name', $v['common_name'], PDO::PARAM_STR);
96
        $stmt->bindValue(':time_unix', intval($v['time_unix']), PDO::PARAM_INT);
97
        $stmt->bindValue(':ifconfig_pool_remote_ip', $v['ifconfig_pool_remote_ip'], PDO::PARAM_STR);
98
        $stmt->bindValue(':ifconfig_ipv6_remote', $v['ifconfig_ipv6_remote'], PDO::PARAM_STR);
99
        $stmt->bindValue(':bytes_received', intval($v['bytes_received']), PDO::PARAM_INT);
100
        $stmt->bindValue(':bytes_sent', intval($v['bytes_sent']), PDO::PARAM_INT);
101
        $stmt->bindValue(':disconnect_time_unix', $this->io->getTime(), PDO::PARAM_INT);
102
        $stmt->execute();
103
104
        return 1 === $stmt->rowCount();
105
    }
106
107
    public function getConnectionHistory($beginTime = null, $endTime = null)
108
    {
109
        if (is_null($beginTime) || !is_int($beginTime)) {
110
            // default from start of current day
111
            $beginTime = strtotime('today', $this->io->getTime());
112
        }
113
        if (is_null($endTime) || !is_int($endTime)) {
114
            // upto now
115
            $endTime = $this->io->getTime();
116
        }
117
118
        $stmt = $this->db->prepare(
119
            sprintf(
120
                'SELECT * 
121
                 FROM %s 
122
                 WHERE 
123
                    disconnect_time_unix NOT NULL
124
                 AND
125
                    disconnect_time_unix >= :begin_time
126
                 AND
127
                    disconnect_time_unix <= :end_time
128
                 ORDER BY disconnect_time_unix DESC',
129
                $this->prefix.'connections'
130
            )
131
        );
132
        $stmt->bindValue(':begin_time', $beginTime, PDO::PARAM_INT);
133
        $stmt->bindValue(':end_time', $endTime, PDO::PARAM_INT);
134
135
        $stmt->execute();
136
137
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
138
    }
139
140
    public static function createTableQueries($prefix)
141
    {
142
        $query = array(
143
            sprintf(
144
                'CREATE TABLE IF NOT EXISTS %s (
145
                    common_name VARCHAR(255) NOT NULL,
146
                    time_unix INTEGER NOT NULL,
147
                    ifconfig_pool_remote_ip VARCHAR(255) NOT NULL,
148
                    ifconfig_ipv6_remote VARCHAR(255) NOT NULL,
149
                    bytes_received INTEGER DEFAULT NULL,
150
                    bytes_sent INTEGER DEFAULT NULL,
151
                    disconnect_time_unix INTEGER DEFAULT NULL
152
                )',
153
                $prefix.'connections'
154
            ),
155
        );
156
157
        return $query;
158
    }
159
160
    public function initDatabase()
161
    {
162
        $queries = self::createTableQueries($this->prefix);
163
        foreach ($queries as $q) {
164
            $this->db->query($q);
165
        }
166
167
        $tables = array('connections');
168
        foreach ($tables as $t) {
169
            // make sure the tables are empty
170
            $this->db->query(
171
                sprintf(
172
                    'DELETE FROM %s',
173
                    $this->prefix.$t
174
                )
175
            );
176
        }
177
    }
178
}
179