Rev 39 | Rev 55 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 39 | Rev 52 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * PHP Utilities - Misc functions |
4 | * PHP Utilities - Misc functions |
5 | * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft |
6 | * |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * you may not use this file except in compliance with the License. |
8 | * you may not use this file except in compliance with the License. |
9 | * You may obtain a copy of the License at |
9 | * You may obtain a copy of the License at |
10 | * |
10 | * |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * |
12 | * |
13 | * Unless required by applicable law or agreed to in writing, software |
13 | * Unless required by applicable law or agreed to in writing, software |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
17 | * limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | // array(89,51,10,10) => 'Y3\012\012' |
20 | // array(89,51,10,10) => 'Y3\012\012' |
21 | function c_literal($byte_array) { |
21 | function c_literal($byte_array) { |
22 | $out = "'"; |
22 | $out = "'"; |
23 | foreach ($byte_array as $c) { |
23 | foreach ($byte_array as $c) { |
24 | if (is_string($c)) $c = ord($c); |
24 | if (is_string($c)) $c = ord($c); |
25 | if ((($c >= 0x00) && ($c <= 0x1F)) || ($c >= 0x7F)) { |
25 | if ((($c >= 0x00) && ($c <= 0x1F)) || ($c >= 0x7F)) { |
26 | // For non-printable characters use octal notation: |
26 | // For non-printable characters use octal notation: |
27 | // \000 ... \377 |
27 | // \000 ... \377 |
28 | $out .= "\\".str_pad(base_convert(''.$c,10,8), 3, '0', STR_PAD_LEFT); |
28 | $out .= "\\".str_pad(base_convert(''.$c,10,8), 3, '0', STR_PAD_LEFT); |
29 | } else { |
29 | } else { |
30 | if (chr($c) == "'") $out .= '\\'; |
30 | if (chr($c) == "'") $out .= '\\'; |
31 | $out .= chr($c); |
31 | $out .= chr($c); |
32 | } |
32 | } |
33 | } |
33 | } |
34 | $out .= "'"; |
34 | $out .= "'"; |
35 | return $out; |
35 | return $out; |
36 | } |
36 | } |
37 | 37 | ||
38 | function c_literal_hexstr($hexstr) { |
38 | function c_literal_hexstr($hexstr) { |
39 | $odd_char = (strlen($hexstr)%2 != 0) ? '0x'.substr($hexstr,-1).'<<4' : ''; |
39 | $odd_char = (strlen($hexstr)%2 != 0) ? '0x'.substr($hexstr,-1).'<<4' : ''; |
40 | $hexstr = substr($hexstr,0,2*(int)floor(strlen($hexstr)/2)); |
40 | $hexstr = substr($hexstr,0,2*(int)floor(strlen($hexstr)/2)); |
41 | if ($hexstr != '') { |
41 | if ($hexstr != '') { |
42 | $ary = str_split(hex2bin($hexstr)); |
42 | $ary = str_split(hex2bin($hexstr)); |
43 | foreach ($ary as &$a) $a = ord($a); |
43 | foreach ($ary as &$a) $a = ord($a); |
44 | return rtrim(c_literal($ary).' '.$odd_char); |
44 | return rtrim(c_literal($ary).' '.$odd_char); |
45 | } else { |
45 | } else { |
46 | return $odd_char; |
46 | return $odd_char; |
47 | } |
47 | } |
48 | } |
48 | } |
49 | 49 | ||
50 | function generateRandomString($length) { |
50 | function generateRandomString($length) { |
51 | // Note: This function can be used in temporary file names, so you |
51 | // Note: This function can be used in temporary file names, so you |
52 | // may not generate illegal file name characters. |
52 | // may not generate illegal file name characters. |
53 | $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
53 | $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
54 | $charactersLength = strlen($characters); |
54 | $charactersLength = strlen($characters); |
55 | $randomString = ''; |
55 | $randomString = ''; |
56 | for ($i = 0; $i < $length; $i++) { |
56 | for ($i = 0; $i < $length; $i++) { |
57 | $randomString .= $characters[rand(0, $charactersLength - 1)]; |
57 | $randomString .= $characters[rand(0, $charactersLength - 1)]; |
58 | } |
58 | } |
59 | return $randomString; |
59 | return $randomString; |
60 | } |
60 | } |
61 | 61 | ||
62 | function trim_br($html) { |
62 | function trim_br($html) { |
63 | $count = 0; |
63 | $count = 0; |
64 | do { $html = preg_replace('@^\s*<\s*br\s*/{0,1}\s*>@isU', '', $html, -1, $count); } while ($count > 0); // left trim |
64 | do { $html = preg_replace('@^\s*<\s*br\s*/{0,1}\s*>@isU', '', $html, -1, $count); } while ($count > 0); // left trim |
65 | do { $html = preg_replace('@<\s*br\s*/{0,1}\s*>\s*$@isU', '', $html, -1, $count); } while ($count > 0); // right trim |
65 | do { $html = preg_replace('@<\s*br\s*/{0,1}\s*>\s*$@isU', '', $html, -1, $count); } while ($count > 0); // right trim |
66 | return $html; |
66 | return $html; |
67 | } |
67 | } |
68 | 68 | ||
69 | function insertWhitespace($str, $index) { |
69 | function insertWhitespace($str, $index) { |
70 | return substr($str, 0, $index) . ' ' . substr($str, $index); |
70 | return substr($str, 0, $index) . ' ' . substr($str, $index); |
71 | } |
71 | } |
72 | 72 | ||
73 | function js_escape($data) { |
73 | function js_escape($data) { |
74 | // TODO.... json_encode?? |
74 | // TODO.... json_encode?? |
75 | $data = str_replace('\\', '\\\\', $data); |
75 | $data = str_replace('\\', '\\\\', $data); |
76 | $data = str_replace('\'', '\\\'', $data); |
76 | $data = str_replace('\'', '\\\'', $data); |
77 | return "'" . $data . "'"; |
77 | return "'" . $data . "'"; |
78 | } |
78 | } |
79 | 79 | ||
80 | function get_calling_function() { |
80 | function get_calling_function() { |
81 | $ex = new Exception(); |
81 | $ex = new Exception(); |
82 | $trace = $ex->getTrace(); |
82 | $trace = $ex->getTrace(); |
83 | if (!isset($trace[2])) return '(main)'; |
83 | if (!isset($trace[2])) return '(main)'; |
84 | $final_call = $trace[2]; |
84 | $final_call = $trace[2]; |
85 | return $final_call['file'].':'.$final_call['line'].'/'.$final_call['function'].'()'; |
85 | return $final_call['file'].':'.$final_call['line'].'/'.$final_call['function'].'()'; |
86 | } |
86 | } |
87 | 87 | ||
88 | function convert_to_utf8_no_bom($cont) { |
88 | function convert_to_utf8_no_bom($cont) { |
89 | if (mb_detect_encoding($cont, "auto", true) != 'UTF-8') { |
- | |
90 | # $cont = mb_convert_encoding($cont, 'UTF-8', 'Windows-1252'); |
- | |
91 | # $cont = mb_convert_encoding($cont, 'UTF-8', 'auto'); |
- | |
92 | $cont = mb_convert_encoding($cont, 'UTF-8'); |
89 | $cont = vts_utf8_encode($cont); |
93 | } |
- | |
94 | 90 | ||
95 | // Remove BOM |
91 | // Remove BOM |
96 | $bom = pack('H*','EFBBBF'); |
92 | $bom = pack('H*','EFBBBF'); |
97 | $cont = preg_replace("/^$bom/", '', $cont); |
93 | $cont = preg_replace("/^$bom/", '', $cont); |
98 | return $cont; |
94 | return $cont; |
99 | } |
95 | } |
- | 96 | ||
- | 97 | function vts_utf8_encode($text) { |
|
- | 98 | $enc = mb_detect_encoding($text, null, true); |
|
- | 99 | if ($enc === false) $enc = mb_detect_encoding($text, ['ASCII', 'UTF-8', 'Windows-1252', 'ISO-8859-1'], true); |
|
- | 100 | if ($enc === false) $enc = null; |
|
- | 101 | ||
- | 102 | if ($enc === 'UTF-8') return $text; |
|
- | 103 | ||
- | 104 | $res = mb_convert_encoding($text, 'UTF-8', $enc); |
|
- | 105 | if ($res === false) $res = iconv('UTF-8', 'UTF-8//IGNORE', $text); |
|
- | 106 | ||
- | 107 | return $res; |
|
- | 108 | } |
|
100 | 109 | ||
101 | function stripHtmlComments($html) { |
110 | function stripHtmlComments($html) { |
102 | // https://stackoverflow.com/questions/11337332/how-to-remove-html-comments-in-php |
111 | // https://stackoverflow.com/questions/11337332/how-to-remove-html-comments-in-php |
103 | $html = preg_replace("~<!--(?!<!)[^\[>].*?-->~s", "", $html); |
112 | $html = preg_replace("~<!--(?!<!)[^\[>].*?-->~s", "", $html); |
104 | return $html; |
113 | return $html; |
105 | } |
114 | } |
106 | 115 | ||
107 | function wildcard_is_dir($dir) { |
116 | function wildcard_is_dir($dir) { |
108 | // Example usage: if (!wildcard_is_dir(OIDplus::localpath().'plugins/'.'*'.'/design/'.$value)) throw new Exception("Design does not exist") |
117 | // Example usage: if (!wildcard_is_dir(OIDplus::localpath().'plugins/'.'*'.'/design/'.$value)) throw new Exception("Design does not exist") |
109 | $dirs = @glob($dir); |
118 | $dirs = @glob($dir); |
110 | if ($dirs) foreach ($dirs as $dir) { |
119 | if ($dirs) foreach ($dirs as $dir) { |
111 | if (is_dir($dir)) return true; |
120 | if (is_dir($dir)) return true; |
112 | } |
121 | } |
113 | return false; |
122 | return false; |
114 | } |
123 | } |
115 | 124 | ||
116 | function isInternetExplorer() { |
125 | function isInternetExplorer() { |
117 | // see also includes/oidplus_base.js |
126 | // see also includes/oidplus_base.js |
118 | $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
127 | $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
119 | return ((strpos($ua,'MSIE ') !== false) || (strpos($ua,'Trident/') !== false)); |
128 | return ((strpos($ua,'MSIE ') !== false) || (strpos($ua,'Trident/') !== false)); |
120 | } |
129 | } |
121 | 130 |