Bearsampp
2026.9.23
Toggle main menu visibility
Loading...
Searching...
No Matches
System.php
Go to the documentation of this file.
1
<?php
15
19
require_once
'PEAR.php'
;
20
require_once
'Console/Getopt.php'
;
21
22
$GLOBALS
[
'_System_temp_files'
] = array();
23
58
class
System
59
{
68
public
static
function
_parseArgs
(
$argv
, $short_options, $long_options =
null
)
69
{
70
if
(!is_array(
$argv
) &&
$argv
!==
null
) {
71
/*
72
// Quote all items that are a short option
73
$av = preg_split('/(\A| )--?[a-z0-9]+[ =]?((?<!\\\\)((,\s*)|((?<!,)\s+))?)/i', $argv, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);
74
$offset = 0;
75
foreach ($av as $a) {
76
$b = trim($a[0]);
77
if ($b[0] == '"' || $b[0] == "'") {
78
continue;
79
}
80
81
$escape = escapeshellarg($b);
82
$pos = $a[1] + $offset;
83
$argv = substr_replace($argv, $escape, $pos, strlen($b));
84
$offset += 2;
85
}
86
*/
87
88
// Find all items, quoted or otherwise
89
preg_match_all(
"/(?:[\"'])(.*?)(?:['\"])|([^\s]+)/"
,
$argv
, $av);
90
$argv
= $av[1];
91
foreach
($av[2] as $k => $a) {
92
if
(empty($a)) {
93
continue
;
94
}
95
$argv
[$k] = trim($a) ;
96
}
97
}
98
99
return
Console_Getopt::getopt2(
$argv
, $short_options, $long_options);
100
}
101
109
protected
static
function
raiseError
($error)
110
{
111
if
(PEAR::isError($error)) {
112
$error = $error->getMessage();
113
}
114
trigger_error($error, E_USER_WARNING);
115
return
false
;
116
}
117
141
protected
static
function
_dirToStruct
($sPath, $maxinst, $aktinst = 0, $silent =
false
)
142
{
143
$struct = array(
'dirs'
=> array(),
'files'
=> array());
144
if
(($dir = @opendir($sPath)) ===
false
) {
145
if
(!$silent) {
146
System::raiseError
(
"Could not open dir $sPath"
);
147
}
148
return
$struct;
// XXX could not open error
149
}
150
151
$struct[
'dirs'
][] = $sPath = realpath($sPath);
// XXX don't add if '.' or '..' ?
152
$list = array();
153
while
(
false
!== ($file = readdir($dir))) {
154
if
($file !=
'.'
&& $file !=
'..'
) {
155
$list[] = $file;
156
}
157
}
158
159
closedir($dir);
160
natsort($list);
161
if
($aktinst < $maxinst || $maxinst == 0) {
162
foreach
($list as $val) {
163
$path = $sPath . DIRECTORY_SEPARATOR . $val;
164
if
(is_dir($path) && !is_link($path)) {
165
$tmp =
System::_dirToStruct
($path, $maxinst, $aktinst+1, $silent);
166
$struct = array_merge_recursive($struct, $tmp);
167
}
else
{
168
$struct[
'files'
][] = $path;
169
}
170
}
171
}
172
173
return
$struct;
174
}
175
184
protected
static
function
_multipleToStruct
($files)
185
{
186
$struct = array(
'dirs'
=> array(),
'files'
=> array());
187
settype($files,
'array'
);
188
foreach
($files as $file) {
189
if
(is_dir($file) && !is_link($file)) {
190
$tmp =
System::_dirToStruct
($file, 0);
191
$struct = array_merge_recursive($tmp, $struct);
192
}
else
{
193
if
(!in_array($file, $struct[
'files'
])) {
194
$struct[
'files'
][] = $file;
195
}
196
}
197
}
198
return
$struct;
199
}
200
210
public
static
function
rm
($args)
211
{
212
$opts
=
System::_parseArgs
($args,
'rf'
);
// "f" does nothing but I like it :-)
213
if
(PEAR::isError(
$opts
)) {
214
return
System::raiseError
(
$opts
);
215
}
216
foreach
(
$opts
[0] as $opt) {
217
if
($opt[0] ==
'r'
) {
218
$do_recursive =
true
;
219
}
220
}
221
$ret =
true
;
222
if
(isset($do_recursive)) {
223
$struct =
System::_multipleToStruct
(
$opts
[1]);
224
foreach
($struct[
'files'
] as $file) {
225
if
(!@unlink($file)) {
226
$ret =
false
;
227
}
228
}
229
230
rsort($struct[
'dirs'
]);
231
foreach
($struct[
'dirs'
] as $dir) {
232
if
(!@rmdir($dir)) {
233
$ret =
false
;
234
}
235
}
236
}
else
{
237
foreach
(
$opts
[1] as $file) {
238
$delete = (is_dir($file)) ?
'rmdir'
:
'unlink'
;
239
if
(!@$delete($file)) {
240
$ret =
false
;
241
}
242
}
243
}
244
return
$ret;
245
}
246
254
public
static
function
mkDir
($args)
255
{
256
$opts
=
System::_parseArgs
($args,
'pm:'
);
257
if
(PEAR::isError(
$opts
)) {
258
return
System::raiseError
(
$opts
);
259
}
260
261
$mode = 0777;
// default mode
262
foreach
(
$opts
[0] as $opt) {
263
if
($opt[0] ==
'p'
) {
264
$create_parents =
true
;
265
} elseif ($opt[0] ==
'm'
) {
266
// if the mode is clearly an octal number (starts with 0)
267
// convert it to decimal
268
if
(strlen($opt[1]) && $opt[1][0] ==
'0'
) {
269
$opt[1] = octdec($opt[1]);
270
}
else
{
271
// convert to int
272
$opt[1] += 0;
273
}
274
$mode = $opt[1];
275
}
276
}
277
278
$ret =
true
;
279
if
(isset($create_parents)) {
280
foreach
(
$opts
[1] as $dir) {
281
$dirstack = array();
282
while
((!file_exists($dir) || !is_dir($dir)) &&
283
$dir != DIRECTORY_SEPARATOR) {
284
array_unshift($dirstack, $dir);
285
$dir = dirname($dir);
286
}
287
288
while
($newdir = array_shift($dirstack)) {
289
if
(!is_writeable(dirname($newdir))) {
290
$ret =
false
;
291
break
;
292
}
293
294
if
(!mkdir($newdir, $mode)) {
295
$ret =
false
;
296
}
297
}
298
}
299
}
else
{
300
foreach
(
$opts
[1] as $dir) {
301
if
((@file_exists($dir) || !is_dir($dir)) && !mkdir($dir, $mode)) {
302
$ret =
false
;
303
}
304
}
305
}
306
307
return
$ret;
308
}
309
323
public
static
function
&
cat
($args)
324
{
325
$ret =
null
;
326
$files = array();
327
if
(!is_array($args)) {
328
$args = preg_split(
'/\s+/'
, $args, -1, PREG_SPLIT_NO_EMPTY);
329
}
330
331
$count_args = count($args);
332
for
($i = 0; $i < $count_args; $i++) {
333
if
($args[$i] ==
'>'
) {
334
$mode =
'wb'
;
335
$outputfile = $args[$i+1];
336
break
;
337
} elseif ($args[$i] ==
'>>'
) {
338
$mode =
'ab+'
;
339
$outputfile = $args[$i+1];
340
break
;
341
}
else
{
342
$files[] = $args[$i];
343
}
344
}
345
$outputfd =
false
;
346
if
(isset($mode)) {
347
if
(!$outputfd = fopen($outputfile, $mode)) {
348
$err =
System::raiseError
(
"Could not open $outputfile"
);
349
return
$err;
350
}
351
$ret =
true
;
352
}
353
foreach
($files as $file) {
354
if
(!$fd = fopen($file,
'r'
)) {
355
System::raiseError
(
"Could not open $file"
);
356
continue
;
357
}
358
while
($cont = fread($fd, 2048)) {
359
if
(is_resource($outputfd)) {
360
fwrite($outputfd, $cont);
361
}
else
{
362
$ret .= $cont;
363
}
364
}
365
fclose($fd);
366
}
367
if
(is_resource($outputfd)) {
368
fclose($outputfd);
369
}
370
return
$ret;
371
}
372
395
public
static
function
mktemp
($args =
null
)
396
{
397
static
$first_time =
true
;
398
$opts
=
System::_parseArgs
($args,
't:d'
);
399
if
(PEAR::isError(
$opts
)) {
400
return
System::raiseError
(
$opts
);
401
}
402
403
foreach
(
$opts
[0] as $opt) {
404
if
($opt[0] ==
'd'
) {
405
$tmp_is_dir =
true
;
406
} elseif ($opt[0] ==
't'
) {
407
$tmpdir = $opt[1];
408
}
409
}
410
411
$prefix = (isset(
$opts
[1][0])) ?
$opts
[1][0] :
'tmp'
;
412
if
(!isset($tmpdir)) {
413
$tmpdir =
System::tmpdir
();
414
}
415
416
if
(!
System::mkDir
(array(
'-p'
, $tmpdir))) {
417
return
false
;
418
}
419
420
$tmp = tempnam($tmpdir, $prefix);
421
if
(isset($tmp_is_dir)) {
422
unlink($tmp);
// be careful possible race condition here
423
if
(!mkdir($tmp, 0700)) {
424
return
System::raiseError
(
"Unable to create temporary directory $tmpdir"
);
425
}
426
}
427
428
$GLOBALS
[
'_System_temp_files'
][] = $tmp;
429
if
(isset($tmp_is_dir)) {
430
//$GLOBALS['_System_temp_files'][] = dirname($tmp);
431
}
432
433
if
($first_time) {
434
PEAR::registerShutdownFunc(array(
'System'
,
'_removeTmpFiles'
));
435
$first_time =
false
;
436
}
437
438
return
$tmp;
439
}
440
445
public
static
function
_removeTmpFiles
()
446
{
447
if
(count(
$GLOBALS
[
'_System_temp_files'
])) {
448
$delete =
$GLOBALS
[
'_System_temp_files'
];
449
array_unshift($delete,
'-r'
);
450
System::rm
($delete);
451
$GLOBALS
[
'_System_temp_files'
] = array();
452
}
453
}
454
463
public
static
function
tmpdir
()
464
{
465
if
(OS_WINDOWS) {
466
if
($var = isset($_ENV[
'TMP'
]) ? $_ENV[
'TMP'
] : getenv(
'TMP'
)) {
467
return
$var;
468
}
469
if
($var = isset($_ENV[
'TEMP'
]) ? $_ENV[
'TEMP'
] : getenv(
'TEMP'
)) {
470
return
$var;
471
}
472
if
($var = isset($_ENV[
'USERPROFILE'
]) ? $_ENV[
'USERPROFILE'
] : getenv(
'USERPROFILE'
)) {
473
return
$var;
474
}
475
if
($var = isset($_ENV[
'windir'
]) ? $_ENV[
'windir'
] : getenv(
'windir'
)) {
476
return
$var;
477
}
478
return
getenv(
'SystemRoot'
) .
'\temp'
;
479
}
480
if
($var = isset($_ENV[
'TMPDIR'
]) ? $_ENV[
'TMPDIR'
] : getenv(
'TMPDIR'
)) {
481
return
$var;
482
}
483
return
realpath(function_exists(
'sys_get_temp_dir'
) ? sys_get_temp_dir() :
'/tmp'
);
484
}
485
495
public
static
function
which
($program, $fallback =
false
)
496
{
497
// enforce API
498
if
(!is_string($program) ||
''
== $program) {
499
return
$fallback;
500
}
501
502
// full path given
503
if
(basename($program) != $program) {
504
$path_elements[] = dirname($program);
505
$program = basename($program);
506
}
else
{
507
$path = getenv(
'PATH'
);
508
if
(!$path) {
509
$path = getenv(
'Path'
);
// some OSes are just stupid enough to do this
510
}
511
512
$path_elements = explode(PATH_SEPARATOR, $path);
513
}
514
515
if
(OS_WINDOWS) {
516
$exe_suffixes = getenv(
'PATHEXT'
)
517
? explode(PATH_SEPARATOR, getenv(
'PATHEXT'
))
518
: array(
'.exe'
,
'.bat'
,
'.cmd'
,
'.com'
);
519
// allow passing a command.exe param
520
if
(strpos($program,
'.'
) !==
false
) {
521
array_unshift($exe_suffixes,
''
);
522
}
523
}
else
{
524
$exe_suffixes = array(
''
);
525
}
526
527
foreach
($exe_suffixes as $suff) {
528
foreach
($path_elements as $dir) {
529
$file = $dir . DIRECTORY_SEPARATOR . $program . $suff;
530
// It's possible to run a .bat on Windows that is_executable
531
// would return false for. The is_executable check is meaningless...
532
if
(OS_WINDOWS) {
533
if
(file_exists($file)) {
534
return
$file;
535
}
536
}
else
{
537
if
(is_executable($file)) {
538
return
$file;
539
}
540
}
541
}
542
}
543
return
$fallback;
544
}
545
568
public
static
function
find
($args)
569
{
570
if
(!is_array($args)) {
571
$args = preg_split(
'/\s+/'
, $args, -1, PREG_SPLIT_NO_EMPTY);
572
}
573
$dir = realpath(array_shift($args));
574
if
(!$dir) {
575
return
array();
576
}
577
$patterns = array();
578
$depth = 0;
579
$do_files = $do_dirs =
true
;
580
$args_count = count($args);
581
for
($i = 0; $i < $args_count; $i++) {
582
switch
($args[$i]) {
583
case
'-type'
:
584
if
(in_array($args[$i+1], array(
'd'
,
'f'
))) {
585
if
($args[$i+1] ==
'd'
) {
586
$do_files =
false
;
587
}
else
{
588
$do_dirs =
false
;
589
}
590
}
591
$i++;
592
break
;
593
case
'-name'
:
594
$name = preg_quote($args[$i+1],
'#'
);
595
// our magic characters ? and * have just been escaped,
596
// so now we change the escaped versions to PCRE operators
597
$name = strtr($name, array(
'\?'
=>
'.'
,
'\*'
=>
'.*'
));
598
$patterns[] =
'('
.$name.
')'
;
599
$i++;
600
break
;
601
case
'-maxdepth'
:
602
$depth = $args[$i+1];
603
break
;
604
}
605
}
606
$path =
System::_dirToStruct
($dir, $depth, 0,
true
);
607
if
($do_files && $do_dirs) {
608
$files = array_merge($path[
'files'
], $path[
'dirs'
]);
609
} elseif ($do_dirs) {
610
$files = $path[
'dirs'
];
611
}
else
{
612
$files = $path[
'files'
];
613
}
614
if
(count($patterns)) {
615
$dsq = preg_quote(DIRECTORY_SEPARATOR,
'#'
);
616
$pattern =
'#(^|'
.$dsq.
')'
.implode(
'|'
, $patterns).
'($|'
.$dsq.
')#'
;
617
$ret = array();
618
$files_count = count($files);
619
for
($i = 0; $i < $files_count; $i++) {
620
// only search in the part of the file below the current directory
621
$filepart = basename($files[$i]);
622
if
(preg_match($pattern, $filepart)) {
623
$ret[] = $files[$i];
624
}
625
}
626
return
$ret;
627
}
628
return
$files;
629
}
630
}
$GLOBALS
$GLOBALS['_PEAR_default_error_mode']
Definition
PEAR.php:46
$GLOBALS
$GLOBALS['_System_temp_files']
Definition
System.php:22
System\_multipleToStruct
static _multipleToStruct($files)
Definition
System.php:184
System\tmpdir
static tmpdir()
Definition
System.php:463
System\_parseArgs
static _parseArgs($argv, $short_options, $long_options=null)
Definition
System.php:68
System\rm
static rm($args)
Definition
System.php:210
System\cat
static & cat($args)
Definition
System.php:323
System\_removeTmpFiles
static _removeTmpFiles()
Definition
System.php:445
System\raiseError
static raiseError($error)
Definition
System.php:109
System\find
static find($args)
Definition
System.php:568
System\which
static which($program, $fallback=false)
Definition
System.php:495
System\mktemp
static mktemp($args=null)
Definition
System.php:395
System\mkDir
static mkDir($args)
Definition
System.php:254
System\_dirToStruct
static _dirToStruct($sPath, $maxinst, $aktinst=0, $silent=false)
Definition
System.php:141
System
$opts
if(PEAR::isError($options)) $opts
Definition
pearcmd.php:150
$argv
$argv
Definition
pearcmd.php:137
sandbox
core
libs
php
pear
pear
System.php
Generated by
1.17.0