-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDocGen.php
More file actions
237 lines (231 loc) · 12 KB
/
Copy pathDocGen.php
File metadata and controls
237 lines (231 loc) · 12 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
// Path to vendor directory.
$Vendor = __DIR__ . DIRECTORY_SEPARATOR . 'vendor';
$loadL10N = function (string $Language) use (&$Vendor) {
$Preferred = $Language;
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'DocGen' . DIRECTORY_SEPARATOR . $Language . '.yml')) {
$Language = preg_replace('~-.*$~', '', $Language);
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'DocGen' . DIRECTORY_SEPARATOR . $Language . '.yml')) {
echo 'Unable to read the "' . $Language . '" language files!';
die;
}
}
$DataDocGen = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'DocGen' . DIRECTORY_SEPARATOR . $Language . '.yml');
$Language = preg_replace('~-.*$~', '', $Language);
if (
!file_exists($Vendor . DIRECTORY_SEPARATOR . 'phpmussel' . DIRECTORY_SEPARATOR . 'frontend' . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $Language . '.yml') ||
!file_exists($Vendor . DIRECTORY_SEPARATOR . 'phpmussel' . DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $Language . '.yml') ||
!file_exists($Vendor . DIRECTORY_SEPARATOR . 'phpmussel' . DIRECTORY_SEPARATOR . 'phpmailer' . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $Language . '.yml')
) {
echo 'Unable to read the "' . $Language . '" language files!';
die;
}
$YAML = new \Maikuolan\Common\YAML();
if (file_exists($Vendor . DIRECTORY_SEPARATOR . 'phpmussel' . DIRECTORY_SEPARATOR . 'frontend' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'references.yml')) {
$Data = file_get_contents($Vendor . DIRECTORY_SEPARATOR . 'phpmussel' . DIRECTORY_SEPARATOR . 'frontend' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'references.yml');
$YAML->process($Data, $YAML->Refs);
}
$Arr = [];
$YAML->process($DataDocGen, $Arr);
foreach (['frontend', 'web', 'phpmailer'] as $Dir) {
$DataThisDir = file_get_contents($Vendor . DIRECTORY_SEPARATOR . 'phpmussel' . DIRECTORY_SEPARATOR . $Dir . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $Language . '.yml');
$YAML->process($DataThisDir, $Arr);
}
$L10N = new \Maikuolan\Common\L10N($Arr, []);
$L10N->autoAssignRules($Preferred);
$L10N->PreferredVariant = $Preferred;
return $L10N;
};
if (!isset($_GET['language'])) {
echo 'No language specified.';
} else {
// Composer's autoloader.
require $Vendor . DIRECTORY_SEPARATOR . 'autoload.php';
$Loader = new \phpMussel\Core\Loader();
$Scanner = new \phpMussel\Core\Scanner($Loader);
$FrontEnd = new \phpMussel\FrontEnd\FrontEnd($Loader, $Scanner);
$Web = new \phpMussel\Web\Web($Loader, $Scanner);
$Loader->Events->addHandler('sendMail', new \phpMussel\PHPMailer\Linker($Loader));
header('Content-Type: text/plain');
$Final = '';
$Data = $loadL10N($_GET['language']);
$First = "```\n" . $Data->getString('link.Configuration') . " (v3)\n│\n";
$Cats = count($Loader->ConfigurationDefaults);
$Current = 1;
foreach ($Loader->ConfigurationDefaults as $Category => $Directives) {
$First .= ($Current === $Cats ? '└───' : '├───') . $Category . "\n";
$Out = $Data->getString('config.' . $Category);
if ($Data->Directionality !== 'rtl') {
$Out = str_replace(
['<code>', '<code class="s">', '<code dir="ltr">', '<code dir="rtl">', '</code>', '<strong>', '</strong>', '<em>', '</em>'],
['`', '`', '`', '`', '`', '__', '__', '*', '*'],
html_entity_decode($Out)
);
} else {
$Out = preg_replace('~<code dir="ltr" class="s">([^<]+)</code>~i', '<strong><code dir="ltr">\1</code></strong>', html_entity_decode($Out));
}
if ($Try = $Data->getString('config.' . $Category . '_hint')) {
if ($Data->Directionality !== 'rtl') {
$Try = str_replace(
['<code>', '<code class="s">', '<code dir="ltr">', '<code dir="rtl">', '</code>', '<strong>', '</strong>', '<em>', '</em>'],
['`', '`', '`', '`', '`', '__', '__', '*', '*'],
html_entity_decode($Try)
);
} else {
$Try = preg_replace('~<code dir="ltr" class="s">([^<]+)</code>~i', '<strong><code dir="ltr">\1</code></strong>', html_entity_decode($Try));
}
$Out .= "\n\n" . $Try;
}
$Final .= sprintf($Data->getString('category'), $Category, $Out) . "\n\n";
foreach ($Directives as $Directive => $Info) {
$Out = $Data->getString('config.' . $Category . '_' . $Directive);
if ($Data->Directionality !== 'rtl') {
$Out = str_replace(
['<code>', '<code class="s">', '<code dir="ltr">', '<code dir="rtl">', '</code>', '<strong>', '</strong>', '<em>', '</em>'],
['`', '`', '`', '`', '`', '__', '__', '*', '*'],
html_entity_decode($Out)
);
} else {
$Out = preg_replace('~<code dir="ltr" class="s">([^<]+)</code>~i', '<strong><code dir="ltr">\1</code></strong>', html_entity_decode($Out));
}
if (in_array($Info['type'], ['string', 'timezone', 'checkbox', 'url', 'email', 'kb'], true)) {
$Type = 'string';
} elseif ($Info['type'] === 'float') {
$Type = 'float';
} elseif ($Info['type'] === 'bool') {
$Type = 'bool';
} else {
$Type = 'int';
}
$Choices = [];
if ($Info['type'] === 'timezone') {
$Choices = ['SYSTEM' => $Data->getString('field.Use system default timezone'), 'UTC' => 'UTC'];
$Info['allow_other'] = true;
} elseif (isset($Info['choices'])) {
$Choices = $Info['choices'];
}
$First .= ($Current === $Cats ? ' ' : '│ ') . $Directive . ' [' . $Type . "]\n";
$Final .= sprintf($Data->getString('directive'), $Directive, $Type, $Out) . "\n\n";
if (!empty($Choices)) {
$Final .= "```\n" . $Directive;
if (isset($Info['labels'])) {
if (!is_array($Info['labels'])) {
$Info['labels'] = [$Info['labels']];
}
foreach ($Info['labels'] as &$Label) {
$Label = $Data->getString($Label) ?: $Label;
}
unset($Label);
$Final .= '───[' . implode(']─[', $Info['labels']) . ']';
}
$Final .= "\n";
$Number = count($Choices);
if (!empty($Info['allow_other'])) {
$Number++;
}
$Iterant = 1;
foreach ($Choices as $Choice => $Value) {
$Value = $Data->getString($Value) ?: $Value;
if ($Type === 'string') {
$Value = '"' . $Value . '"';
} elseif ($Type === 'bool') {
$Value = $Value ? 'true' : 'false';
}
if (strpos($Value, "\n")) {
$Value = explode("\n", $Value);
$Value[1] = wordwrap($Value[1], 76, ($Iterant === $Number ? "\n " : "\n│ "));
$Final .= ($Iterant === $Number ? '└─' : '├─') . $Choice . ' (' . $Value[0] . '): ' . $Value[1] . "\n";
} else {
$Final .= ($Iterant === $Number ? '└─' : '├─') . $Choice . ' (' . $Value . ")\n";
}
$Iterant++;
}
if (!empty($Info['allow_other'])) {
$Final .= '└─…' . $Data->getString('label.Other') . "\n";
}
$Final .= "```\n\n";
}
if (!empty($Info['hints'])) {
$HintValue = '';
if (is_string($Info['hints']) && strpos($Info['hints'], '.') !== false) {
$HintValue = $Data->getString($Info['hints']);
}
if ($HintValue !== '') {
if ($Data->Directionality !== 'rtl') {
$HintValue = str_replace(
['<code>', '<code class="s">', '<code dir="ltr">', '<code dir="rtl">', '</code>', '<strong>', '</strong>', '<em>', '</em>'],
['`', '`', '`', '`', '`', '__', '__', '*', '*'],
html_entity_decode($HintValue)
);
} else {
$HintValue = preg_replace('~<code dir="ltr" class="s">([^<]+)</code>~i', '<strong><code dir="ltr">\1</code></strong>', html_entity_decode($HintValue));
}
$Final .= $HintValue . "\n\n";
} else {
foreach ($Data->arrayFromL10nToArray($Info['hints']) as $HintKey => $HintValue) {
if ($Data->Directionality !== 'rtl') {
$HintValue = str_replace(
['<code>', '<code class="s">', '<code dir="ltr">', '<code dir="rtl">', '</code>', '<strong>', '</strong>', '<em>', '</em>'],
['`', '`', '`', '`', '`', '__', '__', '*', '*'],
html_entity_decode($HintValue)
);
} else {
$HintValue = preg_replace('~<code dir="ltr" class="s">([^<]+)</code>~i', '<strong><code dir="ltr">\1</code></strong>', html_entity_decode($HintValue));
}
if (!is_string($HintKey)) {
$Final .= $HintValue . "\n\n";
continue;
}
$Final .= sprintf("__%s__ %s\n\n", $HintKey, $HintValue);
}
}
}
if (!empty($Info['See also'])) {
$Final .= sprintf($Data->getString('menu_open'), $Data->getString('label.See also')) . "\n";
foreach ($Info['See also'] as $RefKey => $RefLink) {
$RefKey = addcslashes($RefKey, '|');
$Final .= sprintf($Data->getString('menu_item'), $RefKey, $RefLink) . "\n";
}
$Final .= $Data->getString('menu_close') . "\n";
}
}
if ($Current !== $Cats) {
$Current++;
}
}
$Matches = [];
if (preg_match_all('~\{([.,%_ ?!\dA-Za-z()-]+)\}~', $Final, $Matches)) {
$Matches = array_unique($Matches[1]);
foreach ($Matches as $Match) {
if ($Try = $Data->getString($Match)) {
$Final = str_replace('{' . $Match . '}', $Try, $Final);
}
}
}
if (!isset($_GET['autoupdate'])) {
echo $First . "```\n\n" . $Final;
} else {
$Try = $_GET['autoupdate'] . 'readme.' . $_GET['language'] . '.md';
if (is_file($Try) && is_writable($Try)) {
$README = file_get_contents($Try);
if (($Start = strpos($README, '<a name="SECTION5">')) !== false) {
$Start = strpos($README, '```', $Start);
}
if (($Finish = strpos($README, '<a name="SECTION6">')) !== false) {
$Finish = strrpos(substr($README, 0, $Finish), '---');
}
if ($Start === false || $Finish === false) {
echo 'Unable to find configuration section markers in ' . $Try . '!';
} else {
$New = substr($README, 0, $Start) . $First . "```\n\n" . $Final . substr($README, $Finish);
$Handle = fopen($Try, 'wb');
fwrite($Handle, $New);
fclose($Handle);
echo 'Successfully updated ' . $Try . '. :-)';
}
} else {
echo $Try . ' doesn\'t exist or isn\'t writable!';
}
}
}
unset($Web, $FrontEnd, $Scanner, $Loader);