Skip to content

Commit 1923b74

Browse files
committed
12 Sep 2023
1) Added LIB-MInstall - Shared module installer library. 2) Updated all module installers. 3) Added "S"uspended user level, a "non destructive" alternative to deleting accounts.
1 parent 61e046a commit 1923b74

File tree

15 files changed

+355
-553
lines changed

15 files changed

+355
-553
lines changed

admin/pages/PAGE-install-admin.php

Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,34 @@
66

77
// (B) PROCEED WITH INSTALLATION
88
if (isset($_POST["go"])) {
9-
// (B1) UPDATE CORE-CONFIG.PHP
10-
try {
11-
// (B1-1) BACKUP & READ CORE-CONFIG.PHP
12-
copy(PATH_LIB . "CORE-Config.php", PATH_LIB . "CORE-Config.old");
13-
$cfg = file(PATH_LIB . "CORE-Config.php");
14-
15-
// (B1-2) INSERT NEW HOST_ADMIN DEFINITION
16-
$add = <<<EOF
17-
define("HOST_ADMIN", HOST_BASE . "admin/"); // ADDED BY INSTALLER
18-
EOF;
19-
$at = 0;
20-
foreach ($cfg as $l=>$line) {
21-
if (strpos($line, "(B) API ENDPOINT") !== false) {
22-
$at = $l; break;
23-
}
24-
}
25-
$at = $at - 1;
26-
array_splice($cfg, $at, 0, $add . "\r\n");
27-
file_put_contents(PATH_LIB . "CORE-Config.php", implode("", $cfg));
28-
} catch (Exception $ex) {
29-
exit("Unable to update CORE-Config.php - " . $ex->getMessage());
30-
}
31-
9+
// (B1) UPDATE CORE-CONFIG.PHP - INSERT HOST_ADMIN DEFINITION
10+
$_CORE->load("MInstall");
11+
$_CORE->MInstall->insert(
12+
PATH_LIB . "CORE-Config.php", "HOST_ASSETS",
13+
'define("HOST_ADMIN", HOST_BASE . "admin/"); // ADDED BY INSTALLER' . "\r\n"
14+
);
15+
3216
// (B2) UPDATE HOOK-ROUTES.PHP
33-
try {
34-
// (B2-1) SIMPLE CHECKS
35-
require PATH_LIB . "HOOK-Routes.php";
36-
if (!isset($wild)) {
37-
exit("Please define \$wild=[] in HOOK-Routes.php");
38-
}
39-
$wild = count($wild);
40-
unset($routes); unset($override);
41-
42-
// (B2-2) BACKUP & READ HOOK-ROUTES.PHP
43-
copy(PATH_LIB . "HOOK-Routes.php", PATH_LIB . "HOOK-Routes.old");
44-
$cfg = file(PATH_LIB . "HOOK-Routes.php");
45-
46-
// (B2-3) INSERT NEW ADMIN ROUTE
47-
$add = <<<EOF
48-
"admin/" => "ADM-check.php"
49-
EOF;
50-
$at = 0;
51-
foreach ($cfg as $l=>$line) {
52-
if (strpos($line, "\$wild = [") !== false) {
53-
$at = $l; break;
54-
}
55-
}
56-
$at = $at + 1;
57-
array_splice($cfg, $at, 0, $add . ($wild==0?"":",") . " // ADDED BY INSTALLER\r\n");
58-
file_put_contents(PATH_LIB . "HOOK-Routes.php", implode("", $cfg));
59-
} catch (Exception $ex) {
60-
exit("Unable to update HOOK-Routes.php - " . $ex->getMessage());
61-
}
62-
63-
// (B3) CREATE DUMMY ADMIN
17+
// (B2-1) SIMPLE CHECK
18+
require PATH_LIB . "HOOK-Routes.php";
19+
if (!isset($wild)) { exit("Please define \$wild=[] in HOOK-Routes.php"); }
20+
$wild = count($wild);
21+
unset($routes); unset($override);
22+
23+
// (B2-2) INSERT ADMIN ROUTE
24+
$_CORE->MInstall->insert(
25+
PATH_LIB . "HOOK-Routes.php", "\$wild",
26+
' "admin/" => "ADM-check.php"' .
27+
($wild==0 ? "" : ",") .
28+
" // ADDED BY INSTALLER\r\n"
29+
);
30+
31+
// (B3) CREATE ADMIN ACCOUNT
6432
$_CORE->load("Users");
6533
$_CORE->Users->save($_POST["name"], $_POST["email"], $_POST["pass"], "A");
6634

67-
// (B4) DELETE THIS SCRIPT
68-
try {
69-
unlink(PATH_PAGES . "PAGE-install-admin.php");
70-
} catch (Exception $ex) {
71-
exit("Unable to delete PAGE-install-admin.php, please do so manually.");
72-
}
73-
74-
// (B5) DONE
75-
exit("Admin module successfully installed.");
35+
// (B4) CLEAN UP
36+
$_CORE->MInstall->clean("admin"); exit();
7637
}
7738

