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 — 3.0 ( 7abdc1...2251a3 )
by Vermeulen
02:25
created

Secure::getApplicationInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace BFW\Helpers;
4
5
use \Exception;
6
7
/**
8
 * Helpers to securize data
9
 */
10
class Secure
11
{
12
    /**
13
     * Hash a string
14
     * 
15
     * @param string $val String to hash
16
     * 
17
     * @return string
18
     */
19
    public static function hash($val)
20
    {
21
        return substr(hash('sha256', md5($val)), 0, 32);
22
    }
23
24
    /**
25
     * Securize a string for some types with filter_var.
26
     * 
27
     * @param mixed $data String to securize
28
     * @param string $type Type of filter
29
     * 
30
     * @return mixed
31
     * 
32
     * @throws Exception If the type is unknown
33
     */
34
    public static function securiseKnownTypes($data, $type)
35
    {
36
        $filterType = 'text';
37
38
        if ($type === 'int' || $type === 'integer') {
39
            $filterType = FILTER_VALIDATE_INT;
40
        } elseif ($type === 'float' || $type === 'double') {
41
            $filterType = FILTER_VALIDATE_FLOAT;
42
        } elseif ($type === 'bool' || $type === 'boolean') {
43
            $filterType = FILTER_VALIDATE_BOOLEAN;
44
        } elseif ($type === 'email') {
45
            $filterType = FILTER_VALIDATE_EMAIL;
46
        }
47
48
        if ($filterType === 'text') {
49
            throw new Exception('Unknown type');
50
        }
51
52
        return filter_var($data, $filterType);
53
    }
54
55
    /**
56
     * Securize a variable
57
     * 
58
     * @param mixed $data The variable to securize
59
     * @param string $type The type of datas
60
     * @param boolean $htmlentities If use htmlentities function
61
     *  to more securize
62
     * 
63
     * @return mixed
64
     * 
65
     * @throws Exception If a error with a type of data
66
     */
67
    public static function securise($data, $type, $htmlentities)
68
    {
69
        $currentClass = get_called_class();
70
        
71
        if (is_array($data)) {
72
            foreach ($data as $key => $val) {
73
                unset($data[$key]);
74
75
                $key = $currentClass::securise($key, gettype($key), true);
76
                $val = $currentClass::securise($val, $type, $htmlentities);
77
78
                $data[$key] = $val;
79
            }
80
81
            return $data;
82
        }
83
84
        try {
85
            return $currentClass::securiseKnownTypes($data, $type);
86
        } catch (Exception $ex) {
87
            if ($ex->getMessage() !== 'Unknown type') {
88
                throw new Exception($ex->getCode(), $ex->getMessage());
89
            }
90
            //Else : Use securise text type
91
        }
92
93
        $sqlSecureMethod = $currentClass::getSqlSecureMethod();
94
        if ($sqlSecureMethod !== false) {
95
            $data = $sqlSecureMethod($data);
96
        } else {
97
            $data = addslashes($data);
98
        }
99
100
        if ($htmlentities === true) {
101
            $data = htmlentities($data, ENT_COMPAT | ENT_HTML401, 'UTF-8');
102
        }
103
104
        return $data;
105
    }
106
107
    /**
108
     * Get the sqlSecure function declared in bfw config file
109
     * 
110
     * @return boolean|string
111
     */
112
    public static function getSqlSecureMethod()
113
    {
114
        $currentClass   = get_called_class();
115
        $application    = $currentClass::getApplicationInstance();
116
        $secureFunction = $application->getConfig('sqlSecureMethod');
117
118
        if (!is_callable($secureFunction, false)) {
119
            return false;
120
        }
121
122
        return $secureFunction;
123
    }
124
    
125
    protected static function getApplicationInstance()
126
    {
127
        return \BFW\Application::getInstance();
128
    }
129
130
    /**
131
     * Securize an array key's value for a declared type.
132
     * 
133
     * @param array $array The array where is the key
134
     * @param string $key The key where is the value to securize
135
     * @param string $type The type of data
136
     * @param boolean $htmlentities If use htmlentities function
137
     *  to more securize
138
     * 
139
     * @return mixed
140
     * 
141
     * @throws Exception If the key not exist in array
142
     */
143
    public static function getSecurisedKeyInArray(&$array, $key, $type, $htmlentities = false)
144
    {
145
        if (!isset($array[$key])) {
146
            throw new Exception('The key '.$key.' not exist');
147
        }
148
149
        $currentClass = get_called_class();
150
        return $currentClass::securise(trim($array[$key]), $type, $htmlentities);
151
    }
152
153
    /**
154
     * Get a securized value for a key in $_POST array
155
     * 
156
     * @param string $key The key where is the value to securize
157
     * @param string $type The type of data
158
     * @param boolean $htmlentities If use htmlentities function
159
     *  to more securize
160
     * 
161
     * @return mixed
162
     */
163
    public static function getSecurisedPostKey($key, $type, $htmlentities = false)
164
    {
165
        $currentClass = get_called_class();
166
        return $currentClass::getSecurisedKeyInArray($_POST, $key, $type, $htmlentities);
167
    }
168
169
    /**
170
     * Get a securized value for a key in $_GET array
171
     * 
172
     * @param string $key The key where is the value to securize
173
     * @param string $type The type of data
174
     * @param boolean $htmlentities If use htmlentities function
175
     *  to more securize
176
     * 
177
     * @return mixed
178
     */
179
    public static function getSecurisedGetKey($key, $type, $htmlentities = false)
180
    {
181
        $currentClass = get_called_class();
182
        return $currentClass::getSecurisedKeyInArray($_GET, $key, $type, $htmlentities);
183
    }
184
}
185