-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUrlSpool.php
80 lines (68 loc) · 1.52 KB
/
UrlSpool.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
<?php
$usage = <<<USG
USAGE:
php UrlSpool.php pattern datalists [count]
pattern : see readme for syntax
datalists : a valid json encoded array of arrays
count : optional, defaults to 1, number of line-delimited urls to output
USG;
// Check inputs
if (count($argv) < 3) {
die($usage);
}
$outcount = 1;
if (count($argv) >= 4) {
$outcount = (int) $argv[3];
}
$pattern = $argv[1];
$lists = json_decode($argv[2]);
if (is_null($lists) === true) {
die('datalists must be valid json, did not decode');
}
for ($i = 0; $i < $outcount; $i++) {
if ($i !== 0) {
echo "\n";
}
echo parseUrl($pattern, $lists);
}
function parseUrl($pattern, $lists) {
$url = '';
$nowrite = 0;
$reg = array();
for ($pnt = 0; $pnt < strlen($pattern); $pnt++) {
$char = $pattern[$pnt];
if ($char === '>') {
$nowrite = ($nowrite > 1) ? $nowrite - 1 : 0;
continue;
}
if ($char === '<') {
if ($nowrite > 0) {
$nowrite += 1;
} else {
$pnt += 2;
$pivot = $reg[(int) $pattern[$pnt]];
$cond = ($pattern[$pnt + 1] === '!') ? false : true;
$condVal = '';
$pnt = ($cond) ? $pnt + 2 : $pnt + 3;
while ($pattern[$pnt] !== '~') {
$condVal .= $pattern[$pnt++];
}
if (!(($pivot !== $condVal) xor $cond)) {
$nowrite += 1;
}
}
continue;
} elseif ($nowrite === 0) {
if ($char === '$') {
$nid = (int) $pattern[++$pnt];
if (!isset($reg[$nid])) {
$reg[$nid] = $lists[$nid][array_rand($lists[$nid])];
}
$url .= $reg[$nid];
} else {
$url .= $char;
}
}
}
return $url;
}