2016-09-06 04:00:00 +00:00
|
|
|
<?php
|
|
|
|
|
2022-02-16 04:08:28 +00:00
|
|
|
namespace Code\Module\Admin;
|
2016-09-06 04:00:00 +00:00
|
|
|
|
2021-12-02 22:33:36 +00:00
|
|
|
use App;
|
|
|
|
use Michelf\MarkdownExtra;
|
2022-02-16 04:08:28 +00:00
|
|
|
use Code\Render\Theme;
|
2022-02-12 20:43:29 +00:00
|
|
|
|
2016-09-06 04:00:00 +00:00
|
|
|
|
2017-02-25 18:12:41 +00:00
|
|
|
/**
|
|
|
|
* @brief Admin area theme settings.
|
|
|
|
*/
|
2021-12-02 23:02:31 +00:00
|
|
|
class Themes
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function post()
|
|
|
|
{
|
|
|
|
|
|
|
|
$theme = argv(2);
|
|
|
|
if (is_file("view/theme/$theme/php/config.php")) {
|
|
|
|
require_once("view/theme/$theme/php/config.php");
|
|
|
|
/// @FIXME add parent theme if derived
|
|
|
|
if (function_exists('theme_admin_post')) {
|
|
|
|
theme_admin_post($a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info(t('Theme settings updated.'));
|
2021-12-03 03:01:39 +00:00
|
|
|
if (is_ajax()) {
|
2021-12-02 23:02:31 +00:00
|
|
|
return;
|
2021-12-03 03:01:39 +00:00
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
|
|
|
|
goaway(z_root() . '/admin/themes/' . $theme);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Themes admin page.
|
|
|
|
*
|
|
|
|
* @return string with parsed HTML
|
|
|
|
*/
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
$allowed_themes_str = get_config('system', 'allowed_themes');
|
|
|
|
$allowed_themes_raw = explode(',', $allowed_themes_str);
|
|
|
|
$allowed_themes = [];
|
2021-12-03 03:01:39 +00:00
|
|
|
if (count($allowed_themes_raw)) {
|
|
|
|
foreach ($allowed_themes_raw as $x) {
|
|
|
|
if (strlen(trim($x))) {
|
2021-12-02 23:02:31 +00:00
|
|
|
$allowed_themes[] = trim($x);
|
2021-12-03 03:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
|
|
|
|
$themes = [];
|
|
|
|
$files = glob('view/theme/*');
|
|
|
|
if ($files) {
|
|
|
|
foreach ($files as $file) {
|
2022-03-07 05:31:28 +00:00
|
|
|
if (! is_dir($file)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
$f = basename($file);
|
|
|
|
$is_experimental = intval(file_exists($file . '/.experimental'));
|
|
|
|
$is_supported = 1 - (intval(file_exists($file . '/.unsupported'))); // Is not used yet
|
|
|
|
$is_allowed = intval(in_array($f, $allowed_themes));
|
|
|
|
$themes[] = array('name' => $f, 'experimental' => $is_experimental, 'supported' => $is_supported, 'allowed' => $is_allowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count($themes)) {
|
|
|
|
notice(t('No themes found.'));
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Single theme
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (App::$argc == 3) {
|
|
|
|
$theme = App::$argv[2];
|
|
|
|
if (!is_dir("view/theme/$theme")) {
|
|
|
|
notice(t("Item not found."));
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x($_GET, "a") && $_GET['a'] == "t") {
|
|
|
|
check_form_security_token_redirectOnErr('/admin/themes', 'admin_themes', 't');
|
|
|
|
|
|
|
|
// Toggle theme status
|
|
|
|
|
|
|
|
$this->toggle_theme($themes, $theme, $result);
|
|
|
|
$s = $this->rebuild_theme_table($themes);
|
2021-12-03 03:01:39 +00:00
|
|
|
if ($result) {
|
2021-12-02 23:02:31 +00:00
|
|
|
info(sprintf('Theme %s enabled.', $theme));
|
2021-12-03 03:01:39 +00:00
|
|
|
} else {
|
2021-12-02 23:02:31 +00:00
|
|
|
info(sprintf('Theme %s disabled.', $theme));
|
2021-12-03 03:01:39 +00:00
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
|
|
|
|
set_config('system', 'allowed_themes', $s);
|
|
|
|
goaway(z_root() . '/admin/themes');
|
|
|
|
}
|
|
|
|
|
|
|
|
// display theme details
|
|
|
|
|
|
|
|
if ($this->theme_status($themes, $theme)) {
|
|
|
|
$status = "on";
|
|
|
|
$action = t("Disable");
|
|
|
|
} else {
|
|
|
|
$status = "off";
|
|
|
|
$action = t("Enable");
|
|
|
|
}
|
|
|
|
|
2021-12-03 03:01:39 +00:00
|
|
|
$readme = null;
|
2021-12-02 23:02:31 +00:00
|
|
|
if (is_file("view/theme/$theme/README.md")) {
|
|
|
|
$readme = file_get_contents("view/theme/$theme/README.md");
|
|
|
|
$readme = MarkdownExtra::defaultTransform($readme);
|
2021-12-03 03:01:39 +00:00
|
|
|
} elseif (is_file("view/theme/$theme/README")) {
|
2021-12-02 23:02:31 +00:00
|
|
|
$readme = '<pre>' . file_get_contents("view/theme/$theme/README") . '</pre>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$admin_form = '';
|
|
|
|
if (is_file("view/theme/$theme/php/config.php")) {
|
|
|
|
require_once("view/theme/$theme/php/config.php");
|
|
|
|
if (function_exists("theme_admin")) {
|
|
|
|
$admin_form = theme_admin($a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 21:00:41 +00:00
|
|
|
$screenshot = array(Theme::get_screenshot($theme), t('Screenshot'));
|
2021-12-03 03:01:39 +00:00
|
|
|
if (!stristr($screenshot[0], $theme)) {
|
2021-12-02 23:02:31 +00:00
|
|
|
$screenshot = null;
|
2021-12-03 03:01:39 +00:00
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
|
2022-02-12 20:43:29 +00:00
|
|
|
$t = Theme::get_template('admin_plugins_details.tpl');
|
2021-12-02 23:02:31 +00:00
|
|
|
return replace_macros($t, array(
|
|
|
|
'$title' => t('Administration'),
|
|
|
|
'$page' => t('Themes'),
|
|
|
|
'$toggle' => t('Toggle'),
|
|
|
|
'$settings' => t('Settings'),
|
|
|
|
'$baseurl' => z_root(),
|
|
|
|
|
|
|
|
'$plugin' => $theme,
|
|
|
|
'$status' => $status,
|
|
|
|
'$action' => $action,
|
2022-02-24 21:00:41 +00:00
|
|
|
'$info' => Theme::get_info($theme),
|
2021-12-02 23:02:31 +00:00
|
|
|
'$function' => 'themes',
|
|
|
|
'$admin_form' => $admin_form,
|
|
|
|
'$str_author' => t('Author: '),
|
|
|
|
'$str_maintainer' => t('Maintainer: '),
|
|
|
|
'$screenshot' => $screenshot,
|
|
|
|
'$readme' => $readme,
|
|
|
|
|
|
|
|
'$form_security_token' => get_form_security_token('admin_themes'),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List themes
|
|
|
|
*/
|
|
|
|
|
|
|
|
$xthemes = [];
|
|
|
|
if ($themes) {
|
|
|
|
foreach ($themes as $th) {
|
2022-02-20 08:12:31 +00:00
|
|
|
$xthemes[] = array($th['name'], (($th['allowed']) ? "on" : "off"), Theme::get_info($th['name']));
|
2021-12-02 23:02:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:43:29 +00:00
|
|
|
$t = Theme::get_template('admin_plugins.tpl');
|
2021-12-02 23:02:31 +00:00
|
|
|
return replace_macros($t, array(
|
|
|
|
'$title' => t('Administration'),
|
|
|
|
'$page' => t('Themes'),
|
|
|
|
'$submit' => t('Submit'),
|
|
|
|
'$baseurl' => z_root(),
|
|
|
|
'$function' => 'themes',
|
|
|
|
'$plugins' => $xthemes,
|
|
|
|
'$experimental' => t('[Experimental]'),
|
|
|
|
'$unsupported' => t('[Unsupported]'),
|
|
|
|
'$form_security_token' => get_form_security_token('admin_themes'),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Toggle a theme.
|
|
|
|
*
|
|
|
|
* @param array &$themes
|
|
|
|
* @param[in] string $th
|
|
|
|
* @param[out] int &$result
|
|
|
|
*/
|
|
|
|
public function toggle_theme(&$themes, $th, &$result)
|
|
|
|
{
|
|
|
|
for ($x = 0; $x < count($themes); $x++) {
|
|
|
|
if ($themes[$x]['name'] === $th) {
|
|
|
|
if ($themes[$x]['allowed']) {
|
|
|
|
$themes[$x]['allowed'] = 0;
|
|
|
|
$result = 0;
|
|
|
|
} else {
|
|
|
|
$themes[$x]['allowed'] = 1;
|
|
|
|
$result = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $themes
|
|
|
|
* @param string $th
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function theme_status($themes, $th)
|
|
|
|
{
|
|
|
|
for ($x = 0; $x < count($themes); $x++) {
|
|
|
|
if ($themes[$x]['name'] === $th) {
|
|
|
|
if ($themes[$x]['allowed']) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $themes
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function rebuild_theme_table($themes)
|
|
|
|
{
|
|
|
|
$o = '';
|
|
|
|
if (count($themes)) {
|
|
|
|
foreach ($themes as $th) {
|
|
|
|
if ($th['allowed']) {
|
2021-12-03 03:01:39 +00:00
|
|
|
if (strlen($o)) {
|
2021-12-02 23:02:31 +00:00
|
|
|
$o .= ',';
|
2021-12-03 03:01:39 +00:00
|
|
|
}
|
2021-12-02 23:02:31 +00:00
|
|
|
$o .= $th['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $o;
|
|
|
|
}
|
2017-02-26 20:01:20 +00:00
|
|
|
}
|