e39f97f3f72f926cf4a239555acbb01c583c3897
[mailer.git] / inc / libs / theme_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 12/03/2004 *
4  * ===================                          Last change: 12/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : theme manager.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : Theme manager                                    *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Themen-Manager                                   *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 }
43
44 // Create a selection box with installed and activated themes or all if admin
45 function generateThemeSelectionBox () {
46         // Init variables and fill them if set
47         $what = getWhat();
48         $mod = getModule();
49
50         // Construction URL
51         $formAction = "{%url=modules.php?module=" . $mod;
52         if (!empty($what)) $formAction .= "&amp;what=" . $what;
53         $formAction = '%}';
54
55         // Initialize array
56         $themesArray = array(
57                 'theme_unix'   => array(), // Unix name from filesystem
58                 'theme_name'   => array()  // Title
59         );
60
61         // Only activated themes for the user
62         $add = " WHERE `theme_active`='Y'";
63
64         // Do we have admin?
65         if (isAdmin()) $add = '';
66
67         // Select all themes we want
68         $result = SQL_QUERY("SELECT
69         `theme_path`, `theme_name`
70 FROM
71         `{?_MYSQL_PREFIX?}_themes`
72 ".$add."
73 ORDER BY
74         `theme_name` ASC", __FILE__, __LINE__);
75
76         // Load all themes
77         while ($content = SQL_FETCHARRAY($result)) {
78                 // Construct relative include file name
79                 $inc = sprintf("theme/%s/theme.php", secureString($content['theme_path']));
80
81                 // Load it's theme.php file if found
82                 if (isIncludeReadable($inc)) {
83                         // And save all data in array
84                         loadInclude($inc);
85                         $themesArray['theme_unix'][] = $content['theme_path'];
86                         $themesArray['theme_name'][] = $GLOBALS['theme_data']['name'];
87                 } // END - if
88         } // END - while
89
90         // Free the result
91         SQL_FREERESULT($result);
92
93         // Construct selection form for the box template
94         $OUT = '';
95         foreach ($themesArray['theme_unix'] as $key => $theme) {
96                 $OUT .= "  <option value=\"".$theme."\"";
97                 if ($theme == getCurrentTheme()) $OUT .= ' selected="selected"';
98                 $OUT .= ">".$themesArray['theme_name'][$key]."</option>\n";
99         } // END - foreach
100
101         // Remember content
102         $content = array(
103                 'form_action' => $formAction,
104                 'selection'   => $OUT
105         );
106
107         // Return generated selection
108         return loadTemplate('theme_select_form', true, $content);
109 }
110
111 // Get version from name
112 function getThemeVersion ($name) {
113         // Is the extension 'theme' installed?
114         if (!isExtensionActive('theme')) {
115                 // Then abort here
116                 return '!.!';
117         } // END - if
118
119         // Default version 'number'
120         $cver = '?.?';
121
122         // Is the cache entry there?
123         if (isset($GLOBALS['cache_array']['themes']['theme_ver'][$name])) {
124                 // Get the version from cache
125                 $cver = $GLOBALS['cache_array']['themes']['theme_ver'][$name];
126
127                 // Count up
128                 incrementStatsEntry('cache_hits');
129         } elseif (getExtensionVersion('cache') != '0.1.8') {
130                 // Load version from database
131                 $result = SQL_QUERY_ESC("SELECT `theme_ver` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
132                         array($name), __FUNCTION__, __LINE__);
133
134                 // Entry found?
135                 if (SQL_NUMROWS($result) == 1) {
136                         // Fetch data
137                         list($cver) = SQL_FETCHROW($result);
138                 } // END - if
139
140                 // Free result
141                 SQL_FREERESULT($result);
142         }
143
144         // Return version
145         return $cver;
146 }
147
148 // Checks wether a theme is found in db
149 function ifThemeExists ($name) {
150         // Get theme and is it not nul?
151         return ((isExtensionActive('theme')) && (getThemeId($name) > 0));
152 }
153
154 // Checks if a theme is active
155 function isThemeActive ($name) {
156         // Is the extension 'theme' installed?
157         if (!isExtensionActive('theme')) {
158                 // Then abort here
159                 return false;
160         } // END - if
161
162         // Default is nothing active
163         $active = false;
164
165         // Is the cache entry there?
166         if (isset($GLOBALS['cache_array']['themes']['theme_active'][$name])) {
167                 // Get the version from cache
168                 $active = ($GLOBALS['cache_array']['themes']['theme_active'][$name] == 'Y');
169
170                 // Count up
171                 incrementStatsEntry('cache_hits');
172         } elseif (getExtensionVersion('cache') != '0.1.8') {
173                 // Check if current theme is already imported or not
174                 $result = SQL_QUERY_ESC("SELECT `theme_active` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
175                         array($name), __FUNCTION__, __LINE__);
176
177                 // Is the theme active and installed?
178                 $active = (SQL_NUMROWS($result) == 1);
179
180                 // Free result
181                 SQL_FREERESULT($result);
182         }
183
184         // Return result
185         return $active;
186 }
187
188 // Gets current human-readable theme name
189 function getCurrentThemeName () {
190         // Is the extension 'theme' installed?
191         if (!isExtensionActive('theme')) {
192                 // Then abort here
193                 return 'default';
194         } // END - if
195
196         // Get the Uni* name
197         $name = getCurrentTheme();
198
199         // Is the cache entry there?
200         if (isset($GLOBALS['cache_array']['themes']['theme_name'][$name])) {
201                 // Get the version from cache
202                 $name = $GLOBALS['cache_array']['themes']['theme_name'][$name];
203
204                 // Count up
205                 incrementStatsEntry('cache_hits');
206         } elseif (getExtensionVersion('cache') != '0.1.8') {
207                 // Check if current theme is already imported or not
208                 $result = SQL_QUERY_ESC("SELECT `theme_name` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
209                         array($name), __FUNCTION__, __LINE__);
210
211                 // Load theme name
212                 list($name) = SQL_FETCHROW($result);
213
214                 // Free result
215                 SQL_FREERESULT($result);
216         }
217
218         // Return name
219         return $name;
220 }
221
222 // Get current theme name
223 function getActualTheme () {
224         // The default theme is 'default'... ;-)
225         $ret = 'default';
226
227         // Load default theme if not empty from configuration
228         if ((isConfigEntrySet('default_theme')) && (getConfig('default_theme') != '')) $ret = getConfig('default_theme');
229
230         if (!isSessionVariableSet('mxchange_theme')) {
231                 // Set default theme
232                 setTheme($ret);
233         } elseif ((isSessionVariableSet('mxchange_theme')) && (isExtensionInstalledAndNewer('sql_patches', '0.1.4'))) {
234                 //die("<pre>".print_r($GLOBALS['cache_array']['themes'], true)."</pre>");
235                 // Get theme from cookie
236                 $ret = getSession('mxchange_theme');
237
238                 // Is it valid?
239                 if ((!isExtensionActive('theme')) || (getThemeId($ret) == '0')) {
240                         // Fix it to default
241                         $ret = 'default';
242                 } // END - if
243         } elseif ((!isInstalled()) && ((isInstalling()) || (getOutputMode() == true)) && ((isGetRequestElementSet('theme')) || (isPostRequestElementSet('theme')))) {
244                 // Prepare filename for checking
245                 $themeFile = sprintf("theme/%s/theme.php", getRequestElement('theme'));
246
247                 // Installation mode active
248                 if ((isGetRequestElementSet('theme')) && (isIncludeReadable($theme))) {
249                         // Set cookie from URL data
250                         setTheme(getRequestElement('theme'));
251                 } elseif (isIncludeReadable(sprintf("theme/%s/theme.php", secureString(postRequestElement('theme'))))) {
252                         // Set cookie from posted data
253                         setTheme(secureString(postRequestElement('theme')));
254                 }
255
256                 // Set return value
257                 $ret = getSession('mxchange_theme');
258         } else {
259                 // Invalid design, reset cookie
260                 setTheme($ret);
261         }
262
263         // Return theme value
264         return $ret;
265 }
266
267 // Setter for theme in session
268 function setTheme ($newTheme) {
269         setSession('mxchange_theme', $newTheme);
270 }
271
272 // Get id from theme
273 function getThemeId ($name) {
274         // Default id
275         $id = '0';
276
277         // Is the cache entry there?
278         if (isset($GLOBALS['cache_array']['themes']['id'][$name])) {
279                 // Get the version from cache
280                 $id = $GLOBALS['cache_array']['themes']['id'][$name];
281
282                 // Count up
283                 incrementStatsEntry('cache_hits');
284         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
285                 // Check if current theme is already imported or not
286                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
287                         array($name), __FUNCTION__, __LINE__);
288
289                 // Entry found?
290                 if (SQL_NUMROWS($result) == 1) {
291                         // Fetch data
292                         list($id) = SQL_FETCHROW($result);
293                 } // END - if
294
295                 // Free result
296                 SQL_FREERESULT($result);
297         }
298
299         // Return id
300         return $id;
301 }
302
303 ///////////////////////////////////////////////////////////////////////////////
304 //                              Only filter functions
305 ///////////////////////////////////////////////////////////////////////////////
306
307 // Filter for generic handling of theme change
308 function FILTER_HANDLE_THEME_CHANGE () {
309         // Check if new theme is selcted
310         if ((isPostRequestElementSet('new_theme')) && (postRequestElement('new_theme') != getCurrentTheme())) {
311                 // Set new theme for guests
312                 $newTheme = postRequestElement('new_theme');
313
314                 // Change to new theme
315                 setTheme($newTheme);
316
317                 // Remove current from array
318                 removeIncludeFromPool('theme', sprintf("theme/%s/theme.php", getCurrentTheme()));
319
320                 // Add new theme
321                 addIncludeToPool('theme', sprintf("theme/%s/theme.php", $newTheme));
322         } // END - if
323 }
324
325 // Filter for settings theme from user profile, must be executed only if FILTER_FETCH_USER_DATA() ran before
326 function FILTER_SET_USERS_THEME () {
327         // Is the user data valid?
328         if (!isMember()) {
329                 // Do only run for logged in members
330                 debug_report_bug('Please only run this filter for logged in users.');
331         } // END - if
332
333         // Change to new theme
334         setTheme(getUserData('curr_theme'));
335 }
336
337 // [EOF]
338 ?>