Add proper site detection from URL

master
Skylar Ittner 6 years ago
parent b98a18adac
commit 3db76c4d49

@ -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;

@ -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;
}

@ -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 {

@ -0,0 +1,19 @@
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
function formatsiteurl($url) {
if (substr($url, 0) != "/") {
if (strpos($url, "http://") !== 0 && strpos($url, "https://") !== 0) {
$url = "http://$url";
}
}
if (substr($url, -1) != "/") {
$url = $url . "/";
}
return $url;
}

@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
require_once __DIR__ . '/../required.php';
require_once __DIR__ . '/../lib/util.php';
redirectifnotloggedin();
?>
@ -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"];
?>
<tr>
@ -38,7 +39,7 @@ redirectifnotloggedin();
<td>
<a class="btn btn-primary btn-sm" href="app.php?page=editor&siteid=<?php echo $site['siteid']; ?>"><i class="fas fa-edit"></i> <?php lang("editor"); ?></a>
<a class="btn btn-secondary btn-sm" href="app.php?page=sitesettings&siteid=<?php echo $site['siteid']; ?>"><i class="fas fa-cog"></i> <?php lang("settings"); ?></a>
<a class="btn btn-info btn-sm" href="<?php echo $site['url']; ?>" target="_BLANK"><i class="fas fa-eye"></i> <?php lang("view"); ?></a>
<a class="btn btn-info btn-sm" href="<?php echo formatsiteurl($site['url']); ?>" target="_BLANK"><i class="fas fa-eye"></i> <?php lang("view"); ?></a>
</td>
<td><?php echo $site['sitename']; ?></td>
<td><?php echo $site['url']; ?></td>

@ -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]
Loading…
Cancel
Save