Themes.php 5.3 KB

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