forked from jacklul/asuswrt-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common-update.php
93 lines (72 loc) · 2.55 KB
/
common-update.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
<?php
# Why is this written in PHP? I don't know - guess it was easier for me!
define('DS', DIRECTORY_SEPARATOR);
$IDENTIFIERS = ['LOCKFILE', 'ISSTARTEDBYSYSTEM'];
$TARGET_DIRS = [__DIR__ . DS . 'scripts', __DIR__ . DS . 'extras'];
$SOURCE_FILE = __DIR__ . DS . 'common.sh';
######################
// https://stackoverflow.com/a/46697247
function scandir_recursive($dir) {
$result = [];
foreach(scandir($dir) as $filename) {
if ($filename[0] === '.')
continue;
$filePath = $dir . DS . $filename;
if (is_dir($filePath)) {
foreach (scandir_recursive($filePath) as $childFilename) {
$result[] = $filename . DS . $childFilename;
}
} else {
$result[] = $filename;
}
}
return $result;
}
// https://stackoverflow.com/a/6875997
function replace_between($str, $needle_start, $needle_end, $replacement) {
$pos = strpos($str, $needle_start);
$start = $pos === false ? 0 : $pos + strlen($needle_start);
$pos = strpos($str, $needle_end, $start);
$end = $pos === false ? strlen($str) : $pos;
return substr_replace($str, $replacement, $start, $end - $start);
}
if (file_exists($SOURCE_FILE))
$SOURCE_FILE = file_get_contents($SOURCE_FILE);
else
exit($SOURCE_FILE . ' does not exist');
$SOURCES = [];
foreach ($IDENTIFIERS as $IDENTIFIER) {
$REGEX = '/#' . $IDENTIFIER . '_START#(.*)#' . $IDENTIFIER . '_END#/s';
echo 'Fetching: ' . $REGEX . PHP_EOL;
preg_match($REGEX, $SOURCE_FILE, $matches);
if (isset($matches[1])) {
$SOURCES[$IDENTIFIER] = $matches[1];
}
}
$FILES = [];
foreach ($TARGET_DIRS as $TARGET_DIR) {
if (!is_dir($TARGET_DIR))
exit($TARGET_DIR . ' does not exist');
$SCANDIR = scandir_recursive($TARGET_DIR);
foreach ($SCANDIR as $FILE) {
if ($FILE[0] === '.')
continue;
$FILES[] = $TARGET_DIR . DS . $FILE;
}
}
foreach ($FILES as $FILE) {
echo 'Processing ' . $FILE . '...';
$FILE_CONTENTS = file_get_contents($FILE);
$NEW_FILE_CONTENTS = $FILE_CONTENTS;
foreach ($IDENTIFIERS as $IDENTIFIER) {
if (strpos($FILE_CONTENTS, '#' . $IDENTIFIER . '_START#') == false)
continue;
$NEW_FILE_CONTENTS = replace_between($NEW_FILE_CONTENTS, '#' . $IDENTIFIER . '_START#', '#' . $IDENTIFIER . '_END#', $SOURCES[$IDENTIFIER]);
}
if ($NEW_FILE_CONTENTS != $FILE_CONTENTS) {
echo ' modified!' . PHP_EOL;
file_put_contents($FILE, $NEW_FILE_CONTENTS);
} else {
echo ' not modified!' . PHP_EOL;
}
}