]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/theme.php
Restructure theme.php to define a class Theme
[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 = INSTALLDIR.'/local/theme';
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 = common_config('theme', 'dir');
86
87         if (empty($instroot)) {
88             $instroot = INSTALLDIR.'/theme';
89         }
90
91         $fulldir = $instroot.'/'.$name;
92
93         if (file_exists($fulldir) && is_dir($fulldir)) {
94
95             $this->dir = $fulldir;
96
97             $path = common_config('theme', 'path');
98
99             if (empty($path)) {
100                 $path = common_config('site', 'path') . '/theme/';
101             }
102
103             if ($path[strlen($path)-1] != '/') {
104                 $path .= '/';
105             }
106
107             if ($path[0] != '/') {
108                 $path = '/'.$path;
109             }
110
111             $server = common_config('theme', 'server');
112
113             if (empty($server)) {
114                 $server = common_config('site', 'server');
115             }
116
117             // XXX: protocol
118
119             $this->path = 'http://'.$server.$path.$name;
120         }
121     }
122
123     /**
124      * Gets the full local filename of a file in this theme.
125      *
126      * @param string $relative relative name, like 'logo.png'
127      *
128      * @return string full pathname, like /var/www/mublog/theme/default/logo.png
129      */
130
131     function getFile($relative)
132     {
133         return $this->dir.'/'.$relative;
134     }
135
136     /**
137      * Gets the full HTTP url of a file in this theme
138      *
139      * @param string $relative relative name, like 'logo.png'
140      *
141      * @return string full URL, like 'http://example.com/theme/default/logo.png'
142      */
143
144     function getPath($relative)
145     {
146         return $this->path.'/'.$relative;
147     }
148
149     /**
150      * Gets the full path of a file in a theme dir based on its relative name
151      *
152      * @param string $relative relative path within the theme directory
153      * @param string $name     name of the theme; defaults to current theme
154      *
155      * @return string File path to the theme file
156      */
157
158     static function file($relative, $name=null)
159     {
160         $theme = new Theme($name);
161         return $theme->getFile($relative);
162     }
163
164     /**
165      * Gets the full URL of a file in a theme dir based on its relative name
166      *
167      * @param string $relative relative path within the theme directory
168      * @param string $name     name of the theme; defaults to current theme
169      *
170      * @return string URL of the file
171      */
172
173     static function path($relative, $name=null)
174     {
175         $theme = new Theme($name);
176         return $theme->getPath($relative);
177     }
178 }