Themes.php 4.2 KB

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