diff --git a/action.php b/action.php index 498832e..77e9602 100644 --- a/action.php +++ b/action.php @@ -8,6 +8,7 @@ * Make things happen when buttons are pressed and forms submitted. */ require_once __DIR__ . "/required.php"; +require_once __DIR__ . "/lib/util.php"; if ($VARS['action'] !== "signout") { dieifnotloggedin(); @@ -48,6 +49,7 @@ switch ($VARS['action']) { if (is_empty($VARS['color'])) { returnToSender("invalid_parameters"); } + $url = formatsiteurl($VARS['url']); $theme = preg_replace("/[^A-Za-z0-9]/", '', $VARS['theme']); $color = preg_replace("/[^A-Za-z0-9]/", '', $VARS['color']); if (!file_exists(__DIR__ . "/public/themes/$theme/theme.json")) { @@ -57,12 +59,12 @@ switch ($VARS['action']) { returnToSender("invalid_parameters"); } if (is_empty($VARS['siteid'])) { - $database->insert('sites', ["sitename" => $VARS['name'], "url" => $VARS['url'], "theme" => $theme, "color" => $color]); + $database->insert('sites', ["sitename" => $VARS['name'], "url" => $url, "theme" => $theme, "color" => $color]); $siteid = $database->id(); $template = (file_exists(__DIR__ . "/public/themes/$theme/home.php") ? "home" : "default"); $database->insert('pages', ["slug" => "index", "siteid" => $siteid, "title" => "Home", "nav" => "Home", "navorder" => 1, "template" => "template"]); } else { - $database->update('sites', ["sitename" => $VARS['name'], "url" => $VARS['url'], "theme" => $theme, "color" => $color], ["siteid" => $VARS['siteid']]); + $database->update('sites', ["sitename" => $VARS['name'], "url" => $url, "theme" => $theme, "color" => $color], ["siteid" => $VARS['siteid']]); } returnToSender("settings_saved"); break; diff --git a/lib/requiredpublic.php b/lib/requiredpublic.php index e1d7c7e..e5eef9f 100644 --- a/lib/requiredpublic.php +++ b/lib/requiredpublic.php @@ -91,6 +91,19 @@ function getsiteid() { return $id; } } + $host = $_SERVER['HTTP_HOST']; + $args = $_SERVER['QUERY_STRING']; + $path = str_replace("?$args", "", $_SERVER['REQUEST_URI']); + $dir = str_replace("index.php", "", $path); + $sites = $database->select("sites", ["siteid", "url"], ["OR" => ["url[~]" => $host, "url" => $dir]]); + //var_dump($sites); + if (count($sites) == 1) { + return $sites[0]["siteid"]; + } + if (count($sites) > 1) { + var_dump($sites); + die(); + } return $database->get("sites", "siteid"); } @@ -118,3 +131,19 @@ function getpagetemplate() { } return "404"; } + +function formatsiteurl($url) { + if (substr($url, 0) != "/") { + if (strpos($url, "http://") !== 0 && strpos($url, "https://") !== 0) { + if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off") { + $url = "http://$url"; + } else { + $url = "https://$url"; + } + } + } + if (substr($url, -1) != "/") { + $url = $url . "/"; + } + return $url; +} diff --git a/lib/themefunctions.php b/lib/themefunctions.php index 133eba8..a738c81 100644 --- a/lib/themefunctions.php +++ b/lib/themefunctions.php @@ -20,7 +20,7 @@ function get_site_name($echo = true) { function get_site_url($echo = true) { $db = getdatabase(); - $url = $db->get('sites', "url", ["siteid" => getsiteid()]); + $url = formatsiteurl($db->get('sites', "url", ["siteid" => getsiteid()])); if ($echo) { echo $url; } else { @@ -79,7 +79,7 @@ function get_page_url($echo = true, $slug = null) { if (isset($_GET['siteid'])) { $siteid = "&siteid=" . preg_replace("/[^0-9]/", '', $_GET['siteid']); } - $url = get_site_url(false) . "index.php?id=$slug$edit$theme$template$color$siteid"; + $url = formatsiteurl(get_site_url(false)) . "index.php?id=$slug$edit$theme$template$color$siteid"; if ($echo) { echo $url; } else { @@ -186,7 +186,7 @@ function get_header() { function get_theme_url($echo = true) { $db = getdatabase(); $site = $db->get('sites', ["sitename", "url", "theme"], ["siteid" => getsiteid()]); - $url = $site["url"] . "themes/" . SITE_THEME; + $url = formatsiteurl($site["url"]) . "themes/" . SITE_THEME; if ($echo) { echo $url; } else { @@ -206,7 +206,7 @@ function get_theme_color_url($echo = true) { if (!file_exists(__DIR__ . "/../public/themes/" . SITE_THEME . "/colors/" . $site['color'])) { $site['color'] = "default"; } - $url = $site["url"] . "themes/" . SITE_THEME . "/colors/" . $site["color"]; + $url = formatsiteurl($site["url"]) . "themes/" . SITE_THEME . "/colors/" . $site["color"]; if ($echo) { echo $url; } else { diff --git a/lib/util.php b/lib/util.php new file mode 100644 index 0000000..e8359cd --- /dev/null +++ b/lib/util.php @@ -0,0 +1,19 @@ + @@ -30,7 +31,7 @@ redirectifnotloggedin(); 'color' ]); foreach ($sites as $site) { - $theme = json_decode(file_get_contents("public/themes/".$site['theme']."/theme.json"), true); + $theme = json_decode(file_get_contents("public/themes/" . $site['theme'] . "/theme.json"), true); $themename = $theme["name"]; ?> @@ -38,7 +39,7 @@ redirectifnotloggedin(); - + diff --git a/webroot.htaccess b/webroot.htaccess new file mode 100644 index 0000000..6261c03 --- /dev/null +++ b/webroot.htaccess @@ -0,0 +1,9 @@ +RewriteEngine On +RewriteBase / + +RewriteCond %{REQUEST_FILENAME} !-d [OR] +RewriteCond %{REQUEST_URI} ^/$ +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-l + +RewriteRule (.*) /sitewriter/public/$1 [L]