theme.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Utilities for theme files and paths
  18. *
  19. * @category Paths
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Sarven Capadisli <csarven@status.net>
  23. * @copyright 2008-2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Class for querying and manipulating a theme
  29. *
  30. * Themes are directories with some expected sub-directories and files
  31. * in them. They're found in either local/theme (for locally-installed themes)
  32. * or theme/ subdir of installation dir.
  33. *
  34. * Note that the 'local' directory can be overridden as $config['local']['path']
  35. * and $config['local']['dir'] etc.
  36. *
  37. * This used to be a couple of functions, but for various reasons it's nice
  38. * to have a class instead.
  39. *
  40. * @category Output
  41. * @package GNUsocial
  42. * @author Evan Prodromou <evan@status.net>
  43. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  44. */
  45. class Theme
  46. {
  47. const FALLBACK = 'neo';
  48. public $name = null;
  49. public $dir = null;
  50. public $path = null;
  51. protected $metadata = null; // access via getMetadata() lazy-loader
  52. protected $externals = null;
  53. protected $deps = null;
  54. /**
  55. * Constructor
  56. *
  57. * Determines the proper directory and path for this theme.
  58. *
  59. * @param string $name Name of the theme; defaults to config value
  60. * @throws ServerException
  61. */
  62. public function __construct($name = null)
  63. {
  64. if (empty($name)) {
  65. $name = common_config('site', 'theme');
  66. }
  67. if (!self::validName($name)) {
  68. // TRANS: Server exception displayed if a theme name was invalid.
  69. throw new ServerException(_('Invalid theme name.'));
  70. }
  71. $this->name = $name;
  72. // Check to see if it's in the local dir
  73. $localroot = self::localRoot();
  74. $fulldir = $localroot.'/'.$name;
  75. if (file_exists($fulldir) && is_dir($fulldir)) {
  76. $this->dir = $fulldir;
  77. $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
  78. return;
  79. }
  80. // Check to see if it's in the distribution dir
  81. $instroot = self::installRoot();
  82. $fulldir = $instroot.'/'.$name;
  83. if (file_exists($fulldir) && is_dir($fulldir)) {
  84. $this->dir = $fulldir;
  85. $this->path = $this->relativeThemePath('theme', 'theme', $name);
  86. return;
  87. }
  88. // Ruh roh. Fall back to default, then.
  89. common_log(LOG_WARNING, sprintf(
  90. 'Unable to find theme \'%s\', falling back to default theme \'%s\'',
  91. $name,
  92. Theme::FALLBACK
  93. ));
  94. $this->name = Theme::FALLBACK;
  95. $this->dir = $instroot.'/'.Theme::FALLBACK;
  96. $this->path = $this->relativeThemePath('theme', 'theme', Theme::FALLBACK);
  97. }
  98. /**
  99. * Build a full URL to the given theme's base directory, possibly
  100. * using an offsite theme server path.
  101. *
  102. * @param string $group configuration section name to pull paths from
  103. * @param string $fallbackSubdir default subdirectory under PUBLICDIR
  104. * @param string $name theme name
  105. *
  106. * @return string URL
  107. *
  108. * @todo consolidate code with that for other customizable paths
  109. */
  110. protected function relativeThemePath($group, $fallbackSubdir, $name)
  111. {
  112. if (GNUsocial::isHTTPS()) {
  113. $sslserver = common_config($group, 'sslserver');
  114. if (empty($sslserver)) {
  115. $sslserver = common_config('site', 'sslserver');
  116. if (is_string($sslserver) && strlen($sslserver) > 0) {
  117. $server = $sslserver;
  118. } elseif (!empty(common_config('site', 'server'))) {
  119. $server = common_config('site', 'server');
  120. }
  121. $path = common_config('site', 'path') . '/';
  122. if ($fallbackSubdir) {
  123. $path .= $fallbackSubdir . '/';
  124. }
  125. } else {
  126. $server = $sslserver;
  127. $path = common_config($group, 'sslpath');
  128. if (empty($path)) {
  129. $path = common_config($group, 'path');
  130. }
  131. }
  132. $protocol = 'https';
  133. } else {
  134. $path = common_config($group, 'path');
  135. if (empty($path)) {
  136. $path = common_config('site', 'path') . '/';
  137. if ($fallbackSubdir) {
  138. $path .= $fallbackSubdir . '/';
  139. }
  140. }
  141. $server = common_config($group, 'server');
  142. if (empty($server)) {
  143. $server = common_config('site', 'server');
  144. }
  145. $protocol = 'http';
  146. }
  147. if ($path[strlen($path)-1] != '/') {
  148. $path .= '/';
  149. }
  150. if ($path[0] != '/') {
  151. $path = '/'.$path;
  152. }
  153. return $protocol.'://'.$server.$path.$name;
  154. }
  155. /**
  156. * Gets the full local filename of a file in this theme.
  157. *
  158. * @param string $relative relative name, like 'logo.png'
  159. *
  160. * @return string full pathname, like /var/www/mublog/theme/default/logo.png
  161. */
  162. public function getFile($relative)
  163. {
  164. return $this->dir.'/'.$relative;
  165. }
  166. /**
  167. * Gets the full HTTP url of a file in this theme
  168. *
  169. * @param string $relative relative name, like 'logo.png'
  170. *
  171. * @return string full URL, like 'http://example.com/theme/default/logo.png'
  172. */
  173. public function getPath($relative)
  174. {
  175. return $this->path.'/'.$relative;
  176. }
  177. /**
  178. * Fetch a list of other themes whose CSS needs to be pulled in before
  179. * this theme's, based on following the theme.ini 'include' settings.
  180. * (May be empty if this theme has no include dependencies.)
  181. *
  182. * @return array of strings with theme names
  183. */
  184. public function getDeps()
  185. {
  186. if ($this->deps === null) {
  187. $chain = $this->doGetDeps(array($this->name));
  188. array_pop($chain); // Drop us back off
  189. $this->deps = $chain;
  190. }
  191. return $this->deps;
  192. }
  193. protected function doGetDeps($chain)
  194. {
  195. $data = $this->getMetadata();
  196. if (!empty($data['include'])) {
  197. $include = $data['include'];
  198. // Protect against cycles!
  199. if (!in_array($include, $chain)) {
  200. try {
  201. $theme = new Theme($include);
  202. array_unshift($chain, $include);
  203. return $theme->doGetDeps($chain);
  204. } catch (Exception $e) {
  205. common_log(
  206. LOG_ERR,
  207. 'Exception while fetching theme dependencies '
  208. . "for {$this->name}: {$e->getMessage()}"
  209. );
  210. }
  211. }
  212. }
  213. return $chain;
  214. }
  215. /**
  216. * Pull data from the theme's theme.ini file.
  217. * @fixme calling getFile will fall back to default theme, this may be unsafe.
  218. *
  219. * @return array associative of strings
  220. */
  221. public function getMetadata()
  222. {
  223. if (is_null($this->metadata)) {
  224. $this->metadata = $this->doGetMetadata();
  225. }
  226. return $this->metadata;
  227. }
  228. /**
  229. * Pull data from the theme's theme.ini file.
  230. * @fixme calling getFile will fall back to default theme, this may be unsafe.
  231. *
  232. * @return array associative of strings
  233. */
  234. private function doGetMetadata()
  235. {
  236. $iniFile = $this->getFile('theme.ini');
  237. if (file_exists($iniFile)) {
  238. return parse_ini_file($iniFile);
  239. } else {
  240. return [];
  241. }
  242. }
  243. /**
  244. * Get list of any external URLs required by this theme and any
  245. * dependencies. These are lazy-loaded from theme.ini.
  246. *
  247. * @return array of URL strings
  248. * @throws ServerException
  249. */
  250. public function getExternals()
  251. {
  252. if (is_null($this->externals)) {
  253. $data = $this->getMetadata();
  254. if (!empty($data['external'])) {
  255. $ext = (array)$data['external'];
  256. } else {
  257. $ext = array();
  258. }
  259. if (!empty($data['include'])) {
  260. $theme = new Theme($data['include']);
  261. $ext = array_merge($ext, $theme->getExternals());
  262. }
  263. $this->externals = array_unique($ext);
  264. }
  265. return $this->externals;
  266. }
  267. /**
  268. * Gets the full path of a file in a theme dir based on its relative name
  269. *
  270. * @param string $relative relative path within the theme directory
  271. * @param string $name name of the theme; defaults to current theme
  272. *
  273. * @return string File path to the theme file
  274. * @throws ServerException
  275. */
  276. public static function file($relative, $name = null)
  277. {
  278. $theme = new Theme($name);
  279. return $theme->getFile($relative);
  280. }
  281. /**
  282. * Gets the full URL of a file in a theme dir based on its relative name
  283. *
  284. * @param string $relative relative path within the theme directory
  285. * @param string $name name of the theme; defaults to current theme
  286. *
  287. * @return string URL of the file
  288. * @throws ServerException
  289. */
  290. public static function path($relative, $name = null)
  291. {
  292. $theme = new Theme($name);
  293. return $theme->getPath($relative);
  294. }
  295. /**
  296. * list available theme names
  297. *
  298. * @return array list of available theme names
  299. */
  300. public static function listAvailable()
  301. {
  302. $local = self::subdirsOf(self::localRoot());
  303. $install = self::subdirsOf(self::installRoot());
  304. $i = array_search('base', $install);
  305. unset($install[$i]);
  306. return array_merge($local, $install);
  307. }
  308. /**
  309. * Utility for getting subdirs of a directory
  310. *
  311. * @param string $dir full path to directory to check
  312. *
  313. * @return array relative filenames of subdirs, or empty array
  314. */
  315. protected static function subdirsOf($dir)
  316. {
  317. $subdirs = array();
  318. if (is_dir($dir)) {
  319. if (($dh = opendir($dir)) !== false) {
  320. while (($filename = readdir($dh)) !== false) {
  321. if ($filename != '..' && $filename !== '.' &&
  322. is_dir($dir.'/'.$filename)) {
  323. $subdirs[] = $filename;
  324. }
  325. }
  326. closedir($dh);
  327. }
  328. }
  329. return $subdirs;
  330. }
  331. /**
  332. * Local root dir for themes
  333. *
  334. * @return string
  335. */
  336. protected static function localRoot()
  337. {
  338. $basedir = common_config('local', 'dir');
  339. if (empty($basedir)) {
  340. $basedir = PUBLICDIR . '/local';
  341. }
  342. return $basedir . '/theme';
  343. }
  344. /**
  345. * Root dir for themes that are shipped with GNU social
  346. *
  347. * @return string
  348. */
  349. protected static function installRoot()
  350. {
  351. $instroot = common_config('theme', 'dir');
  352. if (empty($instroot)) {
  353. $instroot = PUBLICDIR.'/theme';
  354. }
  355. return $instroot;
  356. }
  357. public static function validName($name)
  358. {
  359. return preg_match('/^[a-z0-9][a-z0-9_-]*$/i', $name);
  360. }
  361. }