7839
// (C) ENTER ADMIN USER/PASSWORD

autocomplete/pages/PAGE-install-autocomplete.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
exit("Unable to create new setting.");
1010
}
1111

12-
// (B) DELETE THIS SCRIPT
13-
try {
14-
unlink(PATH_PAGES . "PAGE-install-autocomplete.php");
15-
} catch (Exception $ex) {
16-
exit("Unable to delete PAGE-install-autocomplete.php, please do so manually.");
17-
}
18-
19-
// (C) DONE
20-
exit("Autocomplete module successfully installed.");
12+
// (B) CLEAN UP
13+
$_CORE->load("MInstall");
14+
$_CORE->MInstall->clean("autocomplete");

calendar/pages/PAGE-install-calendar.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
exit("Unable to import SQL - " . $ex->getMessage());
77
}
88

9-
// (B) DELETE THIS SCRIPT
10-
try {
11-
unlink(PATH_PAGES . "PAGE-install-calendar.php");
12-
} catch (Exception $ex) {
13-
exit("Unable to delete PAGE-install-calendar.php, please do so manually.");
14-
}
15-
16-
// (C) DONE
17-
exit("Calendar module successfully installed.");
9+
// (B) CLEAN UP
10+
$_CORE->load("MInstall");
11+
$_CORE->MInstall->clean("calendar");

comments/pages/PAGE-install-comments.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,8 @@
55
}
66

77
// (B) IMPORT SQL
8-
try {
9-
$_CORE->DB->query(file_get_contents(PATH_LIB . "SQL-Comments.sql"));
10-
} catch (Exception $ex) {
11-
exit("Unable to import SQL - " . $ex->getMessage());
12-
}
13-
14-
// (C) DELETE THIS SCRIPT
15-
try {
16-
unlink(PATH_PAGES . "PAGE-install-comments.php");
17-
} catch (Exception $ex) {
18-
exit("Unable to delete PAGE-install-comments.php, please do so manually.");
19-
}
8+
$_CORE->load("MInstall");
9+
$_CORE->MInstall->sql("Comments");
2010

21-
// (D) DONE
22-
exit("Comments module successfully installed.");
11+
// (C) CLEAN UP
12+
$_CORE->MInstall->clean("comments");

