-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.inc.php
134 lines (115 loc) · 3.36 KB
/
functions.inc.php
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
<?php
namespace GBoudreau\HDHomeRun\Scheduler;
function array_remove($array, $value_to_remove) {
return array_values(array_diff($array, array($value_to_remove)));
}
function strip_accents($text) {
return strtr($text, 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ', 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
}
function _array_shift($array) {
return array_shift($array);
}
function string_contains($haystack, $needle) {
return stripos($haystack, $needle) !== FALSE;
}
function string_begins_with($haystack, $needle) {
return ( stripos($haystack, $needle) === 0 );
}
function string_ends_with($haystack, $needle) {
return ( substr(strtolower($haystack), -strlen($needle)) == strtolower($needle) );
}
function string_contains_array($haystack, $needle_array) {
if (empty($haystack)) {
return FALSE;
}
foreach ($needle_array as $el) {
if (string_contains($haystack, $el)) {
return TRUE;
}
}
return FALSE;
}
function array_contains($haystack, $needle) {
if (empty($haystack)) {
return FALSE;
}
return array_search($needle, $haystack) !== FALSE;
}
function array_clone(array $array) : array {
$new_array = [];
foreach ($array as $k => $v) {
if (is_object($v)) {
$new_array[$k] = clone $v;
} elseif (is_array($v)) {
$new_array[$k] = array_clone($v);
} else {
$new_array[$k] = $v;
}
}
return $new_array;
}
function last($array) {
if (empty($array) || !is_array($array)) {
return FALSE;
}
foreach (array_reverse($array) as $el) {
return $el;
}
}
function clean_dir_name($dir_name) {
return str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $dir_name);
}
function _log(string $log, bool $skip_prefix = FALSE) {
$success = FALSE;
if (!$skip_prefix) {
if (defined('RECORDING_HASH')) {
$log = "[record-" . RECORDING_HASH . "] $log";
} else {
$log = "[cron] $log";
}
$log = "[" . date('Y-m-d H:i:s') . "] $log";
}
$log = str_replace("\t", " ", $log);
$log_file = Config::get('LOG_FILE');
if ($log_file) {
// Log to file
$success = error_log($log . "\n", 3, $log_file);
}
if (!$success) {
// Log to stderr
error_log($log);
}
}
function he($text) {
$text = str_replace(' ', 0x0a00, $text);
$text = htmlentities($text, ENT_COMPAT|ENT_QUOTES, 'UTF-8');
$text = str_replace(0x0a00, ' ', $text);
return $text;
}
function rhe($text) {
return html_entity_decode($text, ENT_COMPAT|ENT_QUOTES, 'UTF-8');
}
function phe($text) {
echo he($text);
}
function js($text) {
return str_replace("'", "\\'", $text);
}
function pjs($text) {
echo js($text);
}
function echo_if($condition, $text_if_true, $text_if_false = '') {
if ($condition) {
echo $text_if_true;
} else {
echo $text_if_false;
}
}
function print_result() {
if (!empty($_REQUEST['result'])) {
$class = (!empty($_REQUEST['result']['success']) ? 'bg-success' : 'bg-danger' );
echo '<div class="card text-white ' . $class . '"><div class="card-body">';
phe($_REQUEST['result']['success'] ?? $_REQUEST['result']['error']);
echo '</div></div>';
}
}