]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/theme.php
null theme ssl setting means 'guess'
[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             $ssl = common_config('theme', 'ssl');
114
115             if (is_null($ssl)) { // null -> guess
116                 if (common_config('site', 'ssl') == 'always' &&
117                     !common_config('theme', 'server')) {
118                     $ssl = true;
119                 } else {
120                     $ssl = false;
121                 }
122             }
123
124             $protocol = ($ssl) ? 'https' : 'http';
125
126             $this->path = $protocol . '://'.$server.$path.$name;
127         }
128     }
129
130     /**
131      * Gets the full local filename of a file in this theme.
132      *
133      * @param string $relative relative name, like 'logo.png'
134      *
135      * @return string full pathname, like /var/www/mublog/theme/default/logo.png
136      */
137
138     function getFile($relative)
139     {
140         return $this->dir.'/'.$relative;
141     }
142
143     /**
144      * Gets the full HTTP url of a file in this theme
145      *
146      * @param string $relative relative name, like 'logo.png'
147      *
148      * @return string full URL, like 'http://example.com/theme/default/logo.png'
149      */
150
151     function getPath($relative)
152     {
153         return $this->path.'/'.$relative;
154     }
155
156     /**
157      * Gets the full path of a file in a theme dir based on its relative name
158      *
159      * @param string $relative relative path within the theme directory
160      * @param string $name     name of the theme; defaults to current theme
161      *
162      * @return string File path to the theme file
163      */
164
165     static function file($relative, $name=null)
166     {
167         $theme = new Theme($name);
168         return $theme->getFile($relative);
169     }
170
171     /**
172      * Gets the full URL of a file in a theme dir based on its relative name
173      *
174      * @param string $relative relative path within the theme directory
175      * @param string $name     name of the theme; defaults to current theme
176      *
177      * @return string URL of the file
178      */
179
180     static function path($relative, $name=null)
181     {
182         $theme = new Theme($name);
183         return $theme->getPath($relative);
184     }
185
186     /**
187      * list available theme names
188      *
189      * @return array list of available theme names
190      */
191
192     static function listAvailable()
193     {
194         $local   = self::subdirsOf(self::localRoot());
195         $install = self::subdirsOf(self::installRoot());
196
197         $i = array_search('base', $install);
198
199         unset($install[$i]);
200
201         return array_merge($local, $install);
202     }
203
204     /**
205      * Utility for getting subdirs of a directory
206      *
207      * @param string $dir full path to directory to check
208      *
209      * @return array relative filenames of subdirs, or empty array
210      */
211
212     protected static function subdirsOf($dir)
213     {
214         $subdirs = array();
215
216         if (is_dir($dir)) {
217             if ($dh = opendir($dir)) {
218                 while (($filename = readdir($dh)) !== false) {
219                     if ($filename != '..' && $filename !== '.' &&
220                         is_dir($dir.'/'.$filename)) {
221                         $subdirs[] = $filename;
222                     }
223                 }
224                 closedir($dh);
225             }
226         }
227
228         return $subdirs;
229     }
230
231     /**
232      * Local root dir for themes
233      *
234      * @return string local root dir for themes
235      */
236
237     protected static function localRoot()
238     {
239         return INSTALLDIR.'/local/theme';
240     }
241
242     /**
243      * Root dir for themes that are shipped with StatusNet
244      *
245      * @return string root dir for StatusNet themes
246      */
247
248     protected static function installRoot()
249     {
250         $instroot = common_config('theme', 'dir');
251
252         if (empty($instroot)) {
253             $instroot = INSTALLDIR.'/theme';
254         }
255
256         return $instroot;
257     }
258 }