core/lib/LIB-MInstall.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
class MInstall extends Core {
3+
// (A) IMPORT SQL FILE
4+
function sql ($module) {
5+
$file = PATH_LIB . "SQL-$module.sql";
6+
if (!file_exists($file)) { exit("$file not found!"); }
7+
try {
8+
$this->DB->query(file_get_contents($file));
9+
} catch (Exception $ex) {
10+
exit("Unable to import $file - " . $ex->getMessage());
11+
}
12+
}
13+
14+
// (B) BACKUP FILE
15+
function backup ($file) {
16+
if (!file_exists($file)) { exit("$file not found!"); }
17+
$ext = pathinfo($file, PATHINFO_EXTENSION);
18+
$bak = $ext == "htaccess" ? "$file.old" : str_replace(".$ext", ".old", $file) ;
19+
if (!copy($file, $bak)) { exit("Failed to backup $file"); }
20+
}
21+
22+
// (C) APPEND TO FILE
23+
function append ($file, $add) {
24+
$this->backup($file);
25+
$fh = fopen($file, "a") or exit("Cannot open $file");
26+
if (fwrite($fh, $add) === false) {
27+
fclose($fh);
28+
exit("Failed to write to $file");
29+
}
30+
fclose($fh);
31+
}
32+
33+
// (D) INSERT INTO FILE
34+
function insert ($file, $search, $add, $offset=0) {
35+
// (D1) BACKUP SPECIFIED FILE
36+
$this->backup($file);
37+
38+
// (D2) SEEK "LINE TO INSERT AT"
39+
$lines = file($file);
40+
$at = -1;
41+
foreach ($lines as $l=>$line) {
42+
if (strpos($line, $search) !== false) { $at = $l + 1 + $offset; break; }
43+
}
44+
if ($at == -1) { exit("Failed to update $file"); }
45+
46+
// (D3) INSERT INTO FILE
47+
array_splice($lines, $at, 0, $add);
48+
if (file_put_contents($file, implode("", $lines)) == false) {
49+
exit("Failed to update $file");
50+
}
51+
}
52+
53+
// (E) CONDITIONAL INSERT
54+
function cinsert ($condition, $file, $search, $add, $offset=0) {
55+
$insert = true;
56+
$stream = fopen($file, "r");
57+
while($line = fgets($stream)) {
58+
if (strpos($line, $condition) !== false) { $insert = false; break; }
59+
}
60+
if ($insert) { $this->insert($file, $search, $add, $offset); }
61+
}
62+
63+
// (F) CLEAN UP
64+
function clean ($module) {
65+
$file = PATH_PAGES . "PAGE-install-$module.php";
66+
if (!unlink($file)) { echo "Failed to delete $file, please do so manually."; }
67+
echo "Installation complete";
68+
}
69+
}
Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,22 @@
11
<?php
22
// (A) IMPORT SQL
3-
try {
4-
$_CORE->DB->query(file_get_contents(PATH_LIB . "SQL-Contents.sql"));
5-
} catch (Exception $ex) {
6-
exit("Unable to import SQL - " . $ex->getMessage());
7-
}
3+
$_CORE->load("MInstall");
4+
$_CORE->MInstall->sql("Contents");
85

96
// (B) UPDATE HOOK-ROUTES.PHP
10-
try {
11-
// (B1) SIMPLE CHECKS
12-
require PATH_LIB . "HOOK-Routes.php";
13-
if (!isset($wild)) {
14-
exit("Please define \$wild=[] in HOOK-Routes.php");
15-
}
16-
$wild = count($wild);
17-
unset($routes); unset($override);
7+
// (B2-1) SIMPLE CHECK
8+
require PATH_LIB . "HOOK-Routes.php";
9+
if (!isset($wild)) { exit("Please define \$wild=[] in HOOK-Routes.php"); }
10+
$wild = count($wild);
11+
unset($routes); unset($override);
1812

19-
// (B2) BACKUP & READ HOOK-ROUTES.PHP
20-
copy(PATH_LIB . "HOOK-Routes.php", PATH_LIB . "HOOK-Routes.old");
21-
$cfg = file(PATH_LIB . "HOOK-Routes.php");
13+
// (B2-2) INSERT CONTENT ROUTE
14+
$_CORE->MInstall->insert(
15+
PATH_LIB . "HOOK-Routes.php", "\$wild",
16+
' "post/" => "POST-load.php"' .
17+
($wild==0 ? "" : ",") .
18+
" // ADDED BY INSTALLER\r\n"
19+
);
2220

23-
// (B3) INSERT NEW POSTS ROUTE
24-
$add = <<<EOF
25-
"post/" => "POST-load.php"
26-
EOF;
27-
$at = 0;
28-
foreach ($cfg as $l=>$line) {
29-
if (strpos($line, "\$wild = [") !== false) {
30-
$at = $l; break;
31-
}
32-
}
33-
$at = $at + 1;
34-
array_splice($cfg, $at, 0, $add . ($wild==0?"":",") . " // ADDED BY INSTALLER\r\n");
35-
file_put_contents(PATH_LIB . "HOOK-Routes.php", implode("", $cfg));
36-
} catch (Exception $ex) {
37-
exit("Unable to update HOOK-Routes.php - " . $ex->getMessage());
38-
}
39-
40-
// (C) DELETE THIS SCRIPT
41-
try {
42-
unlink(PATH_PAGES . "PAGE-install-content.php");
43-
} catch (Exception $ex) {
44-
exit("Unable to delete PAGE-install-content.php, please do so manually.");
45-
}
46-
47-
// (D) DONE
48-
exit("Dynamic Content module successfully installed.");
21+
// (C) CLEAN UP
22+
$_CORE->MInstall->clean("content");

0 commit comments

Comments
 (0)