Skip to content

Instantly share code, notes, and snippets.

@ringmaster
Last active December 12, 2015 12:19
Show Gist options
  • Save ringmaster/4770956 to your computer and use it in GitHub Desktop.
Save ringmaster/4770956 to your computer and use it in GitHub Desktop.
Host many Habaris from one directory, store configurations in a database
<?php if ( !defined( 'HABARI_PATH' ) ) { die( 'No direct access' ); }
/** User-settable stuff */
define('DEBUG', false);
define('SITEDATA_GUID', 'a random string'); // this is used for site guid for the cache and salt on the cache keys
Config::set('cache_class', 'APCCache'); // This should work with any Habari cache class that can run, including filecache
$cache_enabled = true;
/** Don't change the below */
HabariLocale::_t(''); // Loads the Locale class so that _t() can be used within potential errors
// Cache class needs a GUID for the site for grouped settings
Config::set(
'default_options',
array(
'GUID' => SITEDATA_GUID // This will get overridden when the real site options load
)
);
// Pick a key to use in the cache for this site's config array
$cache_key = 'sitedata_configs_' . md5($_SERVER['HTTP_HOST'] . SITEDATA_GUID);
// If we can get the config value from the cache, do so
if($cache_enabled && Cache::has($cache_key) && !isset($_GET['clear_config_cache'])) {
$configs = Cache::get($cache_key);
}
// If at this point there's nothing useful to use for the config from the cache, get it from the sitedata database
if(!isset($configs['connection_string'])) {
DB::connect('sqlite:' . __DIR__ . '/config/sitedata.db', '', '');
$configs = DB::get_keyvalue('SELECT k, v FROM sitedata WHERE domain = :domain', array($_SERVER['HTTP_HOST']));
DB::disconnect();
// If cache capabilities are enabled, stuff the found config details in there
if($cache_enabled) {
Cache::set($cache_key, $configs);
}
}
// Store all the config values from the database into the Config object
foreach($configs as $key => $value) {
Config::set($key, $value);
}
// Clear out any options from the internal options data that we might have needed to make things go this far
Options::clear_cache();
?>
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE sitedata (k varchar(255), v text, domain varchar(255));
INSERT INTO "sitedata" VALUES('connection_string','sqlite:habari.db','habari09.lise');
INSERT INTO "sitedata" VALUES('username','','habari09.lise');
INSERT INTO "sitedata" VALUES('password','','habari09.lise');
INSERT INTO "sitedata" VALUES('prefix','','habari09.lise');
CREATE INDEX domain_idx on sitedata (domain);
COMMIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment