ed45abc62e3bfd0996a296399d7d2d29ab2a80fe
[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
54         // Initialize array
55         $themesArray = array(
56                 'theme_unix'   => array(), // Unix name from filesystem
57                 'theme_name'   => array()  // Title
58         );
59
60         // Only activated themes for the user
61         $add = " WHERE `theme_active`='Y'";
62
63         // Do we have admin?
64         if (isAdmin()) $add = '';
65
66         // Select all themes we want
67         $result = SQL_QUERY("SELECT
68         `theme_path`, `theme_name`
69 FROM
70         `{?_MYSQL_PREFIX?}_themes`
71 ".$add."
72 ORDER BY
73         `theme_name` ASC", __FILE__, __LINE__);
74
75         // Load all themes
76         while ($content = SQL_FETCHARRAY($result)) {
77                 // Construct relative include file name
78                 $inc = sprintf("theme/%s/theme.php", secureString($content['theme_path']));
79
80                 // Load it's theme.php file if found
81                 if (isIncludeReadable($inc)) {
82                         // And save all data in array
83                         loadInclude($inc);
84                         $themesArray['theme_unix'][] = $content['theme_path'];
85                         $themesArray['theme_name'][] = $GLOBALS['theme_data']['name'];
86                 } // END - if
87         } // END - while
88
89         // Free the result
90         SQL_FREERESULT($result);
91
92         // Construct selection form for the box template
93         $OUT = '';
94         foreach ($themesArray['theme_unix'] as $key => $theme) {
95                 $OUT .= "  <option value=\"".$theme."\"";
96                 if ($theme == getCurrentTheme()) $OUT .= ' selected="selected"';
97                 $OUT .= ">".$themesArray['theme_name'][$key]."</option>\n";
98         } // END - foreach
99
100         // Remember content
101         $content = array(
102                 'form_action' => $formAction,
103                 'selection'   => $OUT
104         );
105
106         // Return generated selection
107         return loadTemplate('theme_select_form', true, $content);
108 }
109
110 // Get version from name
111 function getThemeVersion ($name) {
112         // Is the extension 'theme' installed?
113         if (!isExtensionActive('theme')) {
114                 // Then abort here
115                 return '!.!';
116         } // END - if
117
118         // Default version 'number'
119         $cver = '?.?';
120
121         // Is the cache entry there?
122         if (isset($GLOBALS['cache_array']['themes']['theme_ver'][$name])) {
123                 // Get the version from cache
124                 $cver = $GLOBALS['cache_array']['themes']['theme_ver'][$name];
125
126                 // Count up
127                 incrementStatsEntry('cache_hits');
128         } elseif (getExtensionVersion('cache') != '0.1.8') {
129                 // Load version from database
130                 $result = SQL_QUERY_ESC("SELECT `theme_ver` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
131                         array($name), __FUNCTION__, __LINE__);
132
133                 // Entry found?
134                 if (SQL_NUMROWS($result) == 1) {
135                         // Fetch data
136                         list($cver) = SQL_FETCHROW($result);
137                 } // END - if
138
139                 // Free result
140                 SQL_FREERESULT($result);
141         }
142
143         // Return version
144         return $cver;
145 }
146
147 // Checks wether a theme is found in db
148 function ifThemeExists ($name) {
149         // Get theme and is it not nul?
150         return ((isExtensionActive('theme')) && (getThemeId($name) > 0));
151 }
152
153 // Checks if a theme is active
154 function isThemeActive ($name) {
155         // Is the extension 'theme' installed?
156         if (!isExtensionActive('theme')) {
157                 // Then abort here
158                 return false;
159         } // END - if
160
161         // Default is nothing active
162         $active = false;
163
164         // Is the cache entry there?
165         if (isset($GLOBALS['cache_array']['themes']['theme_active'][$name])) {
166                 // Get the version from cache
167                 $active = ($GLOBALS['cache_array']['themes']['theme_active'][$name] == 'Y');
168
169                 // Count up
170                 incrementStatsEntry('cache_hits');
171         } elseif (getExtensionVersion('cache') != '0.1.8') {
172                 // Check if current theme is already imported or not
173                 $result = SQL_QUERY_ESC("SELECT `theme_active` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
174                         array($name), __FUNCTION__, __LINE__);
175
176                 // Is the theme active and installed?
177                 $active = (SQL_NUMROWS($result) == 1);
178
179                 // Free result
180                 SQL_FREERESULT($result);
181         }
182
183         // Return result
184         return $active;
185 }
186
187 // Gets current human-readable theme name
188 function getCurrentThemeName () {
189         // Is the extension 'theme' installed?
190         if (!isExtensionActive('theme')) {
191                 // Then abort here
192                 return 'default';
193         } // END - if
194
195         // Get the Uni* name
196         $name = getCurrentTheme();
197
198         // Is the cache entry there?
199         if (isset($GLOBALS['cache_array']['themes']['theme_name'][$name])) {
200                 // Get the version from cache
201                 $name = $GLOBALS['cache_array']['themes']['theme_name'][$name];
202
203                 // Count up
204                 incrementStatsEntry('cache_hits');
205         } elseif (getExtensionVersion('cache') != '0.1.8') {
206                 // Check if current theme is already imported or not
207                 $result = SQL_QUERY_ESC("SELECT `theme_name` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
208                         array($name), __FUNCTION__, __LINE__);
209
210                 // Load theme name
211                 list($name) = SQL_FETCHROW($result);
212
213                 // Free result
214                 SQL_FREERESULT($result);
215         }
216
217         // Return name
218         return $name;
219 }
220
221 // Get current theme name
222 function getActualTheme () {
223         // The default theme is 'default'... ;-)
224         $ret = 'default';
225
226         // Load default theme if not empty from configuration
227         if ((isConfigEntrySet('default_theme')) && (getConfig('default_theme') != '')) $ret = getConfig('default_theme');
228
229         if (!isSessionVariableSet('mxchange_theme')) {
230                 // Set default theme
231                 setTheme($ret);
232         } elseif ((isSessionVariableSet('mxchange_theme')) && (isExtensionInstalledAndNewer('sql_patches', '0.1.4'))) {
233                 //die("<pre>".print_r($GLOBALS['cache_array']['themes'], true)."</pre>");
234                 // Get theme from cookie
235                 $ret = getSession('mxchange_theme');
236
237                 // Is it valid?
238                 if ((!isExtensionActive('theme')) || (getThemeId($ret) == '0')) {
239                         // Fix it to default
240                         $ret = 'default';
241                 } // END - if
242         } elseif ((!isInstalled()) && ((isInstalling()) || (getOutputMode() == true)) && ((isGetRequestElementSet('theme')) || (isPostRequestElementSet('theme')))) {
243                 // Prepare filename for checking
244                 $themeFile = sprintf("theme/%s/theme.php", getRequestElement('theme'));
245
246                 // Installation mode active
247                 if ((isGetRequestElementSet('theme')) && (isIncludeReadable($theme))) {
248                         // Set cookie from URL data
249                         setTheme(getRequestElement('theme'));
250                 } elseif (isIncludeReadable(sprintf("theme/%s/theme.php", secureString(postRequestElement('theme'))))) {
251                         // Set cookie from posted data
252                         setTheme(secureString(postRequestElement('theme')));
253                 }
254
255                 // Set return value
256                 $ret = getSession('mxchange_theme');
257         } else {
258                 // Invalid design, reset cookie
259                 setTheme($ret);
260         }
261
262         // Return theme value
263         return $ret;
264 }
265
266 // Setter for theme in session
267 function setTheme ($newTheme) {
268         setSession('mxchange_theme', $newTheme);
269 }
270
271 // Get id from theme
272 function getThemeId ($name) {
273         // Default id
274         $id = '0';
275
276         // Is the cache entry there?
277         if (isset($GLOBALS['cache_array']['themes']['id'][$name])) {
278                 // Get the version from cache
279                 $id = $GLOBALS['cache_array']['themes']['id'][$name];
280
281                 // Count up
282                 incrementStatsEntry('cache_hits');
283         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
284                 // Check if current theme is already imported or not
285                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
286                         array($name), __FUNCTION__, __LINE__);
287
288                 // Entry found?
289                 if (SQL_NUMROWS($result) == 1) {
290                         // Fetch data
291                         list($id) = SQL_FETCHROW($result);
292                 } // END - if
293
294                 // Free result
295                 SQL_FREERESULT($result);
296         }
297
298         // Return id
299         return $id;
300 }
301
302 ///////////////////////////////////////////////////////////////////////////////
303 //                              Only filter functions
304 ///////////////////////////////////////////////////////////////////////////////
305
306 // Filter for generic handling of theme change
307 function FILTER_HANDLE_THEME_CHANGE () {
308         // Check if new theme is selcted
309         if ((isPostRequestElementSet('new_theme')) && (postRequestElement('new_theme') != getCurrentTheme())) {
310                 // Set new theme for guests
311                 $newTheme = postRequestElement('new_theme');
312
313                 // Change to new theme
314                 setTheme($newTheme);
315
316                 // Remove current from array
317                 removeIncludeFromPool('theme', sprintf("theme/%s/theme.php", getCurrentTheme()));
318
319                 // Add new theme
320                 addIncludeToPool('theme', sprintf("theme/%s/theme.php", $newTheme));
321         } // END - if
322 }
323
324 // Filter for settings theme from user profile, must be executed only if FILTER_FETCH_USER_DATA() ran before
325 function FILTER_SET_USERS_THEME () {
326         // Is the user data valid?
327         if (!isMember()) {
328                 // Do only run for logged in members
329                 debug_report_bug('Please only run this filter for logged in users.');
330         } // END - if
331
332         // Change to new theme
333         setTheme(getUserData('curr_theme'));
334 }
335
336 // [EOF]
337 ?>