]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/theme.php
Merge branch 'diskcache' into 0.9.x
[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  * This used to be a couple of functions, but for various reasons it's nice
43  * to have a class instead.
44  *
45  * @category Output
46  * @package  StatusNet
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/
50  */
51
52 class Theme
53 {
54     var $dir  = null;
55     var $path = null;
56
57     /**
58      * Constructor
59      *
60      * Determines the proper directory and path for this theme.
61      *
62      * @param string $name Name of the theme; defaults to config value
63      */
64
65     function __construct($name=null)
66     {
67         if (empty($name)) {
68             $name = common_config('site', 'theme');
69         }
70
71         // Check to see if it's in the local dir
72
73         $localroot = self::localRoot();
74
75         $fulldir = $localroot.'/'.$name;
76
77         if (file_exists($fulldir) && is_dir($fulldir)) {
78             $this->dir  = $fulldir;
79             $this->path = common_path('local/theme/'.$name.'/');
80             return;
81         }
82
83         // Check to see if it's in the distribution dir
84
85         $instroot = self::installRoot();
86
87         $fulldir = $instroot.'/'.$name;
88
89         if (file_exists($fulldir) && is_dir($fulldir)) {
90
91             $this->dir = $fulldir;
92
93             $path = common_config('theme', 'path');
94
95             if (empty($path)) {
96                 $path = common_config('site', 'path') . '/theme/';
97             }
98
99             if ($path[strlen($path)-1] != '/') {
100                 $path .= '/';
101             }
102
103             if ($path[0] != '/') {
104                 $path = '/'.$path;
105             }
106
107             $server = common_config('theme', 'server');
108
109             if (empty($server)) {
110                 $server = common_config('site', 'server');
111             }
112
113             // XXX: protocol
114
115             $this->path = 'http://'.$server.$path.$name;
116         }
117     }
118
119     /**
120      * Gets the full local filename of a file in this theme.
121      *
122      * @param string $relative relative name, like 'logo.png'
123      *
124      * @return string full pathname, like /var/www/mublog/theme/default/logo.png
125      */
126
127     function getFile($relative)
128     {
129         return $this->dir.'/'.$relative;
130     }
131
132     /**
133      * Gets the full HTTP url of a file in this theme
134      *
135      * @param string $relative relative name, like 'logo.png'
136      *
137      * @return string full URL, like 'http://example.com/theme/default/logo.png'
138      */
139
140     function getPath($relative)
141     {
142         return $this->path.'/'.$relative;
143     }
144
145     /**
146      * Gets the full path of a file in a theme dir based on its relative name
147      *
148      * @param string $relative relative path within the theme directory
149      * @param string $name     name of the theme; defaults to current theme
150      *
151      * @return string File path to the theme file
152      */
153
154     static function file($relative, $name=null)
155     {
156         $theme = new Theme($name);
157         return $theme->getFile($relative);
158     }
159
160     /**
161      * Gets the full URL of a file in a theme dir based on its relative name
162      *
163      * @param string $relative relative path within the theme directory
164      * @param string $name     name of the theme; defaults to current theme
165      *
166      * @return string URL of the file
167      */
168
169     static function path($relative, $name=null)
170     {
171         $theme = new Theme($name);
172         return $theme->getPath($relative);
173     }
174
175     /**
176      * list available theme names
177      *
178      * @return array list of available theme names
179      */
180
181     static function listAvailable()
182     {
183         $local   = self::subdirsOf(self::localRoot());
184         $install = self::subdirsOf(self::installRoot());
185
186         $i = array_search('base', $install);
187
188         unset($install[$i]);
189
190         return array_merge($local, $install);
191     }
192
193     /**
194      * Utility for getting subdirs of a directory
195      *
196      * @param string $dir full path to directory to check
197      *
198      * @return array relative filenames of subdirs, or empty array
199      */
200
201     protected static function subdirsOf($dir)
202     {
203         $subdirs = array();
204
205         if (is_dir($dir)) {
206             if ($dh = opendir($dir)) {
207                 while (($filename = readdir($dh)) !== false) {
208                     if ($filename != '..' && $filename !== '.' &&
209                         is_dir($dir.'/'.$filename)) {
210                         $subdirs[] = $filename;
211                     }
212                 }
213                 closedir($dh);
214             }
215         }
216
217         return $subdirs;
218     }
219
220     /**
221      * Local root dir for themes
222      *
223      * @return string local root dir for themes
224      */
225
226     protected static function localRoot()
227     {
228         return INSTALLDIR.'/local/theme';
229     }
230
231     /**
232      * Root dir for themes that are shipped with StatusNet
233      *
234      * @return string root dir for StatusNet themes
235      */
236
237     protected static function installRoot()
238     {
239         $instroot = common_config('theme', 'dir');
240
241         if (empty($instroot)) {
242             $instroot = INSTALLDIR.'/theme';
243         }
244
245         return $instroot;
246     }
247 }