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 ( 35efb6...ee0423 )
by François
02:44
created

ConnectionLog::initDatabase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright 2016 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace fkooman\VPN\Server;
19
20
use PDO;
21
use RuntimeException;
22
23
/**
24
 * With this we store events from client-connect and client-disconnect
25
 * from the OpenVPN server.
26
 */
27
class ConnectionLog
28
{
29
    /** @var PDO */
30
    private $db;
31
32
    /** @var string */
33
    private $prefix;
34
35
    public function __construct(PDO $db, $prefix = '')
36
    {
37
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
38
        $this->db = $db;
39
        $this->prefix = $prefix;
40
    }
41
42
    /**
43
     * Handle connecting client.
44
     *
45
     * @param array $v the environment variables from the connect event
46
     *
47
     * @throws RuntimeException if the insert fails
48
     */
49
    public function connect(array $v)
50
    {
51
        $stmt = $this->db->prepare(
52
            sprintf(
53
                'INSERT INTO %s (
54
                    common_name,
55
                    time_unix,
56
                    v4,
57
                    v6
58
                 ) 
59
                 VALUES(
60
                    :common_name, 
61
                    :time_unix, 
62
                    :v4, 
63
                    :v6
64
                 )',
65
                $this->prefix.'connections'
66
            )
67
        );
68
69
        $stmt->bindValue(':common_name', $v['common_name'], PDO::PARAM_STR);
70
        $stmt->bindValue(':time_unix', intval($v['time_unix']), PDO::PARAM_INT);
71
        $stmt->bindValue(':v4', $v['v4'], PDO::PARAM_STR);
72
        $stmt->bindValue(':v6', $v['v6'], PDO::PARAM_STR);
73
        $stmt->execute();
74
75
        if (1 !== $stmt->rowCount()) {
76
            throw new RuntimeException('unable to insert');
77
        }
78
    }
79
80
    /**
81
     * Handle disconnecting client.
82
     *
83
     * @param array $v the environment variables from the disconnect event
84
     *
85
     * @return bool whether the update succeeded, returns false if there is no
86
     *              matching 'connect' in the database
87
     */
88
    public function disconnect(array $v)
89
    {
90
        $stmt = $this->db->prepare(
91
            sprintf(
92
                'UPDATE %s 
93
                    SET bytes_received = :bytes_received, bytes_sent = :bytes_sent, disconnect_time_unix = :disconnect_time_unix
94
                    WHERE common_name = :common_name 
95
                        AND time_unix = :time_unix
96
                        AND v4 = :v4
97
                        AND v6 = :v6',
98
                $this->prefix.'connections'
99
            )
100
        );
101
102
        $stmt->bindValue(':common_name', $v['common_name'], PDO::PARAM_STR);
103
        $stmt->bindValue(':time_unix', intval($v['time_unix']), PDO::PARAM_INT);
104
        $stmt->bindValue(':v4', $v['v4'], PDO::PARAM_STR);
105
        $stmt->bindValue(':v6', $v['v6'], PDO::PARAM_STR);
106
        $stmt->bindValue(':bytes_received', intval($v['bytes_received']), PDO::PARAM_INT);
107
        $stmt->bindValue(':bytes_sent', intval($v['bytes_sent']), PDO::PARAM_INT);
108
        $stmt->bindValue(':disconnect_time_unix', intval($v['disconnect_time_unix']), PDO::PARAM_INT);
109
        $stmt->execute();
110
111
        return 1 === $stmt->rowCount();
112
    }
113
114
    public function getConnectionHistory($minUnix, $maxUnix)
115
    {
116
        $stmt = $this->db->prepare(
117
            sprintf(
118
                'SELECT * 
119
                 FROM %s 
120
                 WHERE 
121
                    disconnect_time_unix NOT NULL
122
                 AND
123
                    disconnect_time_unix >= :min_unix
124
                 AND
125
                    disconnect_time_unix <= :max_unix
126
                 ORDER BY disconnect_time_unix DESC',
127
                $this->prefix.'connections'
128
            )
129
        );
130
        $stmt->bindValue(':min_unix', $minUnix, PDO::PARAM_INT);
131
        $stmt->bindValue(':max_unix', $maxUnix, PDO::PARAM_INT);
132
133
        $stmt->execute();
134
135
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
136
    }
137
138
    public static function createTableQueries($prefix)
139
    {
140
        $query = array(
141
            sprintf(
142
                'CREATE TABLE IF NOT EXISTS %s (
143
                    common_name VARCHAR(255) NOT NULL,
144
                    time_unix INTEGER NOT NULL,
145
                    v4 VARCHAR(255) NOT NULL,
146
                    v6 VARCHAR(255) NOT NULL,
147
                    bytes_received INTEGER DEFAULT NULL,
148
                    bytes_sent INTEGER DEFAULT NULL,
149
                    disconnect_time_unix INTEGER DEFAULT NULL
150
                )',
151
                $prefix.'connections'
152
            ),
153
        );
154
155
        return $query;
156
    }
157
158
    /**
159
     * Remove all log entries older than provided timestamp.
160
     *
161
     * @param int $timeStamp the unix timestamp before which to remove all log entries
162
     */
163 View Code Duplication
    public function housekeeping($timeStamp)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    {
165
        $stmt = $this->db->prepare(
166
            sprintf(
167
                'DELETE FROM %s 
168
                    WHERE disconnect_time_unix < :time_stamp',
169
                $this->prefix.'connections'
170
            )
171
        );
172
173
        $stmt->bindValue(':time_stamp', $timeStamp, PDO::PARAM_INT);
174
        $stmt->execute();
175
176
        return $stmt->rowCount();
177
    }
178
179
    public function initDatabase()
180
    {
181
        $queries = self::createTableQueries($this->prefix);
182
        foreach ($queries as $q) {
183
            $this->db->query($q);
184
        }
185
    }
186
}
187