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 ( 2546ca...d5d7e8 )
by François
10:03
created

ConnectionLog   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 157
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
B connect() 0 30 2
B disconnect() 0 25 1
A getConnectionHistory() 0 23 1
A createTableQueries() 0 19 1
A initDatabase() 0 18 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($minUnix, $maxUnix)
108
    {
109
        $stmt = $this->db->prepare(
110
            sprintf(
111
                'SELECT * 
112
                 FROM %s 
113
                 WHERE 
114
                    disconnect_time_unix NOT NULL
115
                 AND
116
                    disconnect_time_unix >= :min_unix
117
                 AND
118
                    disconnect_time_unix <= :max_unix
119
                 ORDER BY disconnect_time_unix DESC',
120
                $this->prefix.'connections'
121
            )
122
        );
123
        $stmt->bindValue(':min_unix', $minUnix, PDO::PARAM_INT);
124
        $stmt->bindValue(':max_unix', $maxUnix, PDO::PARAM_INT);
125
126
        $stmt->execute();
127
128
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
129
    }
130
131
    public static function createTableQueries($prefix)
132
    {
133
        $query = array(
134
            sprintf(
135
                'CREATE TABLE IF NOT EXISTS %s (
136
                    common_name VARCHAR(255) NOT NULL,
137
                    time_unix INTEGER NOT NULL,
138
                    ifconfig_pool_remote_ip VARCHAR(255) NOT NULL,
139
                    ifconfig_ipv6_remote VARCHAR(255) NOT NULL,
140
                    bytes_received INTEGER DEFAULT NULL,
141
                    bytes_sent INTEGER DEFAULT NULL,
142
                    disconnect_time_unix INTEGER DEFAULT NULL
143
                )',
144
                $prefix.'connections'
145
            ),
146
        );
147
148
        return $query;
149
    }
150
151
    public function initDatabase()
152
    {
153
        $queries = self::createTableQueries($this->prefix);
154
        foreach ($queries as $q) {
155
            $this->db->query($q);
156
        }
157
158
        $tables = array('connections');
159
        foreach ($tables as $t) {
160
            // make sure the tables are empty
161
            $this->db->query(
162
                sprintf(
163
                    'DELETE FROM %s',
164
                    $this->prefix.$t
165
                )
166
            );
167
        }
168
    }
169
}
170