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 * This used to be a couple of functions, but for various reasons it's nice
43 * to have a class instead.
47 * @author Evan Prodromou <evan@status.net>
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49 * @link http://status.net/
60 * Determines the proper directory and path for this theme.
62 * @param string $name Name of the theme; defaults to config value
65 function __construct($name=null)
68 $name = common_config('site', 'theme');
71 // Check to see if it's in the local dir
73 $localroot = self::localRoot();
75 $fulldir = $localroot.'/'.$name;
77 if (file_exists($fulldir) && is_dir($fulldir)) {
78 $this->dir = $fulldir;
79 $this->path = common_path('local/theme/'.$name.'/');
83 // Check to see if it's in the distribution dir
85 $instroot = self::installRoot();
87 $fulldir = $instroot.'/'.$name;
89 if (file_exists($fulldir) && is_dir($fulldir)) {
91 $this->dir = $fulldir;
93 $path = common_config('theme', 'path');
96 $path = common_config('site', 'path') . '/theme/';
99 if ($path[strlen($path)-1] != '/') {
103 if ($path[0] != '/') {
107 $server = common_config('theme', 'server');
109 if (empty($server)) {
110 $server = common_config('site', 'server');
113 $ssl = common_config('theme', 'ssl');
115 if (is_null($ssl)) { // null -> guess
116 if (common_config('site', 'ssl') == 'always' &&
117 !common_config('theme', 'server')) {
124 $protocol = ($ssl) ? 'https' : 'http';
126 $this->path = $protocol . '://'.$server.$path.$name;
131 * Gets the full local filename of a file in this theme.
133 * @param string $relative relative name, like 'logo.png'
135 * @return string full pathname, like /var/www/mublog/theme/default/logo.png
138 function getFile($relative)
140 return $this->dir.'/'.$relative;
144 * Gets the full HTTP url of a file in this theme
146 * @param string $relative relative name, like 'logo.png'
148 * @return string full URL, like 'http://example.com/theme/default/logo.png'
151 function getPath($relative)
153 return $this->path.'/'.$relative;
157 * Gets the full path of a file in a theme dir based on its relative name
159 * @param string $relative relative path within the theme directory
160 * @param string $name name of the theme; defaults to current theme
162 * @return string File path to the theme file
165 static function file($relative, $name=null)
167 $theme = new Theme($name);
168 return $theme->getFile($relative);
172 * Gets the full URL of a file in a theme dir based on its relative name
174 * @param string $relative relative path within the theme directory
175 * @param string $name name of the theme; defaults to current theme
177 * @return string URL of the file
180 static function path($relative, $name=null)
182 $theme = new Theme($name);
183 return $theme->getPath($relative);
187 * list available theme names
189 * @return array list of available theme names
192 static function listAvailable()
194 $local = self::subdirsOf(self::localRoot());
195 $install = self::subdirsOf(self::installRoot());
197 $i = array_search('base', $install);
201 return array_merge($local, $install);
205 * Utility for getting subdirs of a directory
207 * @param string $dir full path to directory to check
209 * @return array relative filenames of subdirs, or empty array
212 protected static function subdirsOf($dir)
217 if ($dh = opendir($dir)) {
218 while (($filename = readdir($dh)) !== false) {
219 if ($filename != '..' && $filename !== '.' &&
220 is_dir($dir.'/'.$filename)) {
221 $subdirs[] = $filename;
232 * Local root dir for themes
234 * @return string local root dir for themes
237 protected static function localRoot()
239 return INSTALLDIR.'/local/theme';
243 * Root dir for themes that are shipped with StatusNet
245 * @return string root dir for StatusNet themes
248 protected static function installRoot()
250 $instroot = common_config('theme', 'dir');
252 if (empty($instroot)) {
253 $instroot = INSTALLDIR.'/theme';