]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/theme.php
Don't trust local HTML either
[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 class Theme
55 {
56     const FALLBACK = 'neo';
57
58     var $name = null;
59     var $dir  = null;
60     var $path = null;
61     protected $metadata = null; // access via getMetadata() lazy-loader
62     protected $externals = null;
63     protected $deps = null;
64
65     /**
66      * Constructor
67      *
68      * Determines the proper directory and path for this theme.
69      *
70      * @param string $name Name of the theme; defaults to config value
71      */
72     function __construct($name=null)
73     {
74         if (empty($name)) {
75             $name = common_config('site', 'theme');
76         }
77         if (!self::validName($name)) {
78             // TRANS: Server exception displayed if a theme name was invalid.
79             throw new ServerException(_('Invalid theme name.'));
80         }
81         $this->name = $name;
82
83         // Check to see if it's in the local dir
84
85         $localroot = self::localRoot();
86
87         $fulldir = $localroot.'/'.$name;
88
89         if (file_exists($fulldir) && is_dir($fulldir)) {
90             $this->dir  = $fulldir;
91             $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
92             return;
93         }
94
95         // Check to see if it's in the distribution dir
96
97         $instroot = self::installRoot();
98
99         $fulldir = $instroot.'/'.$name;
100
101         if (file_exists($fulldir) && is_dir($fulldir)) {
102             $this->dir = $fulldir;
103             $this->path = $this->relativeThemePath('theme', 'theme', $name);
104             return;
105         }
106
107         // Ruh roh. Fall back to default, then.
108
109         common_log(LOG_WARNING, sprintf("Unable to find theme '%s', falling back to default theme '%s'",
110                                         $name,
111                                         Theme::FALLBACK));
112
113         $this->name = Theme::FALLBACK;
114         $this->dir  = $instroot.'/'.Theme::FALLBACK;
115         $this->path = $this->relativeThemePath('theme', 'theme', Theme::FALLBACK);
116
117     }
118
119     /**
120      * Build a full URL to the given theme's base directory, possibly
121      * using an offsite theme server path.
122      *
123      * @param string $group configuration section name to pull paths from
124      * @param string $fallbackSubdir default subdirectory under INSTALLDIR
125      * @param string $name theme name
126      *
127      * @return string URL
128      *
129      * @todo consolidate code with that for other customizable paths
130      */
131     protected function relativeThemePath($group, $fallbackSubdir, $name)
132     {
133         if (GNUsocial::isHTTPS()) {
134             $sslserver = common_config($group, 'sslserver');
135
136             if (empty($sslserver)) {
137                 if (is_string(common_config('site', 'sslserver')) &&
138                     mb_strlen(common_config('site', 'sslserver')) > 0) {
139                     $server = common_config('site', 'sslserver');
140                 } else if (common_config('site', 'server')) {
141                     $server = common_config('site', 'server');
142                 }
143                 $path   = common_config('site', 'path') . '/';
144                 if ($fallbackSubdir) {
145                     $path .= $fallbackSubdir . '/';
146                 }
147             } else {
148                 $server = $sslserver;
149                 $path   = common_config($group, 'sslpath');
150                 if (empty($path)) {
151                     $path = common_config($group, 'path');
152                 }
153             }
154
155             $protocol = 'https';
156         } else {
157             $path = common_config($group, 'path');
158
159             if (empty($path)) {
160                 $path = common_config('site', 'path') . '/';
161                 if ($fallbackSubdir) {
162                     $path .= $fallbackSubdir . '/';
163                 }
164             }
165
166             $server = common_config($group, 'server');
167
168             if (empty($server)) {
169                 $server = common_config('site', 'server');
170             }
171
172             $protocol = 'http';
173         }
174
175         if ($path[strlen($path)-1] != '/') {
176             $path .= '/';
177         }
178
179         if ($path[0] != '/') {
180             $path = '/'.$path;
181         }
182
183         return $protocol.'://'.$server.$path.$name;
184     }
185
186     /**
187      * Gets the full local filename of a file in this theme.
188      *
189      * @param string $relative relative name, like 'logo.png'
190      *
191      * @return string full pathname, like /var/www/mublog/theme/default/logo.png
192      */
193     function getFile($relative)
194     {
195         return $this->dir.'/'.$relative;
196     }
197
198     /**
199      * Gets the full HTTP url of a file in this theme
200      *
201      * @param string $relative relative name, like 'logo.png'
202      *
203      * @return string full URL, like 'http://example.com/theme/default/logo.png'
204      */
205     function getPath($relative)
206     {
207         return $this->path.'/'.$relative;
208     }
209
210     /**
211      * Fetch a list of other themes whose CSS needs to be pulled in before
212      * this theme's, based on following the theme.ini 'include' settings.
213      * (May be empty if this theme has no include dependencies.)
214      *
215      * @return array of strings with theme names
216      */
217     function getDeps()
218     {
219         if ($this->deps === null) {
220             $chain = $this->doGetDeps(array($this->name));
221             array_pop($chain); // Drop us back off
222             $this->deps = $chain;
223         }
224         return $this->deps;
225     }
226
227     protected function doGetDeps($chain)
228     {
229         $data = $this->getMetadata();
230         if (!empty($data['include'])) {
231             $include = $data['include'];
232
233             // Protect against cycles!
234             if (!in_array($include, $chain)) {
235                 try {
236                     $theme = new Theme($include);
237                     array_unshift($chain, $include);
238                     return $theme->doGetDeps($chain);
239                 } catch (Exception $e) {
240                     common_log(LOG_ERR,
241                             "Exception while fetching theme dependencies " .
242                             "for $this->name: " . $e->getMessage());
243                 }
244             }
245         }
246         return $chain;
247     }
248
249     /**
250      * Pull data from the theme's theme.ini file.
251      * @fixme calling getFile will fall back to default theme, this may be unsafe.
252      *
253      * @return associative array of strings
254      */
255     function getMetadata()
256     {
257         if ($this->metadata == null) {
258             $this->metadata = $this->doGetMetadata();
259         }
260         return $this->metadata;
261     }
262
263     /**
264      * Pull data from the theme's theme.ini file.
265      * @fixme calling getFile will fall back to default theme, this may be unsafe.
266      *
267      * @return associative array of strings
268      */
269     private function doGetMetadata()
270     {
271         $iniFile = $this->getFile('theme.ini');
272         if (file_exists($iniFile)) {
273             return parse_ini_file($iniFile);
274         } else {
275             return array();
276         }
277     }
278
279     /**
280      * Get list of any external URLs required by this theme and any
281      * dependencies. These are lazy-loaded from theme.ini.
282      *
283      * @return array of URL strings
284      */
285     function getExternals()
286     {
287         if ($this->externals == null) {
288             $data = $this->getMetadata();
289             if (!empty($data['external'])) {
290                 $ext = (array)$data['external'];
291             } else {
292                 $ext = array();
293             }
294
295             if (!empty($data['include'])) {
296                 $theme = new Theme($data['include']);
297                 $ext = array_merge($ext, $theme->getExternals());
298             }
299
300             $this->externals = array_unique($ext);
301         }
302         return $this->externals;
303     }
304
305     /**
306      * Gets the full path of a file in a theme dir based on its relative name
307      *
308      * @param string $relative relative path within the theme directory
309      * @param string $name     name of the theme; defaults to current theme
310      *
311      * @return string File path to the theme file
312      */
313     static function file($relative, $name=null)
314     {
315         $theme = new Theme($name);
316         return $theme->getFile($relative);
317     }
318
319     /**
320      * Gets the full URL of a file in a theme dir based on its relative name
321      *
322      * @param string $relative relative path within the theme directory
323      * @param string $name     name of the theme; defaults to current theme
324      *
325      * @return string URL of the file
326      */
327     static function path($relative, $name=null)
328     {
329         $theme = new Theme($name);
330         return $theme->getPath($relative);
331     }
332
333     /**
334      * list available theme names
335      *
336      * @return array list of available theme names
337      */
338     static function listAvailable()
339     {
340         $local   = self::subdirsOf(self::localRoot());
341         $install = self::subdirsOf(self::installRoot());
342
343         $i = array_search('base', $install);
344
345         unset($install[$i]);
346
347         return array_merge($local, $install);
348     }
349
350     /**
351      * Utility for getting subdirs of a directory
352      *
353      * @param string $dir full path to directory to check
354      *
355      * @return array relative filenames of subdirs, or empty array
356      */
357     protected static function subdirsOf($dir)
358     {
359         $subdirs = array();
360
361         if (is_dir($dir)) {
362             if ($dh = opendir($dir)) {
363                 while (($filename = readdir($dh)) !== false) {
364                     if ($filename != '..' && $filename !== '.' &&
365                         is_dir($dir.'/'.$filename)) {
366                         $subdirs[] = $filename;
367                     }
368                 }
369                 closedir($dh);
370             }
371         }
372
373         return $subdirs;
374     }
375
376     /**
377      * Local root dir for themes
378      *
379      * @return string local root dir for themes
380      */
381     protected static function localRoot()
382     {
383         $basedir = common_config('local', 'dir');
384
385         if (empty($basedir)) {
386             $basedir = INSTALLDIR . '/local';
387         }
388
389         return $basedir . '/theme';
390     }
391
392     /**
393      * Root dir for themes that are shipped with StatusNet
394      *
395      * @return string root dir for StatusNet themes
396      */
397     protected static function installRoot()
398     {
399         $instroot = common_config('theme', 'dir');
400
401         if (empty($instroot)) {
402             $instroot = INSTALLDIR.'/theme';
403         }
404
405         return $instroot;
406     }
407
408     static function validName($name)
409     {
410         return preg_match('/^[a-z0-9][a-z0-9_-]*$/i', $name);
411     }
412 }