-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
597 lines (537 loc) · 25.3 KB
/
index.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
<?php /// 'HARDCODER HNGTS' ~ boilerplate.catmice-safe;
declare (strict_types = 1);
/// Licence -> https://hngts.com/?mkp=licence~catmice
/// ~ See https://hngts.com/?mkp=catmice~man for more details.
use \H\Api; /*:
requires: PHPv>=8.2 w ctype & mb_string;
(optional: hntgs); */
new class (0, false, true, true, true, false, __DIR__) {
/*:-> get --- STATIC CONFIGURABLE PORTION ---
1st:hrs - int 'hours_for_cache'
2nd:sq - bool 'source_squeeze'
3rd:c - bool 'comments_single'
4th:bc - bool 'block_comments'
5th:s - bool 'signature_print'
6th:ex - bool 'expand_externals'
7th:!! - string 'directory'
*/
private const TRADEMARK = "\342\204\242"; /// string
/// ™ special directory name
private const DIRECTORY_MASK = [ /// Confront to other directory names
'script' => 'script',
//~ 'script' => '.mices', // ? . in front is public, but doesn't need to be.
'style' => 'style',
//~ 'style' => '.cats', // ? . in front means - system directory
];
//~---
private const AT_CHARSET = true; /// bool
/// Cats only.
private const ZERO_HOUR_SKEPTIC = false; /// bool
/// Prevents TOTAL fresh content
private const ZERO_POINT_IMMUTABLE = 0.0020; /// float
/// Works only if $hours === 0 and ZERO_HOUR_SKEPTIC === true
private const STREAM_CONTEXT_TIMEOUT = 30; /// int
/// Timeout for lazy third-party servers
private const CLI_SERVER_STREAM = false; /// bool
/// Prevents fopen and streaming_context over cli-sapi
// --- END OF CONFIGURABLES ! ---
private const OPT_ARGUMENT = [ /// _GET => $this
0 => 'hrs',
'sq' => 'squeeze',
'c' => 'comments',
'bc' => 'blockCmt',
's' => 'sig',
'ex' => 'expandExternals',
];
private const HALT = 'Not a proper request.'; /// Common message.
private ?int $hours = null; /// Constructor Param 1
private ?bool $squeeze = null; /// Constructor Param 2
private ?bool $comments = null; /// Constructor Param 3
private ?bool $blockCmt = null; /// Constructor Param 4
private ?bool $sig = null; /// Constructor Param 5
private ?bool $expandExternals = null; /// Constructor Param 6
private ?string $basedir = null; /// Constructor Param 7
private ?int $pack = null; /// if > 0 (hours), will write to disk
private ?bool $template_call = null; /// Becomes true if template call
private ?string $target_id = null; /// cat or mice
private ?string $mimeLocation = null; /// Path for .mime.types
private mixed $packed = null; /// Holds json template and packed.gz path
public function __construct (...$p) {
/// Check
(\count ($p) !== 7) and die (
'Catmice' . __FUNCTION__
. 'or error: args required 7; less found.'
. PHP_EOL . 'Don\'t mess too much with parameters.'
);
// set ..
foreach ([
'sig' => $p[4],
'squeeze' => $p[1],
'comments' => $p[2],
'blockCmt' => $p[3],
'expandExternals' => $p[5],
] as $oP => $boolVal) {
$this-> $oP = ((bool)$boolVal);
unset ($oP, $boolVal);
} // .. and go !
$this-> hours = ((int)$p[0]); $this-> hook____ ($p[6], $Public);
!\in_array (Observer-> doctype, \array_keys (self::DIRECTORY_MASK))
and die (self::HALT) // <- sEcuritate.
or // .. and main Request and Response - done !
$this-> catmice_file_collector ($Public, $catmice);
}
private function hook____ (string $fBdir, ?string &$Public = null): void {
/// According to the $Public value and passed on $fBdir
/// the script will know whether it is symlinked or not.
$Public = \dirname ($_SERVER['SCRIPT_FILENAME']);
$this-> basedir = \basename (($Public !== $fBdir) ? $Public : $fBdir);
// This is for the options override via _GET parameter
// 'hrs' is sliced and resolved in hngts_dependancy method
// as that predefined variable is part of broader (parent)
// `Hardcoder Next Generation Toolset` project.
foreach (\array_slice (self::OPT_ARGUMENT, 1) as $opt => $property) {
if (isset ($_GET[$opt]) && \property_exists ($this, $property)) {
$this-> $property = ((bool)\filter_input (INPUT_GET, $opt, FILTER_UNSAFE_RAW));
} unset ($property, $opt);
} // --
// Since this script is a part of bigger codebase, but at the same time
// works as a standalone script, replacement for composer and alike follows.
// If `H`(ardcoder)\Api class isn't loaded correctly, we need compatibility mimicry.
// And compatibility mimicry is whether You want to believe or not - named constant.
\defined ('HNG_ACTIVE_PLATFORM') and (!\defined ('Api') and \define ('Api', Api::do()))
or $this-> hngts_dependancy(); $this-> mimeLocation = Api-> dspa ($Public, self::TRADEMARK);
// ^^ Template path becomes ready
}
private function catmice_file_collector (string $dir, ?string &$catmice = null): void {
/// Main operation method
$filteredID = $this-> match_exact_type ($id, $extension, $c_type);
$this-> template_call = \str_starts_with ($filteredID, "\176\176");
$at_charset = ($id === 'cat' && self::AT_CHARSET);
$dL1 = Api-> dspa (\dirname ($dir), $this-> basedir);
$dL2 = Api-> dspa ($dL1, self::DIRECTORY_MASK[Observer-> doctype]);
// Automatically create missing directories (if any).
$trademark = Api-> dspa ($dL2, self::TRADEMARK);
!\is_dir ($trademark) and \mkdir ($trademark, 0775, true);
if (!$this-> template_call) {
$this-> target_id = $id;
$catmice = $this-> catmice_transmute
($filteredID, $extension, $dL1, $dL2, $collect);
$this-> cat_charset_and_sig ($at_charset, $catmice);
}
else {
$_GET = [ $id => $filteredID ]; // overwrite _GET
$this-> target_id = \mb_substr ($filteredID, 2);
if (!$this-> fetch_object_template ($extension, $this-> target_id)) {
echo self::HALT . ' JSON missing or invalid!'; return;
}
$json = $this-> packed['json'];
$this-> packed = $this-> packed['gz'];
foreach (['hours', 'pack', 'sig'] as $prop)
$this-> $prop = $json-> $prop; unset ($prop);
if (\is_file ($this-> packed)) {
$stored = $this-> hours_multiply ($this-> pack);
$since = (\filemtime ($this-> packed) + $stored);
if (\time() < $since) {
$catmice = \gzinflate (
\file_get_contents ($this-> packed)
); $file_ready = true;
}
else {
\unlink ($this-> packed);
$file_ready = false;
}
}
else $file_ready = false;
if ($file_ready !== true || !\file_exists ($this-> packed)) {
$mut = (($id === 'mice') ? 'comments':'expandExternals');
foreach ($json-> concat as $n => $std) {
foreach ([$mut, 'blockCmt', 'squeeze']
as $prop) $this-> $prop = $std-> $prop;
unset ($prop); $catmice .= $this-> catmice_transmute
($std-> $id, $extension, $dL1, $dL2, $collect);
unset ($std, $json-> concat[$n], $n);
} unset ($json, $mut);
$this-> cat_charset_and_sig
($at_charset, $catmice);
}
}
$this-> time_and_headers ($c_type, $catmice);
}
private function match_exact_type (?string &$id, ?string &$ext, ?string &$c_type): ?string {
/*: Self-explanatory */ [$id, $ext, $c_type] = match (Observer-> doctype) {
'style' => [ 'cat', 'css', 'text/css' ]
, 'script' => [ 'mice', 'js', 'text/javascript' ]
, default => [ 'x', 'txt', 'text/plain' ] // <- this never sets
}; return Observer-> filterget[$id];
}
private function cat_charset_and_sig (bool $condition, string &$catmice): void {
/// Prepends charset for css and appends block comment signature
if ($condition) $this-> cat_charset ($catmice);
if ($this-> sig) $catmice .= $this-> catmice_signature();
}
private function hours_multiply (int|float $hours): int {
/*:*/ return ((int)(60 * 60 * $hours));
}
private function catmice_transmute (
string $id, string $ext, string $dL1, string $dL2, ?array &$collect
): string { /// This is most expensive method. Does 90% of the job.
$this-> collect_iterator ($id, $ext, $dL1, $dL2, $collect);
$this-> build_iterated_string ($collect, $catmice);
$this-> collection_mockery ($ext, $catmice);
if ($ext === 'css' && $this-> expandExternals) {
// From url (param) to base64,...
$cfn = 'url'; $pattern = '/' . $cfn . '\((.*?)\)/';
$catmice = \preg_replace_callback ($pattern . 'imsu',
fn($extern) => $this-> expand_externals ($extern, $cfn),
$catmice);
}
return $catmice;
}
private function cat_charset (string &$catmice): void {
/// Prepend utf-8 charset for styles
$At = '@charset "UTF-8";';
$catmice = "{$At} " . \str_replace (
[\strtolower ($At), $At], '', $catmice);
unset ($At); // <- mere habit.
}
private function collect_iterator (
string $q, string $ext, string $dL1, string $dL2, ?array &$collect = null
): void { /// Main iterator and a bLYAatIful! piece of code
$collect = [];
$catmice = \explode ("\176", $q);
foreach ($catmice as $n => $suspect) {
// $suspect is quite unknown here ..
$suspect = \str_replace ('|', DSP, $suspect);
$first = \mb_substr ($suspect, 0, 1);
[ $job, $multi, $target ] = match ($first) {
default => [ 'literal', 'no', $dL2 . DSP ]
, "\044" => [ 'concat', 'yes', $dL2 . DSP . 'chain.' ]
, "\045" => [ 'well', // ....
((\mb_strpos ($suspect, '.') !== false)
? 'no':'yes'), $dL2 . DSP . 'pot.'
], "\056" => [ 'root', 'no', $dL1 . DSP ],
"\140" => [ 'trademark', 'no', $dL2 . DSP . self::TRADEMARK . DSP ]
}; if ($job !== 'literal') $suspect = \mb_substr ($suspect, 1);
if ($multi === 'yes'): $directory = "$target$suspect";
if (!\is_dir ($directory)): $collect[] = '/* Bad `'
. $job . '` for `' . \basename ($directory) . '` */';
else: $slice = \array_slice (scandir ($directory), 2);
\natsort ($slice); foreach ($slice as $count => $file):
if (\str_ends_with ($file, ".{$ext}"))
$collect[$count] = \file_get_contents ($directory. DSP .$file);
unset ($count, $slice);
endforeach; unset ($file);
endif;
elseif ($multi === 'no'):
if ($job === 'well'):
$suspect = \explode ('.', $suspect);
$chop = \array_shift ($suspect);
$target .= $chop; unset ($chop);
$suspect = \implode ('.', $suspect);
$target .= DSP . "$suspect.$ext";
else: $target .= "$suspect.$ext"; endif;
$collect[] = ((!\is_file ($target))
? '/* Bad request for `'
. \basename ($target).'`. */'
: \file_get_contents ($target)
);
endif;
unset ($suspect, $n, $job, $multi, $target, $first);
}
}
private function build_iterated_string (array &$collect, ?string &$catmice = null): void {
/// From collected array values to single - FAT - string
$catmice = ''; foreach ($collect as $n => $sample) {
$catmice .= \trim ($sample) . EOL . EOL;
unset ($collect[$n], $sample, $n);
} $collect = null; unset ($collect);
}
private function collection_mockery (string $extension, string &$catmice): void {
/// Squeezes content, removes block comments and|or line comments
if ($this-> squeeze === true) {
$this-> blockCmt = false;
$this-> comments = false;
$catmice = Api-> no_block_comments ($catmice);
if ($extension === 'js')
$catmice = Api-> no_comments (a: $catmice);
$catmice = Api-> one_line ($catmice);
}
else {
if ($this-> blockCmt !== true)
$catmice = Api-> no_block_comments ($catmice);
if ($extension === 'js' && !$this-> comments)
$catmice = Api-> no_comments (a: $catmice);
}
}
private function expand_externals (array $extern, string $glue): string {
/// Convert suitable images and fonts to base64 string
$suspect = \trim ($extern[1]);
if (\str_starts_with ($suspect, 'data:'))
return $extern[0];
$lC = \mb_substr ($suspect, -1);
$fC = \mb_substr ($suspect, 0, 1);
if ($fC === $lC && \in_array ($fC, [ '"', "'" ]))
$suspect = \trim (\mb_substr ($suspect, 1, -1));
$httpTest = \str_starts_with ($suspect, 'http');
$slashTest = \str_starts_with ($suspect, '//');
if (!$httpTest || !$slashTest) {
$checkLocalAndQuery = true;
$uTest = \parse_url ($suspect);
$imp = ((\str_starts_with ($suspect, '/')) ? '' : '/');
if (!isset ($uTest['query'])) {
// url = ./../.. or /rel/to/file.ext
$localTest = false; $real = \realpath ($suspect);
$suspect = ((!\is_bool ($real) && $suspect !== $real)
? $real : "{$_SERVER['DOCUMENT_ROOT']}$imp{$suspect}");
if (!\file_exists ($suspect)) return $extern[0];
$extension = \pathinfo ($suspect, PATHINFO_EXTENSION);
return $glue . $this-> expansion_summarum ($extern[0],
$extension, \apacheMimeTypes ($extension,
$this-> mimeLocation), $suspect, false
);
}
}
if (\count ($uTest) === 2 && (isset ($uTest['query'], $uTest['path']))) {
if (isset ($checkLocalAndQuery)) unset ($checkLocalAndQuery);
if (!self::CLI_SERVER_STREAM && PHP_SAPI === 'cli-server') return $extern[0];
$httpHost = 'http'.((!isset ($_SERVER['HTTPS'])) ? '':'s')."://{$_SERVER['HTTP_HOST']}";
$suspect = "{$httpHost}{$imp}" . \implode ('?', [ $uTest['path'], $uTest['query'] ]);
$localTest = true; unset ($uTest);
}
// $httpTest = url(http(s)://..)
// $slashTest = url(//www.website.com)
// $localTest = url(/?someget=somevalue)
if (!isset ($checkLocalAndQuery) && ($httpTest || $slashTest || $localTest)) {
if ($slashTest) $suspect = \explode (':', $httpHost)[0] . ":{$suspect}";
$context = \stream_context_create (['http' => ['ignore_errors' => true
, 'timeout' => self::STREAM_CONTEXT_TIMEOUT ]]);
if (!$fp = @\fopen ($suspect, 'r', false, $context))
return $extern[0]; $meta = stream_get_meta_data ($fp);
$this-> stream_wrapper_mimetype ($meta, $mime);
\fclose ($fp); if ($mime === null) return $extern[0];
return $glue . $this-> expansion_summarum ($extern[0],
\explode ('/', $mime)[1], $mime, (@\file_get_contents
($suspect, false, $context) ?? false), true);
}
}
private function stream_wrapper_mimetype (array &$meta, ?string &$fm = null): void {
/// Extract Content-Type from stream_wrapper_data
foreach ($meta['wrapper_data'] as $header) {
$Bomb = \explode (':', $header);
if (isset ($Bomb[1]) && Api-> flatten
($Bomb[0], 1) === 'content-type') {
$fm = Api-> flatten ($Bomb[1], 1);
break;
}
unset ($Bomb, $header);
} $meta = null;
}
private function expansion_summarum (
string $original, string $ext, bool|string $mime, bool|string $suspect, bool $stream
): string { /// Finally returns encoded target or gives back path as is
if (\in_array ($ext, [ 'svg', 'svgz' ])) $mime = 'not_compliant';
return ((!$suspect || !$mime || $mime === 'not_compliant')
? $original : "(data:$mime;charset=utf8;base64,"
. \base64_encode ((($stream) ? $suspect
: \file_get_contents ($suspect)
)) . ')'
);
}
private function catmice_signature(): string {
/// Reveal or hide minor details about `self-decisions`.
if (!$this-> template_call) {
$doctype = Observer-> doctype;
$against = Observer-> filterget[$this-> target_id];
}
else {
$against = $this-> target_id; $dynamic = 'Complicated!';
$doctype = 'Template ' . \strtoupper (Observer-> doctype);
}
return EOL . EOL
. '/**: ' . "Hardcoder::catmice `$doctype` against: \"" . $against ."\""
. EOL . EOL . "\t- Squeezed: " . \var_export (($dynamic ?? $this-> squeeze), true). EOL
. ((Observer-> doctype === 'style')
? "\t- Expand Externals: " . \var_export (($dynamic ?? $this-> expandExternals), true) . EOL
: "\t- Comments persist: " . \var_export (($dynamic ?? $this-> comments), true) . EOL
) . "\t- Block-Comments persist: " . \var_export (($dynamic ?? $this-> blockCmt), true)
. EOL . EOL
. ':*/';
}
private function fetch_object_template (string $ext, string $tpl): bool {
/// Prepare template workspace and parse JSON template into string
$target = $this-> make_template_target ('json', $ext, $tpl);
$tpldir = \dirname ($target); !\is_dir ($tpldir) and \mkdir ($tpldir, 0775, true);
$wall = $tpldir . DSP . 'index.php'; !\is_file ($wall) and \touch ($wall);
if (!\is_file ($target) || !\ctype_alnum ($tpl)) return false;
$this-> packed = [ // ^^ Simple test/ portion abovereturn
'gz' => $this-> make_template_target ('packed.gz', $ext, $tpl),
'json' => (@\json_decode (\file_get_contents ($target), false, JSON_UNESCAPED_UNICODE))
]; return \is_object ($this-> packed['json']);
}
private function make_template_target (string $which, string $ext, string $tpl): string {
/// Returns either json template path or path for packed.gz outcome.
return Api-> dspa ($this-> mimeLocation, '.tpl', "{$ext}.{$tpl}.{$which}");
}
private function template_call_pack (string &$catmice): void {
/// Write template collection to disk and release catmice
echo $catmice; ($this-> template_call && $this-> pack > 0)
and \file_put_contents ($this-> packed,
\gzdeflate ($catmice, 9), LOCK_EX);
$catmice = null;
}
private function time_and_headers (string $c_type, string &$catmice): void {
/// Sets caching hours and calls final headers_and_out method
$ttHours = $hrs = ((!$this-> template_call)
? (Observer-> filterget['hrs'] ?? $this-> hours) : $this-> hours);
$hrs = (($ttHours < 1 && self::ZERO_HOUR_SKEPTIC) ? self::ZERO_POINT_IMMUTABLE
: ((int)$ttHours)); $this-> headers_and_out ($hrs, $c_type, $catmice);
}
private function headers_and_out (int|float $hours, string $c_type, string &$catmice): never {
/// Send response headers, spit collection and optionally gzcompress template to disk
$seconds = $this-> hours_multiply ($hours);
$time = \time(); $gdstring = 'D, d M Y H:i:s';
$expires = \gmdate ($gdstring, ($time + $seconds));
$lastMod = \gmdate ($gdstring, ((int)($time - ($time / 20))));
foreach (\array_merge (['Timing-Allow-Origin' => '*', 'Content-Allow-Origin' => '*'
, 'Cache-Control' => (($hours < self::ZERO_POINT_IMMUTABLE) ? 'no-cache'
: "max-age={$seconds}, immutable"), 'Last-Modified' => "$lastMod GMT",
'Expires' => "$expires GMT", 'X-Content-Type-Options' => "nosniff",
'Content-Encoding' => 'gzip, deflate, br'], (($this-> target_id === 'cat')
? [] : [ 'Content-Security-Policy' => "default-src 'none'",
'X-Content-Security-Policy' => "default-src 'none'"
]), [ 'Content-Type' => "$c_type; charset=utf-8" ]
) as $key => $value): \header ("{$key}: {$value}");
unset ($value, $key); endforeach;
\ob_start(); \ob_start ('ob_gzhandler');
\header ('Transfer-Encoding: gzip, deflate, br');
$this-> template_call_pack ($catmice);
\ob_end_flush(); \header ('Content-Length: '
. \ob_get_length()); \ob_end_flush();
exit;
}
private function hngts_dependancy(): void {
/// Generates missing and vital data from hngts toolset
function apacheMimeTypes (string $extension = '', string $basedir = ''): string|false {
/// This function is a substitue for native 'mime_content_type';
$apache = 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf';
$basename = 'mime.types'; $serialized = "$basedir/$basename";
$ext = (($extension === '') ? 'txt' : $extension);
if (\is_file ($serialized)) {
$MimeType = \unserialize (\file_get_contents ($serialized));
if (\is_array ($MimeType)) return $MimeType[$ext] ?? false;
}
else { //! BlyAfiful block of code. :)
$MimeType = \file ("$apache/$basename", 1|2|4);
\is_array ($MimeType) or die (__FUNCTION__ . ' says: '
. "Raw '$basename' whitelist file creation failed.");
$Mtype = []; foreach ($MimeType as $n => $line) {
if (\mb_substr ($line, 0, 1) !== '#') {
$line = \preg_replace ('/\s+/', ' ', $MimeType[$n]);
$Bomb = \explode (' ', $line);
$Mime = \array_shift ($Bomb);
$Bomb = \array_reverse ($Bomb);
foreach ($Bomb as $e) {
if ($e !== '' && !isset ($MimeType[$e]))
$Mtype[$e] = $Mime; unset ($e);
} unset ($Bomb, $Mime);
} unset ($line, $MimeType[$n], $n);
} unset ($MimeType);
$Mtype = \array_filter ($Mtype);
\is_dir ($basedir) or \mkdir ($basedir, 0755, true);
\file_put_contents ($serialized, \serialize ($Mtype), LOCK_EX);
return $Mtype[$ext] ?? false;
}
}
function globPublicTD (array $list): void {
/// Define `global Public Temporal Data`.
foreach ($list as $name => $value) {
!\defined ($name) and \define ($name, $value);
unset ($value, $name);
}
}
globPublicTD ([
'NSP' => "\134" // Escape \ as Namespace separator.
, 'EOL' => PHP_EOL // goto: https://en.wikipedia.org/wiki/Newline;
, 'PSP' => PATH_SEPARATOR // On unix-alike boxes it is : on m$ boxes it is ;
, 'DSP' => DIRECTORY_SEPARATOR // On unix-alike boxes it is / on m$ boxes it is \
, 'Api' => new class { /// Shortened version of Public \H\Api as public constant fakery
public function dspa (...$suspects): string {
/// From args to: / + arg1/arg2/arg3 ..
$TestMatch = (@\implode (DSP, $suspects));
!\is_bool ($TestMatch)
or die (__METHOD__ . ' args - invalid.'
. EOL . EOL . \print_r ($suspects, true));
unset ($suspects); $t = "\176";
$TestMatch = \ltrim ($TestMatch);
if (\mb_substr ($TestMatch, 0, 1) === $t)
$out = $this-> tildenv ($TestMatch, $t);
return \trim ($this-> no_slash (($out ?? $TestMatch)));
}
public function tildenv (string $path, string $char = "\176"): string {
/*: Expands ~ to $HOME. */ return \str_replace ($char, \getenv ('HOME'), $path);
}
public function no_slash (string $a = ''): string {
/// Directories must not have `/` or `\` at the end
return ((\mb_substr ($a, -1) !== DSP) ? $a : \mb_substr ($a, 0, -1));
}
public function no_comments (string $x = "\057\057", string $a = '', array $merge = []): string {
/// Remove '$x+? ' line comments from strings
$a = \explode (EOL, $a);
if (\count ($a) >= 1) {
$diff = null;
$merged = \array_merge (["\176", "\134", ' '], $merge);
foreach ($a as $int => $b) {
foreach ($merged as $sample) {
$p = \mb_strpos ($b, "$x$sample");
if ($p !== false) {
$a[$int] = \mb_substr ($b, 0, $p);
$diff = $int;
break;
} unset ($p, $sample);
}
if (\is_int ($diff) && \trim ($a[$diff]) === '') {
unset ($a[$diff]); $diff = null;
} unset ($b, $int);
}
}
return \implode (EOL, $a);
}
public function no_block_comments (string $a, string $r = ''): string {
/// Remove C-style block comments from strings
return \preg_replace ('!/\*.*?\*/!s', $r, $a);
}
public function flatten (string $suspect, int $mb = 0): string {
/*: Trim and (mb_)strtolower suspect.
Any other $mb than 1 will not be multibyte.
*/ return \trim (((($mb === 1) ? NSP . 'mb_' : NSP)
. 'strtolower') ($suspect));
}
public function one_line (string $a, bool $too_ugly = false): string {
/// Entire string to one line - ugly or uglier.
return \preg_replace ('/\s+/',
((!$too_ugly) ? ' ' : ''),
(($too_ugly) ? $a : \trim ($a))
);
}
}
, 'Observer' => new class (hrs: self::OPT_ARGUMENT[0]) {
/// Shortened version of Temporal Observer stream
public $doctype; ///
public $filterget = []; ///
public function __construct (string $hrs) {
/// This is where cat and mice keywords are determined.
foreach (['cat' => 'style', 'mice' => 'script' ] as $k => $description) {
if (isset ($_GET[$k]) && !isset ($this-> filterget[$k])) {
$this-> filterget[$k] = \trim (\filter_input (INPUT_GET, $k, FILTER_UNSAFE_RAW));
$this-> doctype = $description; unset ($description, $k);
break;
}
}
if (isset ($_GET[$hrs]) && !isset ($this-> filterget[$hrs])) {
$this-> filterget[$hrs] = ((int)\filter_input (INPUT_GET, $hrs, FILTER_UNSAFE_RAW));
}
}
}
]);
}
};