theme.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. public $widgetOpts;
  48. public $scoped;
  49. const FALLBACK = 'neo';
  50. public $name = null;
  51. public $dir = null;
  52. public $path = null;
  53. protected $metadata = null; // access via getMetadata() lazy-loader
  54. protected $externals = null;
  55. protected $deps = null;
  56. /**
  57. * Constructor
  58. *
  59. * Determines the proper directory and path for this theme.
  60. *
  61. * @param string $name Name of the theme; defaults to config value
  62. * @throws ServerException
  63. */
  64. public function __construct($name = null)
  65. {
  66. if (empty($name)) {
  67. $name = common_config('site', 'theme');
  68. }
  69. if (!self::validName($name)) {
  70. // TRANS: Server exception displayed if a theme name was invalid.
  71. throw new ServerException(_('Invalid theme name.'));
  72. }
  73. $this->name = $name;
  74. // Check to see if it's in the local dir
  75. $localroot = self::localRoot();
  76. $fulldir = $localroot.'/'.$name;
  77. if (file_exists($fulldir) && is_dir($fulldir)) {
  78. $this->dir = $fulldir;
  79. $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
  80. return;
  81. }
  82. // Check to see if it's in the distribution dir
  83. $instroot = self::installRoot();
  84. $fulldir = $instroot.'/'.$name;
  85. if (file_exists($fulldir) && is_dir($fulldir)) {
  86. $this->dir = $fulldir;
  87. $this->path = $this->relativeThemePath('theme', 'theme', $name);
  88. return;
  89. }
  90. // Ruh roh. Fall back to default, then.
  91. common_log(LOG_WARNING, sprintf(
  92. 'Unable to find theme \'%s\', falling back to default theme \'%s\'',
  93. $name,
  94. Theme::FALLBACK
  95. ));
  96. $this->name = Theme::FALLBACK;
  97. $this->dir = $instroot.'/'.Theme::FALLBACK;
  98. $this->path = $this->relativeThemePath('theme', 'theme', Theme::FALLBACK);
  99. }
  100. /**
  101. * Build a full URL to the given theme's base directory, possibly
  102. * using an offsite theme server path.
  103. *
  104. * @param string $group configuration section name to pull paths from
  105. * @param string $fallbackSubdir default subdirectory under PUBLICDIR
  106. * @param string $name theme name
  107. *
  108. * @return string URL
  109. *
  110. * @todo consolidate code with that for other customizable paths
  111. */
  112. protected function relativeThemePath($group, $fallbackSubdir, $name)
  113. {
  114. if (GNUsocial::isHTTPS()) {
  115. $sslserver = common_config($group, 'sslserver');
  116. if (empty($sslserver)) {
  117. $sslserver = common_config('site', 'sslserver');
  118. if (is_string($sslserver) && strlen($sslserver) > 0) {
  119. $server = $sslserver;
  120. } elseif (!empty(common_config('site', 'server'))) {
  121. $server = common_config('site', 'server');
  122. }
  123. $path = common_config('site', 'path') . '/';
  124. if ($fallbackSubdir) {
  125. $path .= $fallbackSubdir . '/';
  126. }
  127. } else {
  128. $server = $sslserver;
  129. $path = common_config($group, 'sslpath');
  130. if (empty($path)) {
  131. $path = common_config($group, 'path');
  132. }
  133. }
  134. $protocol = 'https';
  135. } else {
  136. $path = common_config($group, 'path');
  137. if (empty($path)) {
  138. $path = common_config('site', 'path') . '/';
  139. if ($fallbackSubdir) {
  140. $path .= $fallbackSubdir . '/';
  141. }
  142. }
  143. $server = common_config($group, 'server');
  144. if (empty($server)) {
  145. $server = common_config('site', 'server');
  146. }
  147. $protocol = 'http';
  148. }
  149. if ($path[strlen($path)-1] != '/') {
  150. $path .= '/';
  151. }
  152. if ($path[0] != '/') {
  153. $path = '/'.$path;
  154. }
  155. return $protocol.'://'.$server.$path.$name;
  156. }
  157. /**
  158. * Gets the full local filename of a file in this theme.
  159. *
  160. * @param string $relative relative name, like 'logo.png'
  161. *
  162. * @return string full pathname, like /var/www/mublog/theme/default/logo.png
  163. */
  164. public function getFile($relative)
  165. {
  166. return $this->dir.'/'.$relative;
  167. }
  168. /**
  169. * Gets the full HTTP url of a file in this theme
  170. *
  171. * @param string $relative relative name, like 'logo.png'
  172. *
  173. * @return string full URL, like 'http://example.com/theme/default/logo.png'
  174. */
  175. public function getPath($relative)
  176. {
  177. return $this->path.'/'.$relative;
  178. }
  179. /**
  180. * Fetch a list of other themes whose CSS needs to be pulled in before
  181. * this theme's, based on following the theme.ini 'include' settings.
  182. * (May be empty if this theme has no include dependencies.)
  183. *
  184. * @return array of strings with theme names
  185. */
  186. public function getDeps()
  187. {
  188. if ($this->deps === null) {
  189. $chain = $this->doGetDeps(array($this->name));
  190. array_pop($chain); // Drop us back off
  191. $this->deps = $chain;
  192. }
  193. return $this->deps;
  194. }
  195. protected function doGetDeps($chain)
  196. {
  197. $data = $this->getMetadata();
  198. if (!empty($data['include'])) {
  199. $include = $data['include'];
  200. // Protect against cycles!
  201. if (!in_array($include, $chain)) {
  202. try {
  203. $theme = new Theme($include);
  204. array_unshift($chain, $include);
  205. return $theme->doGetDeps($chain);
  206. } catch (Exception $e) {
  207. common_log(
  208. LOG_ERR,
  209. 'Exception while fetching theme dependencies '
  210. . "for {$this->name}: {$e->getMessage()}"
  211. );
  212. }
  213. }
  214. }
  215. return $chain;
  216. }
  217. /**
  218. * Pull data from the theme's theme.ini file.
  219. * @fixme calling getFile will fall back to default theme, this may be unsafe.
  220. *
  221. * @return array associative of strings
  222. */
  223. public function getMetadata()
  224. {
  225. if (is_null($this->metadata)) {
  226. $this->metadata = $this->doGetMetadata();
  227. }
  228. return $this->metadata;
  229. }
  230. /**
  231. * Pull data from the theme's theme.ini file.
  232. * @fixme calling getFile will fall back to default theme, this may be unsafe.
  233. *
  234. * @return array associative of strings
  235. */
  236. private function doGetMetadata()
  237. {
  238. $iniFile = $this->getFile('theme.ini');
  239. if (file_exists($iniFile)) {
  240. return parse_ini_file($iniFile);
  241. } else {
  242. return [];
  243. }
  244. }
  245. /**
  246. * Get list of any external URLs required by this theme and any
  247. * dependencies. These are lazy-loaded from theme.ini.
  248. *
  249. * @return array of URL strings
  250. * @throws ServerException
  251. */
  252. public function getExternals()
  253. {
  254. if (is_null($this->externals)) {
  255. $data = $this->getMetadata();
  256. if (!empty($data['external'])) {
  257. $ext = (array)$data['external'];
  258. } else {
  259. $ext = array();
  260. }
  261. if (!empty($data['include'])) {
  262. $theme = new Theme($data['include']);
  263. $ext = array_merge($ext, $theme->getExternals());
  264. }
  265. $this->externals = array_unique($ext);
  266. }
  267. return $this->externals;
  268. }
  269. /**
  270. * Gets the full path of a file in a theme dir based on its relative name
  271. *
  272. * @param string $relative relative path within the theme directory
  273. * @param string $name name of the theme; defaults to current theme
  274. *
  275. * @return string File path to the theme file
  276. * @throws ServerException
  277. */
  278. public static function file($relative, $name = null)
  279. {
  280. $theme = new Theme($name);
  281. return $theme->getFile($relative);
  282. }
  283. /**
  284. * Gets the full URL of a file in a theme dir based on its relative name
  285. *
  286. * @param string $relative relative path within the theme directory
  287. * @param string $name name of the theme; defaults to current theme
  288. *
  289. * @return string URL of the file
  290. * @throws ServerException
  291. */
  292. public static function path($relative, $name = null)
  293. {
  294. $theme = new Theme($name);
  295. return $theme->getPath($relative);
  296. }
  297. /**
  298. * list available theme names
  299. *
  300. * @return array list of available theme names
  301. */
  302. public static function listAvailable()
  303. {
  304. $local = self::subdirsOf(self::localRoot());
  305. $install = self::subdirsOf(self::installRoot());
  306. $i = array_search('base', $install);
  307. unset($install[$i]);
  308. return array_merge($local, $install);
  309. }
  310. /**
  311. * Utility for getting subdirs of a directory
  312. *
  313. * @param string $dir full path to directory to check
  314. *
  315. * @return array relative filenames of subdirs, or empty array
  316. */
  317. protected static function subdirsOf($dir)
  318. {
  319. $subdirs = array();
  320. if (is_dir($dir)) {
  321. if (($dh = opendir($dir)) !== false) {
  322. while (($filename = readdir($dh)) !== false) {
  323. if ($filename != '..' && $filename !== '.' &&
  324. is_dir($dir.'/'.$filename)) {
  325. $subdirs[] = $filename;
  326. }
  327. }
  328. closedir($dh);
  329. }
  330. }
  331. return $subdirs;
  332. }
  333. /**
  334. * Local root dir for themes
  335. *
  336. * @return string
  337. */
  338. protected static function localRoot()
  339. {
  340. $basedir = common_config('local', 'dir');
  341. if (empty($basedir)) {
  342. $basedir = PUBLICDIR . '/local';
  343. }
  344. return $basedir . '/theme';
  345. }
  346. /**
  347. * Root dir for themes that are shipped with GNU social
  348. *
  349. * @return string
  350. */
  351. protected static function installRoot()
  352. {
  353. $instroot = common_config('theme', 'dir');
  354. if (empty($instroot)) {
  355. $instroot = PUBLICDIR.'/theme';
  356. }
  357. return $instroot;
  358. }
  359. public static function validName($name)
  360. {
  361. return preg_match('/^[a-z0-9][a-z0-9_-]*$/i', $name);
  362. }
  363. }