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