Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

204 行
5.1 KiB

  1. <?php
  2. /**
  3. * @file src/Core/Theme.php
  4. */
  5. namespace Friendica\Core;
  6. use Friendica\Core\Logger;
  7. use Friendica\Core\System;
  8. /**
  9. * Some functions to handle themes
  10. */
  11. class Theme
  12. {
  13. /**
  14. * @brief Parse theme comment in search of theme infos.
  15. *
  16. * like
  17. * \code
  18. * ..* Name: My Theme
  19. * * Description: My Cool Theme
  20. * . * Version: 1.2.3
  21. * * Author: John <profile url>
  22. * * Maintainer: Jane <profile url>
  23. * *
  24. * \endcode
  25. * @param string $theme the name of the theme
  26. * @return array
  27. */
  28. public static function getInfo($theme)
  29. {
  30. $info = [
  31. 'name' => $theme,
  32. 'description' => "",
  33. 'author' => [],
  34. 'maintainer' => [],
  35. 'version' => "",
  36. 'credits' => "",
  37. 'experimental' => file_exists("view/theme/$theme/experimental"),
  38. 'unsupported' => file_exists("view/theme/$theme/unsupported")
  39. ];
  40. if (!is_file("view/theme/$theme/theme.php")) {
  41. return $info;
  42. }
  43. $a = get_app();
  44. $stamp1 = microtime(true);
  45. $theme_file = file_get_contents("view/theme/$theme/theme.php");
  46. $a->saveTimestamp($stamp1, "file");
  47. $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
  48. if ($result) {
  49. $comment_lines = explode("\n", $matches[0]);
  50. foreach ($comment_lines as $comment_line) {
  51. $comment_line = trim($comment_line, "\t\n\r */");
  52. if ($comment_line != "") {
  53. list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
  54. $key = strtolower($key);
  55. if ($key == "author") {
  56. $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
  57. if ($result) {
  58. $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
  59. } else {
  60. $info['author'][] = ['name' => $value];
  61. }
  62. } elseif ($key == "maintainer") {
  63. $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
  64. if ($result) {
  65. $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
  66. } else {
  67. $info['maintainer'][] = ['name' => $value];
  68. }
  69. } elseif (array_key_exists($key, $info)) {
  70. $info[$key] = $value;
  71. }
  72. }
  73. }
  74. }
  75. return $info;
  76. }
  77. /**
  78. * @brief Returns the theme's screenshot.
  79. *
  80. * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
  81. *
  82. * @param sring $theme The name of the theme
  83. * @return string
  84. */
  85. public static function getScreenshot($theme)
  86. {
  87. $exts = ['.png', '.jpg'];
  88. foreach ($exts as $ext) {
  89. if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
  90. return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
  91. }
  92. }
  93. return(System::baseUrl() . '/images/blank.png');
  94. }
  95. // install and uninstall theme
  96. public static function uninstall($theme)
  97. {
  98. Logger::log("Addons: uninstalling theme " . $theme);
  99. include_once "view/theme/$theme/theme.php";
  100. if (function_exists("{$theme}_uninstall")) {
  101. $func = "{$theme}_uninstall";
  102. $func();
  103. }
  104. }
  105. public static function install($theme)
  106. {
  107. // silently fail if theme was removed
  108. if (!file_exists("view/theme/$theme/theme.php")) {
  109. return false;
  110. }
  111. Logger::log("Addons: installing theme $theme");
  112. include_once "view/theme/$theme/theme.php";
  113. if (function_exists("{$theme}_install")) {
  114. $func = "{$theme}_install";
  115. $func();
  116. return true;
  117. } else {
  118. Logger::log("Addons: FAILED installing theme $theme");
  119. return false;
  120. }
  121. }
  122. /**
  123. * @brief Get the full path to relevant theme files by filename
  124. *
  125. * This function search in the theme directory (and if not present in global theme directory)
  126. * if there is a directory with the file extension and for a file with the given
  127. * filename.
  128. *
  129. * @param string $file Filename
  130. * @param string $root Full root path
  131. * @return string Path to the file or empty string if the file isn't found
  132. */
  133. public static function getPathForFile($file, $root = '')
  134. {
  135. $file = basename($file);
  136. // Make sure $root ends with a slash / if it's not blank
  137. if ($root !== '' && $root[strlen($root) - 1] !== '/') {
  138. $root = $root . '/';
  139. }
  140. $theme_info = get_app()->theme_info;
  141. if (is_array($theme_info) && array_key_exists('extends', $theme_info)) {
  142. $parent = $theme_info['extends'];
  143. } else {
  144. $parent = 'NOPATH';
  145. }
  146. $theme = get_app()->getCurrentTheme();
  147. $thname = $theme;
  148. $ext = substr($file, strrpos($file, '.') + 1);
  149. $paths = [
  150. "{$root}view/theme/$thname/$ext/$file",
  151. "{$root}view/theme/$parent/$ext/$file",
  152. "{$root}view/$ext/$file",
  153. ];
  154. foreach ($paths as $p) {
  155. // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
  156. if (strpos($p, 'NOPATH') !== false) {
  157. continue;
  158. } elseif (file_exists($p)) {
  159. return $p;
  160. }
  161. }
  162. return '';
  163. }
  164. /**
  165. * @brief Return relative path to theme stylesheet file
  166. *
  167. * Provide a sane default if nothing is chosen or the specified theme does not exist.
  168. *
  169. * @param string $theme Theme name
  170. *
  171. * @return string
  172. */
  173. public static function getStylesheetPath($theme)
  174. {
  175. $a = get_app();
  176. $opts = (($a->profile_uid) ? '?f=&puid=' . $a->profile_uid : '');
  177. if (file_exists('view/theme/' . $theme . '/style.php')) {
  178. return 'view/theme/' . $theme . '/style.pcss' . $opts;
  179. }
  180. return 'view/theme/' . $theme . '/style.css';
  181. }
  182. }