Skip to content

Commit ac8347d

Browse files
authored
Update to Cacti PSR (#279)
Note, we should be moving Net\DNS2 into a composer requirement. Handle that separately.
1 parent 02124ae commit ac8347d

138 files changed

Lines changed: 15703 additions & 16350 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Net/DNS2.php

Lines changed: 166 additions & 296 deletions
Large diffs are not rendered by default.

Net/DNS2/BitMap.php

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
* and NSEC3 RR's
2323
*
2424
*/
25-
class Net_DNS2_BitMap
26-
{
25+
class Net_DNS2_BitMap {
2726
/**
2827
* parses a RR bitmap field defined in RFC3845, into an array of RR names.
2928
*
@@ -35,8 +34,7 @@ class Net_DNS2_BitMap
3534
* @access public
3635
*
3736
*/
38-
public static function bitMapToArray($data)
39-
{
37+
public static function bitMapToArray($data) {
4038
if (strlen($data) == 0) {
4139
return [];
4240
}
@@ -46,7 +44,6 @@ public static function bitMapToArray($data)
4644
$length = strlen($data);
4745

4846
while ($offset < $length) {
49-
5047
//
5148
// unpack the window and length values
5249
//
@@ -64,21 +61,20 @@ public static function bitMapToArray($data)
6461
// have a 'B' flag for unpack()
6562
//
6663
$bitstr = '';
64+
6765
foreach ($bitmap as $r) {
6866
$bitstr .= sprintf('%08b', $r);
6967
}
7068

7169
$blen = strlen($bitstr);
72-
for ($i=0; $i<$blen; $i++) {
7370

71+
for ($i = 0; $i < $blen; $i++) {
7472
if ($bitstr[$i] == '1') {
7573
$type = $x['window'] * 256 + $i;
7674

7775
if (isset(Net_DNS2_Lookups::$rr_types_by_id[$type])) {
78-
7976
$output[] = Net_DNS2_Lookups::$rr_types_by_id[$type];
8077
} else {
81-
8278
$output[] = 'TYPE' . $type;
8379
}
8480
}
@@ -97,8 +93,7 @@ public static function bitMapToArray($data)
9793
* @access public
9894
*
9995
*/
100-
public static function arrayToBitMap(array $data)
101-
{
96+
public static function arrayToBitMap(array $data) {
10297
if (cacti_sizeof($data) == 0) {
10398
return '';
10499
}
@@ -109,7 +104,7 @@ public static function arrayToBitMap(array $data)
109104
// go through each RR
110105
//
111106
$max = 0;
112-
$bm = [];
107+
$bm = [];
113108

114109
foreach ($data as $rr) {
115110
$rr = strtoupper($rr);
@@ -118,26 +113,24 @@ public static function arrayToBitMap(array $data)
118113
// get the type id for the RR
119114
//
120115
$type = @Net_DNS2_Lookups::$rr_types_by_name[$rr];
121-
if (isset($type)) {
122116

117+
if (isset($type)) {
123118
//
124119
// skip meta types or qtypes
125120
//
126-
if ( (isset(Net_DNS2_Lookups::$rr_qtypes_by_id[$type]))
121+
if ((isset(Net_DNS2_Lookups::$rr_qtypes_by_id[$type]))
127122
|| (isset(Net_DNS2_Lookups::$rr_metatypes_by_id[$type]))
128123
) {
129124
continue;
130125
}
131-
132126
} else {
133-
134127
//
135128
// if it's not found, then it must be defined as TYPE<id>, per
136129
// RFC3845 section 2.2, if it's not, we ignore it.
137130
//
138-
list($name, $type) = explode('TYPE', $rr);
139-
if (!isset($type)) {
131+
[$name, $type] = explode('TYPE', $rr);
140132

133+
if (!isset($type)) {
141134
continue;
142135
}
143136
}
@@ -148,21 +141,21 @@ public static function arrayToBitMap(array $data)
148141
$current_window = (int)($type / 256);
149142

150143
$val = $type - $current_window * 256.0;
144+
151145
if ($val > $max) {
152146
$max = $val;
153147
}
154148

155-
$bm[$current_window][$val] = 1;
149+
$bm[$current_window][$val] = 1;
156150
$bm[$current_window]['length'] = ceil(($max + 1) / 8);
157151
}
158152

159153
$output = '';
160154

161155
foreach ($bm as $window => $bitdata) {
162-
163156
$bitstr = '';
164157

165-
for ($i=0; $i<$bm[$window]['length'] * 8; $i++) {
158+
for ($i = 0; $i < $bm[$window]['length'] * 8; $i++) {
166159
if (isset($bm[$window][$i])) {
167160
$bitstr .= '1';
168161
} else {
@@ -186,14 +179,13 @@ public static function arrayToBitMap(array $data)
186179
* @access public
187180
*
188181
*/
189-
public static function bigBaseConvert($number)
190-
{
182+
public static function bigBaseConvert($number) {
191183
$result = '';
192184

193-
$bin = substr(chunk_split(strrev($number), 4, '-'), 0, -1);
185+
$bin = substr(chunk_split(strrev($number), 4, '-'), 0, -1);
194186
$temp = preg_split('[-]', $bin, -1, PREG_SPLIT_DELIM_CAPTURE);
195187

196-
for ($i = cacti_sizeof($temp)-1;$i >= 0;$i--) {
188+
for ($i = cacti_sizeof($temp) - 1; $i >= 0; $i--) {
197189
$result = $result . base_convert(strrev($temp[$i]), 2, 16);
198190
}
199191

Net/DNS2/Cache.php

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,17 @@
2121
* A class to provide simple dns lookup caching.
2222
*
2323
*/
24-
class Net_DNS2_Cache
25-
{
26-
/*
27-
* the filename of the cache file
28-
*/
24+
class Net_DNS2_Cache {
25+
// the filename of the cache file
2926
protected $cache_file = '';
3027

31-
/*
32-
* the local data store for the cache
33-
*/
28+
// the local data store for the cache
3429
protected $cache_data = [];
3530

36-
/*
37-
* the size of the cache to use
38-
*/
31+
// the size of the cache to use
3932
protected $cache_size = 0;
4033

41-
/*
42-
* the cache serializer
43-
*/
34+
// the cache serializer
4435
protected $cache_serializer;
4536

4637
/*
@@ -58,8 +49,7 @@ class Net_DNS2_Cache
5849
* @access public
5950
*
6051
*/
61-
public function has($key)
62-
{
52+
public function has($key) {
6353
return isset($this->cache_data[$key]);
6454
}
6555

@@ -72,17 +62,14 @@ public function has($key)
7262
* @access public
7363
*
7464
*/
75-
public function get($key)
76-
{
65+
public function get($key) {
7766
if (isset($this->cache_data[$key])) {
78-
7967
if ($this->cache_serializer == 'json') {
8068
return json_decode($this->cache_data[$key]['object']);
8169
} else {
8270
return unserialize($this->cache_data[$key]['object']);
8371
}
8472
} else {
85-
8673
return false;
8774
}
8875
}
@@ -91,20 +78,19 @@ public function get($key)
9178
* adds a new key/value pair to the cache
9279
*
9380
* @param string $key the key for the new cache entry
94-
* @param mixed $data the data to store in cache
81+
* @param mixed $data the data to store in cache
9582
*
9683
* @return void
9784
* @access public
9885
*
9986
*/
100-
public function put($key, $data)
101-
{
87+
public function put($key, $data) {
10288
$ttl = 86400 * 365;
10389

10490
//
10591
// clear the rdata values
10692
//
107-
$data->rdata = '';
93+
$data->rdata = '';
10894
$data->rdlength = 0;
10995

11096
//
@@ -118,37 +104,35 @@ public function put($key, $data)
118104
// unserialize the actual RR object when it's get() from the cache.
119105
//
120106
foreach ($data->answer as $index => $rr) {
121-
122107
if ($rr->ttl < $ttl) {
123108
$ttl = $rr->ttl;
124109
}
125110

126-
$rr->rdata = '';
111+
$rr->rdata = '';
127112
$rr->rdlength = 0;
128113
}
129-
foreach ($data->authority as $index => $rr) {
130114

115+
foreach ($data->authority as $index => $rr) {
131116
if ($rr->ttl < $ttl) {
132117
$ttl = $rr->ttl;
133118
}
134119

135-
$rr->rdata = '';
120+
$rr->rdata = '';
136121
$rr->rdlength = 0;
137122
}
138-
foreach ($data->additional as $index => $rr) {
139123

124+
foreach ($data->additional as $index => $rr) {
140125
if ($rr->ttl < $ttl) {
141126
$ttl = $rr->ttl;
142127
}
143128

144-
$rr->rdata = '';
129+
$rr->rdata = '';
145130
$rr->rdlength = 0;
146131
}
147132

148133
$this->cache_data[$key] = [
149-
150134
'cache_date' => time(),
151-
'ttl' => $ttl
135+
'ttl' => $ttl
152136
];
153137

154138
if ($this->cache_serializer == 'json') {
@@ -165,25 +149,20 @@ public function put($key, $data)
165149
* @access protected
166150
*
167151
*/
168-
protected function clean()
169-
{
152+
protected function clean() {
170153
if (cacti_sizeof($this->cache_data) > 0) {
171-
172154
//
173155
// go through each entry and adjust their TTL, and remove entries that
174156
// have expired
175157
//
176158
$now = time();
177159

178160
foreach ($this->cache_data as $key => $data) {
179-
180161
$diff = $now - $data['cache_date'];
181162

182163
if ($data['ttl'] <= $diff) {
183-
184164
unset($this->cache_data[$key]);
185165
} else {
186-
187166
$this->cache_data[$key]['ttl'] -= $diff;
188167
$this->cache_data[$key]['cache_date'] = $now;
189168
}
@@ -198,10 +177,8 @@ protected function clean()
198177
* @access protected
199178
*
200179
*/
201-
protected function resize()
202-
{
180+
protected function resize() {
203181
if (cacti_sizeof($this->cache_data) > 0) {
204-
205182
//
206183
// serialize the cache data
207184
//
@@ -216,9 +193,7 @@ protected function resize()
216193
// is smaller than the actual cache data
217194
//
218195
if (strlen($cache) > $this->cache_size) {
219-
220196
while (strlen($cache) > $this->cache_size) {
221-
222197
//
223198
// go through the data, and remove the entries closed to
224199
// their expiration date.
@@ -227,9 +202,7 @@ protected function resize()
227202
$smallest_key = null;
228203

229204
foreach ($this->cache_data as $key => $data) {
230-
231205
if ($data['ttl'] < $smallest_ttl) {
232-
233206
$smallest_ttl = $data['ttl'];
234207
$smallest_key = $key;
235208
}
@@ -251,7 +224,7 @@ protected function resize()
251224
}
252225
}
253226

254-
if ( ($cache == 'a:0:{}') || ($cache == '{}') ) {
227+
if (($cache == 'a:0:{}') || ($cache == '{}')) {
255228
return null;
256229
} else {
257230
return $cache;

0 commit comments

Comments
 (0)