2019-10-20 12:09:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Typemill;
|
|
|
|
|
|
|
|
class Settings
|
|
|
|
{
|
|
|
|
public static function loadSettings()
|
|
|
|
{
|
|
|
|
$defaultSettings = self::getDefaultSettings();
|
|
|
|
$userSettings = self::getUserSettings();
|
|
|
|
|
|
|
|
$settings = $defaultSettings;
|
|
|
|
|
|
|
|
if($userSettings)
|
|
|
|
{
|
|
|
|
$settings = array_merge($defaultSettings, $userSettings);
|
|
|
|
}
|
2019-11-24 07:25:04 +01:00
|
|
|
|
2020-04-07 08:20:22 +02:00
|
|
|
# no individual image sizes are allowed sind 1.3.4
|
|
|
|
$settings['images'] = $defaultSettings['images'];
|
|
|
|
|
2020-04-05 19:13:10 +02:00
|
|
|
# if there is no theme set
|
|
|
|
if(!isset($settings['theme']))
|
|
|
|
{
|
|
|
|
# scan theme folder and get the first theme
|
|
|
|
$themefolder = $settings['rootPath'] . $settings['themeFolder'] . DIRECTORY_SEPARATOR;
|
|
|
|
$themes = array_diff(scandir($themefolder), array('..', '.'));
|
|
|
|
$firsttheme = reset($themes);
|
|
|
|
|
2020-04-20 19:21:56 +02:00
|
|
|
# if there is a theme with an index.twig-file
|
|
|
|
if($firsttheme && file_exists($themefolder . $firsttheme . DIRECTORY_SEPARATOR . 'index.twig'))
|
2020-04-05 19:13:10 +02:00
|
|
|
{
|
|
|
|
$settings['theme'] = $firsttheme;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-20 19:21:56 +02:00
|
|
|
die('You need at least one theme with an index.twig-file in your theme-folder.');
|
2020-04-05 19:13:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:57 +01:00
|
|
|
# i18n
|
2020-01-20 19:41:57 +01:00
|
|
|
# load the strings of the set language
|
|
|
|
$language = $settings['language'];
|
|
|
|
$settings['labels'] = self::getLanguageLabels($language);
|
2020-01-09 15:46:20 +01:00
|
|
|
|
2019-11-24 07:25:04 +01:00
|
|
|
# We know the used theme now so create the theme path
|
|
|
|
$settings['themePath'] = $settings['rootPath'] . $settings['themeFolder'] . DIRECTORY_SEPARATOR . $settings['theme'];
|
|
|
|
|
|
|
|
# if there are no theme settings yet (e.g. no setup yet) use default theme settings
|
|
|
|
if(!isset($settings['themes']))
|
|
|
|
{
|
|
|
|
$themeSettings = self::getObjectSettings('themes', $settings['theme']);
|
|
|
|
$settings['themes'][$settings['theme']] = isset($themeSettings['settings']) ? $themeSettings['settings'] : false;
|
2020-04-05 19:13:10 +02:00
|
|
|
}
|
2019-11-24 07:25:04 +01:00
|
|
|
|
2019-10-20 12:09:45 +02:00
|
|
|
return array('settings' => $settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDefaultSettings()
|
|
|
|
{
|
|
|
|
$rootPath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
return [
|
|
|
|
'determineRouteBeforeAppMiddleware' => true,
|
|
|
|
'displayErrorDetails' => false,
|
|
|
|
'title' => 'TYPEMILL',
|
|
|
|
'author' => 'Unknown',
|
|
|
|
'copyright' => 'Copyright',
|
|
|
|
'language' => 'en',
|
|
|
|
'startpage' => true,
|
|
|
|
'rootPath' => $rootPath,
|
2019-11-24 07:25:04 +01:00
|
|
|
'themeFolder' => 'themes',
|
2019-10-20 12:09:45 +02:00
|
|
|
'themeBasePath' => $rootPath,
|
2019-11-24 07:25:04 +01:00
|
|
|
'themePath' => '',
|
2019-10-20 12:09:45 +02:00
|
|
|
'settingsPath' => $rootPath . 'settings',
|
|
|
|
'userPath' => $rootPath . 'settings' . DIRECTORY_SEPARATOR . 'users',
|
|
|
|
'authorPath' => __DIR__ . DIRECTORY_SEPARATOR . 'author' . DIRECTORY_SEPARATOR,
|
2019-11-24 07:25:04 +01:00
|
|
|
'editor' => 'visual',
|
2020-04-05 19:13:10 +02:00
|
|
|
'formats' => ['markdown', 'headline', 'ulist', 'olist', 'table', 'quote', 'image', 'video', 'file', 'toc', 'hr', 'definition', 'code'],
|
2019-10-20 12:09:45 +02:00
|
|
|
'contentFolder' => 'content',
|
|
|
|
'cache' => true,
|
|
|
|
'cachePath' => $rootPath . 'cache',
|
2020-04-21 08:01:28 +02:00
|
|
|
'version' => '1.3.5',
|
2019-10-20 12:09:45 +02:00
|
|
|
'setup' => true,
|
|
|
|
'welcome' => true,
|
2020-04-05 19:13:10 +02:00
|
|
|
'images' => ['live' => ['width' => 820], 'thumbs' => ['width' => 250, 'height' => 150]],
|
2019-10-20 12:09:45 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getUserSettings()
|
|
|
|
{
|
|
|
|
$yaml = new Models\WriteYaml();
|
|
|
|
|
|
|
|
$userSettings = $yaml->getYaml('settings', 'settings.yaml');
|
|
|
|
|
|
|
|
return $userSettings;
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:46:20 +01:00
|
|
|
|
2020-01-20 18:52:57 +01:00
|
|
|
# i18n
|
|
|
|
public static function getLanguageLabels($language)
|
2020-01-09 15:46:20 +01:00
|
|
|
{
|
2020-01-20 18:52:57 +01:00
|
|
|
# if not present, set the English language
|
|
|
|
if( empty($language) )
|
|
|
|
{
|
|
|
|
$language = 'en';
|
|
|
|
}
|
2020-01-09 15:46:20 +01:00
|
|
|
|
2020-01-20 18:52:57 +01:00
|
|
|
# load the strings of the set language
|
2020-01-09 15:46:20 +01:00
|
|
|
$yaml = new Models\WriteYaml();
|
|
|
|
$labels = $yaml->getYaml('settings/languages', $language.'.yaml');
|
|
|
|
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
|
2020-01-13 15:50:20 +01:00
|
|
|
public static function getVuejsLabels($language)
|
|
|
|
{
|
2020-01-13 16:09:53 +01:00
|
|
|
if( empty($language) ){
|
|
|
|
$language = 'en';
|
|
|
|
}
|
2020-01-13 15:50:20 +01:00
|
|
|
|
|
|
|
// load the strings of the set language
|
|
|
|
$yaml = new Models\WriteYaml();
|
2020-01-13 16:09:53 +01:00
|
|
|
$labels = $yaml->getYaml('settings/languages', 'vuejs-'.$language.'.yaml');
|
2020-01-13 15:50:20 +01:00
|
|
|
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:46:20 +01:00
|
|
|
|
2019-10-20 12:09:45 +02:00
|
|
|
public static function getObjectSettings($objectType, $objectName)
|
|
|
|
{
|
|
|
|
$yaml = new Models\WriteYaml();
|
|
|
|
|
|
|
|
$objectFolder = $objectType . DIRECTORY_SEPARATOR . $objectName;
|
|
|
|
$objectFile = $objectName . '.yaml';
|
|
|
|
$objectSettings = $yaml->getYaml($objectFolder, $objectFile);
|
|
|
|
|
|
|
|
return $objectSettings;
|
|
|
|
}
|
2019-12-31 15:57:45 +01:00
|
|
|
|
2019-10-20 12:09:45 +02:00
|
|
|
public static function createSettings()
|
|
|
|
{
|
|
|
|
$yaml = new Models\WriteYaml();
|
|
|
|
|
|
|
|
# create initial settings file with only setup false
|
|
|
|
if($yaml->updateYaml('settings', 'settings.yaml', array('setup' => false)))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function updateSettings($settings)
|
|
|
|
{
|
|
|
|
# only allow if usersettings already exists (setup has been done)
|
|
|
|
$userSettings = self::getUserSettings();
|
|
|
|
|
|
|
|
if($userSettings)
|
|
|
|
{
|
2020-04-05 19:13:10 +02:00
|
|
|
# whitelist settings that can be stored in usersettings (values are not relevant here, only keys)
|
2020-01-20 18:52:57 +01:00
|
|
|
$allowedUserSettings = ['displayErrorDetails' => true,
|
2020-04-05 19:13:10 +02:00
|
|
|
'title' => true,
|
|
|
|
'copyright' => true,
|
|
|
|
'language' => true,
|
|
|
|
'startpage' => true,
|
|
|
|
'author' => true,
|
|
|
|
'year' => true,
|
2020-04-20 19:21:56 +02:00
|
|
|
'headlineanchors' => true,
|
2020-04-05 19:13:10 +02:00
|
|
|
'theme' => true,
|
|
|
|
'editor' => true,
|
|
|
|
'formats' => true,
|
|
|
|
'setup' => true,
|
|
|
|
'welcome' => true,
|
|
|
|
'images' => true,
|
|
|
|
'plugins' => true,
|
|
|
|
'themes' => true,
|
|
|
|
'latestVersion' => true,
|
|
|
|
'logo' => true,
|
|
|
|
'favicon' => true,
|
2019-10-20 12:09:45 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
# cleanup the existing usersettings
|
|
|
|
$userSettings = array_intersect_key($userSettings, $allowedUserSettings);
|
|
|
|
|
|
|
|
# cleanup the new settings passed as an argument
|
|
|
|
$settings = array_intersect_key($settings, $allowedUserSettings);
|
|
|
|
|
|
|
|
# merge usersettings with new settings
|
|
|
|
$settings = array_merge($userSettings, $settings);
|
|
|
|
|
2020-01-20 18:52:57 +01:00
|
|
|
# write settings to yaml
|
2019-10-20 12:09:45 +02:00
|
|
|
$yaml = new Models\WriteYaml();
|
|
|
|
$yaml->updateYaml('settings', 'settings.yaml', $settings);
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 16:09:06 +01:00
|
|
|
}
|