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/
63 * Determines the proper directory and path for this theme.
65 * @param string $name Name of the theme; defaults to config value
68 function __construct($name=null)
71 $name = common_config('site', 'theme');
74 // Check to see if it's in the local dir
76 $localroot = self::localRoot();
78 $fulldir = $localroot.'/'.$name;
80 if (file_exists($fulldir) && is_dir($fulldir)) {
81 $this->dir = $fulldir;
82 $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
86 // Check to see if it's in the distribution dir
88 $instroot = self::installRoot();
90 $fulldir = $instroot.'/'.$name;
92 if (file_exists($fulldir) && is_dir($fulldir)) {
94 $this->dir = $fulldir;
95 $this->path = $this->relativeThemePath('theme', 'theme', $name);
100 * Build a full URL to the given theme's base directory, possibly
101 * using an offsite theme server path.
103 * @param string $group configuration section name to pull paths from
104 * @param string $fallbackSubdir default subdirectory under INSTALLDIR
105 * @param string $name theme name
109 * @todo consolidate code with that for other customizable paths
112 protected function relativeThemePath($group, $fallbackSubdir, $name)
114 $path = common_config($group, 'path');
117 $path = common_config('site', 'path') . '/';
118 if ($fallbackSubdir) {
119 $path .= $fallbackSubdir . '/';
123 if ($path[strlen($path)-1] != '/') {
127 if ($path[0] != '/') {
131 $server = common_config($group, 'server');
133 if (empty($server)) {
134 $server = common_config('site', 'server');
137 $ssl = common_config($group, 'ssl');
139 if (is_null($ssl)) { // null -> guess
140 if (common_config('site', 'ssl') == 'always' &&
141 !common_config($group, 'server')) {
148 $protocol = ($ssl) ? 'https' : 'http';
150 $path = $protocol . '://'.$server.$path.$name;
155 * Gets the full local filename of a file in this theme.
157 * @param string $relative relative name, like 'logo.png'
159 * @return string full pathname, like /var/www/mublog/theme/default/logo.png
162 function getFile($relative)
164 return $this->dir.'/'.$relative;
168 * Gets the full HTTP url of a file in this theme
170 * @param string $relative relative name, like 'logo.png'
172 * @return string full URL, like 'http://example.com/theme/default/logo.png'
175 function getPath($relative)
177 return $this->path.'/'.$relative;
181 * Gets the full path of a file in a theme dir based on its relative name
183 * @param string $relative relative path within the theme directory
184 * @param string $name name of the theme; defaults to current theme
186 * @return string File path to the theme file
189 static function file($relative, $name=null)
191 $theme = new Theme($name);
192 return $theme->getFile($relative);
196 * Gets the full URL of a file in a theme dir based on its relative name
198 * @param string $relative relative path within the theme directory
199 * @param string $name name of the theme; defaults to current theme
201 * @return string URL of the file
204 static function path($relative, $name=null)
206 $theme = new Theme($name);
207 return $theme->getPath($relative);
211 * list available theme names
213 * @return array list of available theme names
216 static function listAvailable()
218 $local = self::subdirsOf(self::localRoot());
219 $install = self::subdirsOf(self::installRoot());
221 $i = array_search('base', $install);
225 return array_merge($local, $install);
229 * Utility for getting subdirs of a directory
231 * @param string $dir full path to directory to check
233 * @return array relative filenames of subdirs, or empty array
236 protected static function subdirsOf($dir)
241 if ($dh = opendir($dir)) {
242 while (($filename = readdir($dh)) !== false) {
243 if ($filename != '..' && $filename !== '.' &&
244 is_dir($dir.'/'.$filename)) {
245 $subdirs[] = $filename;
256 * Local root dir for themes
258 * @return string local root dir for themes
261 protected static function localRoot()
263 $basedir = common_config('local', 'dir');
265 if (empty($basedir)) {
266 $basedir = INSTALLDIR . '/local';
269 return $basedir . '/theme';
273 * Root dir for themes that are shipped with StatusNet
275 * @return string root dir for StatusNet themes
278 protected static function installRoot()
280 $instroot = common_config('theme', 'dir');
282 if (empty($instroot)) {
283 $instroot = INSTALLDIR.'/theme';