Skip to content

Commit 1287139

Browse files
committed
25 Oct 2022
1) Installer update. 2) LIB-Route.php - Changed htaccess() to init(). Will now change base path in web manifest as well.
1 parent ce1d871 commit 1287139

File tree

2 files changed

+90
-55
lines changed

2 files changed

+90
-55
lines changed

lib/CORE-Install.php

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function import ($pdo, $from=0) {
5151
// @TODO - SET AS NECESSARY
5252
define("I_ALL", [
5353
I_BASE, I_ASSETS, I_LIB, I_PAGES, // I_UPLOADS,
54-
I_LIB . "CORE-Config.php", I_BASE . "index.php"
54+
I_LIB . "CORE-Config.php", I_BASE . "index.php", I_BASE . "CB-manifest.json"
5555
]);
5656

5757
// (B1-2) APACHE + PHP VERSION + EXTENSIONS
@@ -158,15 +158,11 @@ function import ($pdo, $from=0) {
158158
var install = {
159159
// (DD2) HELPER - AJAX FETCH
160160
ajax : (url, phase, after) => {
161-
// (DD2-1) DISABLE GO BUTTON
162-
var go = document.getElementById("gobtn");
163-
go.disabled = true;
164-
165-
// (DD2-2) FORM DATA
161+
// (DD2-1) FORM DATA
166162
let data = new FormData(document.getElementById("iForm"));
167163
data.append("phase", phase);
168164

169-
// (DD2-3) AJAX FETCH
165+
// (DD2-2) AJAX FETCH
170166
fetch(url, { method:"POST", body:data })
171167
.then(res => {
172168
if (res.status==200) { return res.text(); }
@@ -175,20 +171,35 @@ function import ($pdo, $from=0) {
175171
let err = "SERVER ERROR " + res.status;
176172
if (res.status==404) { err += " - Is the host URL correct? Is 'AllowOverride All' set in Apache?`"; }
177173
alert(err);
174+
install.toggle(true);
178175
}
179176
})
180177
.then(txt => {
181178
if (txt=="OK") { after(); }
182-
else if (txt!=undefined) { alert(txt); }
179+
else if (txt!=undefined) {
180+
alert(txt);
181+
install.toggle(true);
182+
}
183183
})
184184
.catch(err => {
185185
alert(`Fetch error - ${err.message}`);
186+
install.toggle(true);
186187
console.error(err);
187-
})
188-
.finally(() => go.disabled = false);
188+
});
189+
},
190+
191+
// (DD3) LOCK/UNLOCK INSTALL FORM
192+
toggle : enable => {
193+
if (enable) {
194+
document.getElementById("gobtn").disabled = false;
195+
document.getElementById("iForm").onsubmit = install.go;
196+
} else {
197+
document.getElementById("gobtn").disabled = true;
198+
document.getElementById("iForm").onsubmit = false;
199+
}
189200
},
190201

191-
// (DD3) RANDOM JWT KEY GENERATOR
202+
// (DD4) RANDOM JWT KEY GENERATOR
192203
// CREDITS https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
193204
rnd : () => {
194205
var result = "";
@@ -200,54 +211,56 @@ function import ($pdo, $from=0) {
200211
document.getElementsByName("jwtkey")[0].value = result;
201212
},
202213

203-
// (DD4) TOGGLE CORS
214+
// (DD5) TOGGLE CORS
204215
cors : allowed => {
205216
let more = document.getElementById("corsmore");
206217
if (allowed==1) { more.classList.remove("hide"); }
207218
else { more.classList.add("hide"); }
208219
},
209220

210-
// (DD5) INSTALL GO
221+
// (DD6) INSTALL GO
211222
go : () => {
223+
// (DD6-1) LOCK INSTALL FORM
224+
install.toggle(false);
225+
212226
<?php if (I_USER) { ?>
213-
// (DD5-1) ADMIN PASSWORD
227+
// (DD6-2) ADMIN PASSWORD
214228
var pass = document.getElementsByName("apass")[0],
215229
cpass = document.getElementsByName("apassc")[0];
216230
if (pass.value != cpass.value) {
217-
go.disabled = false;
218231
alert("Admin passwords do not match!");
232+
install.toggle(true);
219233
return false;
220234
}
221235

222-
// (DD5-2) PASSWORD STRENGTH CHECK - AT LEAST 8 CHARACTERS ALPHANUMERIC
236+
// (DD6-3) PASSWORD STRENGTH CHECK - AT LEAST 8 CHARACTERS ALPHANUMERIC
223237
if (!/^(?=.*[0-9])(?=.*[A-Z]).{8,20}$/i.test(pass.value)) {
224-
go.disabled = false;
225238
alert("Password must be at least 8 characters alphanumeric");
239+
install.toggle(true);
226240
return false;
227241
}
228242
<?php } ?>
229243

230-
// (DD5-3) URL PATH
244+
// (DD6-4) URL PATH
231245
let url = (document.getElementsByName("https")[0].value=="0" ? "http" : "https")
232246
+ "://" + document.getElementsByName("host")[0].value;
233247

234-
// (DD5-4) GENERATE HTACCESS
235-
install.ajax(url, "E", () => {
236-
// (DD5-5) VERIFY HTACCESS + INSTALL
237-
install.ajax(url + "COREVERIFY", "F", () => {
238-
// (DD5-6) DONE
239-
alert("Installation complete, this page will now reload.");
240-
location.href = url;
241-
});
242-
});
248+
// (DD6-5) GENERATE HTACCESS + VERIFY HTACCESS + INSTALL
249+
install.ajax(url, "E", () => install.ajax(url + "COREVERIFY", "F", () => {
250+
alert("Installation complete, this page will now reload.");
251+
location.href = url;
252+
}));
243253
return false;
244254
}
245255
};
256+
257+
// (DD7) ENABLE INSTALL FORM ON WINDOW LOAD
258+
window.onload = () => install.toggle(true);
246259
</script>
247260
</head>
248261
<body>
249262
<?php if (I_APACHE === false || I_REWRITE === false) { ?>
250-
<!-- (DD6) WARNINGS -->
263+
<!-- (DD8) WARNINGS -->
251264
<div class="danger">
252265
The installer cannot verify if you are running Apache Web Server, or if <code>MOD_REWRITE</code> is enabled.
253266
You can still try to proceed if you want.
@@ -256,15 +269,15 @@ function import ($pdo, $from=0) {
256269
</div>
257270
<?php } ?>
258271

259-
<!-- (DD7) HEADER -->
272+
<!-- (DD9) HEADER -->
260273
<div id="iHead">
261274
<img src="assets/favicon.png">
262275
<h1><?=I_NAME?> INSTALLATION</h1>
263276
</div>
264277

265-
<!-- (DD8) INSTALLATION FORM -->
266-
<form id="iForm" onsubmit="return install.go()">
267-
<!-- (DD8-1) HOST URL -->
278+
<!-- (DD10) INSTALLATION FORM -->
279+
<form id="iForm" onsubmit="return false">
280+
<!-- (DD10-1) HOST URL -->
268281
<h2>HOST URL</h2>
269282
<div class="iSec">
270283
<label>HTTP or HTTPS</label>
@@ -274,35 +287,35 @@ function import ($pdo, $from=0) {
274287
</select>
275288
<label>Domain &amp; Path</label>
276289
<input type="text" name="host" required value="<?=I_HOST?>">
277-
<div class="notes">&#9432; Change this only if wrong, include the path if not deployed in root. E.G. <code>site.com/myproject/</code></div>
290+
<div class="notes">* Change this only if wrong, include the path if not deployed in root. E.G. <code>site.com/myproject/</code></div>
278291
</div>
279292

280-
<!-- (DD8-2) API ENDPOINT -->
293+
<!-- (DD10-2) API ENDPOINT -->
281294
<h2>API ENDPOINT</h2>
282295
<div class="iSec">
283296
<label>Enforce HTTPS?</label>
284297
<select name="apihttps">
285298
<option value="0">No</option>
286299
<option value="1"<?=I_HTTPS?" selected":""?>>Yes</option>
287300
</select>
288-
<div class="notes">&#9432; If enforced, API will only respond to HTTPS calls - Recommended to set "yes" for live servers.</div>
301+
<div class="notes">* If enforced, API will only respond to HTTPS calls - Recommended to set "yes" for live servers.</div>
289302
<label>CORS</label>
290303
<select name="apicors" onchange="install.cors(this.value)">
291304
<option value="0">Disallow</option>
292305
<option value="1">Allow</option>
293306
</select>
294-
<div class="notes">&#9432; Allow CORS only if you intend to develop mobile apps, or let third parties access your system.</div>
307+
<div class="notes">* Allow CORS only if you intend to develop mobile apps, or let third parties access your system.</div>
295308
<div id="corsmore" class="hide">
296309
<label>Allowed CORS Domains</label>
297310
<input type="text" name="corsallow">
298-
<div class="notes">&#9432; Leave this blank to allow all websites and apps to access your system (not recommended).</div>
311+
<div class="notes">* Leave this blank to allow all websites and apps to access your system (not recommended).</div>
299312
<div class="notes">
300-
&#9432; To restrict which domains can access your system - Enter the domain name (<code>site-a.com</code>), or multiple domains separated by commas (<code>site-a.com, site-b.com</code>).
313+
* To restrict which domains can access your system - Enter the domain name (<code>site-a.com</code>), or multiple domains separated by commas (<code>site-a.com, site-b.com</code>).
301314
</div>
302315
</div>
303316
</div>
304317

305-
<!-- (DD8-3) DATABASE -->
318+
<!-- (DD10-3) DATABASE -->
306319
<h2>DATABASE</h2>
307320
<div class="iSec">
308321
<label>Host</label>
@@ -315,22 +328,22 @@ function import ($pdo, $from=0) {
315328
<input type="password" name="dbpass" value="<?=I_DB_PASS?>">
316329
</div>
317330

318-
<!-- (DD8-4) EMAIL SEND FROM -->
331+
<!-- (DD10-4) EMAIL SEND FROM -->
319332
<h2>EMAIL</h2>
320333
<div class="iSec">
321334
<label>Sent From</label>
322335
<input type="email" name="mailfrom" value="sys@site.com" required>
323336
</div>
324337

325-
<!-- (DD8-5) JWT & ADMIN USER -->
338+
<!-- (DD10-5) JWT & ADMIN USER -->
326339
<?php if (I_USER) { ?>
327340
<h2>JSON WEB TOKEN</h2>
328341
<div class="iSec">
329342
<label>Secret Key <span onclick="install.rnd()">[RANDOM]</span></label>
330343
<input type="text" name="jwtkey" required>
331344
<label>Issuer</label>
332345
<input type="text" name="jwyiss" required value="<?=$_SERVER["HTTP_HOST"]?>">
333-
<div class="notes">&#9432; Your company name or domain name.</div>
346+
<div class="notes">* Your company name or domain name.</div>
334347
</div>
335348

336349
<h2>ADMIN USER</h2>
@@ -341,13 +354,13 @@ function import ($pdo, $from=0) {
341354
<input type="text" name="aemail" required value="admin@site.com">
342355
<label>Password</label>
343356
<input type="password" name="apass" required>
344-
<div class="notes">&#9432; At least 8 characters alphanumeric.</div>
357+
<div class="notes">* At least 8 characters alphanumeric.</div>
345358
<label>Confirm Password</label>
346359
<input type="password" name="apassc" required>
347360
</div>
348361
<?php } ?>
349362

350-
<!-- (DD8-6) PUSH NOTIFICATION -->
363+
<!-- (DD10-6) PUSH NOTIFICATION -->
351364
<?php if (I_PUSH) { ?>
352365
<h2>WEB PUSH VAPID KEYS</h2>
353366
<div class="iSec">
@@ -356,15 +369,15 @@ function import ($pdo, $from=0) {
356369
<label>Public Key</label>
357370
<input type="text" name="pushpublic" required value="<?=I_VAPID["publicKey"]?>">
358371
<div class="notes">
359-
&#9432; You can regenerate these with:<br>
372+
* You can regenerate these with:<br>
360373
<code>require "lib/webpush/autoload.php";</code><br>
361374
<code>$keys = Minishlink\WebPush\VAPID::createVapidKeys();</code>
362375
</div>
363376
</div>
364377
<?php } ?>
365378

366-
<!-- (DD8-7) GO! -->
367-
<input id="gobtn" type="submit" value="Go!">
379+
<!-- (DD10-7) GO! -->
380+
<input id="gobtn" type="submit" value="Go!" disabled>
368381
</form>
369382
</body>
370383
</html>
@@ -460,7 +473,7 @@ function import ($pdo, $from=0) {
460473
$replace["PUSH_PRIVATE"] = $_POST["pushprivate"];
461474
$replace["PUSH_PUBLIC"] = $_POST["pushpublic"];
462475
}
463-
unset($_POST); unset($hbase);
476+
unset($_POST);
464477

465478
// (F6) BACKUP LIB/CORE-CONFIG.PHP
466479
if (!copy(I_LIB . "CORE-Config.php", I_LIB . "CORE-Config.old")) {
@@ -477,9 +490,19 @@ function import ($pdo, $from=0) {
477490
}}}
478491
try { file_put_contents(I_LIB . "CORE-Config.php", implode("", $cfg)); }
479492
catch (Exception $ex) { exit("Error writing to ". I_LIB ."CORE-Config.php"); }
480-
unset($cfg); unset($replace);
481493

482-
// (F8) ALMOST DONE...
494+
// (F8) UPDATE WEB MANIFEST
495+
$replace = ["start_url", "scope"];
496+
$hbase = parse_url(rtrim($hbase, "/\\") . "/", PHP_URL_PATH);
497+
$cfg = file(I_BASE . "CB-manifest.json") or exit("Cannot read". I_BASE . "CB-manifest.json");
498+
foreach ($cfg as $j=>$line) { foreach ($replace as $r) { if (strpos($line, "\"$r\"") !== false) {
499+
$cfg[$j] = " \"$r\": \"$hbase\",\r\n";
500+
}}}
501+
try { file_put_contents(I_BASE . "CB-manifest.json", implode("", $cfg)); }
502+
catch (Exception $ex) { exit("Error writing to ". I_BASE . "CB-manifest.json"); }
503+
unset($hbase); unset($cfg); unset($replace);
504+
505+
// (F9) ALMOST DONE...
483506
$_PHASE = "G";
484507
}
485508

lib/LIB-Route.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,29 @@ function api () {
147147
require PATH_LIB . "API-$_MOD.php";
148148
}
149149

150-
// (E) REGENERATE HTACCESS FILE
151-
function htaccess () {
152-
$htaccess = PATH_BASE . ".htaccess";
153-
if (file_put_contents($htaccess, implode("\r\n", [
150+
// (E) REGENERATE HTACCESS + MANIFEST FILES
151+
function init () {
152+
// (E1) HTACCESS
153+
$file = PATH_BASE . ".htaccess";
154+
if (file_put_contents($file, implode("\r\n", [
154155
"RewriteEngine On",
155156
"RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]",
156157
"RewriteBase " . HOST_BASE_PATH,
157158
"RewriteRule ^index\.php$ - [L]",
158159
"RewriteCond %{REQUEST_FILENAME} !-f",
159160
"RewriteCond %{REQUEST_FILENAME} !-d",
160161
"RewriteRule . " . HOST_BASE_PATH . "index.php [L]"
161-
])) === false) { throw new Exception("Failed to create $htaccess"); }
162+
])) === false) { throw new Exception("Failed to create $file"); }
163+
164+
// (E2) WEB MANIFEST
165+
$file = PATH_BASE . "CB-manifest.json";
166+
$replace = ["start_url", "scope"];
167+
$cfg = file($file) or exit("Cannot read $file");
168+
foreach ($cfg as $j=>$line) { foreach ($replace as $r) { if (strpos($line, "\"$r\"") !== false) {
169+
$cfg[$j] = " \"$r\": \"".HOST_BASE_PATH."\",\r\n";
170+
}}}
171+
if (file_put_contents($file, implode("", $cfg)) === false) {
172+
throw new Exception("Failed to write $file");
173+
}
162174
}
163175
}

0 commit comments

Comments
 (0)