forked from evilru/quake3-vsp-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvutil.php
More file actions
149 lines (138 loc) · 4.14 KB
/
vutil.php
File metadata and controls
149 lines (138 loc) · 4.14 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
<?php
/* vsp stats processor, copyright 2004-2005, myrddin8 AT gmail DOT com (a924cb279be8cb6089387d402288c9f2) */
function parseFileListing(array $fileLines): array
{
foreach ($fileLines as $line) {
if (
preg_match(
"/([-dl][rwxst-]+).* ([0-9]*) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9])[ ]+(([0-9]{2}:[0-9]{2})|[0-9]{4}) (.+)/",
$line,
$matches
)
) {
// Determine file type (directory, link, etc.) by finding the position of the first char in "-dl"
$fileType = (int) strpos("-dl", $matches[1][0]);
$fileData["line"] = $matches[0];
$fileData["type"] = $fileType;
$fileData["rights"] = $matches[1];
$fileData["number"] = $matches[2];
$fileData["user"] = $matches[3];
$fileData["group"] = $matches[4];
$fileData["size"] = $matches[5];
$fileData["date"] = date("m-d", strtotime($matches[6]));
$fileData["time"] = $matches[7];
$fileData["name"] = $matches[9];
$result[] = $fileData;
}
}
return $result;
}
function parseCommandLineArgs(string $inputStr): array
{
while (
preg_match('/^\s*"(.+)"/U', $inputStr, $match) ||
preg_match("/^\s*([^\s]+)\s*/", $inputStr, $match)
) {
$inputStr = str_replace($match[0], "", $inputStr);
$args["argv"][] = $match[1];
}
$args["argc"] = count($args["argv"]);
return $args;
}
function flushOutputBuffers(): void
{
while (ob_get_level() > 0) {
ob_end_flush();
}
flush();
}
function ensureTrailingSlash(string $path): string
{
return rtrim($path, "\\/") . "/";
}
function copyDirectoryRecursive(string $sourceDir, string $destDir): void
{
$sourceDir = rtrim($sourceDir, "/");
$destDir = rtrim($destDir, "/");
@mkdir($destDir, 0777);
$items = getDirectoryListing($sourceDir);
foreach ($items as $item) {
if ($item) {
$sourcePath = $sourceDir . "/" . $item;
if (strcmp($sourcePath, $destDir) != 0) {
$destPath = $destDir . "/" . $item;
if (is_dir($sourcePath)) {
copyDirectoryRecursive($sourcePath, $destPath);
} else {
copy($sourcePath, $destPath);
}
}
}
}
}
function getDirectoryListing(string $dir): array
{
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (!isset($listingStr)) {
$listingStr = "$entry";
} else {
$listingStr = "$entry\n$listingStr";
}
}
}
closedir($handle);
}
@$listing = explode("\n", $listingStr);
return $listing;
}
function readStdinLine(int $maxLength = 255): string
{
$stdinHandle = fopen("php://stdin", "r");
$line = fgets($stdinHandle, $maxLength);
$line = rtrim($line);
fclose($stdinHandle);
return $line;
}
function ensureDirectoryExists(string $dirPath): bool
{
$dirPath = str_replace("\\", "/", $dirPath);
if (!file_exists($dirPath)) {
$currentPath = "";
foreach (explode("/", $dirPath) as $part) {
$currentPath .= $part . "/";
if (!file_exists($currentPath)) {
$created = mkdir($currentPath, 0775);
}
}
return $created;
}
return true;
}
function sanitizeFilename(string $filename): string
{
$filename = str_replace(['../', '..\\'], '', $filename);
return str_replace(
["\\", "<", ">", "/", "=", ":", "*", "?", '"', " ", "|"],
"_",
$filename
);
}
function secureString($value)
{
global $db;
if (!$db) {
return addslashes($value);
}
return $db->qstr($value);
}
function getElapsedTime(array &$startTime): float
{
$currentTime = gettimeofday();
$elapsed =
(float) ($currentTime["sec"] - $startTime["sec"]) +
(float) ($currentTime["usec"] - $startTime["usec"]) / 1000000;
return $elapsed;
}
?>