Themes.php 6.2 KB

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