From b9325251e89db94439a670a32cd1d00f3db848d3 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 19 Feb 2020 10:28:57 -0500 Subject: [PATCH 1/3] Add Theme::getBackgroundColor and Theme::getThemeColor methods --- src/Core/Theme.php | 52 +++++++++++++++++++++++++++++++ view/theme/duepuntozero/theme.php | 22 +++++++++++++ view/theme/frio/theme.php | 48 ++++++++++++++++++++++++++++ view/theme/quattro/theme.php | 22 +++++++++++++ view/theme/smoothly/theme.php | 22 +++++++++++++ view/theme/vier/theme.php | 22 +++++++++++++ 6 files changed, 188 insertions(+) diff --git a/src/Core/Theme.php b/src/Core/Theme.php index ff3265c90a..f7dce3c298 100644 --- a/src/Core/Theme.php +++ b/src/Core/Theme.php @@ -257,4 +257,56 @@ class Theme return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : ''); } + + /** + * Returns the background color of the provided theme if available. + * + * @param string $theme + * @param int|null $uid Current logged-in user id + * @return string|null + */ + public static function getBackgroundColor(string $theme, $uid = null) + { + $theme = Strings::sanitizeFilePathItem($theme); + + $return = null; + + // silently fail if theme was removed or if $theme is funky + if (file_exists("view/theme/$theme/theme.php")) { + include_once "view/theme/$theme/theme.php"; + + $func = "{$theme}_get_background_color"; + if (function_exists($func)) { + $return = $func($uid); + } + } + + return $return; + } + + /** + * Returns the theme color of the provided theme if available. + * + * @param string $theme + * @param int|null $uid Current logged-in user id + * @return string|null + */ + public static function getThemeColor(string $theme, int $uid = null) + { + $theme = Strings::sanitizeFilePathItem($theme); + + $return = null; + + // silently fail if theme was removed or if $theme is funky + if (file_exists("view/theme/$theme/theme.php")) { + include_once "view/theme/$theme/theme.php"; + + $func = "{$theme}_get_theme_color"; + if (function_exists($func)) { + $return = $func($uid); + } + } + + return $return; + } } diff --git a/view/theme/duepuntozero/theme.php b/view/theme/duepuntozero/theme.php index 33ad00baff..10427df6f0 100644 --- a/view/theme/duepuntozero/theme.php +++ b/view/theme/duepuntozero/theme.php @@ -98,3 +98,25 @@ $(document).ready(function() { EOT; } + +/** + * @param int|null $uid + * @return null + * @see \Friendica\Core\Theme::getBackgroundColor() + * @TODO Implement this function + */ +function duepuntozero_get_background_color(int $uid = null) +{ + return null; +} + +/** + * @param int|null $uid + * @return null + * @see \Friendica\Core\Theme::getThemeColor() + * @TODO Implement this function + */ +function duepuntozero_get_theme_color(int $uid = null) +{ + return null; +} diff --git a/view/theme/frio/theme.php b/view/theme/frio/theme.php index f256bec695..daeb22a5ed 100644 --- a/view/theme/frio/theme.php +++ b/view/theme/frio/theme.php @@ -366,3 +366,51 @@ function frio_display_item(App $a, &$arr) } $arr['output']['subthread'] = $subthread; } + +/** + * @param int|null $uid + * @return string + * @see \Friendica\Core\Theme::getBackgroundColor() + */ +function frio_get_background_color(int $uid = null) +{ + $background_color = DI::config()->get('frio', 'background_color') ?: '#ededed'; + + if ($uid) { + $background_color = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $background_color; + } + + $scheme = DI::config()->get('frio', 'scheme', DI::config()->get('frio', 'schema')); + $scheme = Strings::sanitizeFilePathItem($scheme); + + if ($scheme && ($scheme != '---') && file_exists('view/theme/frio/scheme/' . $scheme . '.php')) { + $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php'; + require_once $schemefile; + } + + return $background_color; +} + +/** + * @param int|null $uid + * @return string + * @see \Friendica\Core\Theme::getThemeColor() + */ +function frio_get_theme_color(int $uid = null) +{ + $nav_bg = DI::config()->get('frio', 'nav_bg') ?: '#708fa0'; + + if ($uid) { + $nav_bg = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $nav_bg; + } + + $scheme = DI::config()->get('frio', 'scheme', DI::config()->get('frio', 'schema')); + $scheme = Strings::sanitizeFilePathItem($scheme); + + if ($scheme && ($scheme != '---') && file_exists('view/theme/frio/scheme/' . $scheme . '.php')) { + $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php'; + require_once $schemefile; + } + + return $nav_bg; +} diff --git a/view/theme/quattro/theme.php b/view/theme/quattro/theme.php index d54517de71..3ff9302c85 100644 --- a/view/theme/quattro/theme.php +++ b/view/theme/quattro/theme.php @@ -14,3 +14,25 @@ function quattro_init(App $a) { DI::page()['htmlhead'] .= ''; DI::page()['htmlhead'] .= '';; } + +/** + * @param int|null $uid + * @return null + * @see \Friendica\Core\Theme::getBackgroundColor() + * @TODO Implement this function + */ +function quattro_get_background_color(int $uid = null) +{ + return null; +} + +/** + * @param int|null $uid + * @return null + * @see \Friendica\Core\Theme::getThemeColor() + * @TODO Implement this function + */ +function quattro_get_theme_color(int $uid = null) +{ + return null; +} diff --git a/view/theme/smoothly/theme.php b/view/theme/smoothly/theme.php index e6f40749f9..e20e9003f9 100644 --- a/view/theme/smoothly/theme.php +++ b/view/theme/smoothly/theme.php @@ -93,3 +93,25 @@ if (! function_exists('_js_in_foot')) { return DI::page()['bottom'] = Renderer::replaceMacros($tpl, $bottom); } } + +/** + * @param int|null $uid + * @return null + * @see \Friendica\Core\Theme::getBackgroundColor() + * @TODO Implement this function + */ +function smoothly_get_background_color(int $uid = null) +{ + return null; +} + +/** + * @param int|null $uid + * @return null + * @see \Friendica\Core\Theme::getThemeColor() + * @TODO Implement this function + */ +function smoothly_get_theme_color(int $uid = null) +{ + return null; +} diff --git a/view/theme/vier/theme.php b/view/theme/vier/theme.php index e5d5e1ce97..fcbb148dc7 100644 --- a/view/theme/vier/theme.php +++ b/view/theme/vier/theme.php @@ -364,3 +364,25 @@ function vier_community_info() $tpl = Renderer::getMarkupTemplate('communityhome.tpl'); DI::page()['right_aside'] = Renderer::replaceMacros($tpl, $aside); } + +/** + * @param int|null $uid + * @return null + * @see \Friendica\Core\Theme::getBackgroundColor() + * @TODO Implement this function + */ +function vier_get_background_color(int $uid = null) +{ + return null; +} + +/** + * @param int|null $uid + * @return null + * @see \Friendica\Core\Theme::getThemeColor() + * @TODO Implement this function + */ +function vier_get_theme_color(int $uid = null) +{ + return null; +} From 930d395d7b69dab81c382a3e0fc725131c780667 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 19 Feb 2020 10:29:13 -0500 Subject: [PATCH 2/3] Update manifest output with config/theme info --- src/Module/Manifest.php | 21 +++++++++++---------- view/templates/manifest.tpl | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Module/Manifest.php b/src/Module/Manifest.php index c0af708fa8..d40624cf80 100644 --- a/src/Module/Manifest.php +++ b/src/Module/Manifest.php @@ -22,7 +22,7 @@ namespace Friendica\Module; use Friendica\BaseModule; -use Friendica\Core\Renderer; +use Friendica\Core; use Friendica\DI; class Manifest extends BaseModule @@ -31,18 +31,19 @@ class Manifest extends BaseModule { $config = DI::config(); - $tpl = Renderer::getMarkupTemplate('manifest.tpl'); - header('Content-type: application/manifest+json'); - $touch_icon = $config->get('system', 'touch_icon', 'images/friendica-128.png'); - if ($touch_icon == '') { - $touch_icon = 'images/friendica-128.png'; - } + $touch_icon = $config->get('system', 'touch_icon') ?: 'images/friendica-128.png'; - $output = Renderer::replaceMacros($tpl, [ - '$touch_icon' => $touch_icon, - '$title' => $config->get('config', 'sitename', 'Friendica'), + $theme = DI::config()->get('system', 'theme'); + + $tpl = Core\Renderer::getMarkupTemplate('manifest.tpl'); + $output = Core\Renderer::replaceMacros($tpl, [ + '$touch_icon' => $touch_icon, + '$title' => $config->get('config', 'sitename', 'Friendica'), + '$description' => $config->get('config', 'info', DI::l10n()->t('A Decentralized Social Network')), + '$background_color' => Core\Theme::getBackgroundColor($theme), + '$theme_color' => Core\Theme::getThemeColor($theme), ]); echo $output; diff --git a/view/templates/manifest.tpl b/view/templates/manifest.tpl index 6f9294774d..f38c67db3d 100644 --- a/view/templates/manifest.tpl +++ b/view/templates/manifest.tpl @@ -2,8 +2,17 @@ "name": "{{$title}}", "start_url": "{{$baseurl}}", "display": "standalone", - "description": "A Decentralized Social Network", + "description": "{{$description}}", +{{if $background_color}} + "theme_color": "{{$theme_color}}", +{{/if}} +{{if $background_color}} + "background_color": "{{$background_color}}", +{{/if}} + "short_name": "Friendica", "icons": [{ - "src": "{{$baseurl}}/{{$touch_icon}}" + "src": "{{$baseurl}}/{{$touch_icon}}", + "sizes": "128x128", + "type": "image/png" }] -} +} \ No newline at end of file From 33c7b623266e41d041e856c4c33a4faa7454c85e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 19 Feb 2020 13:29:55 -0500 Subject: [PATCH 3/3] Move manifest JSON from template to array --- src/Module/Manifest.php | 34 ++++++++++++++++++++++------------ view/templates/manifest.tpl | 18 ------------------ 2 files changed, 22 insertions(+), 30 deletions(-) delete mode 100644 view/templates/manifest.tpl diff --git a/src/Module/Manifest.php b/src/Module/Manifest.php index d40624cf80..bfe758fa97 100644 --- a/src/Module/Manifest.php +++ b/src/Module/Manifest.php @@ -31,23 +31,33 @@ class Manifest extends BaseModule { $config = DI::config(); - header('Content-type: application/manifest+json'); - $touch_icon = $config->get('system', 'touch_icon') ?: 'images/friendica-128.png'; $theme = DI::config()->get('system', 'theme'); - $tpl = Core\Renderer::getMarkupTemplate('manifest.tpl'); - $output = Core\Renderer::replaceMacros($tpl, [ - '$touch_icon' => $touch_icon, - '$title' => $config->get('config', 'sitename', 'Friendica'), - '$description' => $config->get('config', 'info', DI::l10n()->t('A Decentralized Social Network')), - '$background_color' => Core\Theme::getBackgroundColor($theme), - '$theme_color' => Core\Theme::getThemeColor($theme), - ]); + $manifest = [ + 'name' => $config->get('config', 'sitename', 'Friendica'), + 'start_url' => DI::baseUrl()->get(), + 'display' => 'standalone', + 'description' => $config->get('config', 'info', DI::l10n()->t('A Decentralized Social Network')), + 'short_name' => 'Friendica', + 'icons' => [ + [ + 'src' => DI::baseUrl()->get() . '/' . $touch_icon, + 'sizes' => '128x128', + 'type' => 'image/png', + ], + ], + ]; - echo $output; + if ($background_color = Core\Theme::getBackgroundColor($theme)) { + $manifest['background_color'] = $background_color; + } - exit(); + if ($theme_color = Core\Theme::getThemeColor($theme)) { + $manifest['theme_color'] = $theme_color; + } + + Core\System::jsonExit($manifest, 'application/manifest+json'); } } diff --git a/view/templates/manifest.tpl b/view/templates/manifest.tpl deleted file mode 100644 index f38c67db3d..0000000000 --- a/view/templates/manifest.tpl +++ /dev/null @@ -1,18 +0,0 @@ -{ - "name": "{{$title}}", - "start_url": "{{$baseurl}}", - "display": "standalone", - "description": "{{$description}}", -{{if $background_color}} - "theme_color": "{{$theme_color}}", -{{/if}} -{{if $background_color}} - "background_color": "{{$background_color}}", -{{/if}} - "short_name": "Friendica", - "icons": [{ - "src": "{{$baseurl}}/{{$touch_icon}}", - "sizes": "128x128", - "type": "image/png" - }] -} \ No newline at end of file