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