-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.php
More file actions
297 lines (249 loc) · 9.4 KB
/
Copy pathstrings.php
File metadata and controls
297 lines (249 loc) · 9.4 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
<?php
namespace Puchiko\strings;
use function Puchiko\array\array_equals;
/**
* Format a file size in bytes into a human-readable string (B, KB, MB).
*
* @param int $bytes The file size in bytes.
* @return string Formatted file size string.
*/
function formatFileSize(int $bytes): string {
// If the size is 1 MB (1024 * 1024 bytes) or more
if ($bytes >= 1024 * 1024) {
// Divide by 1 MB and round to 2 decimal places
return round($bytes / (1024 * 1024), 2) . ' MB';
}
// If the size is 1 KB (1024 bytes) or more, but less than 1 MB
elseif ($bytes >= 1024) {
// Divide by 1 KB and cast to integer
return (int)($bytes / 1024) . ' KB';
}
// If the size is less than 1 KB
else {
// Return the size in bytes
return $bytes . ' B';
}
}
// detect if a string contains html tags
function containsHtmlTags(string $string): bool {
return $string !== strip_tags($string);
}
/**
* Builds a URL with query parameters that differ from the defaults.
*
* @param string $baseUrl The base URL without query parameters.
* @param array $defaults Default values for each query parameter.
* @param array $userParams User-specified parameters to compare with defaults.
* @param bool $isAppending Whether the base URL already contains a '?' and to use a '?' when appending vs a '&'. Routes/modes in Kokonotsuba aready have ? set so this will be set true in most cases.
*
* @return string The resulting URL with only non-default parameters.
*/
function buildSmartQuery(string $baseUrl, array $defaults, array $userParams, bool $isAppending = true): string {
$query = [];
foreach ($userParams as $key => $value) {
// Skip empty values
if (empty($value)) {
continue;
}
// Handle array parameters specially
if (is_array($value)) {
// Only include if different from defaults (order-insensitive)
if (!isset($defaults[$key]) || !array_equals($value, $defaults[$key] ?? [])) {
$query[$key] = implode(' ', $value);
}
} else {
// Only include if different from defaults
if (!isset($defaults[$key]) || $value !== $defaults[$key]) {
$query[$key] = $value;
}
}
}
if($isAppending) {
$urlKey = '&';
} else {
$urlKey = '?';
}
// Build URL using RFC1738 encoding
$url = $baseUrl . (empty($query) ? '' : $urlKey . http_build_query($query, '', '&', PHP_QUERY_RFC1738));
return $url;
}
// Currently a simple minify
function html_minify($buffer){
$search = array(
// Remove whitespaces after tags
'/\>[^\S ]+/s',
// Remove whitespaces before tags
'/[^\S ]+\</s',
// Remove multiple whitespace sequences
'/(\s)+/s',
);
$replace = array('>', '<', '\\1');
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
/**
* zlib versions after 1.2.9 b0rks php_handle_swc function in php: https://bugs.php.net/bug.php?id=74910
* so getimagesize() doesn't work with on compressed swfs
* this is a replacement for getimagesize() to use on .swf files
*/
function getswfsize($file) {
$swf = file_get_contents($file);
$swf = unpack(
'a3signature/'.
'Cversion/'.
'Vlength/'.
'a*payload', $swf);
extract($swf);
if ($signature == 'CWS') {
$type = IMAGETYPE_SWC;
$payload = gzuncompress($payload);
} else if ($signature == 'FWS') {
$type = IMAGETYPE_SWF;
} else {
return false;
}
$payload = substr($payload, 0, 17);
$payload = array_values(unpack('C*', $payload));
$nbits = _getbits($payload, 0, 5);
$w = (_getbits($payload, 5 + $nbits * 1, $nbits) -
_getbits($payload, 5 + $nbits * 0, $nbits)) / 20;
$h = (_getbits($payload, 5 + $nbits * 3, $nbits) -
_getbits($payload, 5 + $nbits * 2, $nbits)) / 20;
return [$w, $h, $type, 'width="'.$w.'" height="'.$h.'"',
'mime' => 'application/x-shockwave-flash'];
}
function _getbits($buffer, $pos, $count){
$result = 0;
for ($loop = $pos; $loop < $pos + $count; $loop++) {
$result = $result +
(((($buffer[$loop >> 3]) >> (7 - ($loop % 8))) & 0x01) << ($count - ($loop - $pos) - 1));
}
return $result;
}
function generateUid($length = 8) {
$randomData = bin2hex(random_bytes(8));
$uid = uniqid($randomData, true);
$uid = str_replace('.', '', $uid);
$uid = substr($uid, 0, $length);
return $uid;
}
function sanitizeStr(string $str, bool $isAdmin = false, bool $injectHtml = false, bool $convertNewLines = false): string {
// Trim whitespace from both ends of the string
$str = trim($str);
// Remove potentially problematic characters (e.g., control characters not allowed in XML 1.1)
// Reference: http://www.w3.org/TR/2006/REC-xml11-20060816/#charsets
$str = preg_replace(
'/([\x01-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\x{FDD0}-\x{FDDF}])/u',
'',
htmlspecialchars($str, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')
);
// Convert single quote to HTML entity (htmlspecialchars doesn't convert it by default)
$str = str_replace("'", "'", $str);
// Allow HTML tags when $injectHtml is true and the user is an admin ($isAdmin)
if ($isAdmin && $injectHtml) {
// Convert <tag> back to <tag>
$str = preg_replace('/<(.*?)>/', '<$1>', $str);
}
// Convert newlines to <br> tags if $convertNewLines is true
if ($convertNewLines) {
$str = nl2br($str);
}
return $str;
}
function strlenUnicode($str) {
return mb_strlen($str, 'UTF-8');
}
/**
* Extract GET parameters from a given URL.
*
* This function takes a URL as input, parses it, and returns
* an associative array of GET (query string) parameters.
*
* @param string $url The full URL containing GET parameters.
* @return array An associative array of GET parameters, or an empty array if none are found.
*/
function extractGetParams($url) {
// Parse the URL and extract its components
$parsedUrl = parse_url($url);
// Check if a query string exists in the URL
if (!isset($parsedUrl['query'])) {
return []; // Return an empty array if no query string is present
}
// Initialize an empty array to hold the GET parameters
$getParams = [];
// Parse the query string into an associative array
parse_str($parsedUrl['query'], $getParams);
// Return the associative array of GET parameters
return $getParams;
}
/**
* Generate a URL with the given filters (without pagination).
*
* This function generates a URL with the given filters (e.g., keyword, field, method) and no pagination.
*
* @param string $baseUrl The base URL for the page (e.g., $this->modulePageUrl).
* @param array $filters An associative array of filter names and their values (e.g., ['keyword' => 'test', 'field' => 'com']).
* @return string The full URL with filters (pagination is not handled by this function).
*/
function generateFilteredUrl(string $baseUrl, array $filters = []) {
// Start with the base URL
$url = $baseUrl . '&';
// Append filters to the URL
foreach ($filters as $key => $value) {
// URL encode each filter value to ensure it is safe for URLs
$url .= urlencode($key) . '=' . urlencode($value) . '&';
}
// Remove the last '&' character
return rtrim($url, '&');
}
function autoLink(string $text, string $refUrl = ''): string {
$pattern = '~https?://[^\s<\[]+~i';
return preg_replace_callback($pattern, function ($m) use ($refUrl) {
// 1) Normalize any pre-escaped entities in the matched URL
$url = html_entity_decode($m[0], ENT_QUOTES | ENT_HTML5, 'UTF-8');
// 2) (Optional) Allow only http/https
if (!preg_match('~^https?://~i', $url)) {
return $m[0];
}
// 3) Escape once for HTML attribute; also escape label to be safe in HTML text
$href = htmlspecialchars($url, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$label = htmlspecialchars($url, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
return '<a href="'. $refUrl . $href . '" rel="nofollow noreferrer" target="_blank">' . $label . '</a>';
}, $text);
}
/**
* Safely truncates a string to a maximum length without breaking multibyte characters (e.g., emojis, non-Latin chars).
* Optionally appends an ellipsis (…) if truncation occurs.
*
* @param string $text The input text to truncate.
* @param int $maxLength The maximum number of characters to keep.
* @param string $encoding The character encoding (default is UTF-8).
* @param bool $addEllipsis Whether to append "…" if the text was truncated.
* @return string The safely truncated string.
*/
function truncateText(
string $text,
int $maxLength,
string $encoding = 'UTF-8',
bool $addEllipsis = true
): string {
// If the text length is within the limit, return as is
if (mb_strlen($text, $encoding) <= $maxLength) {
return $text;
}
// Truncate to the desired length (minus 1 if we're adding an ellipsis)
$truncatedLength = $addEllipsis ? $maxLength - 1 : $maxLength;
// Use mb_substr to avoid breaking multibyte characters
$truncated = mb_substr($text, 0, $truncatedLength, $encoding);
// Append ellipsis if desired
return $addEllipsis ? $truncated . '(' . html_entity_decode('…', ENT_QUOTES, $encoding) . ')' : $truncated;
}
/**
* Convert newlines to <br> tags for HTML display
*
* @param string $str The input string.
* @return string The string with newlines converted to <br> tags.
*/
function newLinesToBreakLines(string $str): string {
return nl2br($str, false);
}