Themes.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_Themes extends Minz_Model {
  4. private static string $themesUrl = '/themes/';
  5. private static string $defaultIconsUrl = '/themes/icons/';
  6. public static string $defaultTheme = 'Origine';
  7. /** @return list<string> */
  8. public static function getList(): array {
  9. return array_values(array_diff(
  10. scandir(PUBLIC_PATH . self::$themesUrl) ?: [],
  11. ['..', '.']
  12. ));
  13. }
  14. /** @return array<string,array{id:string,name:string,author:string,description:string,version:float|string,files:array<string>,theme-color?:string|array{dark?:string,light?:string,default?:string}}> */
  15. public static function get(): array {
  16. $themes_list = self::getList();
  17. $list = [];
  18. foreach ($themes_list as $theme_dir) {
  19. $theme = self::get_infos($theme_dir);
  20. if (is_array($theme) && trim($theme['name']) !== '') {
  21. $list[$theme_dir] = $theme;
  22. }
  23. }
  24. return $list;
  25. }
  26. /**
  27. * @return false|array{id:string,name:string,author:string,description:string,version:float|string,files:array<string>,theme-color?:string|array{dark?:string,light?:string,default?:string}}
  28. */
  29. public static function get_infos(string $theme_id): array|false {
  30. $theme_dir = PUBLIC_PATH . self::$themesUrl . $theme_id;
  31. if (is_dir($theme_dir)) {
  32. $json_filename = $theme_dir . '/metadata.json';
  33. if (file_exists($json_filename)) {
  34. $content = file_get_contents($json_filename) ?: '';
  35. $res = json_decode($content, true);
  36. if (is_array($res)) {
  37. $result = [
  38. 'id' => $theme_id,
  39. 'name' => is_string($res['name'] ?? null) ? $res['name'] : '',
  40. 'author' => is_string($res['author'] ?? null) ? $res['author'] : '',
  41. 'description' => is_string($res['description'] ?? null) ? $res['description'] : '',
  42. 'version' => is_string($res['version'] ?? null) || is_numeric($res['version'] ?? null) ? $res['version'] : '0',
  43. 'files' => is_array($res['files']) && is_array_values_string($res['files']) ? array_values($res['files']) : [],
  44. 'theme-color' => is_string($res['theme-color'] ?? null) ? $res['theme-color'] : '',
  45. ];
  46. if (empty($result['theme-color']) && is_array($res['theme-color'])) {
  47. $result['theme-color'] = [
  48. 'dark' => is_string($res['theme-color']['dark'] ?? null) ? $res['theme-color']['dark'] : '',
  49. 'light' => is_string($res['theme-color']['light'] ?? null) ? $res['theme-color']['light'] : '',
  50. 'default' => is_string($res['theme-color']['default'] ?? null) ? $res['theme-color']['default'] : '',
  51. ];
  52. }
  53. return $result;
  54. }
  55. }
  56. }
  57. return false;
  58. }
  59. private static string $themeIconsUrl;
  60. /** @var array<string,int> */
  61. private static array $themeIcons;
  62. /**
  63. * @return false|array{id:string,name:string,author:string,description:string,version:float|string,files:array<string>,theme-color?:string|array{dark?:string,light?:string,default?:string}}
  64. */
  65. public static function load(string $theme_id): array|false {
  66. $infos = self::get_infos($theme_id);
  67. if (empty($infos)) {
  68. if ($theme_id !== self::$defaultTheme) { //Fall-back to default theme
  69. return self::load(self::$defaultTheme);
  70. }
  71. $themes_list = self::getList();
  72. if (!empty($themes_list)) {
  73. if ($theme_id !== $themes_list[0]) { //Fall-back to first theme
  74. return self::load($themes_list[0]);
  75. }
  76. }
  77. return false;
  78. }
  79. self::$themeIconsUrl = self::$themesUrl . $theme_id . '/icons/';
  80. self::$themeIcons = is_dir(PUBLIC_PATH . self::$themeIconsUrl) ? array_fill_keys(array_diff(
  81. scandir(PUBLIC_PATH . self::$themeIconsUrl) ?: [],
  82. ['..', '.']
  83. ), 1) : [];
  84. return $infos;
  85. }
  86. public static function title(string $name): string {
  87. $titles = [
  88. 'opml-dyn' => 'sub.category.dynamic_opml',
  89. ];
  90. return $titles[$name] ?? '';
  91. }
  92. public static function alt(string $name): string {
  93. $alts = [
  94. 'add' => '➕', //✚
  95. 'all' => '☰',
  96. 'bookmark-add' => '➕', //✚
  97. 'bookmark-tag' => '📑',
  98. 'category' => '🗂️', //☷
  99. 'close' => '❌',
  100. 'configure' => '⚙️',
  101. 'debug' => '🐛',
  102. 'down' => '🔽', //▽
  103. 'error' => '❌',
  104. 'favorite' => '⭐', //★
  105. 'FreshRSS-logo' => '⊚',
  106. 'help' => 'ℹ️', //ⓘ
  107. 'icon' => '⊚',
  108. 'important' => '📌',
  109. 'key' => '🔑', //⚿
  110. 'label' => '🏷️',
  111. 'link' => '↗️', //↗
  112. 'look' => '👀', //👁
  113. 'login' => '🔒',
  114. 'logout' => '🔓',
  115. 'next' => '⏩',
  116. 'non-starred' => '☆',
  117. 'notice' => 'ℹ️', //ⓘ
  118. 'opml-dyn' => '⚡',
  119. 'prev' => '⏪',
  120. 'read' => '☑️', //☑
  121. 'rss' => '📣', //☄
  122. 'unread' => '🔲', //☐
  123. 'refresh' => '🔃', //↻
  124. 'search' => '🔍',
  125. 'share' => '♻️', //♺
  126. 'sort-down' => '⬇️', //↓
  127. 'sort-up' => '⬆️', //↑
  128. 'starred' => '⭐', //★
  129. 'stats' => '📈', //%
  130. 'tag' => '🔖', //⚐
  131. 'up' => '🔼', //△
  132. 'view-normal' => '📰', //☰
  133. 'view-global' => '📖', //☷
  134. 'view-reader' => '📜',
  135. 'warning' => '⚠️', //△
  136. ];
  137. return $alts[$name] ?? '';
  138. }
  139. // TODO: Change for enum in PHP 8.1+
  140. public const ICON_DEFAULT = 0;
  141. public const ICON_IMG = 1;
  142. public const ICON_URL = 2;
  143. public const ICON_EMOJI = 3;
  144. public static function icon(string $name, int $type = self::ICON_DEFAULT): string {
  145. $alt = self::alt($name);
  146. if ($alt == '') {
  147. return '';
  148. }
  149. $url = $name . '.svg';
  150. $url = isset(self::$themeIcons[$url]) ? (self::$themeIconsUrl . $url) : (self::$defaultIconsUrl . $url);
  151. $title = self::title($name);
  152. if ($title != '') {
  153. $title = ' title="' . _t($title) . '"';
  154. }
  155. if ($type == self::ICON_DEFAULT) {
  156. if ((FreshRSS_Context::hasUserConf() && FreshRSS_Context::userConf()->icons_as_emojis)
  157. // default to emoji alternate for some icons
  158. ) {
  159. $type = self::ICON_EMOJI;
  160. } else {
  161. $type = self::ICON_IMG;
  162. }
  163. }
  164. return match ($type) {
  165. self::ICON_URL => Minz_Url::display($url),
  166. self::ICON_IMG => '<img class="icon" src="' . Minz_Url::display($url) . '" loading="lazy" alt="' . $alt . '"' . $title . ' />',
  167. self::ICON_EMOJI, => '<span class="icon"' . $title . '>' . $alt . '</span>',
  168. default => '<span class="icon"' . $title . '>' . $alt . '</span>',
  169. };
  170. }
  171. }