Subversion Repositories php_utils

Rev

Rev 52 | Rev 56 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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