198 lines
5.8 KiB
PHP
198 lines
5.8 KiB
PHP
<?php
|
|
global $c;
|
|
|
|
$strDBCSFile = "dbcs.txt";
|
|
$strDBCS = "sqlite:sqlite.db";
|
|
|
|
if (!file_exists($strDBCSFile))
|
|
file_put_contents($strDBCSFile, $strDBCS);
|
|
|
|
$strDBCS = trim(file_get_contents($strDBCSFile));
|
|
|
|
$c = new DatabaseConnection($strDBCS);
|
|
|
|
$intInitialize = 1;
|
|
if ($intInitialize == 1)
|
|
{
|
|
$c->query([
|
|
"create_users_table.sql",
|
|
"create_sessions_table.sql",
|
|
"create_links_table.sql",
|
|
"create_posts_table.sql",
|
|
"create_settings_table.sql"]);
|
|
|
|
$varLinks = $c->query("SELECT * from links");
|
|
|
|
if (count($varLinks) < 1)
|
|
{
|
|
$c->query(
|
|
"INSERT into links (label, url, icon, position, visibility)
|
|
values
|
|
('Home', '/', 'home', 'navbar', ''),
|
|
('Post', '/post', 'edit', 'navbar', 'user'),
|
|
|
|
('Home', '/', 'home', 'sidebar', ''),
|
|
('Edit Links', '/edit/links', 'link', 'sidebar', 'admin'),
|
|
('Edit CSS', '/settings/css', 'code', 'sidebar', 'admin'),
|
|
('Edit JS', '/settings/js', 'code', 'sidebar', 'admin'),
|
|
|
|
('Copyright © 2025', '/', 'building', 'footer', '')"
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
class Settings
|
|
{
|
|
public static function get($strSettingName, $strDefault="", $intSave=0)
|
|
{
|
|
global $c;
|
|
|
|
$varExisting = $c->query("
|
|
SELECT *
|
|
from settings
|
|
where
|
|
setting like ?
|
|
order by
|
|
id desc",
|
|
$strSettingName);
|
|
|
|
if (count($varExisting) > 0)
|
|
return $varExisting[0]["value"];
|
|
|
|
if ($intSave)
|
|
Settings::set($strSettingName, $strDefault);
|
|
|
|
return $strDefault;
|
|
}
|
|
|
|
public static function set($strSettingName, $strValue)
|
|
{
|
|
global $c;
|
|
|
|
$varExisting = $c->query("
|
|
SELECT *
|
|
from settings
|
|
where
|
|
setting like ?
|
|
order by
|
|
id desc",
|
|
$strSettingName);
|
|
|
|
if (count($varExisting) !== 1)
|
|
{
|
|
$c->query("DELETE from settings where setting like ?", $strSettingName);
|
|
$c->query("INSERT into settings (setting, value) values (?, ?)", $strSettingName, $strValue);
|
|
}
|
|
|
|
$c->query(
|
|
"UPDATE settings
|
|
set
|
|
value = ?
|
|
where setting like ?",
|
|
$strValue,
|
|
$strSettingName);
|
|
}
|
|
}
|
|
|
|
|
|
class UserAuth
|
|
{
|
|
public static function getUser()
|
|
{
|
|
global $c;
|
|
try
|
|
{
|
|
$strToken = Cookie::get("token");
|
|
|
|
if ($strToken !== null)
|
|
if (strlen($strToken) > 0)
|
|
{
|
|
$varSessions = $c->query(
|
|
"SELECT *
|
|
from sessions as s
|
|
join users as u on u.username = s.username
|
|
where
|
|
s.token = ?
|
|
and (
|
|
s.expires is null
|
|
or s.expires > current_timestamp
|
|
)",
|
|
$strToken);
|
|
|
|
if (count($varSessions) == 1)
|
|
return $varSessions[0];
|
|
}
|
|
}
|
|
catch (Exception $x) {}
|
|
return null;
|
|
}
|
|
|
|
public static function has($strColumnName)
|
|
{
|
|
global $c;
|
|
$varUser = UserAuth::getUser();
|
|
|
|
if ($varUser == null)
|
|
return false;
|
|
|
|
if (array_key_exists($strColumnName, $varUser))
|
|
if (intval($varUser[$strColumnName]) > 0)
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function require($strColumnName)
|
|
{
|
|
if (!UserAuth::has($strColumnName))
|
|
{
|
|
BootstrapRender::message("You do not have permission to do that, please sign into an account that does.", "warning");
|
|
Respond::redirect("/user/signin");
|
|
}
|
|
}
|
|
|
|
public static function visible($strVisibility)
|
|
{
|
|
global $c;
|
|
|
|
if (UserAuth::has("is_admin"))
|
|
return true;
|
|
|
|
$varUser = UserAuth::getUser();
|
|
$strUsername = $varUser["username"] ?? null;
|
|
$varRegex = [
|
|
["/user/i", ($varUser == null)],
|
|
["/admin/i", (!UserAuth::has("is_admin"))],
|
|
];
|
|
|
|
// Support arrays with username and visibility keys:
|
|
if (is_array($strVisibility))
|
|
{
|
|
if (array_key_exists("username", $strVisibility))
|
|
if ($strVisibility["username"] == $strUsername)
|
|
return true;
|
|
|
|
if (!array_key_exists("visibility", $strVisibility))
|
|
return false;
|
|
|
|
$strVisibility = $strVisibility["visibility"];
|
|
}
|
|
|
|
if (preg_match("/{$strUsername}/i", $strVisibility)) return true;
|
|
if (preg_match("/(every|any|all)/i", $strVisibility)) return true;
|
|
|
|
$intExit = 0;
|
|
|
|
foreach ($varRegex as $re)
|
|
if (preg_match($re[0], $strVisibility))
|
|
if ($re[1])
|
|
$intExit = 1;
|
|
|
|
if ($intExit == 1)
|
|
return false;
|
|
return true;
|
|
}
|
|
}
|
|
?>
|