-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjbxl_tools.php
More file actions
358 lines (303 loc) · 10.2 KB
/
jbxl_tools.php
File metadata and controls
358 lines (303 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
//
// by Fumi.Iseki 2007/03/24
// 2012/04/12
// 2013/04/20
// 2013/09/21
// 2014/11/28
// 2016/05/26
// 2019/08/21
// 2022/07/06
// 2025/02/03
//
$jbxl_tools_ver = 2025020300;
//
if (!defined('JBXL_TOOLS_VER') and !defined('_JBXL_TOOLS')) {
define('JBXL_TOOLS_VER', $jbxl_tools_ver);
/****************************************************************************************
//
// function jbxl_chng_timeformat($format)
// function jbxl_strftime($format, $timestamp=null)
//
// function jbxl_to_subnetformats($strips)
// function jbxl_match_ipaddr($ip, $ipaddr_subnets)
// function jbxl_randstr($len=8, $lowcase=false)
//
// function jbxl_get_ipresolv_url($ip)
//
// function jbxl_get_url_params_array($urlstr)
// function jbxl_get_url_params_str($params, $amp=false)
// function jbxl_make_url($serverURI, $portnum=0)
//
*****************************************************************************************/
//
// Change time format from strftime to IntlDateFormatter
//
function jbxl_chng_timeformat($format)
{
$f1 = array('/%Y/', '/%y/', '/%m/', '/%-m/', '/%B/', '/%b/', '/%h/', '/%d/', '/%-d/', '/%j/', '/%U/', '/%W/', '/%w/', '/%A/', '/%a/');
$t1 = array('yyyy', 'yy', 'MM', 'M', 'MMMM', 'MMM', 'MMM', 'dd', 'd', 'D', 'ww', 'ww', 'c', 'EEEE', 'EEE');
$f2 = array('/%H/', '/%-H/', '/%I/', '/%-I/', '/%p/', '/%M/', '/%-M/', '/%S/', '/%-S/', '/%Z/', '/%z/', '/%c/', '/%X/', '/%x/');
$t2 = array('HH', 'H', 'hh', 'h', 'a', 'mm', 'm', 'ss', 's', 'VV', 'xx', 'yyyy/MM/dd HH:mm:ss', 'HH:mm:ss', 'yyyy/MM/dd');
$nwfrmt = preg_replace($f1, $t1, $format);
$nwfrmt = preg_replace($f2, $t2, $nwfrmt);
return $nwfrmt;
}
//
// PHP8.1 より非推奨となった strftime() の代替関数.jbxl_chng_timeformat() を使用.
//
function jbxl_strftime($format, $timestamp=null)
{
if (version_compare(PHP_VERSION, '8.1.0', '>=')) {
$dt = new DateTime();
if ($timestamp!=null) $dt->setTimestamp($timestamp);
//
$newformat = jbxl_chng_timeformat($format);
$locale = setlocale(LC_ALL, locale_get_default());
//$formatter = new IntlDateFormatter(setlocale(LC_CTYPE, 0), IntlDateFormatter::NONE,
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE,
IntlDateFormatter::NONE, null, IntlDateFormatter::GREGORIAN, $newformat);
try {
return $formatter->format($dt);
} catch(\Error $e) {
//print_r($e);
}
}
else {
return strftime($format, $timestamp);
}
}
//
// IPアドレスを "," または半角空白で区切って記述した文字列から,有効なIPアドレス
// とサブネットを8bitずつ取り出す.CIDER対応
//
// 入力例:" 0.2.1.1/2 222.222.111.222/255., 123.31.6.000 202.26.156.2/20, 202.26.144.0/255.255.255.0 , "
//
// 戻り値:
// $return[index]['ipaddr'][0〜3の数] 8bit区切りの IPアドレス
// $return[index]['subnet'][0〜3の数] 8bit区切りの netmaskアドレス
//
function jbxl_to_subnetformats($strips)
{
$return = array();
$tmpips = preg_split("/[ ,]/ ", $strips);
foreach($tmpips as $value) {
if (!empty($value)) $ipfmts[] = $value;
}
if (empty($ipfmts)) return $return;
unset($tempips);
// omission of subnetmask
foreach($ipfmts as $ipfmt) {
$tempips = explode('/', $ipfmt);
if (empty($tempips[0])) continue;
if (empty($tempips[1])) {
$ips = explode('.', $tempips[0]);
$tempips[1] = '';
for ($i=0; $i<4; $i++) {
if (empty($ips[$i])) $tempips[1].= '0';
else $tempips[1].= '255';
if ($i!=3) $tempips[1].= '.';
}
unset($ips);
}
$ipaddr_subnets[] = $tempips;
}
if (empty($ipaddr_subnets)) return $return;
//
$index = 0;
foreach($ipaddr_subnets as $ipaddr_subnet) {
$ips = explode('.', $ipaddr_subnet[0]);
$sub = explode('.', $ipaddr_subnet[1]);
if (count($sub)==1 and $sub[0]<=32) { // CIDER -> SubnetMask
$cider = $sub[0];
$nbyte = (int)($cider/8);
$nbit = $cider - $nbyte*8;
for ($i=0; $i<$nbyte; $i++) {
$sub[$i] = 255;
}
if ($nbyte!=4) {
$nsub = 0;
$base = 128;
for ($i=0; $i<$nbit; $i++) {
$nsub += $base;
$base = $base/2;
}
$sub[$nbyte] = $nsub;
}
}
for ($i=0; $i<4; $i++) {
if (!empty($ips[$i])) $return[$index]['ipaddr'][$i] = (int)$ips[$i];
else $return[$index]['ipaddr'][$i] = (int)0;
if (!empty($sub[$i])) $return[$index]['subnet'][$i] = (int)$sub[$i];
else $return[$index]['subnet'][$i] = (int)0;
}
$index++;
}
return $return;
}
//
// $ip が $ipaddr_subnetsの中に含まれるか検査する.
// $ipaddr_subnets は jbxl_to_subnetformats()が出力したものを使用すること.
// $ip の内容の形式はチェックしない.これは呼び出し側の責任.
//
function jbxl_match_ipaddr($ip, array $ipaddr_subnets)
{
$ipa = explode('.', $ip);
if (empty($ipa)) return false;
for ($i=1; $i<4; $i++) {
if (empty($ipa[$i])) $ipa[$i] = 0;
}
foreach($ipaddr_subnets as $ipaddr_subnet) {
$ips = $ipaddr_subnet['ipaddr'];
$sub = $ipaddr_subnet['subnet'];
$match_f = true;
for ($i=0; $i<4; $i++) {
$check1 = $ipa[$i] & $sub[$i];
$check2 = $ips[$i] & $sub[$i];
if ($check1 != $check2) {
$match_f = false;
break;
}
}
if ($match_f) {
//print_r($ips);
//print_r($sub);
return true;
}
}
return false;
}
$JBXLBaseChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
function jbxl_randstr($len=8, $lowcase=false)
{
global $JBXLBaseChar;
if ($lowcase) $rndmax = 25;
else $rndmax = strlen($JBXLBaseChar) - 1;
$return = "";
for($i=0; $i<$len; $i++) {
$randnum = mt_rand(0, $rndmax);
$return .= substr($JBXLBaseChar, $randnum, 1);
//$return .= $JBXLBaseChar{mt_rand(0, $rndmax)}; // Deprecated
}
return $return;
}
function jbxl_get_ipresolv_url($ip, $region='APNIC')
{
if (!preg_match('/(^\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $match)) return '';
if ($match[1]>255 or $match[2]>255 or $match[3]>255 or $match[4]>255) return '';
if ($match[1]=='127' or $match[1]=='10') return '';
if ($match[1]=='172' and $match[2]>='16' and $match[2]<='31') return '';
if ($match[1]=='192' and $match[2]=='168') return '';
if ($region=='JPNIC') {
$url = 'http://whois.nic.ad.jp/cgi-bin/whois_gw?type=NET&key='.$ip; // JPNIC
}
else {
$url = 'http://wq.apnic.net/apnic-bin/whois.pl?searchtext='.$ip; // APNIC
}
return $url;
}
function jbxl_get_url_params_array($urlstr)
{
$strs = explode('?', $urlstr);
$paramstr = $strs[0];
if (array_key_exists(1, $strs)) $paramstr = $strs[1];
$ret = array();
$params = explode('&', $paramstr);
foreach($params as $param) {
if (substr($param, 0, 4)=='amp;') $param = substr($param, 5);
$temps = explode('=', $param);
$ret[$temps[0]] = '';
if (array_key_exists(1, $temps)) $ret[$temps[0]] = $temps[1];
}
return $ret;
}
//
// $params: パラメータの入っている配列
// $amp: 先頭文字を '&' にするか? false の場合は 先頭文字は '?'
//
function jbxl_get_url_params_str($params, $amp=false)
{
$ret = '';
if (!is_array($params)) return $ret;
$no = 0;
foreach($params as $key => $param) {
if ($no==0 and !$amp) {
$ret .= '?'.$key.'='.$param;
}
else {
$ret .= '&'.$key.'='.$param;
}
$no++;
}
return $ret;
}
//
// 入力された FSDN, URL に対して http(s)://ABC.EFG:#/ の形を生成する
//
function jbxl_make_url($serverURI, $portnum=0)
{
$url = '';
$host = 'localhost';
$port = 80;
$protocol = 'http';
if ($serverURI!=null) {
$uri = preg_split("/[:\/]/", $serverURI);
// with http:// or https://
if (array_key_exists(3, $uri)) {
$protocol = $uri[0];
$host = $uri[3];
//
if (array_key_exists(4, $uri)) {
$port = $uri[4];
}
else {
if ($portnum!=0) {
$port = $portnum;
}
else {
if ($uri[0]=='http') $port = 80;
else if ($uri[0]=='https') $port = 443;
else if ($uri[0]=='ftp') $port = 21;
// else if ....
}
}
}
// with no http:// and https://
else {
$host = $uri[0];
if (array_key_exists(1, $uri)) {
$port = $uri[1];
}
else {
if ($portnum!=0) {
$port = $portnum;
}
else {
$port = 80;
}
}
}
//
if ($port==443) {
$url = 'https://'.$host.':'.$port.'/';
$protocol = 'https';
}
else if ($port==80) {
$url = 'http://'.$host.'/';
$protocol = 'http';
}
else if ($port==21) {
$url = 'ftp://'.$host.'/';
$protocol = 'ftp';
}
else {
$url = $protocol.'://'.$host.':'.$port.'/';
}
}
$server['url'] = $url;
$server['host'] = $host;
$server['port'] = $port;
$server['porotocol'] = $protocol;
return $server;
}
} // !defined('JBXL_TOOLS_VER')