]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/theme.php
try to show HTTPS-encrypted theme files for HTTPS-encrypted pages
[quix0rs-gnu-social.git] / lib / theme.php
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
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Class for querying and manipulating a theme
37  *
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.
41  *
42  * Note that the 'local' directory can be overridden as $config['local']['path']
43  * and $config['local']['dir'] etc.
44  *
45  * This used to be a couple of functions, but for various reasons it's nice
46  * to have a class instead.
47  *
48  * @category Output
49  * @package  StatusNet
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/
53  */
54
55 class Theme
56 {
57     var $name = null;
58     var $dir  = null;
59     var $path = null;
60
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
69     function __construct($name=null)
70     {
71         if (empty($name)) {
72             $name = common_config('site', 'theme');
73         }
74         if (!self::validName($name)) {
75             throw new ServerException("Invalid theme name.");
76         }
77         $this->name = $name;
78
79         // Check to see if it's in the local dir
80
81         $localroot = self::localRoot();
82
83         $fulldir = $localroot.'/'.$name;
84
85         if (file_exists($fulldir) && is_dir($fulldir)) {
86             $this->dir  = $fulldir;
87             $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
88             return;
89         }
90
91         // Check to see if it's in the distribution dir
92
93         $instroot = self::installRoot();
94
95         $fulldir = $instroot.'/'.$name;
96
97         if (file_exists($fulldir) && is_dir($fulldir)) {
98
99             $this->dir = $fulldir;
100             $this->path = $this->relativeThemePath('theme', 'theme', $name);
101         }
102     }
103
104     /**
105      * Build a full URL to the given theme's base directory, possibly
106      * using an offsite theme server path.
107      *
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
111      *
112      * @return string URL
113      *
114      * @todo consolidate code with that for other customizable paths
115      */
116
117     protected function relativeThemePath($group, $fallbackSubdir, $name)
118     {
119         if (StatusNet::isHTTPS()) {
120
121             $sslserver = common_config($group, 'sslserver');
122
123             if (empty($sslserver)) {
124                 $server = common_config('site', 'server');
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
137             if ($path[strlen($path)-1] != '/') {
138                 $path .= '/';
139             }
140
141             if ($path[0] != '/') {
142                 $path = '/'.$path;
143             }
144
145             return 'https://'.$server.$path.$name;
146
147         } else {
148
149             $path = common_config($group, 'path');
150
151             if (empty($path)) {
152                 $path = common_config('site', 'path') . '/';
153                 if ($fallbackSubdir) {
154                     $path .= $fallbackSubdir . '/';
155                 }
156             }
157
158             if ($path[strlen($path)-1] != '/') {
159                 $path .= '/';
160             }
161
162             if ($path[0] != '/') {
163                 $path = '/'.$path;
164             }
165
166             $server = common_config($group, 'server');
167
168             if (empty($server)) {
169                 $server = common_config('site', 'server');
170             }
171
172             return 'http://'.$server.$path.$name;
173         }
174     }
175
176     /**
177      * Gets the full local filename of a file in this theme.
178      *
179      * @param string $relative relative name, like 'logo.png'
180      *
181      * @return string full pathname, like /var/www/mublog/theme/default/logo.png
182      */
183
184     function getFile($relative)
185     {
186         return $this->dir.'/'.$relative;
187     }
188
189     /**
190      * Gets the full HTTP url of a file in this theme
191      *
192      * @param string $relative relative name, like 'logo.png'
193      *
194      * @return string full URL, like 'http://example.com/theme/default/logo.png'
195      */
196
197     function getPath($relative)
198     {
199         return $this->path.'/'.$relative;
200     }
201
202     /**
203      * Fetch a list of other themes whose CSS needs to be pulled in before
204      * this theme's, based on following the theme.ini 'include' settings.
205      * (May be empty if this theme has no include dependencies.)
206      *
207      * @return array of strings with theme names
208      */
209     function getDeps()
210     {
211         $chain = $this->doGetDeps(array($this->name));
212         array_pop($chain); // Drop us back off
213         return $chain;
214     }
215
216     protected function doGetDeps($chain)
217     {
218         $data = $this->getMetadata();
219         if (!empty($data['include'])) {
220             $include = $data['include'];
221
222             // Protect against cycles!
223             if (!in_array($include, $chain)) {
224                 try {
225                     $theme = new Theme($include);
226                     array_unshift($chain, $include);
227                     return $theme->doGetDeps($chain);
228                 } catch (Exception $e) {
229                     common_log(LOG_ERR,
230                             "Exception while fetching theme dependencies " .
231                             "for $this->name: " . $e->getMessage());
232                 }
233             }
234         }
235         return $chain;
236     }
237
238     /**
239      * Pull data from the theme's theme.ini file.
240      * @fixme calling getFile will fall back to default theme, this may be unsafe.
241      *
242      * @return associative array of strings
243      */
244     function getMetadata()
245     {
246         $iniFile = $this->getFile('theme.ini');
247         if (file_exists($iniFile)) {
248             return parse_ini_file($iniFile);
249         } else {
250             return array();
251         }
252     }
253
254     /**
255      * Gets the full path of a file in a theme dir based on its relative name
256      *
257      * @param string $relative relative path within the theme directory
258      * @param string $name     name of the theme; defaults to current theme
259      *
260      * @return string File path to the theme file
261      */
262
263     static function file($relative, $name=null)
264     {
265         $theme = new Theme($name);
266         return $theme->getFile($relative);
267     }
268
269     /**
270      * Gets the full URL of a file in a theme dir based on its relative name
271      *
272      * @param string $relative relative path within the theme directory
273      * @param string $name     name of the theme; defaults to current theme
274      *
275      * @return string URL of the file
276      */
277
278     static function path($relative, $name=null)
279     {
280         $theme = new Theme($name);
281         return $theme->getPath($relative);
282     }
283
284     /**
285      * list available theme names
286      *
287      * @return array list of available theme names
288      */
289
290     static function listAvailable()
291     {
292         $local   = self::subdirsOf(self::localRoot());
293         $install = self::subdirsOf(self::installRoot());
294
295         $i = array_search('base', $install);
296
297         unset($install[$i]);
298
299         return array_merge($local, $install);
300     }
301
302     /**
303      * Utility for getting subdirs of a directory
304      *
305      * @param string $dir full path to directory to check
306      *
307      * @return array relative filenames of subdirs, or empty array
308      */
309
310     protected static function subdirsOf($dir)
311     {
312         $subdirs = array();
313
314         if (is_dir($dir)) {
315             if ($dh = opendir($dir)) {
316                 while (($filename = readdir($dh)) !== false) {
317                     if ($filename != '..' && $filename !== '.' &&
318                         is_dir($dir.'/'.$filename)) {
319                         $subdirs[] = $filename;
320                     }
321                 }
322                 closedir($dh);
323             }
324         }
325
326         return $subdirs;
327     }
328
329     /**
330      * Local root dir for themes
331      *
332      * @return string local root dir for themes
333      */
334
335     protected static function localRoot()
336     {
337         $basedir = common_config('local', 'dir');
338
339         if (empty($basedir)) {
340             $basedir = INSTALLDIR . '/local';
341         }
342
343         return $basedir . '/theme';
344     }
345
346     /**
347      * Root dir for themes that are shipped with StatusNet
348      *
349      * @return string root dir for StatusNet themes
350      */
351
352     protected static function installRoot()
353     {
354         $instroot = common_config('theme', 'dir');
355
356         if (empty($instroot)) {
357             $instroot = INSTALLDIR.'/theme';
358         }
359
360         return $instroot;
361     }
362
363     static function validName($name)
364     {
365         return preg_match('/^[a-z0-9][a-z0-9_-]*$/i', $name);
366     }
367 }