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