-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmb.php
More file actions
136 lines (118 loc) · 5.16 KB
/
mb.php
File metadata and controls
136 lines (118 loc) · 5.16 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
<?php
# CLI utility to manage ModBook
# Check if config.php file exists
if (file_exists("config.php")) {
# Load user configuration to obtain status of mods
include('config.php');
} else {
# Copy the defaults.php file to config.php
if (copy("defaults.php", "config.php")) {
# Load newly generated user configuration file
include('config.php');
}
}
# Load mods file to access $modList
include('mods.php');
# Prints text, makes it green if supported terminal is detected.
function _print($str) {
# Sets the default value of $hasColorSupport to False
$hasColorSupport = False;
# Obtains the used terminal from the environment variable TERM
$env = getenv('TERM');
# Only check if the terminal supports color, when $env is set
if (isset($env)) {
# A list of terminals that support color
$supportedTerms = ['xterm', 'xterm-256color', 'screen', 'screen-256color', 'linux', 'cygwin'];
# Sets $hasColorSupport to True, if $env is in $supportedTerms
$hasColorSupport = in_array($env, $supportedTerms);
}
# Checks if $hasColorSupport is True
if ($hasColorSupport) {
# Prints the text with color
echo "\033[1;32m".$str."\033[0m\n";
} else {
# Prints the text without color
echo $str."\n";
}
}
# Attempts to remove $file, reports back the status with $altName
function _clear_file($file, $altName) {
# Check if $file exists
if (file_exists($file)) {
# Delete $file and check if it was deleted successfully
if (unlink($file)) {
# Print success for $altName deletion
_print($altName . " cleared.");
} else {
# Print failed for $altName deletion
_print("Failed to clear " . $altName . ".");
}
} else {
# Print that $altName was not found
_print($altName . " was not found.");
}
}
# Removes $folder when it is empty, reports back the status with $altName
function _clear_folder($folder, $altName) {
# Check if $folder folder exists
if (file_exists(realpath($folder))) {
# Read files in $folder
$files = scandir(realpath($folder));
# Check if $folder is empty
if (count($files) == 0 || count($files) == 2) {
# $folder is empty, attempt to remove it
if(rmdir(realpath($folder))) {
# Print that the $folder got removed
_print("Empty " . $altName . " folder got removed.");
} else {
# Print that the removal of the empty $folder failed
_print("Failed to remove empty " . $altName . " folder.");
}
} else {
# Print that the $folder is not empty, and is therefore not removed
_print($altName . " folder is not empty. Not removing it.");
}
} else {
# Print that $folder is not found
_print($altName . " folder not found.");
}
}
# Prints theme name
_print("ModBook - A modular BookStack theme");
if ($argc == 1) { # No arguments passed, show available commands
# Show empty line
echo "\n";
# Information for lsmod argument
_print("lsmod - Shows all available mods in this version.");
# Information for clear argument
_print("clear - Clears cache if available.");
} elseif ($argv[1] == "lsmod") { # Run if lsmod is passed
# Show header of the following content
_print("Mods:");
# Parse through all available mods in $modList
foreach ($modList as &$mod) {
# Check if mod is enabled
$modEnabled = $$mod ? "[Enabled]" : "[Disabled]";
# Print the mod name and status
_print(" - ".$mod . " " . $modEnabled);
# Create path of readme.txt file, which contains a short description of the mod
$readmePath = __DIR__."/mods/".$mod."/readme.txt";
# Only show readme.txt contents, if the file exists
if (file_exists($readmePath)) {
# Show readme.txt contents, indented for visual reasons
_print(" - " . file_get_contents($readmePath));
}
}
} elseif ($argv[1] == "clear") { # Run if clear is passed
# Clear files
_clear_file(__DIR__."/config.hash", "config.hash");
_clear_file(__DIR__."/../../public/uploads/ModBook/modStyles.css", "public/uploads/ModBook/modStyles.css");
_clear_file(__DIR__."/../../public/uploads/ModBook/modStylesGlobal.css", "public/uploads/ModBook/modStylesGlobal.css");
_clear_file(__DIR__."/../../public/uploads/ModBook/modScripts.js", "public/uploads/ModBook/modScripts.js");
# Clear folder
_clear_folder(__DIR__."/../../public/uploads/ModBook", "public/uploads/ModBook");
} else {
# Print that the passed argument is invalid
_print("Invalid command.");
}
?>