theme.php 12 KB

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