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 ( 6e5ecc...f24dc9 )
by François
02:17
created

Utils::writeTempConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
/**
3
 * Copyright 2015 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 RuntimeException;
21
22
class Utils
23
{
24
    public static function exec($cmd, $throwExceptionOnFailure = true)
25
    {
26
        exec($cmd, $output, $returnValue);
27
28
        if (0 !== $returnValue) {
29
            if ($throwExceptionOnFailure) {
30
                throw new RuntimeException(
31
                    sprintf('command "%s" did not complete successfully (%d)', $cmd, $returnValue)
32
                );
33
            }
34
        }
35
    }
36
37
    public static function writeTempConfig($tmpConfig, array $configFileData)
38
    {
39
        if (false === @file_put_contents($tmpConfig, implode(PHP_EOL, $configFileData))) {
40
            throw new RuntimeException('unable to write temporary config file');
41
        }
42
    }
43
44
    public static function normalizeIP($ipAddress)
45
    {
46
        return inet_ntop(inet_pton($ipAddress));
47
    }
48
49
    /**
50
     * Convert IPv4 CIDR address to IPv6 address with prefix containing the
51
     * IPv4 address with the same prefix.
52
     *
53
     * @param string $v6p the IPv6 prefix, e.g.: fd00:4242:4242:1194
54
     * @param string $v4c the IPv4 CIDR, e.g. 10.42.42.0/24
55
     *
56
     * @return string the IPv6 address range containing the IPv4 CIDR, e.g.:
57
     *                fd00:4242:4242:1194:0:ffff:0a2a:2a00/120
58
     */
59
    public static function convert4to6($v6p, $v4c)
60
    {
61
        list($net4, $prefix4) = explode('/', $v4c);
62
        $prefix6 = 128 - (32 - $prefix4);
63
        $v4e = str_split(bin2hex(inet_pton($net4)), 4);
64
65
        return sprintf(
66
            '%s/%d',
67
            self::normalizeIP(
68
                sprintf('%s::ffff:%s:%s', $v6p, $v4e[0], $v4e[1])
69
            ),
70
            $prefix6
71
        );
72
    }
73
74
    public static function getActiveLeases($leaseDir)
75
    {
76
        $activeLeases = [];
77
        foreach (glob(sprintf('%s/*', $leaseDir)) as $leaseFile) {
78
            $activeLeases[] = basename($leaseFile);
79
        }
80
81
        return $activeLeases;
82
    }
83
84
    public static function addRoute4($v4, $dev)
85
    {
86
        self::delRoute4($v4, false);
87
        self::flushRouteCache4();
88
        $cmd = sprintf('/usr/bin/sudo /sbin/ip -4 ro add %s/32 dev %s', $v4, $dev);
89
        self::exec($cmd);
90
    }
91
92
    public static function addRoute6($v6, $dev)
93
    {
94
        self::delRoute6($v6, false);
95
        self::flushRouteCache6();
96
        $cmd = sprintf('/usr/bin/sudo /sbin/ip -6 ro add %s/128 dev %s', $v6, $dev);
97
        self::exec($cmd);
98
    }
99
100
    public static function delRoute4($v4, $throwExceptionOnFailure = true)
101
    {
102
        $cmd = sprintf('/usr/bin/sudo /sbin/ip -4 ro del %s/32', $v4);
103
        self::exec($cmd, $throwExceptionOnFailure);
104
        self::flushRouteCache4();
105
    }
106
107
    public static function delRoute6($v6, $throwExceptionOnFailure = true)
108
    {
109
        $cmd = sprintf('/usr/bin/sudo /sbin/ip -6 ro del %s/128', $v6);
110
        self::exec($cmd, $throwExceptionOnFailure);
111
        self::flushRouteCache6();
112
    }
113
114
    private static function flushRouteCache4()
115
    {
116
        $cmd = '/usr/bin/sudo /sbin/ip -4 ro flush cache';
117
        self::exec($cmd, false);
118
    }
119
120
    private static function flushRouteCache6()
121
    {
122
        $cmd = '/usr/bin/sudo /sbin/ip -6 ro flush cache';
123
        self::exec($cmd, false);
124
    }
125
}
126