Skip to content

Commit c776f80

Browse files
committed
12 Sep 2023
1) Added a hidden "Suspended" user level - Should anyone want to suspend and not delete user accounts. 2) Push - Added user ID. It is now possible to send to specific users. 3) Small route fix for funky URLs such as http://site.com//FOO
1 parent 3d88148 commit c776f80

File tree

8 files changed

+167
-57
lines changed

8 files changed

+167
-57
lines changed

lib/CORE-Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
// (H) USER LEVELS - IF YOU WANT TO INCLUDE ROLES IN THE FUTURE
5959
define("USR_LVL", [
60-
"A" => "Admin", "U" => "User"
60+
"A" => "Admin", "U" => "User", "S" => "Suspended"
6161
]);
6262

6363
// (I) PUSH NOTIFICATION

lib/HOOK-SESS-Load.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
$user = $this->DB->fetch(
88
"SELECT * FROM `users` WHERE `user_id`=?", [$_SESSION["user"]["user_id"]]
99
);
10-
if (is_array($user)) {
11-
unset($user["user_password"]);
12-
$_SESSION["user"] = $user;
13-
} else {
10+
if (!is_array($user) || (isset($user["user_level"]) && $user["user_level"]=="S")) {
1411
$this->destroy();
1512
throw new Exception("Invalid or expired session.");
13+
} else {
14+
unset($user["user_password"]);
15+
$_SESSION["user"] = $user;
1616
}
1717
}

lib/LIB-Forgot.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ function request ($email) {
2424
$this->error = "$email is not an active account.";
2525
return false;
2626
}
27+
if ($user["user_level"] == "S") {
28+
$this->error = "$email is not an active account.";
29+
return false;
30+
}
2731

2832
// (B3) CHECK PREVIOUS REQUEST (PREVENT SPAM)
2933
$req = $this->Users->hashGet($user["user_id"], "P");

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+
}

lib/LIB-Push.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
class Push extends Core {
33
// (A) SAVE SUBSCRIBER
44
function save ($endpoint, $sub) {
5-
$this->DB->replace("webpush", ["endpoint", "data"], [$endpoint, $sub]);
5+
$this->DB->replace("webpush",
6+
["endpoint", "user_id", "data"],
7+
[
8+
$endpoint,
9+
isset($_SESSION["user"]) ? $_SESSION["user"]["user_id"] : null,
10+
$sub
11+
]
12+
);
613
return true;
714
}
815

@@ -12,8 +19,8 @@ function del ($endpoint) {
1219
return true;
1320
}
1421

15-
// (C) SEND PUSH
16-
function send ($title, $body, $icon=null, $image=null) {
22+
// (C) SEND PUSH NOTIFICATION
23+
function send ($title, $body, $icon=null, $image=null, $uid=null) {
1724
// (C1) MAY TAKE A LONG TIME IF THERE ARE A LOT OF INACTIVE...
1825
set_time_limit(45);
1926

@@ -25,8 +32,12 @@ function send ($title, $body, $icon=null, $image=null) {
2532
"privateKey" => PUSH_PRIVATE
2633
]]);
2734

28-
// (C3) SEND TO SUBSCRIBERS
29-
$this->DB->query("SELECT `data` FROM `webpush`");
35+
// (C3) SEND TO SUBSCRIBER(S)
36+
$this->DB->query(
37+
"SELECT `data` FROM `webpush`" .
38+
($uid==null ? "" : " WHERE `user_id`=?"),
39+
$uid==null ? null : [$uid]
40+
);
3041
while ($r = $this->DB->stmt->fetchColumn()) {
3142
// (C3-1) SUBSCRIBER
3243
$sub = Minishlink\WebPush\Subscription::create(json_decode($r, true));

lib/LIB-Route.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ class Route extends Core {
99

1010
// (A) RUN URL ROUTING ENGINE
1111
function run () : void {
12-
// (A1) CLEAN CURRENT URL PATH
12+
// (A1) GET URL PATH SEGMENT
13+
$this->path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
14+
15+
// (A2) SPECIAL CASE
16+
// e.g. http://site.com//, http://site.com//XYZ
17+
if ($this->path == "") {
18+
$this->load("PAGE-404.php", 404);
19+
exit();
20+
}
21+
22+
// (A3) CLEAN CURRENT URL PATH
1323
// http://site.com/ > $this->path = "/"
1424
// http://site.com/hello/world/ > $this->path = "hello/world/"
15-
$this->path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
25+
$this->path = preg_replace("~/{2,}~", "/", $this->path);
1626
if (substr($this->path, 0, strlen(HOST_BASE_PATH)) == HOST_BASE_PATH) {
1727
$this->path = substr($this->path, strlen(HOST_BASE_PATH));
1828
}

0 commit comments

Comments
 (0)