Skip to main content
Topic: Multi Tenancy - Multi Forums Via Various Domains In A Single Installation (Read 22699 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Re: Multi Tenancy - Multi Forums Via Various Domains In A Single Installation

Reply #45

I've just finally realized just what this addon really does, very cool  8)

Re: Multi Tenancy - Multi Forums Via Various Domains In A Single Installation

Reply #46

If one is used to multiisite in wordpress, this addon idea is something like that.

Re: Multi Tenancy - Multi Forums Via Various Domains In A Single Installation

Reply #47

Regarding modifications in the bootstrap.php, basically I am thinking to replace the following code:
Code: [Select]
		// Where the Settings.php file is located
$settings_loc = __DIR__ . '/Settings.php';

With this:
Code: [Select]
		global $dboardurl, $dhost, $tboardurl, $thost;

// Get default $boardurl and $cookiename from default Settings.php
$ds_loc = __DIR__ . '/Settings.php';
$bu_ori = array();
$handle = @fopen($ds_loc, 'r');
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle);
if(strpos($buffer, '$boardurl') !== FALSE)
$bu_ori[] = $buffer;
if(strpos($buffer, '$cookiename') !== FALSE)
$cn_ori[] = $buffer;
}
fclose($handle);
}
preg_match('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $bu_ori[0], $bu_match);
$dboardurl = $bu_match[0];
$dhost = parse_url($dboardurl, PHP_URL_HOST) . (!empty(parse_url($dboardurl, PHP_URL_PATH)) ? parse_url($dboardurl, PHP_URL_PATH) : '');
preg_match("/(?:(?:\"(?:\\\\\"|[^\"])+\")|(?:'(?:\\\'|[^'])+'))/is", $cn_ori[0], $cn_match);
$dcookie = str_replace("'", '', $cn_match[0]);

// Now define tenancy host and boardurl
$thost = $_SERVER['HTTP_HOST'] . (!empty(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME'])) ? str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']) : '');
$tboardurl = parse_url($dboardurl, PHP_URL_SCHEME) . '://' . $thost;

// Where the Settings.php file is located
if ($dhost === $thost)
$settings_loc = $ds_loc;
else {
$settings_loc = __DIR__ . '/.tenancy/' . str_replace('/', '.', $thost) . '.php';
if (!file_exists($settings_loc)) {
copy($ds_loc, $settings_loc);
file_put_contents($settings_loc, str_replace($dhost, $thost, file_get_contents($settings_loc)));
file_put_contents($settings_loc, str_replace($dcookie, uniqid(), file_get_contents($settings_loc)));
}
}

This will basically check the default $boardurl and $cookiename in default Settings.php and create a new settings (copied from default) for each tenant in .tenancy folder with its own tenancy $boardurl and $cookiename.

Edited: I think this modification can be done via bootsrap.php extension like "class TenantBootstrap extend Bootstrap" something and then overwrite the whole "private function loadSettingsFile()" overriding with "public function bringUpBasics()" (since private cannot be overriden by child class or something like that) but I am not so sure on how to achieve that in ElkArte yet.

I think I might attend the later approach, later.
Last Edit: April 19, 2019, 12:41:58 am by ahrasis

Re: Multi Tenancy - Multi Forums Via Various Domains In A Single Installation

Reply #48

Ok. The above modifications to bootstrap.php are kinda ugly, so I rewrote again.

This time I modified the following:
Code: [Select]
		else
{
require_once($settings_loc);
}
}

To this (which I think is cleaner, easier to read and comprehend):
Code: [Select]
		else
{
require_once($settings_loc);
}
$this->resetBoardUrl();
}

/**
* Reset $boardurl in Settings.php
*/
private function resetBoardUrl()
{
global $boardurl, $dboardurl, $dhost, $tboardurl, $thost;

// Default $boardurl and host
if (!empty(parse_url($boardurl, PHP_URL_PATH))
$dhost = parse_url($boardurl, PHP_URL_HOST) . parse_url($boardurl, PHP_URL_PATH);
else
$dhost = parse_url($boardurl, PHP_URL_HOST);
$dboardurl = $boardurl;

// Tenancy $boardurl and host
if (!empty(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME'])))
$thost = $_SERVER['HTTP_HOST'] .  str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']);
else
$thost = $_SERVER['HTTP_HOST'];
$tboardurl = 'http' . (parse_url($boardurl, PHP_URL_SCHEME) === 'https' ? 's' : '') . '://' . $thost;

// Switch to tenancy $boardurl if default is not the same
if ($dboardurl != $tboardurl | $dhost != $thost)
$boardurl = $tboardurl;
}

This code does not need a settings file, so I am in dilemma whether to create it for each of the tenant or not. I can reuse the previous code in tenancy.admin.php and there tenancy.template.php can create the file upon adding a tenant.

Re: Multi Tenancy - Multi Forums Via Various Domains In A Single Installation

Reply #49

Installing ElkArte 1.1.7 (testing) package and running it with this addon seems working good so far. Hopefully nothing much is changed until ElkArte 2.0.0 or I will have to spend some times revisiting - fixing or re-writing this addon. I really hope ElkArte 2.0.0 is multisite ready like WP but that is hoping too much, I think.