1
0
mirror of https://github.com/typemill/typemill.git synced 2025-01-17 21:38:25 +01:00
php-typemill/system/Settings.php

245 lines
7.3 KiB
PHP
Raw Normal View History

2019-10-20 12:09:45 +02:00
<?php
namespace Typemill;
2020-07-04 08:06:18 +02:00
use Laminas\Permissions\Acl\Acl;
use Laminas\Permissions\Acl\Role\GenericRole as Role;
use Laminas\Permissions\Acl\Resource\GenericResource as Resource;
2019-10-20 12:09:45 +02:00
class Settings
{
public static function loadSettings()
{
$defaultSettings = self::getDefaultSettings();
$userSettings = self::getUserSettings();
$settings = $defaultSettings;
if($userSettings)
{
$settings = array_merge($defaultSettings, $userSettings);
}
2020-04-07 08:20:22 +02:00
# no individual image sizes are allowed sind 1.3.4
$settings['images'] = $defaultSettings['images'];
# we have to check if the theme has been deleted
$themefolder = $settings['rootPath'] . $settings['themeFolder'] . DIRECTORY_SEPARATOR;
# if there is no theme in settings or theme has been deleted
if(!isset($settings['theme']) OR !file_exists($themefolder . $settings['theme']))
2020-04-05 19:13:10 +02:00
{
# scan theme folder and get the first theme
$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
}
}
# 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-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',
'langattr' => 'en',
2019-10-20 12:09:45 +02:00
'startpage' => true,
'rootPath' => $rootPath,
'themeFolder' => 'themes',
2019-10-20 12:09:45 +02:00
'themeBasePath' => $rootPath,
'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,
'editor' => 'visual',
'formats' => ['markdown', 'headline', 'ulist', 'olist', 'table', 'quote', 'notice', 'image', 'video', 'file', 'toc', 'hr', 'definition', 'code'],
2019-10-20 12:09:45 +02:00
'contentFolder' => 'content',
'cache' => true,
'cachePath' => $rootPath . 'cache',
2020-07-17 11:17:42 +02:00
'version' => '1.3.8',
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-06-25 07:51:52 +02:00
public static function whichLanguage()
{
# Check which languages are available
$langs = [];
$path = __DIR__ . '/author/languages/*.yaml';
foreach (glob($path) as $filename)
{
$langs[] = basename($filename,'.yaml');
}
# Detect browser language
2020-07-15 10:53:27 +02:00
$accept_lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : false;
$lang = in_array($accept_lang, $langs) ? $accept_lang : 'en';
return $lang;
}
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;
}
2020-04-30 17:19:16 +02:00
2019-10-20 12:09:45 +02:00
public static function createSettings()
{
$yaml = new Models\WriteYaml();
2020-04-30 17:19:16 +02:00
$language = self::whichLanguage();
2020-04-30 17:19:16 +02:00
2019-10-20 12:09:45 +02:00
# create initial settings file with only setup false
2020-04-30 17:19:16 +02:00
if($yaml->updateYaml('settings', 'settings.yaml', array('setup' => false, 'language' => $language)))
2019-10-20 12:09:45 +02:00
{
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)
$allowedUserSettings = ['displayErrorDetails' => true,
2020-04-05 19:13:10 +02:00
'title' => true,
'copyright' => true,
'language' => true,
'langattr' => true,
2020-04-05 19:13:10 +02:00
'startpage' => true,
'author' => true,
'year' => true,
2020-07-15 10:53:27 +02:00
'access' => 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,
2020-07-15 10:53:27 +02:00
'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);
# write settings to yaml
2019-10-20 12:09:45 +02:00
$yaml = new Models\WriteYaml();
$yaml->updateYaml('settings', 'settings.yaml', $settings);
}
}
2020-07-04 08:06:18 +02:00
public static function loadResources()
{
return ['content',
2020-07-11 20:28:27 +02:00
'mycontent',
2020-07-04 08:06:18 +02:00
'user',
'userlist',
2020-07-11 20:28:27 +02:00
'system'];
2020-07-04 08:06:18 +02:00
}
public static function loadRolesAndPermissions()
{
$member['name'] = 'member';
2020-07-11 20:28:27 +02:00
$member['inherits'] = NULL;
2020-07-04 08:06:18 +02:00
$member['permissions'] = ['user' => ['view','update','delete']];
$author['name'] = 'author';
$author['inherits'] = 'member';
2020-07-11 20:28:27 +02:00
$author['permissions'] = ['mycontent' => ['view', 'create', 'update'],
'content' => ['view']];
2020-07-04 08:06:18 +02:00
$editor['name'] = 'editor';
$editor['inherits'] = 'author';
2020-07-11 20:28:27 +02:00
$editor['permissions'] = [ 'mycontent' => ['delete', 'publish', 'unpublish'],
'content' => ['create', 'update', 'delete', 'publish', 'unpublish']];
2020-07-04 08:06:18 +02:00
2020-07-16 12:53:08 +02:00
return ['member' => $member,'author' => $author, 'editor' => $editor];
2020-07-04 08:06:18 +02:00
}
public static function createAcl($roles, $resources)
{
$acl = new Acl();
foreach($resources as $resource)
{
$acl->addResource(new Resource($resource));
}
# add administrator role
$acl->addRole(new Role('administrator'));
$acl->allow('administrator');
# add all other roles dynamically
foreach($roles as $role)
{
$acl->addRole(new Role($role['name']), $role['inherits']);
2020-07-11 20:28:27 +02:00
foreach($role['permissions'] as $resource => $permissions)
2020-07-04 08:06:18 +02:00
{
$acl->allow($role['name'], $resource, $permissions);
}
}
return $acl;
}
}