theme.php 12 KB

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