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/
64 * Determines the proper directory and path for this theme.
66 * @param string $name Name of the theme; defaults to config value
69 function __construct($name=null)
72 $name = common_config('site', 'theme');
74 if (!self::validName($name)) {
75 throw new ServerException("Invalid theme name.");
79 // Check to see if it's in the local dir
81 $localroot = self::localRoot();
83 $fulldir = $localroot.'/'.$name;
85 if (file_exists($fulldir) && is_dir($fulldir)) {
86 $this->dir = $fulldir;
87 $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
91 // Check to see if it's in the distribution dir
93 $instroot = self::installRoot();
95 $fulldir = $instroot.'/'.$name;
97 if (file_exists($fulldir) && is_dir($fulldir)) {
99 $this->dir = $fulldir;
100 $this->path = $this->relativeThemePath('theme', 'theme', $name);
105 * Build a full URL to the given theme's base directory, possibly
106 * using an offsite theme server path.
108 * @param string $group configuration section name to pull paths from
109 * @param string $fallbackSubdir default subdirectory under INSTALLDIR
110 * @param string $name theme name
114 * @todo consolidate code with that for other customizable paths
117 protected function relativeThemePath($group, $fallbackSubdir, $name)
119 if (StatusNet::isHTTPS()) {
121 $sslserver = common_config($group, 'sslserver');
123 if (empty($sslserver)) {
124 if (is_string(common_config('site', 'sslserver')) &&
125 mb_strlen(common_config('site', 'sslserver')) > 0) {
126 $server = common_config('site', 'sslserver');
127 } else if (common_config('site', 'server')) {
128 $server = common_config('site', 'server');
130 $path = common_config('site', 'path') . '/';
131 if ($fallbackSubdir) {
132 $path .= $fallbackSubdir . '/';
135 $server = $sslserver;
136 $path = common_config($group, 'sslpath');
138 $path = common_config($group, 'path');
146 $path = common_config($group, 'path');
149 $path = common_config('site', 'path') . '/';
150 if ($fallbackSubdir) {
151 $path .= $fallbackSubdir . '/';
155 $server = common_config($group, 'server');
157 if (empty($server)) {
158 $server = common_config('site', 'server');
164 if ($path[strlen($path)-1] != '/') {
168 if ($path[0] != '/') {
172 return $protocol.'://'.$server.$path.$name;
176 * Gets the full local filename of a file in this theme.
178 * @param string $relative relative name, like 'logo.png'
180 * @return string full pathname, like /var/www/mublog/theme/default/logo.png
183 function getFile($relative)
185 return $this->dir.'/'.$relative;
189 * Gets the full HTTP url of a file in this theme
191 * @param string $relative relative name, like 'logo.png'
193 * @return string full URL, like 'http://example.com/theme/default/logo.png'
196 function getPath($relative)
198 return $this->path.'/'.$relative;
202 * Fetch a list of other themes whose CSS needs to be pulled in before
203 * this theme's, based on following the theme.ini 'include' settings.
204 * (May be empty if this theme has no include dependencies.)
206 * @return array of strings with theme names
210 $chain = $this->doGetDeps(array($this->name));
211 array_pop($chain); // Drop us back off
215 protected function doGetDeps($chain)
217 $data = $this->getMetadata();
218 if (!empty($data['include'])) {
219 $include = $data['include'];
221 // Protect against cycles!
222 if (!in_array($include, $chain)) {
224 $theme = new Theme($include);
225 array_unshift($chain, $include);
226 return $theme->doGetDeps($chain);
227 } catch (Exception $e) {
229 "Exception while fetching theme dependencies " .
230 "for $this->name: " . $e->getMessage());
238 * Pull data from the theme's theme.ini file.
239 * @fixme calling getFile will fall back to default theme, this may be unsafe.
241 * @return associative array of strings
243 function getMetadata()
245 $iniFile = $this->getFile('theme.ini');
246 if (file_exists($iniFile)) {
247 return parse_ini_file($iniFile);
254 * Gets the full path of a file in a theme dir based on its relative name
256 * @param string $relative relative path within the theme directory
257 * @param string $name name of the theme; defaults to current theme
259 * @return string File path to the theme file
262 static function file($relative, $name=null)
264 $theme = new Theme($name);
265 return $theme->getFile($relative);
269 * Gets the full URL of a file in a theme dir based on its relative name
271 * @param string $relative relative path within the theme directory
272 * @param string $name name of the theme; defaults to current theme
274 * @return string URL of the file
277 static function path($relative, $name=null)
279 $theme = new Theme($name);
280 return $theme->getPath($relative);
284 * list available theme names
286 * @return array list of available theme names
289 static function listAvailable()
291 $local = self::subdirsOf(self::localRoot());
292 $install = self::subdirsOf(self::installRoot());
294 $i = array_search('base', $install);
298 return array_merge($local, $install);
302 * Utility for getting subdirs of a directory
304 * @param string $dir full path to directory to check
306 * @return array relative filenames of subdirs, or empty array
309 protected static function subdirsOf($dir)
314 if ($dh = opendir($dir)) {
315 while (($filename = readdir($dh)) !== false) {
316 if ($filename != '..' && $filename !== '.' &&
317 is_dir($dir.'/'.$filename)) {
318 $subdirs[] = $filename;
329 * Local root dir for themes
331 * @return string local root dir for themes
334 protected static function localRoot()
336 $basedir = common_config('local', 'dir');
338 if (empty($basedir)) {
339 $basedir = INSTALLDIR . '/local';
342 return $basedir . '/theme';
346 * Root dir for themes that are shipped with StatusNet
348 * @return string root dir for StatusNet themes
351 protected static function installRoot()
353 $instroot = common_config('theme', 'dir');
355 if (empty($instroot)) {
356 $instroot = INSTALLDIR.'/theme';
362 static function validName($name)
364 return preg_match('/^[a-z0-9][a-z0-9_-]*$/i', $name);