Themes.php 5.1 KB

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