1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bootstrapping functions, essential and needed for Anax to work together with some common helpers. |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* i18n, internationalization, send all strings though this function to enable i18n. Inspired by Drupal´s t()-function. |
11
|
|
|
* |
12
|
|
|
* @param string $str the string to check up for translation. |
13
|
|
|
* @param array $args associative array with arguments to be |
14
|
|
|
* replaced in the str. |
15
|
|
|
* - !variable: Inserted as is. Use this for text |
16
|
|
|
* that has already been sanitized. |
17
|
|
|
* - @variable: Escaped to HTML using htmlEnt(). Use |
18
|
|
|
* this for anything displayed on a page on the site. |
19
|
|
|
* |
20
|
|
|
* @return string the translated string. |
21
|
|
|
*/ |
22
|
|
|
function t($str, $args = []) |
23
|
|
|
{ |
24
|
|
|
/* |
25
|
|
|
if (CLydia::Instance()->config["i18n"]) { |
26
|
|
|
$str = gettext($str); |
27
|
|
|
} |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
// santitize and replace arguments |
31
|
|
|
if (!empty($args)) { |
32
|
|
|
foreach ($args as $key => $val) { |
33
|
|
|
switch ($key[0]) { |
34
|
|
|
case "@": |
35
|
|
|
$args[$key] = htmlentities($val); |
36
|
|
|
break; |
37
|
|
|
|
38
|
|
|
case "!": |
39
|
|
|
default: /* pass through */ |
40
|
|
|
break; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
return strtr($str, $args); |
44
|
|
|
} |
45
|
|
|
return $str; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sort array but maintain index when compared items are equal. |
52
|
|
|
* http://www.php.net/manual/en/function.usort.php#38827 |
53
|
|
|
* |
54
|
|
|
* @param array &$array input array |
55
|
|
|
* @param callable $cmp_function custom function to compare values |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
* |
59
|
|
|
*/ |
60
|
|
|
function mergesort(&$array, $cmp_function) |
61
|
|
|
{ |
62
|
|
|
// Arrays of size < 2 require no action. |
63
|
|
|
if (count($array) < 2) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
// Split the array in half |
67
|
|
|
$halfway = count($array) / 2; |
68
|
|
|
$array1 = array_slice($array, 0, $halfway); |
69
|
|
|
$array2 = array_slice($array, $halfway); |
70
|
|
|
// Recurse to sort the two halves |
71
|
|
|
mergesort($array1, $cmp_function); |
72
|
|
|
mergesort($array2, $cmp_function); |
73
|
|
|
// If all of $array1 is <= all of $array2, just append them. |
74
|
|
|
if (call_user_func($cmp_function, end($array1), $array2[0]) < 1) { |
75
|
|
|
$array = array_merge($array1, $array2); |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
// Merge the two sorted arrays into a single sorted array |
79
|
|
|
$array = array(); |
80
|
|
|
$ptr1 = $ptr2 = 0; |
81
|
|
|
while ($ptr1 < count($array1) && $ptr2 < count($array2)) { |
82
|
|
|
if (call_user_func($cmp_function, $array1[$ptr1], $array2[$ptr2]) < 1) { |
83
|
|
|
$array[] = $array1[$ptr1++]; |
84
|
|
|
} else { |
85
|
|
|
$array[] = $array2[$ptr2++]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
// Merge the remainder |
89
|
|
|
while ($ptr1 < count($array1)) { |
90
|
|
|
$array[] = $array1[$ptr1++]; |
91
|
|
|
} |
92
|
|
|
while ($ptr2 < count($array2)) { |
93
|
|
|
$array[] = $array2[$ptr2++]; |
94
|
|
|
} |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Glob recursivly. |
102
|
|
|
* http://in.php.net/manual/en/function.glob.php#106595 |
103
|
|
|
* |
104
|
|
|
* @param string $pattern pattern to search for |
105
|
|
|
* @param int $flags flags to use, as in glob() |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
* |
109
|
|
|
*/ |
110
|
|
|
function glob_recursive($pattern, $flags = 0) |
111
|
|
|
{ |
112
|
|
|
$files = glob($pattern, $flags); |
113
|
|
|
foreach (glob(dirname($pattern) . "/*", GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { |
114
|
|
|
$files = array_merge($files, glob_recursive($dir . "/" . basename($pattern), $flags)); |
115
|
|
|
} |
116
|
|
|
return $files; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* array_merge_recursive does indeed merge arrays, but it converts values with duplicate |
123
|
|
|
* keys to arrays rather than overwriting the value in the first array with the duplicate |
124
|
|
|
* value in the second array, as array_merge does. I.e., with array_merge_recursive, |
125
|
|
|
* this happens (documented behavior): |
126
|
|
|
* |
127
|
|
|
* array_merge_recursive(array('key' => 'org value'), array('key' => 'new value')); |
128
|
|
|
* => array('key' => array('org value', 'new value')); |
129
|
|
|
* |
130
|
|
|
* array_merge_recursive_distinct does not change the datatypes of the values in the arrays. |
131
|
|
|
* Matching keys' values in the second array overwrite those in the first array, as is the |
132
|
|
|
* case with array_merge, i.e.: |
133
|
|
|
* |
134
|
|
|
* array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value')); |
135
|
|
|
* => array('key' => array('new value')); |
136
|
|
|
* |
137
|
|
|
* Parameters are passed by reference, though only for performance reasons. They're not |
138
|
|
|
* altered by this function. |
139
|
|
|
* |
140
|
|
|
* @param array $array1 |
141
|
|
|
* @param array $array2 |
142
|
|
|
* @return array |
143
|
|
|
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk> |
144
|
|
|
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com> |
145
|
|
|
*/ |
146
|
|
|
function array_merge_recursive_distinct(array &$array1, array &$array2) |
147
|
|
|
{ |
148
|
|
|
$merged = $array1; |
149
|
|
|
|
150
|
|
|
foreach ($array2 as $key => &$value) { |
151
|
|
|
if (is_array($value) && isset($merged [$key]) && is_array($merged [$key])) { |
152
|
|
|
$merged [$key] = array_merge_recursive_distinct($merged [$key], $value); |
153
|
|
|
} else { |
154
|
|
|
$merged [$key] = $value; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $merged; |
159
|
|
|
} |
160
|
|
|
|