3 * StatusNet, the distributed open-source microblogging tool
5 * Utilities for theme files and paths
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.
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.
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/>.
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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
36 * Class for querying and manipulating a theme
38 * Themes are directories with some expected sub-directories and files
39 * in them. They're found in either local/theme (for locally-installed themes)
40 * or theme/ subdir of installation dir.
42 * Note that the 'local' directory can be overridden as $config['local']['path']
43 * and $config['local']['dir'] etc.
45 * This used to be a couple of functions, but for various reasons it's nice
46 * to have a class instead.
50 * @author Evan Prodromou <evan@status.net>
51 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52 * @link http://status.net/
56 const FALLBACK = 'neo';
61 protected $metadata = null; // access via getMetadata() lazy-loader
62 protected $externals = null;
63 protected $deps = null;
68 * Determines the proper directory and path for this theme.
70 * @param string $name Name of the theme; defaults to config value
71 * @throws ServerException
73 function __construct($name = null)
76 $name = common_config('site', 'theme');
78 if (!self::validName($name)) {
79 // TRANS: Server exception displayed if a theme name was invalid.
80 throw new ServerException(_('Invalid theme name.'));
84 // Check to see if it's in the local dir
86 $localroot = self::localRoot();
88 $fulldir = $localroot.'/'.$name;
90 if (file_exists($fulldir) && is_dir($fulldir)) {
91 $this->dir = $fulldir;
92 $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
96 // Check to see if it's in the distribution dir
98 $instroot = self::installRoot();
100 $fulldir = $instroot.'/'.$name;
102 if (file_exists($fulldir) && is_dir($fulldir)) {
103 $this->dir = $fulldir;
104 $this->path = $this->relativeThemePath('theme', 'theme', $name);
108 // Ruh roh. Fall back to default, then.
110 common_log(LOG_WARNING, sprintf("Unable to find theme '%s', falling back to default theme '%s'",
114 $this->name = Theme::FALLBACK;
115 $this->dir = $instroot.'/'.Theme::FALLBACK;
116 $this->path = $this->relativeThemePath('theme', 'theme', Theme::FALLBACK);
121 * Build a full URL to the given theme's base directory, possibly
122 * using an offsite theme server path.
124 * @param string $group configuration section name to pull paths from
125 * @param string $fallbackSubdir default subdirectory under PUBLICDIR
126 * @param string $name theme name
130 * @todo consolidate code with that for other customizable paths
132 protected function relativeThemePath($group, $fallbackSubdir, $name)
134 if (GNUsocial::isHTTPS()) {
135 $sslserver = common_config($group, 'sslserver');
137 if (empty($sslserver)) {
138 if (is_string(common_config('site', 'sslserver')) &&
139 mb_strlen(common_config('site', 'sslserver')) > 0) {
140 $server = common_config('site', 'sslserver');
141 } else if (common_config('site', 'server')) {
142 $server = common_config('site', 'server');
144 $path = common_config('site', 'path') . '/';
145 if ($fallbackSubdir) {
146 $path .= $fallbackSubdir . '/';
149 $server = $sslserver;
150 $path = common_config($group, 'sslpath');
152 $path = common_config($group, 'path');
158 $path = common_config($group, 'path');
161 $path = common_config('site', 'path') . '/';
162 if ($fallbackSubdir) {
163 $path .= $fallbackSubdir . '/';
167 $server = common_config($group, 'server');
169 if (empty($server)) {
170 $server = common_config('site', 'server');
176 if ($path[strlen($path)-1] != '/') {
180 if ($path[0] != '/') {
184 return $protocol.'://'.$server.$path.$name;
188 * Gets the full local filename of a file in this theme.
190 * @param string $relative relative name, like 'logo.png'
192 * @return string full pathname, like /var/www/mublog/theme/default/logo.png
194 function getFile($relative)
196 return $this->dir.'/'.$relative;
200 * Gets the full HTTP url of a file in this theme
202 * @param string $relative relative name, like 'logo.png'
204 * @return string full URL, like 'http://example.com/theme/default/logo.png'
206 function getPath($relative)
208 return $this->path.'/'.$relative;
212 * Fetch a list of other themes whose CSS needs to be pulled in before
213 * this theme's, based on following the theme.ini 'include' settings.
214 * (May be empty if this theme has no include dependencies.)
216 * @return array of strings with theme names
220 if ($this->deps === null) {
221 $chain = $this->doGetDeps(array($this->name));
222 array_pop($chain); // Drop us back off
223 $this->deps = $chain;
228 protected function doGetDeps($chain)
230 $data = $this->getMetadata();
231 if (!empty($data['include'])) {
232 $include = $data['include'];
234 // Protect against cycles!
235 if (!in_array($include, $chain)) {
237 $theme = new Theme($include);
238 array_unshift($chain, $include);
239 return $theme->doGetDeps($chain);
240 } catch (Exception $e) {
242 "Exception while fetching theme dependencies " .
243 "for $this->name: " . $e->getMessage());
251 * Pull data from the theme's theme.ini file.
252 * @fixme calling getFile will fall back to default theme, this may be unsafe.
254 * @return array associative of strings
256 function getMetadata()
258 if ($this->metadata == null) {
259 $this->metadata = $this->doGetMetadata();
261 return $this->metadata;
265 * Pull data from the theme's theme.ini file.
266 * @fixme calling getFile will fall back to default theme, this may be unsafe.
268 * @return array associative of strings
270 private function doGetMetadata()
272 $iniFile = $this->getFile('theme.ini');
273 if (file_exists($iniFile)) {
274 return parse_ini_file($iniFile);
281 * Get list of any external URLs required by this theme and any
282 * dependencies. These are lazy-loaded from theme.ini.
284 * @return array of URL strings
285 * @throws ServerException
287 function getExternals()
289 if ($this->externals == null) {
290 $data = $this->getMetadata();
291 if (!empty($data['external'])) {
292 $ext = (array)$data['external'];
297 if (!empty($data['include'])) {
298 $theme = new Theme($data['include']);
299 $ext = array_merge($ext, $theme->getExternals());
302 $this->externals = array_unique($ext);
304 return $this->externals;
308 * Gets the full path of a file in a theme dir based on its relative name
310 * @param string $relative relative path within the theme directory
311 * @param string $name name of the theme; defaults to current theme
313 * @return string File path to the theme file
314 * @throws ServerException
316 static function file($relative, $name=null)
318 $theme = new Theme($name);
319 return $theme->getFile($relative);
323 * Gets the full URL of a file in a theme dir based on its relative name
325 * @param string $relative relative path within the theme directory
326 * @param string $name name of the theme; defaults to current theme
328 * @return string URL of the file
329 * @throws ServerException
331 static function path($relative, $name=null)
333 $theme = new Theme($name);
334 return $theme->getPath($relative);
338 * list available theme names
340 * @return array list of available theme names
342 static function listAvailable()
344 $local = self::subdirsOf(self::localRoot());
345 $install = self::subdirsOf(self::installRoot());
347 $i = array_search('base', $install);
351 return array_merge($local, $install);
355 * Utility for getting subdirs of a directory
357 * @param string $dir full path to directory to check
359 * @return array relative filenames of subdirs, or empty array
361 protected static function subdirsOf($dir)
366 if ($dh = opendir($dir)) {
367 while (($filename = readdir($dh)) !== false) {
368 if ($filename != '..' && $filename !== '.' &&
369 is_dir($dir.'/'.$filename)) {
370 $subdirs[] = $filename;
381 * Local root dir for themes
385 protected static function localRoot()
387 $basedir = common_config('local', 'dir');
389 if (empty($basedir)) {
390 $basedir = PUBLICDIR . '/local';
393 return $basedir . '/theme';
397 * Root dir for themes that are shipped with GNU social
401 protected static function installRoot()
403 $instroot = common_config('theme', 'dir');
405 if (empty($instroot)) {
406 $instroot = PUBLICDIR.'/theme';
412 static function validName($name)
414 return preg_match('/^[a-z0-9][a-z0-9_-]*$/i', $name);