Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Detect subdirectory install and set subdirectory path in env.php and .htaccess for smooth subdir install

givanz28f49fac6d90
diff --git a/cli.php b/cli.php
index 225c1c6..c06e5f2 100755
--- a/cli.php
+++ b/cli.php
@@ -40,13 +40,13 @@ php cli.php admin module=plugin/plugins action=checkPluginAndActivate plugin=mar
php cli.php admin module=content/posts action=delete post_id=1
#fresh install MySQL
-php cli.php install host=127.0.0.1 user=root password= database=vvveb admin[email][email protected] admin[password]=admin
+php cli.php install host=127.0.0.1 user=root password= database=vvveb admin[email][email protected] admin[username]=admin admin[password]=admin
#fresh install PgSQL
-php cli.php install engine=pgsql host=127.0.0.1 user=postgres password= database=vvveb admin[email][email protected] admin[password]=admin
+php cli.php install engine=pgsql host=127.0.0.1 user=postgres password= database=vvveb admin[email][email protected] admin[username]=admin admin[password]=admin
#fresh install SQLite
-php cli.php install engine=sqlite admin[email][email protected] admin[password]=admin
+php cli.php install engine=sqlite admin[email][email protected] admin[username]=admin admin[password]=admin
#import markdown posts from folder /docs/user into site with id 5
php cli.php admin module=plugins/markdown-import/settings action=import site_id=5 settings[path]=/docs/user
@@ -66,6 +66,7 @@ php cli.php app module=cron
php cli.php app request_uri=/hello-world
\n
MSG;
+namespace Vvveb;
define('V_VERSION', '0.0.1');
@@ -73,6 +74,10 @@ function is_installed() {
return file_exists(DIR_ROOT . 'config' . DS . 'db.php');
}
+function detectSubDir() {
+ return false;
+}
+
$params = implode('&', array_slice($argv, 2));
parse_str($params, $_GET);
parse_str($params, $_POST);
diff --git a/index.php b/index.php
index 99bb73c..f616626 100755
--- a/index.php
+++ b/index.php
@@ -39,7 +39,19 @@ function is_installed() {
return file_exists(DIR_ROOT . 'config' . DS . 'db.php');
}
-$installPathRedirect = (V_SUBDIR_INSTALL ? V_SUBDIR_INSTALL : '') . '/install/index.php';
+if (! isset($FILE)) {
+ $FILE = __FILE__;
+}
+
+function detectSubDir() {
+ global $FILE;
+
+ $uri = $_SERVER['DOCUMENT_URI'] ?? $_SERVER['SCRIPT_NAME'] ?? '';
+ $path = '/' . trim(str_replace([DIR_ROOT, '\\', 'public'], ['', '/', ''], $FILE), '/ ');
+ return str_replace($path, '', $uri);
+}
+
+$subdir = (V_SUBDIR_INSTALL ? V_SUBDIR_INSTALL : detectSubDir());
if (! defined('APP')) {
if (is_installed()) {
@@ -60,14 +72,14 @@ if (! defined('APP')) {
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], 'install') === false) {
//avoid redirect loop
- die(header("Location: $installPathRedirect"));
+ die(header("Location: $subdir/install/index.php"));
}
}
} elseif (! is_installed() && (! defined('APP') || APP != 'install')) {
defined('APP') || define('APP', 'install');
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], 'install') === false) {
- die(header("Location: $installPathRedirect"));
+ die(header("Location: $subdir/install/index.php"));
}
}
@@ -80,8 +92,8 @@ if (! isset($PUBLIC_THEME_PATH)) {
}
if (! defined('PUBLIC_PATH')) {
- define('PUBLIC_PATH', (V_SUBDIR_INSTALL ? V_SUBDIR_INSTALL : '') . $PUBLIC_PATH);
- define('PUBLIC_THEME_PATH', (V_SUBDIR_INSTALL ? V_SUBDIR_INSTALL : '') . $PUBLIC_THEME_PATH);
+ define('PUBLIC_PATH', $subdir . $PUBLIC_PATH);
+ define('PUBLIC_THEME_PATH', $subdir . $PUBLIC_THEME_PATH);
}
require_once DIR_SYSTEM . 'core/startup.php';
diff --git a/install/controller/index.php b/install/controller/index.php
index fd352b0..2af7b50 100755
--- a/install/controller/index.php
+++ b/install/controller/index.php
@@ -46,6 +46,8 @@ define('DEFAULT_LANG', 'en_US');
#[\AllowDynamicProperties]
class Index extends Base {
private $config = ['engine' => 'mysqli', 'host' => '127.0.0.1', 'database' => 'vvveb', 'user' => 'root', 'password' => '', 'port' => null, 'prefix' => ''];
+
+ private $subdir = '';
function __construct() {
if (! ($lang = sess('language'))) {
@@ -59,6 +61,8 @@ class Index extends Base {
if ($lang) {
setLanguage($lang);
}
+
+ $this->subdir = (V_SUBDIR_INSTALL ? V_SUBDIR_INSTALL : \Vvveb\detectSubDir());
if (\Vvveb\is_installed()) {
$admin = false;
@@ -76,7 +80,7 @@ class Index extends Base {
}
if ($admin && isset($admin['status']) && $admin['status'] == '1') {
- header('Location: /');
+ header('Location: ' . $this->subdir. '/');
die(__('Already installed! To reinstall remove config/db.php') . "\n");
} else {
@@ -113,6 +117,16 @@ class Index extends Base {
return $notMet;
}
+ function replaceInFile($file, $search, $replace) {
+ $content = file_get_contents($file);
+
+ if ($content) {
+ $content = str_replace($search, $replace, $content);
+
+ return file_put_contents($file, $content);
+ }
+ }
+
private function writeConfig($data) {
return \Vvveb\setConfig('db', $data);
$configFile = DIR_ROOT . 'config/db.php';
@@ -367,7 +381,10 @@ class Index extends Base {
@\Vvveb\setConfig($app . '.key', Str::random(32));
}
- if (V_SUBDIR_INSTALL) {
+ $subdir = $this->request->post['subdir'] ?? $this->subdir ?? false;
+
+ if ($subdir) {
+ $subdir = '/' . trim($subdir, '/ ');
//add subdir path to menu links
$menus = new menuSQL();
@@ -375,10 +392,14 @@ class Index extends Base {
$menuItems = $menus->get(['menu_id' => $menu_id, 'language_id' => 1])['menu'] ?? [];
foreach ($menuItems as $menuItem) {
- $data = ['url' => V_SUBDIR_INSTALL . $menuItem['url'], 'menu_item_content' => []];
+ $data = ['url' => $this->subdir . $menuItem['url'], 'menu_item_content' => []];
$menus->editMenuItem(['menu_item' => $data, 'menu_item_id' => $menuItem['menu_item_id']]);
}
}
+
+ //try to set subdir in env.php and .htaccess
+ $this->replaceInFile(DIR_ROOT . 'env.php', "define('V_SUBDIR_INSTALL', false" , "define('V_SUBDIR_INSTALL', '{$subdir}'");
+ $this->replaceInFile(DIR_ROOT . '.htaccess', 'RewriteRule ^ index.php [L]' , "RewriteRule ^ {$subdir}/index.php [L]");
}
if ($error) {
@@ -388,7 +409,7 @@ class Index extends Base {
$success = __('Installation succesful!');
$this->view->success[] = $success;
$admin_path = \Vvveb\adminPath();
- $admin_path = str_replace(V_SUBDIR_INSTALL, '', $admin_path);
+ $admin_path = str_replace($this->subdir, '', $admin_path);
$location = preg_replace('@/install.*$@', $admin_path . "/index.php?success=$success&errors=$error", ($_SERVER['REQUEST_URI'] ?? ''));
header("Location: $location");
diff --git a/install/index.php b/install/index.php
index ad4256e..d914dfe 100755
--- a/install/index.php
+++ b/install/index.php
@@ -48,4 +48,8 @@ if ($extensions) {
echo "<div style='text-align:center;margin:2rem;'>$extensions</div>";
}
+if (! isset($FILE)) {
+ $FILE = __FILE__;
+}
+
return include __DIR__ . '/../index.php';
diff --git a/public/vadmin/index.php b/public/vadmin/index.php
index 43b2a00..9441e8e 100755
--- a/public/vadmin/index.php
+++ b/public/vadmin/index.php
@@ -23,4 +23,5 @@
$PUBLIC_PATH = '/';
$PUBLIC_THEME_PATH = '/admin/';
+$FILE = __FILE__;
include __DIR__ . '/../../admin/index.php';
diff --git a/system/core/frontcontroller.php b/system/core/frontcontroller.php
index ec6a126..16cda05 100755
--- a/system/core/frontcontroller.php
+++ b/system/core/frontcontroller.php
@@ -79,8 +79,15 @@ class FrontController {
return self :: $status;
}
+ static function setStatus($status) {
+ return self :: $status = $status;
+ }
+
static private function callAction($controller, $action = 'index') {
- //header(' ', true, $statusCode);
+ if ($statusCode !== 200) {
+ header(' ', true, $statusCode);
+ }
+
if (include_once DIR_APP . DS . 'controller' . DS . "$controller.php") {
$controller = 'Vvveb\Controller\\' . $controller;
self :: $moduleName = $moduleName = $controller;
@@ -166,6 +173,7 @@ class FrontController {
$view->setType($response->getType());
$view->render();
+ die();
//return $response->output();
//self :: closeConnections();
//$view->render($service, true, $service);
@@ -369,7 +377,7 @@ class FrontController {
$uri = $_SERVER['REQUEST_URI'] ?? '';
if (V_SUBDIR_INSTALL) {
- $uri = str_replace(V_SUBDIR_INSTALL, '', $uri);
+ $uri = substr($uri, strlen(V_SUBDIR_INSTALL));
}
$uri = preg_replace('/\?.*$/', '', $uri);